Cleans up the rather coplex upload_image() function
Change-Id: Ie1ef200b905dd5d17ef887390d620817208f2bf5
... | ... |
@@ -55,26 +55,28 @@ function upload_image { |
55 | 55 |
local image_url=$1 |
56 | 56 |
local token=$2 |
57 | 57 |
|
58 |
+ local image image_fname image_name |
|
59 |
+ |
|
58 | 60 |
# Create a directory for the downloaded image tarballs. |
59 | 61 |
mkdir -p $FILES/images |
60 |
- IMAGE_FNAME=`basename "$image_url"` |
|
62 |
+ image_fname=`basename "$image_url"` |
|
61 | 63 |
if [[ $image_url != file* ]]; then |
62 | 64 |
# Downloads the image (uec ami+akistyle), then extracts it. |
63 |
- if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then |
|
64 |
- wget -c $image_url -O $FILES/$IMAGE_FNAME |
|
65 |
+ if [[ ! -f $FILES/$image_fname || "$(stat -c "%s" $FILES/$image_fname)" = "0" ]]; then |
|
66 |
+ wget -c $image_url -O $FILES/$image_fname |
|
65 | 67 |
if [[ $? -ne 0 ]]; then |
66 | 68 |
echo "Not found: $image_url" |
67 | 69 |
return |
68 | 70 |
fi |
69 | 71 |
fi |
70 |
- IMAGE="$FILES/${IMAGE_FNAME}" |
|
72 |
+ image="$FILES/${image_fname}" |
|
71 | 73 |
else |
72 | 74 |
# File based URL (RFC 1738): file://host/path |
73 | 75 |
# Remote files are not considered here. |
74 | 76 |
# *nix: file:///home/user/path/file |
75 | 77 |
# windows: file:///C:/Documents%20and%20Settings/user/path/file |
76 |
- IMAGE=$(echo $image_url | sed "s/^file:\/\///g") |
|
77 |
- if [[ ! -f $IMAGE || "$(stat -c "%s" $IMAGE)" == "0" ]]; then |
|
78 |
+ image=$(echo $image_url | sed "s/^file:\/\///g") |
|
79 |
+ if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then |
|
78 | 80 |
echo "Not found: $image_url" |
79 | 81 |
return |
80 | 82 |
fi |
... | ... |
@@ -82,14 +84,14 @@ function upload_image { |
82 | 82 |
|
83 | 83 |
# OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading |
84 | 84 |
if [[ "$image_url" =~ 'openvz' ]]; then |
85 |
- IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" |
|
86 |
- glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format ami --disk-format ami < "${IMAGE}" |
|
85 |
+ image_name="${image_fname%.tar.gz}" |
|
86 |
+ glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$image_name" --is-public=True --container-format ami --disk-format ami < "${image}" |
|
87 | 87 |
return |
88 | 88 |
fi |
89 | 89 |
|
90 | 90 |
# vmdk format images |
91 | 91 |
if [[ "$image_url" =~ '.vmdk' ]]; then |
92 |
- IMAGE_NAME="${IMAGE_FNAME%.vmdk}" |
|
92 |
+ image_name="${image_fname%.vmdk}" |
|
93 | 93 |
|
94 | 94 |
# Before we can upload vmdk type images to glance, we need to know it's |
95 | 95 |
# disk type, storage adapter, and networking adapter. These values are |
... | ... |
@@ -102,17 +104,17 @@ function upload_image { |
102 | 102 |
# If the filename does not follow the above format then the vsphere |
103 | 103 |
# driver will supply default values. |
104 | 104 |
|
105 |
- vmdk_adapter_type="" |
|
106 |
- vmdk_disktype="" |
|
107 |
- vmdk_net_adapter="" |
|
105 |
+ local vmdk_disktype="" |
|
106 |
+ local vmdk_net_adapter="" |
|
107 |
+ local path_len |
|
108 | 108 |
|
109 | 109 |
# vmdk adapter type |
110 |
- vmdk_adapter_type="$(head -25 $IMAGE | { grep -a -F -m 1 'ddb.adapterType =' $IMAGE || true; })" |
|
110 |
+ local vmdk_adapter_type="$(head -25 $image | { grep -a -F -m 1 'ddb.adapterType =' $image || true; })" |
|
111 | 111 |
vmdk_adapter_type="${vmdk_adapter_type#*\"}" |
112 | 112 |
vmdk_adapter_type="${vmdk_adapter_type%?}" |
113 | 113 |
|
114 | 114 |
# vmdk disk type |
115 |
- vmdk_create_type="$(head -25 $IMAGE | { grep -a -F -m 1 'createType=' $IMAGE || true; })" |
|
115 |
+ local vmdk_create_type="$(head -25 $image | { grep -a -F -m 1 'createType=' $image || true; })" |
|
116 | 116 |
vmdk_create_type="${vmdk_create_type#*\"}" |
117 | 117 |
vmdk_create_type="${vmdk_create_type%\"*}" |
118 | 118 |
|
... | ... |
@@ -120,17 +122,16 @@ function upload_image { |
120 | 120 |
`"should use a descriptor-data pair." |
121 | 121 |
if [[ "$vmdk_create_type" = "monolithicSparse" ]]; then |
122 | 122 |
vmdk_disktype="sparse" |
123 |
- elif [[ "$vmdk_create_type" = "monolithicFlat" || \ |
|
124 |
- "$vmdk_create_type" = "vmfs" ]]; then |
|
123 |
+ elif [[ "$vmdk_create_type" = "monolithicFlat" || "$vmdk_create_type" = "vmfs" ]]; then |
|
125 | 124 |
# Attempt to retrieve the *-flat.vmdk |
126 |
- flat_fname="$(head -25 $IMAGE | { grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $IMAGE || true; })" |
|
125 |
+ local flat_fname="$(head -25 $image | { grep -G 'RW\|RDONLY [0-9]+ FLAT\|VMFS' $image || true; })" |
|
127 | 126 |
flat_fname="${flat_fname#*\"}" |
128 | 127 |
flat_fname="${flat_fname%?}" |
129 | 128 |
if [[ -z "$flat_fname" ]]; then |
130 |
- flat_fname="$IMAGE_NAME-flat.vmdk" |
|
129 |
+ flat_fname="$image_name-flat.vmdk" |
|
131 | 130 |
fi |
132 |
- path_len=`expr ${#image_url} - ${#IMAGE_FNAME}` |
|
133 |
- flat_url="${image_url:0:$path_len}$flat_fname" |
|
131 |
+ path_len=`expr ${#image_url} - ${#image_fname}` |
|
132 |
+ local flat_url="${image_url:0:$path_len}$flat_fname" |
|
134 | 133 |
warn $LINENO "$descriptor_data_pair_msg"` |
135 | 134 |
`" Attempt to retrieve the *-flat.vmdk: $flat_url" |
136 | 135 |
if [[ $flat_url != file* ]]; then |
... | ... |
@@ -138,29 +139,29 @@ function upload_image { |
138 | 138 |
"$(stat -c "%s" $FILES/$flat_fname)" = "0" ]]; then |
139 | 139 |
wget -c $flat_url -O $FILES/$flat_fname |
140 | 140 |
fi |
141 |
- IMAGE="$FILES/${flat_fname}" |
|
141 |
+ image="$FILES/${flat_fname}" |
|
142 | 142 |
else |
143 |
- IMAGE=$(echo $flat_url | sed "s/^file:\/\///g") |
|
144 |
- if [[ ! -f $IMAGE || "$(stat -c "%s" $IMAGE)" == "0" ]]; then |
|
143 |
+ image=$(echo $flat_url | sed "s/^file:\/\///g") |
|
144 |
+ if [[ ! -f $image || "$(stat -c "%s" $image)" == "0" ]]; then |
|
145 | 145 |
echo "Flat disk not found: $flat_url" |
146 | 146 |
return 1 |
147 | 147 |
fi |
148 | 148 |
fi |
149 |
- IMAGE_NAME="${flat_fname}" |
|
149 |
+ image_name="${flat_fname}" |
|
150 | 150 |
vmdk_disktype="preallocated" |
151 | 151 |
elif [[ "$vmdk_create_type" = "streamOptimized" ]]; then |
152 | 152 |
vmdk_disktype="streamOptimized" |
153 | 153 |
elif [[ -z "$vmdk_create_type" ]]; then |
154 | 154 |
# *-flat.vmdk provided: attempt to retrieve the descriptor (*.vmdk) |
155 | 155 |
# to retrieve appropriate metadata |
156 |
- if [[ ${IMAGE_NAME: -5} != "-flat" ]]; then |
|
156 |
+ if [[ ${image_name: -5} != "-flat" ]]; then |
|
157 | 157 |
warn $LINENO "Expected filename suffix: '-flat'."` |
158 |
- `" Filename provided: ${IMAGE_NAME}" |
|
158 |
+ `" Filename provided: ${image_name}" |
|
159 | 159 |
else |
160 |
- descriptor_fname="${IMAGE_NAME:0:${#IMAGE_NAME} - 5}.vmdk" |
|
161 |
- path_len=`expr ${#image_url} - ${#IMAGE_FNAME}` |
|
162 |
- flat_path="${image_url:0:$path_len}" |
|
163 |
- descriptor_url=$flat_path$descriptor_fname |
|
160 |
+ descriptor_fname="${image_name:0:${#image_name} - 5}.vmdk" |
|
161 |
+ path_len=`expr ${#image_url} - ${#image_fname}` |
|
162 |
+ local flat_path="${image_url:0:$path_len}" |
|
163 |
+ local descriptor_url=$flat_path$descriptor_fname |
|
164 | 164 |
warn $LINENO "$descriptor_data_pair_msg"` |
165 | 165 |
`" Attempt to retrieve the descriptor *.vmdk: $descriptor_url" |
166 | 166 |
if [[ $flat_path != file* ]]; then |
... | ... |
@@ -189,35 +190,35 @@ function upload_image { |
189 | 189 |
# NOTE: For backwards compatibility reasons, colons may be used in place |
190 | 190 |
# of semi-colons for property delimiters but they are not permitted |
191 | 191 |
# characters in NTFS filesystems. |
192 |
- property_string=`echo "$IMAGE_NAME" | { grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$' || true; }` |
|
192 |
+ property_string=`echo "$image_name" | { grep -oP '(?<=-)(?!.*-).*[:;].*[:;].*$' || true; }` |
|
193 | 193 |
IFS=':;' read -a props <<< "$property_string" |
194 | 194 |
vmdk_disktype="${props[0]:-$vmdk_disktype}" |
195 | 195 |
vmdk_adapter_type="${props[1]:-$vmdk_adapter_type}" |
196 | 196 |
vmdk_net_adapter="${props[2]:-$vmdk_net_adapter}" |
197 | 197 |
|
198 |
- glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format bare --disk-format vmdk --property vmware_disktype="$vmdk_disktype" --property vmware_adaptertype="$vmdk_adapter_type" --property hw_vif_model="$vmdk_net_adapter" < "${IMAGE}" |
|
198 |
+ glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$image_name" --is-public=True --container-format bare --disk-format vmdk --property vmware_disktype="$vmdk_disktype" --property vmware_adaptertype="$vmdk_adapter_type" --property hw_vif_model="$vmdk_net_adapter" < "${image}" |
|
199 | 199 |
return |
200 | 200 |
fi |
201 | 201 |
|
202 | 202 |
# XenServer-vhd-ovf-format images are provided as .vhd.tgz |
203 | 203 |
# and should not be decompressed prior to loading |
204 | 204 |
if [[ "$image_url" =~ '.vhd.tgz' ]]; then |
205 |
- IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}" |
|
206 |
- FORCE_VM_MODE="" |
|
207 |
- if [[ "$IMAGE_NAME" =~ 'cirros' ]]; then |
|
205 |
+ image_name="${image_fname%.vhd.tgz}" |
|
206 |
+ local force_vm_mode="" |
|
207 |
+ if [[ "$image_name" =~ 'cirros' ]]; then |
|
208 | 208 |
# Cirros VHD image currently only boots in PV mode. |
209 | 209 |
# Nova defaults to PV for all VHD images, but |
210 | 210 |
# the glance setting is needed for booting |
211 | 211 |
# directly from volume. |
212 |
- FORCE_VM_MODE="--property vm_mode=xen" |
|
212 |
+ force_vm_mode="--property vm_mode=xen" |
|
213 | 213 |
fi |
214 | 214 |
glance \ |
215 | 215 |
--os-auth-token $token \ |
216 | 216 |
--os-image-url http://$GLANCE_HOSTPORT \ |
217 | 217 |
image-create \ |
218 |
- --name "$IMAGE_NAME" --is-public=True \ |
|
218 |
+ --name "$image_name" --is-public=True \ |
|
219 | 219 |
--container-format=ovf --disk-format=vhd \ |
220 |
- $FORCE_VM_MODE < "${IMAGE}" |
|
220 |
+ $force_vm_mode < "${image}" |
|
221 | 221 |
return |
222 | 222 |
fi |
223 | 223 |
|
... | ... |
@@ -225,93 +226,94 @@ function upload_image { |
225 | 225 |
# and should not be decompressed prior to loading. |
226 | 226 |
# Setting metadata, so PV mode is used. |
227 | 227 |
if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then |
228 |
- IMAGE_NAME="${IMAGE_FNAME%.xen-raw.tgz}" |
|
228 |
+ image_name="${image_fname%.xen-raw.tgz}" |
|
229 | 229 |
glance \ |
230 | 230 |
--os-auth-token $token \ |
231 | 231 |
--os-image-url http://$GLANCE_HOSTPORT \ |
232 | 232 |
image-create \ |
233 |
- --name "$IMAGE_NAME" --is-public=True \ |
|
233 |
+ --name "$image_name" --is-public=True \ |
|
234 | 234 |
--container-format=tgz --disk-format=raw \ |
235 |
- --property vm_mode=xen < "${IMAGE}" |
|
235 |
+ --property vm_mode=xen < "${image}" |
|
236 | 236 |
return |
237 | 237 |
fi |
238 | 238 |
|
239 |
- KERNEL="" |
|
240 |
- RAMDISK="" |
|
241 |
- DISK_FORMAT="" |
|
242 |
- CONTAINER_FORMAT="" |
|
243 |
- UNPACK="" |
|
244 |
- case "$IMAGE_FNAME" in |
|
239 |
+ local kernel="" |
|
240 |
+ local ramdisk="" |
|
241 |
+ local disk_format="" |
|
242 |
+ local container_format="" |
|
243 |
+ local unpack="" |
|
244 |
+ local img_property="" |
|
245 |
+ case "$image_fname" in |
|
245 | 246 |
*.tar.gz|*.tgz) |
246 | 247 |
# Extract ami and aki files |
247 |
- [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] && |
|
248 |
- IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" || |
|
249 |
- IMAGE_NAME="${IMAGE_FNAME%.tgz}" |
|
250 |
- xdir="$FILES/images/$IMAGE_NAME" |
|
248 |
+ [ "${image_fname%.tar.gz}" != "$image_fname" ] && |
|
249 |
+ image_name="${image_fname%.tar.gz}" || |
|
250 |
+ image_name="${image_fname%.tgz}" |
|
251 |
+ local xdir="$FILES/images/$image_name" |
|
251 | 252 |
rm -Rf "$xdir"; |
252 | 253 |
mkdir "$xdir" |
253 |
- tar -zxf $IMAGE -C "$xdir" |
|
254 |
- KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do |
|
254 |
+ tar -zxf $image -C "$xdir" |
|
255 |
+ kernel=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do |
|
255 | 256 |
[ -f "$f" ] && echo "$f" && break; done; true) |
256 |
- RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do |
|
257 |
+ ramdisk=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do |
|
257 | 258 |
[ -f "$f" ] && echo "$f" && break; done; true) |
258 |
- IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do |
|
259 |
+ image=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do |
|
259 | 260 |
[ -f "$f" ] && echo "$f" && break; done; true) |
260 |
- if [[ -z "$IMAGE_NAME" ]]; then |
|
261 |
- IMAGE_NAME=$(basename "$IMAGE" ".img") |
|
261 |
+ if [[ -z "$image_name" ]]; then |
|
262 |
+ image_name=$(basename "$image" ".img") |
|
262 | 263 |
fi |
263 | 264 |
;; |
264 | 265 |
*.img) |
265 |
- IMAGE_NAME=$(basename "$IMAGE" ".img") |
|
266 |
- format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }') |
|
266 |
+ image_name=$(basename "$image" ".img") |
|
267 |
+ local format=$(qemu-img info ${image} | awk '/^file format/ { print $3; exit }') |
|
267 | 268 |
if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then |
268 |
- DISK_FORMAT=$format |
|
269 |
+ disk_format=$format |
|
269 | 270 |
else |
270 |
- DISK_FORMAT=raw |
|
271 |
+ disk_format=raw |
|
271 | 272 |
fi |
272 |
- CONTAINER_FORMAT=bare |
|
273 |
+ container_format=bare |
|
273 | 274 |
;; |
274 | 275 |
*.img.gz) |
275 |
- IMAGE_NAME=$(basename "$IMAGE" ".img.gz") |
|
276 |
- DISK_FORMAT=raw |
|
277 |
- CONTAINER_FORMAT=bare |
|
278 |
- UNPACK=zcat |
|
276 |
+ image_name=$(basename "$image" ".img.gz") |
|
277 |
+ disk_format=raw |
|
278 |
+ container_format=bare |
|
279 |
+ unpack=zcat |
|
279 | 280 |
;; |
280 | 281 |
*.qcow2) |
281 |
- IMAGE_NAME=$(basename "$IMAGE" ".qcow2") |
|
282 |
- DISK_FORMAT=qcow2 |
|
283 |
- CONTAINER_FORMAT=bare |
|
282 |
+ image_name=$(basename "$image" ".qcow2") |
|
283 |
+ disk_format=qcow2 |
|
284 |
+ container_format=bare |
|
284 | 285 |
;; |
285 | 286 |
*.iso) |
286 |
- IMAGE_NAME=$(basename "$IMAGE" ".iso") |
|
287 |
- DISK_FORMAT=iso |
|
288 |
- CONTAINER_FORMAT=bare |
|
287 |
+ image_name=$(basename "$image" ".iso") |
|
288 |
+ disk_format=iso |
|
289 |
+ container_format=bare |
|
289 | 290 |
;; |
290 |
- *) echo "Do not know what to do with $IMAGE_FNAME"; false;; |
|
291 |
+ *) echo "Do not know what to do with $image_fname"; false;; |
|
291 | 292 |
esac |
292 | 293 |
|
293 | 294 |
if is_arch "ppc64"; then |
294 |
- IMG_PROPERTY="--property hw_cdrom_bus=scsi" |
|
295 |
+ img_property="--property hw_cdrom_bus=scsi" |
|
295 | 296 |
fi |
296 | 297 |
|
297 |
- if [ "$CONTAINER_FORMAT" = "bare" ]; then |
|
298 |
- if [ "$UNPACK" = "zcat" ]; then |
|
299 |
- glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" $IMG_PROPERTY --is-public True --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < <(zcat --force "${IMAGE}") |
|
298 |
+ if [ "$container_format" = "bare" ]; then |
|
299 |
+ if [ "$unpack" = "zcat" ]; then |
|
300 |
+ glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$image_name" $img_property --is-public True --container-format=$container_format --disk-format $disk_format < <(zcat --force "${image}") |
|
300 | 301 |
else |
301 |
- glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" $IMG_PROPERTY --is-public True --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < "${IMAGE}" |
|
302 |
+ glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$image_name" $img_property --is-public True --container-format=$container_format --disk-format $disk_format < "${image}" |
|
302 | 303 |
fi |
303 | 304 |
else |
304 | 305 |
# Use glance client to add the kernel the root filesystem. |
305 | 306 |
# We parse the results of the first upload to get the glance ID of the |
306 | 307 |
# kernel for use when uploading the root filesystem. |
307 |
- KERNEL_ID=""; RAMDISK_ID=""; |
|
308 |
- if [ -n "$KERNEL" ]; then |
|
309 |
- KERNEL_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-kernel" $IMG_PROPERTY --is-public True --container-format aki --disk-format aki < "$KERNEL" | grep ' id ' | get_field 2) |
|
308 |
+ local kernel_id="" ramdisk_id=""; |
|
309 |
+ if [ -n "$kernel" ]; then |
|
310 |
+ kernel_id=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$image_name-kernel" $img_property --is-public True --container-format aki --disk-format aki < "$kernel" | grep ' id ' | get_field 2) |
|
310 | 311 |
fi |
311 |
- if [ -n "$RAMDISK" ]; then |
|
312 |
- RAMDISK_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-ramdisk" $IMG_PROPERTY --is-public True --container-format ari --disk-format ari < "$RAMDISK" | grep ' id ' | get_field 2) |
|
312 |
+ if [ -n "$ramdisk" ]; then |
|
313 |
+ ramdisk_id=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$image_name-ramdisk" $img_property --is-public True --container-format ari --disk-format ari < "$ramdisk" | grep ' id ' | get_field 2) |
|
313 | 314 |
fi |
314 |
- glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "${IMAGE_NAME%.img}" $IMG_PROPERTY --is-public True --container-format ami --disk-format ami ${KERNEL_ID:+--property kernel_id=$KERNEL_ID} ${RAMDISK_ID:+--property ramdisk_id=$RAMDISK_ID} < "${IMAGE}" |
|
315 |
+ glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "${image_name%.img}" $img_property --is-public True --container-format ami --disk-format ami ${kernel_id:+--property kernel_id=$kernel_id} ${ramdisk_id:+--property ramdisk_id=$ramdisk_id} < "${image}" |
|
315 | 316 |
fi |
316 | 317 |
} |
317 | 318 |
|