Browse code

Add tools/upload_image.sh

* moves the image upload logic from stack.sh to functions upload_image()
* tools/upload_image.sh which is a thin wrapper around upload_image()

Change-Id: I8746beebf50cf623b6fe903d6497e66e3fa5dda6

Dean Troyer authored on 2012/04/14 05:58:37
Showing 3 changed files
... ...
@@ -419,7 +419,7 @@ function is_service_enabled() {
419 419
 
420 420
 # remove extra commas from the input string (ENABLED_SERVICES)
421 421
 function _cleanup_service_list () {
422
-	echo "$1" | sed -e '
422
+    echo "$1" | sed -e '
423 423
         s/,,/,/g;
424 424
         s/^,//;
425 425
         s/,$//
... ...
@@ -618,6 +618,105 @@ function trueorfalse() {
618 618
 }
619 619
 
620 620
 
621
+# Retrieve an image from a URL and upload into Glance
622
+# Uses the following variables:
623
+#   **FILES** must be set to the cache dir
624
+#   **GLANCE_HOSTPORT**
625
+# upload_image image-url glance-token
626
+function upload_image() {
627
+    local image_url=$1
628
+    local token=$2
629
+
630
+    # Create a directory for the downloaded image tarballs.
631
+    mkdir -p $FILES/images
632
+
633
+    # Downloads the image (uec ami+aki style), then extracts it.
634
+    IMAGE_FNAME=`basename "$image_url"`
635
+    if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
636
+        wget -c $image_url -O $FILES/$IMAGE_FNAME
637
+        if [[ $? -ne 0 ]]; then
638
+            echo "Not found: $image_url"
639
+            return
640
+        fi
641
+    fi
642
+
643
+    # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
644
+    if [[ "$image_url" =~ 'openvz' ]]; then
645
+        IMAGE="$FILES/${IMAGE_FNAME}"
646
+        IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
647
+        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}"
648
+        return
649
+    fi
650
+
651
+    KERNEL=""
652
+    RAMDISK=""
653
+    DISK_FORMAT=""
654
+    CONTAINER_FORMAT=""
655
+    UNPACK=""
656
+    case "$IMAGE_FNAME" in
657
+        *.tar.gz|*.tgz)
658
+            # Extract ami and aki files
659
+            [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
660
+                IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
661
+                IMAGE_NAME="${IMAGE_FNAME%.tgz}"
662
+            xdir="$FILES/images/$IMAGE_NAME"
663
+            rm -Rf "$xdir";
664
+            mkdir "$xdir"
665
+            tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
666
+            KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
667
+                     [ -f "$f" ] && echo "$f" && break; done; true)
668
+            RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
669
+                     [ -f "$f" ] && echo "$f" && break; done; true)
670
+            IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
671
+                     [ -f "$f" ] && echo "$f" && break; done; true)
672
+            if [[ -z "$IMAGE_NAME" ]]; then
673
+                IMAGE_NAME=$(basename "$IMAGE" ".img")
674
+            fi
675
+            ;;
676
+        *.img)
677
+            IMAGE="$FILES/$IMAGE_FNAME";
678
+            IMAGE_NAME=$(basename "$IMAGE" ".img")
679
+            DISK_FORMAT=raw
680
+            CONTAINER_FORMAT=bare
681
+            ;;
682
+        *.img.gz)
683
+            IMAGE="$FILES/${IMAGE_FNAME}"
684
+            IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
685
+            DISK_FORMAT=raw
686
+            CONTAINER_FORMAT=bare
687
+            UNPACK=zcat
688
+            ;;
689
+        *.qcow2)
690
+            IMAGE="$FILES/${IMAGE_FNAME}"
691
+            IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
692
+            DISK_FORMAT=qcow2
693
+            CONTAINER_FORMAT=bare
694
+            ;;
695
+        *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
696
+    esac
697
+
698
+    if [ "$CONTAINER_FORMAT" = "bare" ]; then
699
+        if [ "$UNPACK" = "zcat" ]; then
700
+            glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --public --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < <(zcat --force "${IMAGE}")
701
+        else
702
+            glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --public --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < "${IMAGE}"
703
+        fi
704
+    else
705
+        # Use glance client to add the kernel the root filesystem.
706
+        # We parse the results of the first upload to get the glance ID of the
707
+        # kernel for use when uploading the root filesystem.
708
+        KERNEL_ID=""; RAMDISK_ID="";
709
+        if [ -n "$KERNEL" ]; then
710
+            KERNEL_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-kernel" --public --container-format aki --disk-format aki < "$KERNEL" | grep ' id ' | get_field 2)
711
+        fi
712
+        if [ -n "$RAMDISK" ]; then
713
+            RAMDISK_ID=$(glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-ramdisk" --public --container-format ari --disk-format ari < "$RAMDISK" | grep ' id ' | get_field 2)
714
+        fi
715
+        glance --os-auth-token $token --os-image-url http://$GLANCE_HOSTPORT image-create --name "${IMAGE_NAME%.img}" --public --container-format ami --disk-format ami ${KERNEL_ID:+--property kernel_id=$KERNEL_ID} ${RAMDISK_ID:+--property ramdisk_id=$RAMDISK_ID} < "${IMAGE}"
716
+    fi
717
+}
718
+
719
+
621 720
 # yum wrapper to set arguments correctly
