bugfix: dpkg was interrupted in one ubuntu instance
[bottlenecks.git] / testsuites / rubbos / puppet_manifests / internal / run_rubbos_internal.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2016 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
12 ## run_rubbos_interlnal.sh is executed in rubbos-controller
13 ## Usage: run_rubbos_internal.sh rubbos.conf local_result_dir
14 if [ $# -ne 2 ];then
15   echo "Need Two argument!"
16   exit 1
17 fi
18 local_cfg_path=$1
19 local_results_dir=$2
20 controller_host=`hostname`
21
22 #REMOTE_GIT_REPO=git://git.opnfv.org/bottlenecks
23 REMOTE_GIT_REPO=https://gerrit.opnfv.org/gerrit/bottlenecks
24 REMOTE_ARTIFACTS_REPO=http://artifacts.opnfv.org/bottlenecks/rubbos/rubbos_files.tar.gz
25 LOCAL_GIT_REPO=/home/ubuntu/btnks-git
26 LOCAL_ARTIFACTS_REPO=/home/ubuntu/btnks-artifacts
27 LOCAL_RUBBOS_MANIFESTS_HOME=/home/ubuntu/btnks-git/bottlenecks/testsuites/rubbos/puppet_manifests
28 LOCAL_ARTIFACTS_RUBBOS_HOME=/home/ubuntu/btnks-artifacts/rubbos_files
29
30 SSH_ARGS="-o StrictHostKeyChecking=no -o BatchMode=yes -i /home/ubuntu/.ssh/id_rsa"
31
32 # conf properties from the input config file
33 client_servers=
34 web_servers=
35 app_servers=
36 cjdbc_controller=
37 database_servers=
38 database_port=3306
39 db_username=rubbos
40 db_password=rubbos
41 clients_per_node=
42 rubbos_app=
43 rubbos_app_tools=
44 rubbos_home=
45
46 # conf info used in this script
47 bench_client=
48 clients_arr=
49 remote_client_arr=
50 remote_client_servers=
51 clients_per_node_arr=
52 webservers_arr=
53 appservers_arr=
54 dbservers_arr=
55 all_agents_arr=
56 hostname_arr=
57 hostip_arr=
58
59 # Other variables used in this script
60 class_nodes=
61
62 read_conf() {
63   while read line
64   do
65     if [ ${#line} -gt 0 ] && [ ${line:0:1} != "#" ] && [ ${line:0:1} != "[" ];then
66       line=(${line//=/ })
67       case ${line[0]} in
68         "controller" )
69           e_arr=(${line[1]//:/ })
70           controller_host=${e_arr[0]}
71           controller_ip=${e_arr[2]}
72           hostname_arr=("${hostname_arr[@]}" "${e_arr[0]}")
73           hostip_arr=("${hostip_arr[@]}" "${e_arr[2]}");;
74         "client_servers" )
75           elems=(${line[1]//,/ })
76           for e in "${elems[@]}";do
77             e_arr=(${e//:/ })
78             client_servers=${client_servers}${e_arr[0]}","
79             hostname_arr=("${hostname_arr[@]}" "${e_arr[0]}")
80             hostip_arr=("${hostip_arr[@]}" "${e_arr[1]}")
81           done
82           client_servers=${client_servers%,};;
83         "web_servers" )
84           e_arr=(${line[1]//:/ })
85           web_servers=${web_servers}${e_arr[0]}","
86           hostname_arr=("${hostname_arr[@]}" "${e_arr[0]}")
87           hostip_arr=("${hostip_arr[@]}" "${e_arr[1]}")
88           web_servers=${e_arr[0]};;
89         "app_servers" )
90           elems=(${line[1]//,/ })
91           for e in "${elems[@]}";do
92             e_arr=(${e//:/ })
93             app_servers=${app_servers}${e_arr[0]}","
94             hostname_arr=("${hostname_arr[@]}" "${e_arr[0]}")
95             hostip_arr=("${hostip_arr[@]}" "${e_arr[1]}")
96           done
97           app_servers=${app_servers%,};;
98         "cjdbc_controller" )
99           if [ "x"${line[1]} != "x" ]; then
100             e_arr=(${line[1]//:/ })
101             hostname_arr=("${hostname_arr[@]}" "${e_arr[0]}")
102             hostip_arr=("${hostip_arr[@]}" "${e_arr[1]}")
103             cjdbc_controller=${e_arr[0]}
104           fi
105           ;;
106         "db_servers" )
107           elems=(${line[1]//,/ })
108           for e in "${elems[@]}";do
109             e_arr=(${e//:/ })
110             database_servers=${database_servers}${e_arr[0]}","
111             hostname_arr=("${hostname_arr[@]}" "${e_arr[0]}")
112             hostip_arr=("${hostip_arr[@]}" "${e_arr[1]}")
113           done
114           database_servers=${database_servers%,};;
115         "database_port" )
116           database_port=${line[1]};;
117         "db_username" )
118           db_username=${line[1]};;
119         "db_password" )
120           db_password=${line[1]};;
121         "clients_per_node" )
122           clients_per_node=${line[@]:1:${#line[@]-1}};;
123         "rubbos_app" )
124           rubbos_app=${line[1]};;
125         "rubbos_app_tools" )
126           rubbos_app_tools=${line[1]};;
127         "rubbos_home" )
128           rubbos_home=${line[1]};;
129       esac
130     fi
131   done < $local_cfg_path
132
133   clients_arr=(${client_servers//,/ })
134   clients_per_node_arr=(${clients_per_node})
135   webservers_arr=(${web_servers//,/ })
136   appservers_arr=(${app_servers//,/ })
137   dbservers_arr=(${database_servers//,/ })
138   all_agents_arr=("${clients_arr[@]}" "${webservers_arr[@]}" "${appservers_arr[@]}" "${dbservers_arr[@]}")
139   bench_client=${clients_arr[0]}
140   len=${#clients_arr[@]}
141   if [ $len -gt 1 ]; then
142     remote_clients_arr=(${clients_arr[@]:1:$len-1})
143     remote_client_servers=${client_servers#*,}
144   fi
145
146   echo "-------------Main conf info:----------"
147   i=1
148   while [ $i -lt ${#hostname_arr[@]} ]; do
149     echo ${hostip_arr[$i]}" "${hostname_arr[$i]}
150     let i=i+1
151   done
152   echo "clients_arr:           "${clients_arr[@]}
153   echo "bench_client:          "$bench_client
154   echo "remote_client_servers: "$remote_client_servers
155   echo "remote_clients_arr:    "${remote_clients_arr[@]}
156   echo "clients_per_node_arr:  "${clients_per_node_arr[@]}
157   echo "webservers_arr:        "${webservers_arr[@]}
158   echo "appservers_arr:        "${appservers_arr[@]}
159   echo "dbservers_arr:         "${dbservers_arr[@]}
160   echo "all agents:            "${all_agents_arr[@]}
161 }
162
163 fetch_remote_resources() {
164   if [ -d $LOCAL_GIT_REPO ];then
165     rm -rf $LOCAL_GIT_REPO
166   fi
167   mkdir -p $LOCAL_GIT_REPO
168   sudo dpkg --configure -a
169   sudo apt-get install -y git
170   cd $LOCAL_GIT_REPO
171   git clone ${REMOTE_GIT_REPO}
172
173   if [ -d $LOCAL_ARTIFACTS_REPO ];then
174     rm -rf $LOCAL_ARTIFACTS_REPO
175   fi
176   mkdir -p $LOCAL_ARTIFACTS_REPO
177   cd $LOCAL_ARTIFACTS_REPO
178   wget -nv ${REMOTE_ARTIFACTS_REPO}
179   tar xvzf rubbos_files.tar.gz
180
181   if [ -d $local_results_dir ];then
182     rm -rf $local_results_dir
183   fi
184   mkdir -p $local_results_dir
185 }
186
187 # ssh all vms/instances once only after first creation
188 direct_ssh() {
189   echo "127.0.0.1 $(hostname)" >> /etc/hosts
190   echo "write hosts file: 127.0.0.1 $(hostname)"
191   cp ${LOCAL_GIT_REPO}/bottlenecks/utils/infra_setup/bottlenecks_key/bottlenecks_key /home/ubuntu/.ssh/id_rsa
192   sudo chmod 0600 /home/ubuntu/.ssh/id_rsa
193   echo 'StrictHostKeyChecking no' > /home/ubuntu/.ssh/config
194   sudo chown -R ubuntu:ubuntu /home/ubuntu/.ssh
195   i=1
196   while [ $i -lt ${#hostip_arr[@]} ]; do
197     echo ${hostip_arr[$i]}" "${hostname_arr[$i]} >> /etc/hosts
198     let i=i+1
199   done
200   echo "Done controller."
201
202   i=1
203   while [ $i -lt ${#hostip_arr[@]} ]; do
204     if [ ${hostname_arr[$i]} == ${controller_host} ];then
205       let i=i+1
206       continue
207     fi
208     echo "Processing: "${hostip_arr[$i]}" "${hostname_arr[$i]}
209     echo "ssh *sudo hostname* test:"
210     ssh ${SSH_ARGS} ubuntu@${hostip_arr[$i]} "sudo hostname"
211
212     ssh ${SSH_ARGS} ubuntu@${hostip_arr[$i]} "sudo cp /etc/hosts /home/ubuntu/ && sudo chmod 646 /home/ubuntu/hosts"
213     ssh ${SSH_ARGS} ubuntu@${hostip_arr[$i]} "echo 127.0.0.1 ${hostname_arr[$i]} >> /home/ubuntu/hosts"
214     j=1
215     while [ $j -lt ${#hostip_arr[@]} ];do
216       local host_item=${hostip_arr[$j]}" "${hostname_arr[$j]}
217       ssh ${SSH_ARGS} ubuntu@${hostip_arr[$i]} "echo ${host_item} >> /home/ubuntu/hosts"
218       let j=j+1
219     done
220     ssh ${SSH_ARGS} ubuntu@${hostip_arr[$i]} "sudo chmod 644 /home/ubuntu/hosts && sudo cp /home/ubuntu/hosts /etc/ && sudo rm -rf /home/ubuntu/hosts"
221     echo "done hosts"
222
223     sudo ssh ${SSH_ARGS} ubuntu@${hostip_arr[$i]} "echo 'StrictHostKeyChecking no' > /home/ubuntu/.ssh/config"
224     sudo scp ${SSH_ARGS} /home/ubuntu/.ssh/id_rsa ubuntu@${hostip_arr[$i]}:/home/ubuntu/.ssh/
225
226     let i=i+1
227   done
228 }
229
230 start_puppet_service() {
231   # Start puppetserver
232   sudo service puppetserver stop
233   sudo service puppetserver start
234   sudo service puppetserver status
235   # Start all puppet agents
236   for host in "${all_agents_arr[@]}";do
237     echo "start puppet agent on:"${host}
238     ssh ${SSH_ARGS} ubuntu@${host} "sudo service puppet status"
239     ssh ${SSH_ARGS} ubuntu@${host} "sudo service puppet stop"
240     ssh ${SSH_ARGS} ubuntu@${host} "sudo service puppet start --no-client"
241     ssh ${SSH_ARGS} ubuntu@${host} "sudo service puppet status"
242   done
243
244   sudo puppet cert list --all
245   sudo puppet cert sign --all
246   sudo puppet cert list --all
247 }
248
249 # inline function
250 # It requires one local file path which needs to be replaced
251 _replace_text() {
252   echo "_replace file: "$1
253   sed -i 's#REPLACED_RUBBOS_APP_TOOLS#'${rubbos_app_tools}'#g' $1
254   sed -i 's#REPLACED_RUBBOS_APP#'${rubbos_app}'#g' $1
255   sed -i 's#REPLACED_RUBBOS_HOME#'${rubbos_home}'#g' $1
256   local mysql_jdbc_url="jdbc:mysql://"${database_servers}":"${database_port}"/rubbos"
257   sed -i 's#REPLACED_MYSQL_JDBC_DB_URL#'${mysql_jdbc_url}'#g' $1
258   sed -i 's/REPLACED_MYSQL_USERNAME/'${db_username}'/g' $1
259   sed -i 's/REPLACED_MYSQL_PASSWORD/'${db_password}'/g' $1
260   handler_details=
261   handlers=
262   i=0
263   while [ $i -lt ${#appservers_arr} ];do
264     handler_name="s"$i
265     handlers=${handlers}${handler_name}","
266     handler_details=${handler_details}"worker."${handler_name}".port=8009\n"
267     handler_details=${handler_details}"worker."${handler_name}".host="${appservers_arr[$i]}"\n"
268     handler_details=${handler_details}"worker."${handler_name}".type=ajp13\n"
269     handler_details=${handler_details}"worker."${handler_name}".lbfactor=1\n"
270     let i=i+1
271   done
272   handlers=${handlers%,}
273   sed -i 's/REPLACED_HANDLERS_DETAILS/'${handler_details}'/g' $1
274   sed -i 's/REPLACED_HANDLERS/'${handlers}'/g' $1
275
276   sed -i 's/REPLACED_WEB_SERVER/'${web_servers}'/g' $1
277   sed -i 's/REPLACED_APPLICATION_SERVER/'${app_servers}'/g' $1
278   sed -i 's/REPLACED_DB_SERVER/'${database_servers}'/g' $1
279   sed -i 's/REPLACED_CLIENT_SERVERS/'${remote_client_servers}'/g' $1
280 }
281
282 # inline function
283 # it requires one input string
284 _to_puppet_class_nodes() {
285   echo "_to_puppet_class_nodes "$1
286   class_nodes=
287   nodes_arr=(${1//,/ })
288   i=0
289   while [ $i -lt ${#nodes_arr[@]} ];do
290     class_nodes=${class_nodes}"'"${nodes_arr[$i]}"',"
291     let i=i+1
292   done
293   class_nodes=${class_nodes%,}
294 }
295
296 # inline function
297 _execute_catalog() {
298   for host in "${clients_arr[@]}"; do
299     ssh ${SSH_ARGS} ubuntu@${host} 'sudo puppet agent -t' &
300   done
301   for host in "${webservers_arr[@]}"; do
302     ssh ${SSH_ARGS} ubuntu@${host} 'sudo puppet agent -t'
303   done
304   for host in "${appservers_arr[@]}"; do
305     ssh ${SSH_ARGS} ubuntu@${host} 'sudo puppet agent -t'
306   done
307   for host in "${dbservers_arr[@]}"; do
308     ssh ${SSH_ARGS} ubuntu@${host} 'sudo puppet agent -t'
309   done
310 }
311
312 prepare_manifests() {
313   # copy manifests
314   sudo cp -r ${LOCAL_RUBBOS_MANIFESTS_HOME}/modules/* /etc/puppet/modules/
315
316   # copy rubbos_files
317   sudo cp -r ${LOCAL_ARTIFACTS_RUBBOS_HOME}/modules/* /etc/puppet/modules/
318
319   # adjust corresponding configuration files (pre-catalog)
320   _replace_text /etc/puppet/modules/rubbos_tomcat/files/tomcat_sl/build.properties
321   _replace_text /etc/puppet/modules/rubbos_tomcat/files/tomcat_sl/Config.java
322   _replace_text /etc/puppet/modules/rubbos_tomcat/files/tomcat_sl/mysql.properties
323   _replace_text /etc/puppet/modules/rubbos_httpd/files/apache_conf/workers.properties
324   _replace_text /etc/puppet/modules/rubbos_httpd/files/apache_conf/httpd.conf
325   _replace_text /etc/puppet/modules/rubbos_client/files/build.properties
326   _replace_text /etc/puppet/modules/rubbos_client/files/rubbos.properties.template
327   _replace_text /etc/puppet/modules/rubbos_client/files/run_emulator.sh
328 }
329
330 execute_catalog() {
331   # start all (exec catalog)
332   if [ "x"$1 == "xstart" ];then
333     echo "--> Start to execute catalogs in all agents..."
334     sudo cp ${LOCAL_RUBBOS_MANIFESTS_HOME}/site_on.pp /etc/puppet/manifests/site.pp
335     _execute_catalog
336     echo "--> Finish to execute catalogs in all agents."
337   elif [ "x"$1 == "xclean" ];then
338     echo "--> Cleanup all agents..."
339     sudo cp ${LOCAL_RUBBOS_MANIFESTS_HOME}/site_off.pp /etc/puppet/manifests/site.pp
340     _execute_catalog
341    echo "--> Finish to cleanup all agents."
342   fi
343 }
344
345 run_emulator() {
346   # prepare data in db servers
347   for host in "${dbservers_arr[@]}"; do
348     ssh ${SSH_ARGS} ubuntu@${host} 'sudo scp ubuntu@'${controller_host}':/etc/puppet/modules/rubbos_mysql/files/rubbos_data_sql.tar.gz '${rubbos_home}''
349     ssh ${SSH_ARGS} ubuntu@${host} 'cd '${rubbos_home}' && sudo ./prepare_rubbos_mysql_db.sh ./rubbos_data_sql.tar.gz ./rubbos_data_sql_dir'
350   done
351
352   # run emulator.sh ( Modify rubbos.properties file first)
353   ssh ${SSH_ARGS} ubuntu@${bench_client} 'sudo rm -rf '${rubbos_home}'/bench/bench'
354   for x in "${clients_per_node_arr[@]}";do
355     echo "run emulator with clients_per_node="$x
356     for host in "${clients_arr[@]}";do
357       ssh ${SSH_ARGS} ubuntu@${host} "sed -e 's/REPLACED_NUMBER_OF_CLIENTS_PER_NODE/'${x}'/g' '${rubbos_home}'/Client/rubbos.properties.template > '${rubbos_home}'/Client/rubbos.properties "
358     done
359     ssh ${SSH_ARGS} ubuntu@${bench_client} 'cd '${rubbos_home}'/bench && ./run_emulator.sh'
360   done
361 }
362
363 collect_results() {
364   # collect results, from bench_host to controller
365   scp ${SSH_ARGS} -r ubuntu@${bench_client}:${rubbos_home}/bench/bench/* ${local_results_dir}
366 }
367
368 process_results() {
369   # post-process results and push to the database of dashboard
370   python ${LOCAL_GIT_REPO}/bottlenecks/utils/dashboard/process_data.py -i ${local_results_dir} \
371         -c ${LOCAL_GIT_REPO}/bottlenecks/utils/dashboard/dashboard.yaml \
372         -s rubbos \
373         -o ${local_results_dir}/rubbos.out \
374         -u no
375 }
376
377 main() {
378   echo "==> read_conf:"
379   read_conf
380   echo "==> fetch_remote_resources:"
381   fetch_remote_resources
382   echo "==> direct_ssh:"
383   direct_ssh
384   echo "==> start_puppet_service:"
385   start_puppet_service
386   echo "==> prepare_manifests:"
387   prepare_manifests
388   echo "==> execute_catalog start:"
389   execute_catalog start
390   echo "==> run_emulator:"
391   run_emulator
392   echo "==> collect_results (to controller:${local_results_dir}):"
393   collect_results
394   echo "==> process_results:"
395   process_results
396   echo "==> execute_catalog clean:"
397   execute_catalog clean
398 }
399
400 main