-
Notifications
You must be signed in to change notification settings - Fork 40
/
image-create-openstack.sh
executable file
·110 lines (94 loc) · 2.87 KB
/
image-create-openstack.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
107
108
109
110
#!/usr/bin/env bash
# Checks and uploads specified Image to user's OpenStack account.
# Downloads image first from an Amazon S3 storage account to host,
# then uploads image to openstack teenancy.
# Uses curl and python-glanceclient to do the job.
#
# Env vars
# KN_IMAGE_NAME
# KN_IMAGE_BUCKET_URL
# Exit immediately if a command exits with a non-zero status
set -e
echo "Started script image-create-openstack"
if [ -z "$KN_IMAGE_NAME" ]; then
echo >&2 "env KN_IMAGE_NAME must be set for this script to run"
exit 1
fi
KN_IMAGE_BUCKET_URL=${KN_IMAGE_BUCKET_URL:-"https://s3.eu-central-1.amazonaws.com/kubenow-eu-central-1"}
file_name="$KN_IMAGE_NAME.qcow2"
# Check if image is present already
echo "List images available in OpenStack..."
image_list="$(glance image-list)"
image_id="$(printf '%s' "$image_list" |
grep "\s$KN_IMAGE_NAME\s" |
awk -F "|" '{print $2;}' |
tr -d '[:space:]')"
# If it exist then exit here
if [ -n "$image_id" ]; then
echo "file exists - no need to upload, exit image-upload script"
exit 0
fi
# If it doesn't exist then download it
echo "Image not present in OpenStack"
echo "Downloading image to local /tmp/"
curl "$KN_IMAGE_BUCKET_URL/$file_name" \
-o "/tmp/$file_name" \
--connect-timeout 30 \
--max-time 1800
echo "Download md5 sum file"
curl "$KN_IMAGE_BUCKET_URL/$file_name.md5" \
-o "/tmp/$file_name.md5" \
--connect-timeout 30 \
--max-time 1800
# Verify md5sum of downloaded file
echo "Check md5 sum"
md5result=$(
cd /tmp
md5sum -c "$file_name.md5"
)
if [[ "$md5result" != *": OK"* ]]; then
echo >&2 "Wrong checksum of downloaded image."
echo >&2 "Something might have failed on file transfer."
echo >&2 "Please try again."
exit 1
else
echo "Checksum of downloaded image OK"
fi
# Upload image
echo "Uploading image"
glance image-create \
--file "/tmp/$file_name" \
--disk-format qcow2 \
--min-disk 20 \
--container-format bare \
--name "$KN_IMAGE_NAME" \
--progress
echo "Verify md5 of present/uploaded image..."
echo "List images available in OpenStack..."
image_list="$(glance image-list)"
image_id="$(printf '%s' "$image_list" |
grep "\s$KN_IMAGE_NAME\s" |
awk -F "|" '{print $2;}' |
tr -d '[:space:]')"
# Get checksum of uploaded file
image_details="$(glance image-show "$image_id")"
checksum="$(printf '%s' "$image_details" |
grep -w "checksum" |
awk -F "|" '{print $3;}' |
tr -d '[:space:]')"
# Get checksum of bucket image
echo "Download md5 sum file"
curl "$KN_IMAGE_BUCKET_URL/$file_name.md5" \
-o "/tmp/$file_name.md5" \
--connect-timeout 30 \
--max-time 1800
md5only=$(cut -f1 -d ' ' "/tmp/$file_name.md5")
if [ "$md5only" != "$checksum" ]; then
echo >&2 "Wrong checksum of present/uploaded image."
echo >&2 "Something might have failed on file transfer."
echo >&2 "Please delete image $KN_IMAGE_NAME from Openstack and try again."
exit 1
else
echo "Checksum OK"
fi
echo "Image upload done"