622 721
 # yum_install package [package ...]
623 722
 function yum_install() {
... ...
@@ -2141,21 +2141,17 @@ is_service_enabled swift3 || \
2141 2141
 
2142 2142
 # Upload an image to glance.
2143 2143
 #
2144
-# The default image is a small ***TTY*** testing image, which lets you login
2145
-# the username/password of root/password.
2144
+# The default image is cirros, a small testing image, which lets you login as root
2146 2145
 #
2147
-# TTY also uses ``cloud-init``, supporting login via keypair and sending scripts as
2146
+# cirros also uses ``cloud-init``, supporting login via keypair and sending scripts as
2148 2147
 # userdata.  See https://help.ubuntu.com/community/CloudInit for more on cloud-init
2149 2148
 #
2150 2149
 # Override ``IMAGE_URLS`` with a comma-separated list of uec images.
2151 2150
 #
2152
-#  * **natty**: http://uec-images.ubuntu.com/natty/current/natty-server-cloudimg-amd64.tar.gz
2153 2151
 #  * **oneiric**: http://uec-images.ubuntu.com/oneiric/current/oneiric-server-cloudimg-amd64.tar.gz
2152
+#  * **precise**: http://uec-images.ubuntu.com/precise/current/precise-server-cloudimg-amd64.tar.gz
2154 2153
 
2155 2154
 if is_service_enabled g-reg; then
2156
-    # Create a directory for the downloaded image tarballs.
2157
-    mkdir -p $FILES/images
2158
-
2159 2155
     TOKEN=$(keystone  token-get | grep ' id ' | get_field 2)
2160 2156
 
2161 2157
     # Option to upload legacy ami-tty, which works with xenserver
... ...
@@ -2164,87 +2160,7 @@ if is_service_enabled g-reg; then
2164 2164
     fi
2165 2165
 
2166 2166
     for image_url in ${IMAGE_URLS//,/ }; do
2167
-        # Downloads the image (uec ami+aki style), then extracts it.
2168
-        IMAGE_FNAME=`basename "$image_url"`
2169
-        if [[ ! -f $FILES/$IMAGE_FNAME || "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
2170
-            wget -c $image_url -O $FILES/$IMAGE_FNAME
2171
-        fi
2172
-
2173
-        # OpenVZ-format images are provided as .tar.gz, but not decompressed prior to loading
2174
-        if [[ "$image_url" =~ 'openvz' ]]; then
2175
-            IMAGE="$FILES/${IMAGE_FNAME}"
2176
-            IMAGE_NAME="${IMAGE_FNAME%.tar.gz}"
2177
-            glance --os-auth-token $TOKEN --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --public --container-format ami --disk-format ami < "$IMAGE"
2178
-            continue
2179
-        fi
2180
-
2181
-        KERNEL=""
2182
-        RAMDISK=""
2183
-        DISK_FORMAT=""
2184
-        CONTAINER_FORMAT=""
2185
-        UNPACK=""
2186
-        
2187
-        case "$IMAGE_FNAME" in
2188
-            *.tar.gz|*.tgz)
2189
-                # Extract ami and aki files
2190
-                [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
2191
-                    IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
2192
-                    IMAGE_NAME="${IMAGE_FNAME%.tgz}"
2193
-                xdir="$FILES/images/$IMAGE_NAME"
2194
-                rm -Rf "$xdir";
2195
-                mkdir "$xdir"
2196
-                tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
2197
-                KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
2198
-                         [ -f "$f" ] && echo "$f" && break; done; true)
2199
-                RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
2200
-                         [ -f "$f" ] && echo "$f" && break; done; true)
2201
-                IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
2202
-                         [ -f "$f" ] && echo "$f" && break; done; true)
2203
-                if [[ -z "$IMAGE_NAME" ]]; then
2204
-                    IMAGE_NAME=$(basename "$IMAGE" ".img")
2205
-                fi
2206
-                ;;
2207
-            *.img)
2208
-                IMAGE="$FILES/$IMAGE_FNAME";
2209
-                IMAGE_NAME=$(basename "$IMAGE" ".img")
2210
-                DISK_FORMAT=raw
2211
-                CONTAINER_FORMAT=bare
2212
-                ;;
2213
-            *.img.gz)
2214
-                IMAGE="$FILES/${IMAGE_FNAME}"
2215
-                IMAGE_NAME=$(basename "$IMAGE" ".img.gz")
2216
-                DISK_FORMAT=raw
2217
-                CONTAINER_FORMAT=bare
2218
-                UNPACK=zcat
2219
-                ;;
2220
-            *.qcow2)
2221
-                IMAGE="$FILES/${IMAGE_FNAME}"
2222
-                IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
2223
-                DISK_FORMAT=qcow2
2224
-                CONTAINER_FORMAT=bare
2225
-                ;;
2226
-            *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
2227
-        esac
2228
-
2229
-        if [ "$CONTAINER_FORMAT" = "bare" ]; then
2230
-            if [ "$UNPACK" = "zcat" ]; then
2231
-                glance --os-auth-token $TOKEN --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT <  <(zcat --force "${IMAGE}")
2232
-            else
2233
-                glance --os-auth-token $TOKEN --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME" --is-public=True --container-format=$CONTAINER_FORMAT --disk-format $DISK_FORMAT < ${IMAGE}
2234
-            fi
2235
-        else
2236
-            # Use glance client to add the kernel the root filesystem.
2237
-            # We parse the results of the first upload to get the glance ID of the
2238
-            # kernel for use when uploading the root filesystem.
2239
-            KERNEL_ID=""; RAMDISK_ID="";
2240
-            if [ -n "$KERNEL" ]; then
2241
-                KERNEL_ID=$(glance --os-auth-token $TOKEN --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-kernel" --is-public=True --container-format aki --disk-format aki < "$KERNEL" | grep ' id ' | get_field 2)
2242
-            fi
2243
-            if [ -n "$RAMDISK" ]; then
2244
-                RAMDISK_ID=$(glance --os-auth-token $TOKEN --os-image-url http://$GLANCE_HOSTPORT image-create --name "$IMAGE_NAME-ramdisk" --is-public=True --container-format ari --disk-format ari < "$RAMDISK" | grep ' id ' | get_field 2)
2245
-            fi
2246
-            glance --os-auth-token $TOKEN --os-image-url http://$GLANCE_HOSTPORT image-create --name "${IMAGE_NAME%.img}" --is-public=True --container-format ami --disk-format ami ${KERNEL_ID:+--property kernel_id=$KERNEL_ID} ${RAMDISK_ID:+--property ramdisk_id=$RAMDISK_ID} < "${IMAGE}"
2247
-        fi
2167
+        upload_image $image_url $TOKEN
2248 2168
     done
2249 2169
 fi
2250 2170
 
2251 2171
new file mode 100755
... ...
@@ -0,0 +1,42 @@
0
+#!/usr/bin/env bash
1
+# upload_image.sh - Retrieve and upload an image into Glance
2
+#
3
+# upload_image.sh <image-url>
4
+#
5
+# Assumes credentials are set via OS_* environment variables
6
+
7
+function usage {
8
+    echo "$0 - Retrieve and upload an image into Glance"
9
+    echo ""
10
+    echo "Usage: $0 <image-url> [...]"
11
+    echo ""
12
+    echo "Assumes credentials are set via OS_* environment variables"
13
+    exit 1
14
+}
15
+
16
+# Keep track of the current directory
17
+TOOLS_DIR=$(cd $(dirname "$0") && pwd)
18
+TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
19
+
20
+# Import common functions
21
+source $TOP_DIR/functions
22
+
23
+# Import configuration
24
+source $TOP_DIR/openrc "" "" "" ""
25
+
26
+# Find the cache dir
27
+FILES=$TOP_DIR/files
28
+
29
+if [[ -z "$1" ]]; then
30
+    usage
31
+fi
32
+
33
+# Get a token to authenticate to glance
34
+TOKEN=$(keystone token-get | grep ' id ' | get_field 2)
35
+
36
+# Glance connection info.  Note the port must be specified.
37
+GLANCE_HOSTPORT=${GLANCE_HOSTPORT:-$GLANCE_HOST:9292}
38
+
39
+for IMAGE in "$*"; do
40
+    upload_image $IMAGE $TOKEN
41
+done