-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_profile.sh
executable file
·107 lines (92 loc) · 3.55 KB
/
create_profile.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
# Description:
# This script is used to create the profile.
#
# Usage:
# create_profile.sh
# create_profile.sh <Image URL>
#
# History:
# v1.0 2020-01-19 charles.shih Init version
# v2.0 2020-02-03 charles.shih Split create_profile and update_profile
# v2.1 2020-02-03 charles.shih Add additional configuration
# v2.2 2020-02-05 charles.shih Add Aliyun parameters
# v2.3 2020-02-06 charles.shih Fix a bug while profile does not exist
# v2.4 2020-04-27 charles.shih Offer a chance to replace IMAGE LABEL
pf=./profile
# Show the overwrite information
if [ -f "$pf" ]; then
read -p "Overwrite target file ($pf) [y/N]? " answer
[ "$answer" != "y" ] && exit 0
: >$pf
else
touch $pf || exit 1
fi
# Get image URL
if [ ! -z "$1" ]; then
image_url=$1
elif [ -z "$image_url" ]; then
read -p "Enter image URL: " image_url
fi
# Verify image URL
# Ex. http://download.xxx.xxxxxx.com/rhel-x/nightly/RHEL-x/xxxxxxxxxxxx/compose/BaseOS/xxxxxx/images/rhel-guest-image-xxxx-xxxxxxxx.x.xxxxxx.qcow2
[[ ! $image_url =~ .qcow2$ ]] && echo -e "ERROR: a qcow2 image is expected." && return 1
# Parse other information
image_name=$(basename $image_url)
repo_baseurl=$(echo $image_url | sed 's#/compose.*##')
image_label=$(basename $repo_baseurl)
image_arch=$(echo $image_url | sed 's#/images.*##' | xargs basename)
# Update IMAGE LABEL with COMPOSE ID?
compose_id=$(curl $repo_baseurl/COMPOSE_ID 2>/dev/null)
if [ ! -z "$compose_id" ] && [ "$image_label" != "$compose_id" ]; then
echo -e "\nNotice: The IMAGE LABEL is different from the COMPOSE ID!"
echo -e "IMAGE LABEL: $image_label\nCOMPOSE ID : $compose_id"
read -t 30 -p "Do you want to overwrite IMAGE LABEL with COMPOSE ID [Y/n]? (in 30s) " answer
echo
if [ "$answer" != "N" ] && [ "$answer" != "n" ]; then
image_label=$compose_id
echo "Overwrited with \"$compose_id\"."
fi
fi
# Get ALIYUN_IMAGE_ARCH
if [ "$image_arch" = "aarch64" ]; then
aliyun_image_arch=arm64
else
aliyun_image_arch=x86_64
fi
# Get parameters
workspace="/var/lib/libvirt/images/$image_label"
image_file="$workspace/$image_name"
# Confirm the information
echo -e "\nPlease confirm the following information:"
echo -e "IMAGE URL : $image_url"
echo -e "IMAGE NAME : $image_name"
echo -e "IMAGE ARCH : $image_arch"
echo -e "IMAGE LABEL : $image_label"
echo -e "COMPOSE ID : $compose_id"
echo -e "WORKSPACE : $workspace"
echo -e "IMAGE FILE : $image_file"
echo -e "REPO BASEURL: $repo_baseurl"
echo -e "\nIf you need a correction, press <Ctrl+C> in 30 seconds... Or press <Enter> to continue immediately..."
read -t 30
# Write profile
echo -e "Writing to $pf..."
$(dirname $0)/update_profile.sh IMAGE_URL $image_url
$(dirname $0)/update_profile.sh IMAGE_NAME $image_name
$(dirname $0)/update_profile.sh IMAGE_LABEL $image_label
$(dirname $0)/update_profile.sh IMAGE_ARCH $image_arch
$(dirname $0)/update_profile.sh WORKSPACE $workspace
$(dirname $0)/update_profile.sh IMAGE_FILE $image_file
$(dirname $0)/update_profile.sh REPO_BASEURL $repo_baseurl
# Additional parameters
$(dirname $0)/update_profile.sh ROOT_PASSWD
$(dirname $0)/update_profile.sh SSH_IDENTITY
# Cloud parameters
$(dirname $0)/update_profile.sh ALIYUN_REGION cn-beijing
$(dirname $0)/update_profile.sh ALIYUN_BUCKET rhel-test
$(dirname $0)/update_profile.sh ALIYUN_FOLDER $image_label
$(dirname $0)/update_profile.sh ALIYUN_IMAGE_NAME $image_label-$aliyun_image_arch
$(dirname $0)/update_profile.sh ALIYUN_IMAGE_ARCH $aliyun_image_arch
$(dirname $0)/update_profile.sh ALIYUN_IMAGE_SIZE 40
$(dirname $0)/update_profile.sh ALIYUN_IMAGE_DESC '"Created by image2clouds."'
exit 0