Browse code

upload_image.sh should handle file URLs

upload_image.sh doesn't handle correctly file URLs: a file URL works only
if the file is already in the cache.
This patch provides support for file URLs of local files (RFC 1738)
http://tools.ietf.org/html/rfc1738

Change-Id: I107299c543cfa189e32848c32eefdbeb51a5e1f5
Closes-Bug: #1251752

Arnaud Legendre authored on 2013/11/16 09:06:03
Showing 1 changed files
... ...
@@ -1337,11 +1337,24 @@ function upload_image() {
1337 1337
     # Create a directory for the downloaded image tarballs.
1338 1338
     mkdir -p $FILES/images
1339 1339
 
1340
-    # Downloads the image (uec ami+aki style), then extracts it.
1341
-    IMAGE_FNAME=`basename "$image_url"`
1342
-    if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
1343
-        wget -c $image_url -O $FILES/$IMAGE_FNAME
1344
-        if [[ $? -ne 0 ]]; then
1340
+    if [[ $image_url != file* ]]; then
1341
+        # Downloads the image (uec ami+aki style), then extracts it.
1342
+        IMAGE_FNAME=`basename "$image_url"`
1343
+        if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
1344
+             wget -c $image_url -O $FILES/$IMAGE_FNAME
1345
+             if [[ $? -ne 0 ]]; then
1346
+                 echo "Not found: $image_url"
1347
+                 return
1348
+             fi
1349
+        fi
1350
+        IMAGE="$FILES/${IMAGE_FNAME}"
1351
+    else
1352
+        # File based URL (RFC 1738): file://host/path
1353
+        # Remote files are not considered here.
1354
+        # *nix: file:///home/user/path/file
1355
+        # windows: file:///C:/Documents%20and%20Settings/user/path/file
1356
+        IMAGE=$(echo $image_url | sed "s/^file:\/\///g")
1357
+        if [[ ! -f $IMAGE || "$(stat -c "%s" $IMAGE)" == "0" ]]; then
1345 1358
             echo "Not found: $image_url"
1346 1359
             return
1347 1360
         fi
... ...
@@ -1349,7 +1362,6 @@ function upload_image() {
1349 1349
 
1350 1350
     # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
1351 1351
     if [[ "$image_url" =~ 'openvz' ]]; then
1352
-        IMAGE="$FILES/${IMAGE_FNAME}"
1353 1352
         IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
1354 1353
         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}"
1355 1354
         return
... ...
@@ -1357,7 +1369,6 @@ function upload_image() {
1357 1357
 
1358 1358
     # vmdk format images
1359 1359
     if [[ "$image_url" =~ '.vmdk' ]]; then
1360
-        IMAGE="$FILES/${IMAGE_FNAME}"
1361 1360
         IMAGE_NAME="${IMAGE_FNAME%.vmdk}"
1362 1361
 
1363 1362
         # Before we can upload vmdk type images to glance, we need to know it's
... ...
@@ -1408,7 +1419,6 @@ function upload_image() {
1408 1408
     # XenServer-vhd-ovf-format images are provided as .vhd.tgz
1409 1409
     # and should not be decompressed prior to loading
1410 1410
     if [[ "$image_url" =~ '.vhd.tgz' ]]; then
1411
-        IMAGE="$FILES/${IMAGE_FNAME}"
1412 1411
         IMAGE_NAME="${IMAGE_FNAME%.vhd.tgz}"
1413 1412
         glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format=ovf --disk-format=vhd < "${IMAGE}"
1414 1413
         return
... ...
@@ -1418,7 +1428,6 @@ function upload_image() {
1418 1418
     # and should not be decompressed prior to loading.
1419 1419
     # Setting metadata, so PV mode is used.
1420 1420
     if [[ "$image_url" =~ '.xen-raw.tgz' ]]; then
1421
-        IMAGE="$FILES/${IMAGE_FNAME}"
1422 1421
         IMAGE_NAME="${IMAGE_FNAME%.xen-raw.tgz}"
1423 1422
         glance \
1424 1423
             --os-auth-token $token \
... ...
@@ -1456,7 +1465,6 @@ function upload_image() {
1456 1456
             fi
1457 1457
             ;;
1458 1458
         *.img)
1459
-            IMAGE="$FILES/$IMAGE_FNAME";
1460 1459
             IMAGE_NAME=$(basename "$IMAGE" ".img")
1461 1460
             format=$(qemu-img info ${IMAGE} | awk '/^file format/ { print $3; exit }')
1462 1461
             if [[ ",qcow2,raw,vdi,vmdk,vpc," =~ ",$format," ]]; then
... ...
@@ -1467,20 +1475,17 @@ function upload_image() {
1467 1467
             CONTAINER_FORMAT=bare
1468 1468
             ;;
1469 1469
         *.img.gz)
1470
-            IMAGE="$FILES/${IMAGE_FNAME}"
1471 1470
             IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
1472 1471
             DISK_FORMAT=raw
1473 1472
             CONTAINER_FORMAT=bare
1474 1473
             UNPACK=zcat
1475 1474
             ;;
1476 1475
         *.qcow2)
1477
-            IMAGE="$FILES/${IMAGE_FNAME}"
1478 1476
             IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
1479 1477
             DISK_FORMAT=qcow2
1480 1478
             CONTAINER_FORMAT=bare
1481 1479
             ;;
1482 1480
         *.iso)
1483
-            IMAGE="$FILES/${IMAGE_FNAME}"
1484 1481
             IMAGE_NAME=$(basename "$IMAGE" ".iso")
1485 1482
             DISK_FORMAT=iso
1486 1483
             CONTAINER_FORMAT=bare