Add ci scripts for vstf tu cases.
[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     echo "begin to clean the image"
43     glance image-delete ${MANAGER_IMAGE_NAME};glance image-delete "${TARGET_IMAGE_NAME}";glance image-delete "${TESTER_IMAGE_NAME}"
44     if glance image-list; then
45         for image in $(glance image-list | grep -e "${MANAGER_IMAGE_NAME}" | awk '{print $2}'); do
46             echo "[INFO]clean up image $image"
47             glance image-delete $image || true
48         done
49         for image in $(glance image-list | grep  -e "${TARGET_IMAGE_NAME}" | awk '{print $2}'); do
50             echo "[INFO]clean up image $image"
51             glance image-delete $image || true
52         done
53         for image in $(glance image-list | grep  -e "${TESTER_IMAGE_NAME}" | awk '{print $2}'); do
54             echo "[INFO]clean up image $image"
55             glance image-delete $image || true
56         done
57     fi
58
59     if nova keypair-list; then
60         for key in $(nova keypair-list | grep -e $KEY_NAME | awk '{print $2}'); do
61             echo "[INFO]clean up key $key"
62             nova keypair-delete $key || true
63         done
64     fi
65
66     #check the default flavor m1.large existing
67     if nova flavor-list; then
68         flag=`nova flavor-list | grep "m1.large "`
69         echo "[INFO]the flavor m1.large num is $flag"
70     fi
71     
72     #delete image file
73     rm -rf /tmp/vstf-manager.img;rm -rf /tmp/vstf-agent.img ;rm -rf /tmp/vstf-agent_1.img
74     return 0
75  
76 }
77
78 vstf_register()
79 {
80     echo "[INFO]download vstf images"
81     #download vstf-manager and vstf-agent image
82     #curl --connect-timeout 10 -o /tmp/vstf-manager.img $MANAGER_IMAGE_URL -v
83     #curl --connect-timeout 10 -o /tmp/vstf-agent.img $AGENT_IMAGE_URL -v
84     curl --connect-timeout 10 -o /tmp/vstf-manager.img $MANAGER_IMAGE_URL -v
85     curl --connect-timeout 10 -o /tmp/vstf-agent.img $MANAGER_IMAGE_URL -v
86     curl --connect-timeout 10 -o /tmp/vstf-agent_1.img $MANAGER_IMAGE_URL -v
87     #echo "begin to test downloading from vstf directory!!!!!!"
88     #curl --connect-timeout 10 -o /tmp/vstf-test.txt
89     #echo "begin to cat /tmp/vstf-test.txt vstf directory!!!!!!"
90     #cat /tmp/vstf-test.txt
91     #register
92     echo "[INFO]register vstf manager and agent images"
93     result=$(glance image-create \
94         --name $MANAGER_IMAGE_NAME \
95         --disk-format qcow2 \
96         --container-format bare \
97         --file /tmp/vstf-manager.img)
98     echo "Manager image register result $result."
99
100     result=$(glance image-create \
101         --name $TESTER_IMAGE_NAME \
102         --disk-format qcow2 \
103         --container-format bare \
104         --file /tmp/vstf-agent.img)
105     echo "Agent image register result $result."
106
107     result=$(glance image-create \
108         --name $TARGET_IMAGE_NAME \
109         --disk-format qcow2 \
110         --container-format bare \
111         --file /tmp/vstf-agent_1.img)
112     echo "Agent image register result $result."
113
114     glance image-list
115
116     rm -rf /tmp/vstf-manager.img;rm -rf /tmp/vstf-agent.img ;rm -rf /tmp/vstf-agent_1.img
117 }
118
119 #vstf logic function here
120 vstf_create_heat_template()
121 {
122     echo "create vstf instance using heat template"
123     echo "upload keypair"
124     nova keypair-add --pub_key $KEY_PATH/bottlenecks_key.pub $KEY_NAME
125     nova keypair-list   
126     echo "use heat template to create stack"
127     cd ${HOT_PATH}
128     heat stack-create vstf -f ${TEMPLATE_NAME}
129
130 }
131
132 wait_heat_stack_complete()
133 {
134     retry=0
135     while true
136     do
137         status=$(heat stack-list | grep vstf | awk '{print $6}')
138         if [ x$status = x"CREATE_COMPLETE" ]; then
139             echo "vstf stacke create complete"
140             heat stack-show vstf
141             nova list | grep vstf-
142             break;
143         elif [ x$status = x"CREATE_FAILED" ]; then
144             echo "bottlenecks stacke create failed !!!"
145             heat stack-show vstf
146             exit 1
147         fi
148
149         if [ "$BOTTLENECKS_DEBUG" == "True" ]; then
150             heat stack-show vstf
151             nova list | grep vstf-
152             for i in $(nova list | grep "vstf-" | grep ERROR | awk '{print $2}')
153             do
154                  nova show $i
155             done
156         fi
157         sleep 1
158         let retry+=1
159         if [[ $retry -ge $1 ]];then
160             echo "Heat vstf stack create timeout, status $status !!!"
161             exit 1
162         fi
163     done
164 }
165
166
167 vstf_check_instance_ok()
168 {
169     wait_heat_stack_complete 120 
170     
171     return 0
172 }
173
174 vstf_launch()
175 {
176     cd ${HOT_PATH}
177     bash -x ./launch_vstf.sh
178     
179 }
180
181 vstf_test()
182 {
183     cd ${HOT_PATH}
184     bash -x ./vstf_test.sh
185 }
186
187 main()
188 {
189     echo "bottlenecks vstf: create instances with heat template"
190
191     BOTTLENECKS_REPO=https://gerrit.opnfv.org/gerrit/bottlenecks
192     BOTTLENECKS_REPO_DIR=/tmp/opnfvrepo_vstf/bottlenecks
193     #vstf parameter here
194     MANAGER_IMAGE_URL=http://artifacts.opnfv.org/bottlenecks/vstf-manager-new.img
195     AGENT_IMAGE_URL=http://artifacts.opnfv.org/bottlenecks/vstf-agent-new.img
196     #MANAGER_IMAGE_URL=http://artifacts.opnfv.org/bottlenecks/rubbos/bottlenecks-trusty-server.img
197     #AGENT_IMAGE_URL=http://artifacts.opnfv.org/bottlenecks/rubbos/bottlenecks-trusty-server.img
198     MANAGER_IMAGE_NAME="vstf-manager"
199     TESTER_IMAGE_NAME="vstf-tester"
200     TARGET_IMAGE_NAME="vstf-target" 
201    
202     KEY_PATH=$BOTTLENECKS_REPO_DIR/utils/infra_setup/bottlenecks_key
203     HOT_PATH=$BOTTLENECKS_REPO_DIR/utils/infra_setup/heat_template/vstf_heat_template
204     KEY_NAME=vstf-key
205     #use the default openstack flavor m1.large
206     FLAVOR_NAME="m1.large"
207     TEMPLATE_NAME=bottleneck_vstf.yaml
208     PUBLIC_NET_NAME=net04_ext
209
210     #load adminrc 
211     bottlenecks_env_prepare
212     #vstf function here
213     vstf_cleanup
214     vstf_register
215     vstf_create_heat_template
216     vstf_check_instance_ok
217     heat stack-list
218     nova list
219     sleep 100
220     vstf_launch
221     sleep 30
222     vstf_test
223     sleep 10
224     echo "[INFO]bottleneck vstf testsuite done ,results in the directory ${HOT_PATH}/result"
225     echo "[INFO]Begin to clean up the vstf heat-stack and image"
226     vstf_cleanup
227     sleep 30
228     heat stack-list
229     nova list
230     
231 }
232
233 main
234 set +ex