JIRA: BOTTLENECKS-29
[bottlenecks.git] / utils / infra_setup / heat_template / vstf_heat_template / vstf_HOT_create_instance.sh
1 #!/bin/bash
2
3 set -ex
4
5 GERRIT_REFSPEC_DEBUG=$1
6
7 echo "vstf DEBUG test"
8 echo "vstf workflow goes here"
9
10 bottlenecks_env_prepare()
11 {
12     if [ -d $BOTTLENECKS_REPO_DIR ]; then
13         rm -rf ${BOTTLENECKS_REPO_DIR}
14     fi
15
16     mkdir -p ${BOTTLENECKS_REPO_DIR}
17     git config --global http.sslVerify false
18     git clone ${BOTTLENECKS_REPO} ${BOTTLENECKS_REPO_DIR}
19     if [ x"$GERRIT_REFSPEC_DEBUG" != x ]; then
20         cd ${BOTTLENECKS_REPO_DIR}
21         git fetch $BOTTLENECKS_REPO $GERRIT_REFSPEC_DEBUG && git checkout FETCH_HEAD
22         cd -
23     fi
24
25     #obtain installer(openstack) IP, etc, use rubbos's temporarily, later we can amend this
26     source $BOTTLENECKS_REPO_DIR/rubbos/rubbos_scripts/1-1-1/scripts/env_preparation.sh
27 }
28
29 vstf_cleanup()
30 {
31     echo "[INFO]Begin to clean up vstf heat-stack ,glance images and keypairs"
32     #heat stack-delete bottlenecks
33     sleep 30
34     if heat stack-list; then
35         for stack in $(heat stack-list | grep -e " vstf " | awk '{print $2}'); do
36             echo "[INFO]clean up stack $stack"
37             heat stack-delete $stack || true
38             sleep 30
39         done
40     fi
41     
42     glance image-delete ${MANAGER_IMAGE_NAME};glance image-delete "${AGENT_IMAGE_NAME}"
43     if glance image-list; then
44         for image in $(glance image-list | grep -e "${MANAGER_IMAGE_NAME}" | awk '{print $2}'); do
45             echo "[INFO]clean up image $image"
46             glance image-delete $image || true
47         done
48         for image in $(glance image-list | grep  -e "${AGENT_IMAGE_NAME}" | awk '{print $2}'); do
49             echo "[INFO]clean up image $image"
50             glance image-delete $image || true
51         done
52     fi
53
54     if nova keypair-list; then
55         for key in $(nova keypair-list | grep -e $KEY_NAME | awk '{print $2}'); do
56             echo "[INFO]clean up key $key"
57             nova keypair-delete $key || true
58         done
59     fi
60
61     #check the default flavor m1.large existing
62     if nova flavor-list; then
63         flag=`nova flavor-list | grep "m1.large "`
64         echo "[INFO]the flavor m1.large num is $flag"
65     fi
66     return 0
67 }
68
69 vstf_register()
70 {
71     echo "[INFO]download vstf images"
72     #download vstf-manager and vstf-agent image
73     #curl --connect-timeout 10 -o /tmp/vstf-manager.img $MANAGER_IMAGE_URL -v
74     #curl --connect-timeout 10 -o /tmp/vstf-agent.img $AGENT_IMAGE_URL -v
75     curl --connect-timeout 10 -o /tmp/vstf-manager.img $MANAGER_IMAGE_URL -v
76     curl --connect-timeout 10 -o /tmp/vstf-agent.img $AGENT_IMAGE_URL -v
77     #echo "begin to test downloading from vstf directory!!!!!!"
78     #curl --connect-timeout 10 -o /tmp/vstf-test.txt
79     #echo "begin to cat /tmp/vstf-test.txt vstf directory!!!!!!"
80     #cat /tmp/vstf-test.txt
81     #register
82     echo "[INFO]register vstf manager and agent images"
83     result=$(glance image-create \
84         --name $MANAGER_IMAGE_NAME \
85         --disk-format qcow2 \
86         --container-format bare \
87         --file /tmp/vstf-manager.img)
88     echo "Manager image register result $result."
89
90     result=$(glance image-create \
91         --name $AGENT_IMAGE_NAME \
92         --disk-format qcow2 \
93         --container-format bare \
94         --file /tmp/vstf-agent.img)
95     echo "Agent image register result $result."
96
97     glance image-list
98
99     rm -rf /tmp/vstf-manager.img;rm -rf /tmp/vstf-agent.img
100 }
101
102 #vstf logic function here
103 vstf_create_heat_template()
104 {
105     echo "create vstf instance using heat template"
106     echo "upload keypair"
107     nova keypair-add --pub_key $KEY_PATH/bottlenecks_key.pub $KEY_NAME
108     nova keypair-list   
109     echo "use heat template to create stack"
110     cd ${HOT_PATH}
111     heat stack-create vstf -f ${TEMPLATE_NAME}
112
113 }
114
115 wait_heat_stack_complete()
116 {
117     retry=0
118     while true
119     do
120         status=$(heat stack-list | grep vstf | awk '{print $6}')
121         if [ x$status = x"CREATE_COMPLETE" ]; then
122             echo "vstf stacke create complete"
123             heat stack-show vstf
124             nova list | grep vstf-
125             break;
126         elif [ x$status = x"CREATE_FAILED" ]; then
127             echo "bottlenecks stacke create failed !!!"
128             heat stack-show vstf
129             exit 1
130         fi
131
132         if [ "$BOTTLENECKS_DEBUG" == "True" ]; then
133             heat stack-show vstf
134             nova list | grep vstf-
135             for i in $(nova list | grep "vstf-" | grep ERROR | awk '{print $2}')
136             do
137                  nova show $i
138             done
139         fi
140         sleep 1
141         let retry+=1
142         if [[ $retry -ge $1 ]];then
143             echo "Heat vstf stack create timeout, status $status !!!"
144             exit 1
145         fi
146     done
147 }
148
149
150 vstf_check_instance_ok()
151 {
152     wait_heat_stack_complete 120 
153     
154     return 0
155 }
156
157 vstf_launch()
158 {
159     cd ${HOT_PATH}
160     bash -x ./launch_vstf.sh
161     
162 }
163
164 vstf_test()
165 {
166     cd ${HOT_PATH}
167     bash -x ./vstf_test.sh
168 }
169
170 main()
171 {
172     echo "bottlenecks vstf: create instances with heat template"
173
174     BOTTLENECKS_REPO=https://gerrit.opnfv.org/gerrit/bottlenecks
175     BOTTLENECKS_REPO_DIR=/tmp/opnfvrepo_vstf/bottlenecks
176     #vstf parameter here
177     MANAGER_IMAGE_URL=http://artifacts.opnfv.org/bottlenecks/vstf/vstf-manager.img
178     AGENT_IMAGE_URL=http://artifacts.opnfv.org/bottlenecks/vstf/vstf-agent.img
179     MANAGER_IMAGE_URL=http://artifacts.opnfv.org/bottlenecks/rubbos/bottlenecks-trusty-server.img
180     AGENT_IMAGE_URL=http://artifacts.opnfv.org/bottlenecks/rubbos/bottlenecks-trusty-server.img
181     MANAGER_IMAGE_NAME="vstf-manager"
182     AGENT_IMAGE_NAME="vstf-agent"
183     KEY_PATH=$BOTTLENECKS_REPO_DIR/utils/infra_setup/bottlenecks_key
184     HOT_PATH=$BOTTLENECKS_REPO_DIR/utils/infra_setup/heat_template/vstf_heat_template
185     KEY_NAME=vstf-key
186     #use the default openstack flavor m1.large
187     FLAVOR_NAME="m1.large"
188     TEMPLATE_NAME=bottleneck_vstf.yaml
189     PUBLIC_NET_NAME=net04_ext
190
191     #load adminrc 
192     bottlenecks_env_prepare
193
194     #vstf function here
195     vstf_cleanup
196     vstf_register
197     vstf_create_heat_template
198     vstf_check_instance_ok
199     heat stack-list
200     nova list
201     sleep 100
202     vstf_launch
203     #sleep 30
204     vstf_test
205     #sleep 10
206     #echo "[INFO]bottleneck vstf testsuite done ,results in the directory ${HOT_PATH}/result"
207     echo "[INFO]Begin to clean up the vstf heat-stack and image"
208     vstf_cleanup
209     sleep 30
210     heat stack-list
211     nova list
212     
213 }
214
215 main
216 set +ex