amend heat template instance creation script
[bottlenecks.git] / utils / infra_setup / heat_template / HOT_create_instance.sh
1 #!/bin/bash
2
3 set -ex
4
5 bottlenecks_env_prepare()
6 {
7    if [ -d $BOTTLENECKS_REPO_DIR ]; then
8        rm -rf ${BOTTLENECKS_REPO_DIR}
9    fi
10
11    mkdir -p ${BOTTLENECKS_REPO_DIR}
12    git config --global http.sslVerify false
13    git clone ${BOTTLENECKS_REPO} ${BOTTLENECKS_REPO_DIR}
14
15    source $BOTTLENECKS_REPO_DIR/rubbos/rubbos_scripts/1-1-1/scripts/env_preparation.sh
16 }
17
18 bottlenecks_create_instance()
19 {
20    echo "create bottlenecks instance using heat template"
21
22    echo "upload keypair"
23    nova keypair-add --pub_key $KEY_PATH/bottlenecks_key.pub $KEY_NAME
24    #need FIX, only upload the public key? should be keypair
25
26    echo "use heat template to create stack"
27    cd $HOT_PATH
28    heat stack-create bottlenecks -f ${TEMPLATE_NAME} -P "image=$IMAGE_NAME;key=$KEY_NAME;public_network=$PUBLIC_NET_NAME"
29    sleep 60
30    heat stack-list
31    heat stack-show bottlenecks
32    nova list
33    heat stack-delete bottlenecks
34
35    #need FIX, use stack to create 9 VMs
36 }
37
38 bottlenecks_cleanup()
39 {
40    echo "clean up bottlenecks images"
41
42    if ! glance image-list; then
43        return
44    fi
45
46    #need to check
47    for image in $(glance image-list | grep -e $IMAGE_NAME | awk '{print $2}'); do
48        echo "clean up image $image"
49        glance image-delete $iamge || true
50    done
51 }
52
53 bottlenecks_build_image()
54 {
55    echo "build bottlenecks image"
56
57    #need FIX
58 }
59
60 bottlenecks_load_cirros_image()
61 {
62    echo "load bottlenecks cirros image"
63
64    wget http://download.cirros-cloud.net/0.3.3/cirros-0.3.3-x86_64-disk.img -O /tmp/cirros.img
65
66    result=$(glance image-create \
67        --name cirros-0.3.3 \
68        --disk-format qcow2 \
69        --container-format bare \
70        --file /tmp/cirros.img)
71    echo "$result"
72
73    rm -rf /tmp/cirros.img
74
75    IMAGE_ID_CIRROS=$(echo "$result" | grep " id " | awk '{print $(NF-1)}')
76    if [ -z "$IMAGE_ID_CIRROS" ]; then
77         echo 'failed to upload cirros image to openstack'
78         exit 1
79    fi
80
81    echo "cirros image id: $IMAGE_ID_CIRROS"
82 }
83
84 bottlenecks_load_image()
85 {
86    echo "load bottlenecks image"
87
88    result=$(glance --os-image-api-version 1 image-create \
89       --name $IMAGE_NAME \
90       --is-public true --disk-format qcow2 \
91       --container-format bare \
92       --file $IMAGE_FILE_NAME)
93    echo "$result"
94
95    GLANCE_IMAGE_ID=$(echo "$result" | grep " id " | awk '{print $(NF-1)}')
96
97    if [ -z "$GLANCE_IMAGE_ID" ]; then
98        echo 'add image to glance failed'
99        exit 1
100    fi
101
102    sudo rm -f $IMAGE_FILE_NAME
103
104    echo "add glance image completed: $GLANCE_IMAGE_ID"
105 }
106
107 main()
108 {
109    echo "create instances with heat template"
110
111    BOTTLENECKS_REPO=https://gerrit.opnfv.org/gerrit/bottlenecks
112    BOTTLENECKS_REPO_DIR=/tmp/opnfvrepo/bottlenecks
113    #IMAGE_URL=http://205.177.226.235:9999
114    IMAGE_NAME=cirros-0.3.3
115    #need FIX, need a script to transfer the image from the url to be the installer images
116    KEY_PATH=$BOTTLENECKS_REPO_DIR/utils/infra_setup/bottlenecks_key
117    HOT_PATH=$BOTTLENECKS_REPO_DIR/utils/infra_setup/heat_template
118    KEY_NAME=bottlenecks_key
119    TEMPLATE_NAME=bottlenecks_template1.yaml
120    PUBLIC_NET_NAME=net04_ext
121    #need FIX
122    #IMAGE_FILE_NAME=""
123
124    bottlenecks_env_prepare
125    #bottlenecks_cleanup
126    #bottlenecks_build_image
127    bottlenecks_load_cirros_image
128    bottlenecks_create_instance
129 }
130
131 main
132 set +ex
133