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