b08d2fb7cd6392721f22d9d1afa48facb842a48b
[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 apt-get update
169   sudo dpkg --configure -a
170   sudo rm /var/lib/apt/lists/lock
171   sudo rm /var/cache/apt/archives/lock
172   sudo apt-get install -y git
173   cd $LOCAL_GIT_REPO
174   git clone ${REMOTE_GIT_REPO}
175
176   if [ -d $LOCAL_ARTIFACTS_REPO ];then
177     rm -rf $LOCAL_ARTIFACTS_REPO
178   fi
179   mkdir -p $LOCAL_ARTIFACTS_REPO
180   cd $LOCAL_ARTIFACTS_REPO
181   wget -nv ${REMOTE_ARTIFACTS_REPO}
182   tar xvzf rubbos_files.tar.gz
183
184   if [ -d $local_results_dir ];then
185     rm -rf $local_results_dir
186   fi
187   mkdir -p $local_results_dir
188 }
189
190 # ssh all vms/instances once only after first creation
191 direct_ssh() {
192   echo "127.0.0.1 $(hostname)" >> /etc/hosts
193   echo "write hosts file: 127.0.0.1 $(hostname)"
194   cp /home/ubuntu/bottlenecks_key /home/ubuntu/.ssh/id_rsa
195   sudo chmod 0600 /home/ubuntu/.ssh/id_rsa
196   echo 'StrictHostKeyChecking no' > /home/ubuntu/.ssh/config
197   sudo chown -R ubuntu:ubuntu /home/ubuntu/.ssh
198   i=1
199   while [ $i -lt ${#hostip_arr[@]} ]; do
200     echo ${hostip_arr[$i]}" "${hostname_arr[$i]} >> /etc/hosts
201     let i=i+1
202   done
203   echo "Done controller."
204
205   i=1
206   while [ $i -lt ${#hostip_arr[@]} ]; do
207     if [ ${hostname_arr[$i]} == ${controller_host} ];then
208       let i=i+1
209       continue
210     fi
211     echo "Processing: "${hostip_arr[$i]}" "${hostname_arr[$i]}
212     echo "ssh *sudo hostname* test:"
213     ssh ${SSH_ARGS} ubuntu@${hostip_arr[$i]} "sudo hostname"
214
215     ssh ${SSH_ARGS} ubuntu@${hostip_arr[$i]} "sudo cp /etc/hosts /home/ubuntu/ && sudo chmod 646 /home/ubuntu/hosts"
216     ssh ${SSH_ARGS} ubuntu@${hostip_arr[$i]} "echo 127.0.0.1 ${hostname_arr[$i]} >> /home/ubuntu/hosts"
217     j=1
218     while [ $j -lt ${#hostip_arr[@]} ];do
219       local host_item=${hostip_arr[$j]}" "${hostname_arr[$j]}
220       ssh ${SSH_ARGS} ubuntu@${hostip_arr[$i]} "echo ${host_item} >> /home/ubuntu/hosts"
221       let j=j+1
222     done
223     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"
224     echo "done hosts"
225
226     sudo ssh ${SSH_ARGS} ubuntu@${hostip_arr[$i]} "echo 'StrictHostKeyChecking no' > /home/ubuntu/.ssh/config"
227     sudo scp ${SSH_ARGS} /home/ubuntu/.ssh/id_rsa ubuntu@${hostip_arr[$i]}:/home/ubuntu/.ssh/
228
229     let i=i+1
230   done
231 }
232
233 start_puppet_service() {
234   # Start puppetserver
235   sudo service puppetserver stop
236   sudo service puppetserver start
237   sudo service puppetserver status
238   # Start all puppet agents
239   for host in "${all_agents_arr[@]}";do
240     echo "start puppet agent on:"${host}
241     ssh ${SSH_ARGS} ubuntu@${host} "sudo service puppet status"
242     ssh ${SSH_ARGS} ubuntu@${host} "sudo service puppet stop"
243     ssh ${SSH_ARGS} ubuntu@${host} "sudo service puppet start --no-client"
244     ssh ${SSH_ARGS} ubuntu@${host} "sudo service puppet status"
245   done
246
247   sudo puppet cert list --all
248   sudo puppet cert sign --all
249   sudo puppet cert list --all
250 }
251
252 # inline function
253 # It requires one local file path which needs to be replaced
254 _replace_text() {
255   echo "_replace file: "$1
256   sed -i 's#REPLACED_RUBBOS_APP_TOOLS#'${rubbos_app_tools}'#g' $1
257   sed -i 's#REPLACED_RUBBOS_APP#'${rubbos_app}'#g' $1
258   sed -i 's#REPLACED_RUBBOS_HOME#'${rubbos_home}'#g' $1
259   local mysql_jdbc_url="jdbc:mysql://"${database_servers}":"${database_port}"/rubbos"
260   sed -i 's#REPLACED_MYSQL_JDBC_DB_URL#'${mysql_jdbc_url}'#g' $1
261   sed -i 's/REPLACED_MYSQL_USERNAME/'${db_username}'/g' $1
262   sed -i 's/REPLACED_MYSQL_PASSWORD/'${db_password}'/g' $1
263   handler_details=
264   handlers=
265   i=0
266   while [ $i -lt ${#appservers_arr} ];do
267     handler_name="s"$i
268     handlers=${handlers}${handler_name}","
269     handler_details=${handler_details}"worker."${handler_name}".port=8009\n"
270     handler_details=${handler_details}"worker."${handler_name}".host="${appservers_arr[$i]}"\n"
271     handler_details=${handler_details}"worker."${handler_name}".type=ajp13\n"
272     handler_details=${handler_details}"worker."${handler_name}".lbfactor=1\n"
273     let i=i+1
274   done
275   handlers=${handlers%,}
276   sed -i 's/REPLACED_HANDLERS_DETAILS/'${handler_details}'/g' $1
277   sed -i 's/REPLACED_HANDLERS/'${handlers}'/g' $1
278
279   sed -i 's/REPLACED_WEB_SERVER/'${web_servers}'/g' $1
280   sed -i 's/REPLACED_APPLICATION_SERVER/'${app_servers}'/g' $1
281   sed -i 's/REPLACED_DB_SERVER/'${database_servers}'/g' $1
282   sed -i 's/REPLACED_CLIENT_SERVERS/'${remote_client_servers}'/g' $1
283 }
284
285 # inline function
286 # it requires one input string
287 _to_puppet_class_nodes() {
288   echo "_to_puppet_class_nodes "$1
289   class_nodes=
290   nodes_arr=(${1//,/ })
291   i=0
292   while [ $i -lt ${#nodes_arr[@]} ];do
293     class_nodes=${class_nodes}"'"${nodes_arr[$i]}"',"
294     let i=i+1
295   done
296   class_nodes=${class_nodes%,}
297 }
298
299 # inline function
300 _execute_catalog() {
301   for host in "${clients_arr[@]}"; do
302     ssh ${SSH_ARGS} ubuntu@${host} 'sudo puppet agent -t' &
303   done
304   for host in "${webservers_arr[@]}"; do
305     ssh ${SSH_ARGS} ubuntu@${host} 'sudo puppet agent -t'
306   done
307   for host in "${appservers_arr[@]}"; do
308     ssh ${SSH_ARGS} ubuntu@${host} 'sudo puppet agent -t'
309   done
310   for host in "${dbservers_arr[@]}"; do
311     ssh ${SSH_ARGS} ubuntu@${host} 'sudo puppet agent -t'
312   done
313 }
314
315 prepare_manifests() {
316   # copy manifests
317   sudo cp -r ${LOCAL_RUBBOS_MANIFESTS_HOME}/modules/* /etc/puppet/modules/
318
319   # copy rubbos_files
320   sudo cp -r ${LOCAL_ARTIFACTS_RUBBOS_HOME}/modules/* /etc/puppet/modules/
321
322   # adjust corresponding configuration files (pre-catalog)
323   _replace_text /etc/puppet/modules/rubbos_tomcat/files/tomcat_sl/build.properties
324   _replace_text /etc/puppet/modules/rubbos_tomcat/files/tomcat_sl/Config.java
325   _replace_text /etc/puppet/modules/rubbos_tomcat/files/tomcat_sl/mysql.properties
326   _replace_text /etc/puppet/modules/rubbos_httpd/files/apache_conf/workers.properties
327   _replace_text /etc/puppet/modules/rubbos_httpd/files/apache_conf/httpd.conf
328   _replace_text /etc/puppet/modules/rubbos_client/files/build.properties
329   _replace_text /etc/puppet/modules/rubbos_client/files/rubbos.properties.template
330   _replace_text /etc/puppet/modules/rubbos_client/files/run_emulator.sh
331 }
332
333 execute_catalog() {
334   # start all (exec catalog)
335   if [ "x"$1 == "xstart" ];then
336     echo "--> Start to execute catalogs in all agents..."
337     sudo cp ${LOCAL_RUBBOS_MANIFESTS_HOME}/site_on.pp /etc/puppet/manifests/site.pp
338     _execute_catalog
339     echo "--> Finish to execute catalogs in all agents."
340   elif [ "x"$1 == "xclean" ];then
341     echo "--> Cleanup all agents..."
342     sudo cp ${LOCAL_RUBBOS_MANIFESTS_HOME}/site_off.pp /etc/puppet/manifests/site.pp
343     _execute_catalog
344    echo "--> Finish to cleanup all agents."
345   fi
346 }
347
348 run_emulator() {
349   # prepare data in db servers
350   for host in "${dbservers_arr[@]}"; do
351     ssh ${SSH_ARGS} ubuntu@${host} 'sudo scp ubuntu@'${controller_host}':/etc/puppet/modules/rubbos_mysql/files/rubbos_data_sql.tar.gz '${rubbos_home}''
352     ssh ${SSH_ARGS} ubuntu@${host} 'cd '${rubbos_home}' && sudo ./prepare_rubbos_mysql_db.sh ./rubbos_data_sql.tar.gz ./rubbos_data_sql_dir'
353   done
354
355   # run emulator.sh ( Modify rubbos.properties file first)
356   ssh ${SSH_ARGS} ubuntu@${bench_client} 'sudo rm -rf '${rubbos_home}'/bench/bench'
357   for x in "${clients_per_node_arr[@]}";do
358     echo "run emulator with clients_per_node="$x
359     for host in "${clients_arr[@]}";do
360       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 "
361     done
362     ssh ${SSH_ARGS} ubuntu@${bench_client} 'cd '${rubbos_home}'/bench && ./run_emulator.sh'
363   done
364 }
365
366 collect_results() {
367   # collect results, from bench_host to controller
368   scp ${SSH_ARGS} -r ubuntu@${bench_client}:${rubbos_home}/bench/bench/* ${local_results_dir}
369 }
370
371 process_results() {
372   # post-process results and push to the database of dashboard
373   python ${LOCAL_GIT_REPO}/bottlenecks/utils/dashboard/process_data.py -i ${local_results_dir} \
374         -c ${LOCAL_GIT_REPO}/bottlenecks/utils/dashboard/dashboard.yaml \
375         -s rubbos \
376         -o ${local_results_dir}/rubbos.out \
377         -u no
378 }
379
380 main() {
381   echo "==> read_conf:"
382   read_conf
383   echo "==> fetch_remote_resources:"
384   fetch_remote_resources
385   echo "==> direct_ssh:"
386   direct_ssh
387   echo "==> start_puppet_service:"
388   start_puppet_service
389   echo "==> prepare_manifests:"
390   prepare_manifests
391   echo "==> execute_catalog start:"
392   execute_catalog start
393   echo "==> run_emulator:"
394   run_emulator
395   echo "==> collect_results (to controller:${local_results_dir}):"
396   collect_results
397   echo "==> process_results:"
398   process_results
399   echo "==> execute_catalog clean:"
400   execute_catalog clean
401 }
402
403 main