12dc17a6d6aa62cc384081a75f1c7cfcb0463145
[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_ARTIFACTS_REPO=http://artifacts.opnfv.org/bottlenecks/rubbos/rubbos_files.tar.gz
24 LOCAL_GIT_REPO=/btnks-git
25 LOCAL_ARTIFACTS_REPO=/btnks-artifacts
26 LOCAL_RUBBOS_MANIFESTS_HOME=/btnks-git/bottlenecks/testsuites/rubbos/puppet_manifests
27 LOCAL_ARTIFACTS_RUBBOS_HOME=/btnks-artifacts/rubbos_files
28
29 # conf properties from the input config file
30 client_servers=
31 web_servers=
32 app_servers=
33 cjdbc_controller=
34 database_servers=
35 database_port=3306
36 db_username=rubbos
37 db_password=rubbos
38 clients_per_node=
39 rubbos_app=
40 rubbos_app_tools=
41 rubbos_home=
42
43 # conf info used in this script
44 bench_client=
45 clients_arr=
46 remote_client_arr=
47 remote_client_servers=
48 clients_per_node_arr=
49 webservers_arr=
50 appservers_arr=
51 dbservers_arr=
52 all_agents_arr=
53
54 # Other variables used in this script
55 class_nodes=
56
57 read_conf() {
58   while read line
59   do
60     if [ ${#line} -gt 0 ] && [ ${line:0:1} != "#" ] && [ ${line:0:1} != "[" ];then
61       line=(${line//=/ })
62       case ${line[0]} in
63         "client_servers" )
64           client_servers=${line[1]};;
65         "web_servers" )
66           web_servers=${line[1]};;
67         "app_servers" )
68           app_servers=${line[1]};;
69         "cjdbc_controller" )
70           cjdbc_controller=${line[1]};;
71         "database_servers" )
72           database_servers=${line[1]};;
73         "database_port" )
74           database_port=${line[1]};;
75         "db_username" )
76           db_username=${line[1]};;
77         "db_password" )
78           db_password=${line[1]};;
79         "clients_per_node" )
80           clients_per_node=${line[@]:1:${#line[@]-1}};;
81         "rubbos_app" )
82           rubbos_app=${line[1]};;
83         "rubbos_app_tools" )
84           rubbos_app_tools=${line[1]};;
85         "rubbos_home" )
86           rubbos_home=${line[1]};;
87       esac
88     fi
89   done < $local_cfg_path
90
91   clients_arr=(${client_servers//,/ })
92   clients_per_node_arr=(${clients_per_node})
93   webservers_arr=(${web_servers//,/ })
94   appservers_arr=(${app_servers//,/ })
95   dbservers_arr=(${database_servers//,/ })
96   all_agents_arr=("${clients_arr[@]}" "${webservers_arr[@]}" "${appservers_arr[@]}" "${dbservers_arr[@]}")
97   bench_client=${clients_arr[0]}
98   len=${#clients_arr[@]}
99   if [ $len -gt 1 ]; then
100     remote_clients_arr=(${clients_arr[@]:1:$len-1})
101     remote_client_servers=${client_servers#*,}
102   fi
103
104   echo "-------------Main conf info:----------"
105   echo "clients_arr:           "${clients_arr[@]}
106   echo "bench_client:          "$bench_client
107   echo "remote_client_servers: "$remote_client_servers
108   echo "remote_clients_arr:    "${remote_clients_arr[@]}
109   echo "clients_per_node_arr:  "${clients_per_node_arr[@]}
110   echo "webservers_arr:        "${webservers_arr[@]}
111   echo "appservers_arr:        "${appservers_arr[@]}
112   echo "dbservers_arr:         "${dbservers_arr[@]}
113   echo "all agents:            "${all_agents_arr[@]}
114 }
115
116 fetch_remote_resources() {
117   if [ -d $LOCAL_GIT_REPO ];then
118     rm -rf $LOCAL_GIT_REPO
119   fi
120   mkdir -p $LOCAL_GIT_REPO
121   sudo apt-get install -y git
122   cd $LOCAL_GIT_REPO
123   git clone ${REMOTE_GIT_REPO}
124
125   if [ -d $LOCAL_ARTIFACTS_REPO ];then
126     rm -rf $LOCAL_ARTIFACTS_REPO
127   fi
128   mkdir -p $LOCAL_ARTIFACTS_REPO
129   cd $LOCAL_ARTIFACTS_REPO
130   wget ${REMOTE_ARTIFACTS_REPO}
131   tar xvzf rubbos_files.tar.gz
132
133   if [ -d $local_results_dir ];then
134     rm -rf $local_results_dir
135   fi
136   mkdir -p $local_results_dir
137 }
138
139 # inline function
140 # It requires one local file path which needs to be replaced
141 _replace_text() {
142   echo "_replace file: "$1
143   sed -i 's#REPLACED_RUBBOS_APP_TOOLS#'${rubbos_app_tools}'#g' $1
144   sed -i 's#REPLACED_RUBBOS_APP#'${rubbos_app}'#g' $1
145   sed -i 's#REPLACED_RUBBOS_HOME#'${rubbos_home}'#g' $1
146   local mysql_jdbc_url="jdbc:mysql://"${database_servers}":"${database_port}"/rubbos"
147   sed -i 's#REPLACED_MYSQL_JDBC_DB_URL#'${mysql_jdbc_url}'#g' $1
148   sed -i 's/REPLACED_MYSQL_USERNAME/'${db_username}'/g' $1
149   sed -i 's/REPLACED_MYSQL_PASSWORD/'${db_password}'/g' $1
150   handler_details=
151   handlers=
152   i=0
153   while [ $i -lt ${#appservers_arr} ];do
154     handler_name="s"$i
155     handlers=${handlers}${handler_name}","
156     handler_details=${handler_details}"worker."${handler_name}".port=8009\n"
157     handler_details=${handler_details}"worker."${handler_name}".host="${appservers_arr[$i]}"\n"
158     handler_details=${handler_details}"worker."${handler_name}".type=ajp13\n"
159     handler_details=${handler_details}"worker."${handler_name}".lbfactor=1\n"
160     let i=i+1
161   done
162   handlers=${handlers%,}
163   sed -i 's/REPLACED_HANDLERS_DETAILS/'${handler_details}'/g' $1
164   sed -i 's/REPLACED_HANDLERS/'${handlers}'/g' $1
165
166   sed -i 's/REPLACED_WEB_SERVER/'${web_servers}'/g' $1
167   sed -i 's/REPLACED_APPLICATION_SERVER/'${app_servers}'/g' $1
168   sed -i 's/REPLACED_DB_SERVER/'${database_servers}'/g' $1
169   sed -i 's/REPLACED_CLIENT_SERVERS/'${remote_client_servers}'/g' $1
170 }
171
172 # inline function
173 # it requires one input string
174 _to_puppet_class_nodes() {
175   echo "_to_puppet_class_nodes "$1
176   class_nodes=
177   nodes_arr=(${1//,/ })
178   i=0
179   while [ $i -lt ${#nodes_arr[@]} ];do
180     class_nodes=${class_nodes}"'"${nodes_arr[$i]}"',"
181     let i=i+1
182   done
183   class_nodes=${class_nodes%,}
184 }
185
186 # inline function
187 _execute_catalog() {
188   # use *class_nodes* to receive return vale
189   _to_puppet_class_nodes ${client_servers}
190   sed -i 's/REPLACED_CLIENT_NODES/'${class_nodes}'/g' /etc/puppet/manifests/site.pp
191
192   _to_puppet_class_nodes ${web_servers}
193   sed -i 's/REPLACED_HTTPD_NODES/'${class_nodes}'/g' /etc/puppet/manifests/site.pp
194
195   _to_puppet_class_nodes ${app_servers}
196   sed -i 's/REPLACED_TOMCAT_NODES/'${class_nodes}'/g' /etc/puppet/manifests/site.pp
197
198   _to_puppet_class_nodes ${database_servers}
199   sed -i 's/REPLACED_MYSQL_NODES/'${class_nodes}'/g' /etc/puppet/manifests/site.pp
200
201   for host in "${clients_arr[@]}"; do
202     ssh ubuntu@${host} 'sudo puppet agent -t' &
203   done
204   for host in "${webservers_arr[@]}"; do
205     ssh ubuntu@${host} 'sudo puppet agent -t'
206   done
207   for host in "${appservers_arr[@]}"; do
208     ssh ubuntu@${host} 'sudo puppet agent -t'
209   done
210   for host in "${dbservers_arr[@]}"; do
211     ssh ubuntu@${host} 'sudo puppet agent -t'
212   done
213 }
214
215 prepare_manifests() {
216   # copy manifests
217   sudo cp -r ${LOCAL_RUBBOS_MANIFESTS_HOME}/modules/* /etc/puppet/modules/
218
219   # copy rubbos_files
220   sudo cp -r ${LOCAL_ARTIFACTS_RUBBOS_HOME}/modules/* /etc/puppet/modules/
221
222   # adjust corresponding configuration files (pre-catalog)
223   _replace_text /etc/puppet/modules/rubbos_tomcat/files/tomcat_sl/build.properties
224   _replace_text /etc/puppet/modules/rubbos_tomcat/files/tomcat_sl/Config.java
225   _replace_text /etc/puppet/modules/rubbos_tomcat/files/tomcat_sl/mysql.properties
226   _replace_text /etc/puppet/modules/rubbos_httpd/files/apache_conf/workers.properties
227   _replace_text /etc/puppet/modules/rubbos_httpd/files/apache_conf/httpd.conf
228   _replace_text /etc/puppet/modules/rubbos_client/files/build.properties
229   _replace_text /etc/puppet/modules/rubbos_client/files/rubbos.properties.template
230   _replace_text /etc/puppet/modules/rubbos_client/files/run_emulator.sh
231 }
232
233 execute_catalog() {
234   # start all (exec catalog)
235   echo "--> Cleanup all agents..."
236   sudo cp ${LOCAL_RUBBOS_MANIFESTS_HOME}/site_off.pp /etc/puppet/manifests/site.pp
237   _execute_catalog
238
239   sleep 3s
240
241   echo "--> Start to execute catalogs in all agents..."
242   sudo cp ${LOCAL_RUBBOS_MANIFESTS_HOME}/site_on.pp /etc/puppet/manifests/site.pp
243   _execute_catalog
244   echo "--> Finish to execute catalogs in all agents."
245 }
246
247 run_emulator() {
248   # prepare data in db servers
249   for host in "${dbservers_arr[@]}"; do
250     ssh ubuntu@${host} 'sudo scp ubuntu@'${controller_host}':/etc/puppet/modules/rubbos_mysql/files/rubbos_data_sql.tar.gz '${rubbos_home}''
251     ssh ubuntu@${host} 'cd '${rubbos_home}' && sudo ./prepare_rubbos_mysql_db.sh ./rubbos_data_sql.tar.gz ./rubbos_data_sql_dir'
252   done
253
254   # run emulator.sh ( Modify rubbos.properties file first)
255   ssh ubuntu@${bench_client} 'sudo rm -rf '${rubbos_home}'/bench/bench'
256   for x in "${clients_per_node_arr[@]}";do
257     echo "run emulator with clients_per_node="$x
258     for host in "${clients_arr[@]}";do
259       ssh ubuntu@${host} "sed -e 's/REPLACED_NUMBER_OF_CLIENTS_PER_NODE/'${x}'/g' '${rubbos_home}'/Client/rubbos.properties.template > '${rubbos_home}'/Client/rubbos.properties "
260     done
261     ssh ubuntu@${bench_client} 'cd '${rubbos_home}'/bench && ./run_emulator.sh'
262   done
263 }
264
265 collect_results() {
266   # collect results, from bench_host to controller
267   scp -r ubuntu@${bench_client}:${rubbos_home}/bench/bench/* ${local_results_dir}
268 }
269
270 process_results() {
271   # post-process results and push to the database of dashboard
272   python ${LOCAL_GIT_REPO}/bottlenecks/utils/dashboard/process_data.py -i ${local_results_dir} \
273         -c ${LOCAL_GIT_REPO}/bottlenecks/utils/dashboard/dashboard.yaml \
274         -s rubbos \
275         -o ${local_results_dir}/rubbos.out \
276         -u no
277 }
278
279 main() {
280   echo "==> read_conf:"
281   read_conf
282   echo "==> fetch_remote_resources:"
283   fetch_remote_resources
284   echo "==> prepare_manifests:"
285   prepare_manifests
286   echo "==> execute_catalog:"
287   execute_catalog
288   echo "==> run_emulator:"
289   run_emulator
290   echo "==> collect_results (to controller:${local_results_dir}):"
291   collect_results
292   echo "==> process_results:"
293   process_results
294 }
295
296 main