1a80d9503aedeafd42f8d7be7785bafa6fc24624
[bottlenecks.git] / utils / infra_setup / heat_template / rubbos_heat_template / 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
11 set -x
12
13 git_checkout()
14 {
15     if git cat-file -e $1^{commit} 2>/dev/null; then
16         # branch, tag or sha1 object
17         git checkout $1
18     else
19         # refspec / changeset
20         git fetch --tags --progress $2 $1
21         git checkout FETCH_HEAD
22     fi
23 }
24
25 bottlenecks_env_prepare() {
26     set -e
27     echo "Bottlenecks env prepare start $(date)"
28     git config --global http.sslVerify false
29
30     if [ ! -d $BOTTLENECKS_REPO_DIR ]; then
31         git clone $BOTTLENECKS_REPO $BOTTLENECKS_REPO_DIR
32     fi
33     cd $BOTTLENECKS_REPO_DIR
34     git checkout master && git pull
35     git_checkout $BOTTLENECKS_BRANCH $BOTTLENECKS_REPO
36     cd -
37
38     echo "Creating openstack credentials .."
39     if [ ! -d $RELENG_REPO_DIR ]; then
40         git clone $RELENG_REPO $RELENG_REPO_DIR
41     fi
42     cd $RELENG_REPO_DIR
43     git checkout master && git pull
44     git_checkout $RELENG_BRANCH $RELENG_REPO
45     cd -
46
47     # Create openstack credentials
48     $RELENG_REPO_DIR/utils/fetch_os_creds.sh \
49         -d /tmp/openrc \
50         -i ${INSTALLER_TYPE} -a ${INSTALLER_IP}
51
52     source /tmp/openrc
53
54     chmod 600 $KEY_PATH/bottlenecks_key
55
56     echo "Bottlenecks env prepare end $(date)"
57     set +e
58 }
59
60 wait_heat_stack_complete() {
61     retry=0
62     while true
63     do
64         status=$(heat stack-list | grep bottlenecks | awk '{print $6}')
65         if [ x$status = x"CREATE_COMPLETE" ]; then
66             echo "bottlenecks stacke create complete"
67             heat stack-show bottlenecks
68             nova list | grep rubbos_
69             break;
70         elif [ x$status = x"CREATE_FAILED" ]; then
71             echo "bottlenecks stacke create failed !!!"
72             heat stack-show bottlenecks
73             exit 1
74         fi
75
76         #if [ $BOTTLENECKS_DEBUG = True ]; then
77         if false; then
78             heat stack-show bottlenecks
79             nova list | grep rubbos_
80             for i in $(nova list | grep rubbos_ | grep ERROR | awk '{print $2}')
81             do
82                  nova show $i
83             done
84         fi
85         sleep 1
86         let retry+=1
87         if [[ $retry -ge $1 ]];then
88             echo "Heat stack create timeout, status $status !!!"
89             exit 1
90         fi
91     done
92 }
93
94 wait_rubbos_control_ok() {
95     control_ip=$(nova list | grep rubbos_control | awk '{print $13}')
96
97     retry=0
98     until timeout 3s ssh $ssh_args ubuntu@$control_ip "exit" >/dev/null 2>&1
99     do
100         echo "retry connect rubbos control $retry"
101         sleep 1
102         let retry+=1
103         if [[ $retry -ge $1 ]];then
104             echo "rubbos control start timeout !!!"
105             exit 1
106         fi
107     done
108     ssh $ssh_args ubuntu@$control_ip "uname -a"
109 }
110
111 bottlenecks_check_instance_ok()
112 {
113     echo "Bottlenecks check instance ok start $(date)"
114
115     wait_heat_stack_complete 120
116     wait_rubbos_control_ok 300
117     nova list | grep rubbos_
118     if [ $BOTTLENECKS_DEBUG = True ]; then
119         date
120         while true
121         do
122             for i in rubbos_benchmark rubbos_client1 rubbos_client2 rubbos_client3 \
123                      rubbos_client4 rubbos_control rubbos_httpd rubbos_mysql1 rubbos_tomcat1
124             do
125                echo "logging $i"
126                nova console-log $i | tail -n 2 | grep Cloud-init | grep finished
127                if [ $? != 0 ]; then
128                    break
129                fi
130                if [ $i = rubbos_tomcat1 ]; then
131                    echo "all vm Cloud-init finished!"
132                    date
133                    return
134                fi
135             done
136             sleep 10
137         done
138     fi
139
140     echo "Bottlenecks check instance ok end $(date)"
141 }
142
143 bottlenecks_create_instance()
144 {
145     echo "Bottlenecks create instance using heat template start $(date)"
146
147     echo "upload keypair"
148     nova keypair-add --pub_key $KEY_PATH/bottlenecks_key.pub $KEY_NAME
149
150     echo "create flavor"
151     nova flavor-create $FLAVOR_NAME 200 4096 20 2
152
153     echo "use heat template to create stack"
154     cd $HOT_PATH
155     heat stack-create bottlenecks -f ${TEMPLATE_NAME} \
156          -P "image=$IMAGE_NAME;key_name=$KEY_NAME;public_net=$EXTERNAL_NET;flavor=$FLAVOR_NAME"
157
158     echo "Bottlenecks create instance using heat template end $(date)"
159 }
160
161 bottlenecks_rubbos_wait_finish()
162 {
163     echo "Start checking rubbos running status..."
164     retry=0
165     while true
166     do
167         ssh $ssh_args ubuntu@$control_ip "FILE=/tmp/rubbos_finished; if [ -f \$FILE ]; then exit 0; else exit 1; fi"
168         if [ $? = 0 ]; then
169             echo "Rubbos test case successfully finished :)"
170             return 0
171         fi
172         echo "Rubbos running $retry ..."
173         sleep 30
174         let retry+=1
175         if [[ $retry -ge $1 ]]; then
176             echo "Rubbos test case timeout :("
177             return 1
178         fi
179     done
180 }
181
182 bottlenecks_rubbos_run()
183 {
184     echo "Run Rubbos"
185     control_ip=$(nova list | grep rubbos_control | awk '{print $13}')
186     for i in rubbos_benchmark rubbos_client1 rubbos_client2 rubbos_client3 \
187              rubbos_client4 rubbos_control rubbos_httpd rubbos_mysql1 \
188              rubbos_tomcat1
189     do
190           ip=$(nova list | grep $i | awk '{print $12}' | awk -F [=,] '{print $2}')
191           echo "$i=$ip" >> $BOTTLENECKS_REPO_DIR/utils/infra_setup/vm_dev_setup/hosts.conf
192     done
193
194     nameserver_ip=$(grep -m 1 '^nameserver' \
195         /etc/resolv.conf | awk '{ print $2 '})
196     echo "nameserver_ip=$nameserver_ip" >> $BOTTLENECKS_REPO_DIR/utils/infra_setup/vm_dev_setup/hosts.conf
197
198     echo "GERRIT_REFSPEC_DEBUG=$GERRIT_REFSPEC_DEBUG" >> $BOTTLENECKS_REPO_DIR/utils/infra_setup/vm_dev_setup/hosts.conf
199     echo "BOTTLENECKS_BRANCH=$BOTTLENECKS_BRANCH" >> $BOTTLENECKS_REPO_DIR/utils/infra_setup/vm_dev_setup/hosts.conf
200
201     echo "NODE_NAME=$NODE_NAME" >> $BOTTLENECKS_REPO_DIR/utils/infra_setup/vm_dev_setup/hosts.conf
202     echo "INSTALLER_TYPE=$INSTALLER_TYPE" >> $BOTTLENECKS_REPO_DIR/utils/infra_setup/vm_dev_setup/hosts.conf
203     echo "BOTTLENECKS_VERSION=$BOTTLENECKS_VERSION" >> $BOTTLENECKS_REPO_DIR/utils/infra_setup/vm_dev_setup/hosts.conf
204     echo "BOTTLENECKS_DB_TARGET=$BOTTLENECKS_DB_TARGET" >> $BOTTLENECKS_REPO_DIR/utils/infra_setup/vm_dev_setup/hosts.conf
205     echo "PACKAGE_URL=$PACKAGE_URL" >> $BOTTLENECKS_REPO_DIR/utils/infra_setup/vm_dev_setup/hosts.conf
206
207     scp $ssh_args -r \
208         $BOTTLENECKS_REPO_DIR/utils/infra_setup/vm_dev_setup \
209         ubuntu@$control_ip:/tmp
210     ssh $ssh_args \
211         ubuntu@$control_ip "bash /tmp/vm_dev_setup/setup_env.sh" &
212
213     bottlenecks_rubbos_wait_finish 200
214
215     if [ x"$GERRIT_REFSPEC_DEBUG" != x ]; then
216         # TODO fix hard coded path
217         scp $ssh_args \
218             ubuntu@$control_ip:"/bottlenecks/rubbos/rubbos_results/2015-01-20T081237-0700.tgz" /tmp
219     fi
220
221     rm -rf $BOTTLENECKS_REPO_DIR/utils/infra_setup/vm_dev_setup/hosts.conf
222 }
223
224 bottlenecks_cleanup()
225 {
226     echo "Bottlenecks cleanup start $(date)"
227
228     if heat stack-list; then
229         for stack in $(heat stack-list | grep -e bottlenecks | awk '{print $2}'); do
230             echo "clean up stack $stack"
231             heat stack-delete $stack || true
232             sleep 30
233         done
234     fi
235
236     if glance image-list; then
237         for image in $(glance image-list | grep -e $IMAGE_NAME | awk '{print $2}'); do
238             echo "clean up image $image"
239             glance image-delete $image || true
240         done
241     fi
242
243     if nova keypair-list; then
244         for key in $(nova keypair-list | grep -e $KEY_NAME | awk '{print $2}'); do
245             echo "clean up key $key"
246             nova keypair-delete $key || true
247         done
248     fi
249
250     if nova flavor-list; then
251         for flavor in $(nova flavor-list | grep -e $FLAVOR_NAME | awk '{print $2}'); do
252             echo "clean up flavor $flavor"
253             nova flavor-delete $flavor || true
254         done
255     fi
256
257     echo "Bottlenecks cleanup end $(date)"
258 }
259
260 bottlenecks_load_bottlenecks_image()
261 {
262     echo "Bottlenecks load image start $(date)"
263
264     curl --connect-timeout 10 -o /tmp/bottlenecks-trusty-server.img $IMAGE_URL -v
265
266     result=$(glance image-create \
267         --name $IMAGE_NAME \
268         --disk-format qcow2 \
269         --container-format bare \
270         --file /tmp/bottlenecks-trusty-server.img)
271     echo "$result"
272
273     rm -rf /tmp/bottlenecks-trusty-server.img
274
275     IMAGE_ID_BOTTLENECKS=$(echo "$result" | grep " id " | awk '{print $(NF-1)}')
276     if [ -z "$IMAGE_ID_BOTTLENECKS" ]; then
277          echo 'failed to upload bottlenecks image to openstack'
278          exit 1
279     fi
280
281     echo "bottlenecks image end id: $IMAGE_ID_BOTTLENECKS $(date)"
282 }
283
284 main()
285 {
286     echo "main start $(date)"
287
288     : ${BOTTLENECKS_DEBUG:='True'}
289     : ${BOTTLENECKS_REPO:='https://gerrit.opnfv.org/gerrit/bottlenecks'}
290     : ${BOTTLENECKS_REPO_DIR:='/tmp/opnfvrepo/bottlenecks'}
291     : ${BOTTLENECKS_BRANCH:='master'} # branch, tag, sha1 or refspec
292     : ${RELENG_REPO:='https://gerrit.opnfv.org/gerrit/releng'}
293     : ${RELENG_REPO_DIR:='/tmp/opnfvrepo/releng'}
294     : ${RELENG_BRANCH:='master'} # branch, tag, sha1 or refspec
295     : ${IMAGE_NAME:='bottlenecks-trusty-server'}
296     KEY_PATH=$BOTTLENECKS_REPO_DIR/utils/infra_setup/bottlenecks_key
297     HOT_PATH=$BOTTLENECKS_REPO_DIR/utils/infra_setup/heat_template
298     : ${KEY_NAME:='bottlenecks-key'}
299     : ${FLAVOR_NAME:='bottlenecks-flavor'}
300     : ${TEMPLATE_NAME:='bottlenecks_rubbos_hot.yaml'}
301     ssh_args="-o StrictHostKeyChecking=no -o BatchMode=yes -i $KEY_PATH/bottlenecks_key"
302     : ${EXTERNAL_NET:='net04_ext'}
303     : ${PACKAGE_URL:='http://artifacts.opnfv.org/bottlenecks'}
304     : ${NODE_NAME:='opnfv-jump-2'}
305     : ${INSTALLER_TYPE:='fuel'}
306     : ${INSTALLER_IP:='10.20.0.2'}
307     # TODO fix for dashboard
308     : ${BOTTLENECKS_VERSION:='master'}
309     : ${BOTTLENECKS_DB_TARGET:='213.77.62.197'}
310     IMAGE_URL=${PACKAGE_URL}/rubbos/bottlenecks-trusty-server.img
311
312     bottlenecks_env_prepare
313     set -x
314     bottlenecks_cleanup
315     bottlenecks_load_bottlenecks_image
316     bottlenecks_create_instance
317     bottlenecks_check_instance_ok
318     bottlenecks_rubbos_run
319     bottlenecks_cleanup
320     echo "main end $(date)"
321 }
322
323 main
324 set +x
325