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