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