blob: fac71100c904285fdaa8521db8b642c64cb95663 [file] [log] [blame]
Naoki Shiota9a1e6d12014-04-03 14:47:12 -07001#! /bin/bash
2
3set -e
4
5### Env vars used by this script. (default value) ###
Naoki Shiota05721b32014-04-29 14:41:12 -07006# $ONOS_HOME : path of root directory of ONOS repository (~/ONOS)
7# $ONOS_CLUSTER_HOME : path of ONOS cluster tools directory (this script's dir)
8# $REMOTE_ONOS_HOME : path of root directory of ONOS repository in remote hosts (ONOS)
9# $ONOS_CLUSTER_LOGDIR : path of log output directory (~/ONOS/cluster-mgmt/logs)
10# $SSH : command name to access host
11# $PSSH : command name to access hosts in parallel
12# $SCP : command name to copy config file to each host
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070013#####################################################
14
15
16### Variables read from ONOS config file ###
17ONOS_HOME=${ONOS_HOME:-${HOME}/ONOS}
18
19source ${ONOS_HOME}/scripts/common/utils.sh
20
21CLUSTER_HOME=${ONOS_CLUSTER_HOME:-$(cd `dirname $0`; pwd)}
22CLUSTER_CONF_DIR=${CLUSTER_HOME}/conf
23CLUSTER_CONF=${ONOS_CLUSTER_CONF:-${CLUSTER_CONF_DIR}/onos-cluster.conf}
24CLUSTER_TEMPLATE_DIR=${CLUSTER_CONF_DIR}/template
Naoki Shiota05721b32014-04-29 14:41:12 -070025CLUSTER_LOGDIR=${ONOS_CLUSTER_LOGDIR:-${CLUSTER_HOME}/logs}
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070026
27REMOTE_ONOS_HOME=${REMOTE_ONOS_HOME:-ONOS}
28REMOTE_ONOS_CONF_DIR=${REMOTE_ONOS_HOME}/conf
29
30if [ ! -f ${CLUSTER_CONF} ]; then
31 echo "${CLUSTER_CONF} not found."
32 exit 1
33fi
Naoki Shiota9109a1e2014-05-13 11:11:01 -070034CLUSTER_HOSTS=$(read-conf ${CLUSTER_CONF} cluster.hosts.names `hostname` | tr ',' ' ')
35CLUSTER_BACKEND=$(read-conf ${CLUSTER_CONF} cluster.hosts.backend)
36CLUSTER_RC_PROTOCOL=$(read-conf ${CLUSTER_CONF} cluster.hosts.ramcloud.protocol "fast+udp")
37CLUSTER_RC_SERVER_REPLICAS=$(read-conf ${CLUSTER_CONF} cluster.hosts.ramcloud.server.replicas "0")
38CLUSTER_HC_NETWORK=$(read-conf ${CLUSTER_CONF} cluster.hosts.hazelcast.network)
39CLUSTER_HC_ADDR=$(read-conf ${CLUSTER_CONF} cluster.hosts.hazelcast.multicast.address "224.2.2.3")
40CLUSTER_HC_PORT=$(read-conf ${CLUSTER_CONF} cluster.hosts.hazelcast.multicast.port "54327")
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070041############################################
42
43
44ONOS_CONF_TEMPLATE=${CLUSTER_TEMPLATE_DIR}/onos_node.conf.template
45
46
47### Parallel SSH settings ###
48SSH=${SSH:-ssh}
49PSSH=${PSSH:-parallel-ssh}
50PSSH_CONF=${CLUSTER_CONF_DIR}/pssh.hosts
51SCP=${SCP:-scp}
52#############################
53
54
55############# Common functions #############
Naoki Shiota05721b32014-04-29 14:41:12 -070056function print-usage {
57 local scriptname=`basename $0`
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070058 local usage="Usage: setup/deploy/start/stop/status ONOS cluster.
Naoki Shiota05721b32014-04-29 14:41:12 -070059 \$ ${scriptname} setup [-f]
60 Set up ONOS cluster using ${CLUSTER_CONF}.
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070061 If -f option is used, all existing files will be overwritten without confirmation.
Naoki Shiota05721b32014-04-29 14:41:12 -070062 \$ ${scriptname} deploy [-f]
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070063 Deliver node config files to cluster nodes.
64 If -f option is used, all existing files will be overwritten without confirmation.
Naoki Shiota05721b32014-04-29 14:41:12 -070065 \$ ${scriptname} start
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070066 Start ONOS cluster
Naoki Shiota05721b32014-04-29 14:41:12 -070067 \$ ${scriptname} stop
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070068 Stop ONOS cluster
Naoki Shiota05721b32014-04-29 14:41:12 -070069 \$ ${scriptname} status
70 Show status of ONOS-cluster
71 \$ ${scriptname} cmd {command to execute}
72 Execute command on all hosts in parallel"
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070073
74 echo "${usage}"
75}
76
77############################################
78
79
80############# Setup functions ##############
81
82function list-zk-hosts {
83 local list=()
84 for host in ${CLUSTER_HOSTS}; do
85 local zk_host_string=$(read-conf ${CLUSTER_CONF} "cluster.${host}.zk.host")
86
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070087 if [ -z "${zk_host_string}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070088 # falling back to ip
89 zk_host_string=$(read-conf ${CLUSTER_CONF} "cluster.${host}.ip")
90 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070091 if [ -z "${zk_host_string}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070092 # falling back to hostname
93 zk_host_string=${host}
94 fi
95
96 list=("${list[@]}" ${zk_host_string})
97 done
98
99 # join with comma
100 local IFS=,
101 echo "${list[*]}"
102}
103
104function list-hc-hosts {
105 local list=()
106 for host in ${CLUSTER_HOSTS}; do
107 local hc_host_string=$(read-conf ${CLUSTER_CONF} "cluster.${host}.hazelcast.ip")
108
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700109 if [ -z "${hc_host_string}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700110 # falling back to ip
111 hc_host_string=$(read-conf ${CLUSTER_CONF} "cluster.${host}.ip")
112 fi
113
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700114 if [ -z "${hc_host_string}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700115 # falling back to hostname
116 hc_host_string=${host}
117 fi
118
119 list=("${list[@]}" ${hc_host_string})
120 done
121
122 local IFS=,
123 echo "${list[*]}"
124}
125
126function create-pssh-conf {
127 local tempfile=`begin-conf-creation ${PSSH_CONF}`
128
Naoki Shiotab76bc652014-04-28 19:26:13 -0700129 local filename=`basename ${PSSH_CONF}`
130 echo -n "Creating ${filename} ... "
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700131 # creation of pssh config file
132 for host in ${CLUSTER_HOSTS}; do
133 local user=$(read-conf ${CLUSTER_CONF} remote.${host}.ssh.user)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700134 if [ -z "${user}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700135 # falling back to common setting
136 user=$(read-conf ${CLUSTER_CONF} remote.common.ssh.user)
137 fi
138
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700139 if [ -z "${user}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700140 echo ${host} >> ${tempfile}
141 else
142 echo ${user}@${host} >> ${tempfile}
143 fi
144 done
145
146 end-conf-creation ${PSSH_CONF}
Naoki Shiotab76bc652014-04-28 19:26:13 -0700147 echo "DONE"
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700148}
149
150# create-onos-conf {hostname}
151function create-onos-conf {
152 local host_name=${1}
153
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700154 if [ -z "${host_name}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700155 echo "FAILED"
156 echo "[ERROR] invalid hostname ${host_name}"
157 exit 1
158 fi
159
160 local onos_conf="${CLUSTER_CONF_DIR}/onos_node.${host_name}.conf"
161 local tempfile=`begin-conf-creation ${onos_conf}`
Naoki Shiotab76bc652014-04-28 19:26:13 -0700162 local filename=`basename ${onos_conf}`
163 echo -n "Creating ${filename} ... "
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700164
165 cp ${ONOS_CONF_TEMPLATE} ${tempfile}
166
167 local prefix="cluster.${host}"
168
169 local host_ip=$(read-conf ${CLUSTER_CONF} "${prefix}.ip")
170 local host_string=${host_ip}
171 if [ -z "${host_string}" ]; then
172 host_string=${host_name}
173 fi
174 local host_role=$(read-conf ${CLUSTER_CONF} "${prefix}.role")
175 local zk_hosts=`list-zk-hosts`
176 local rc_ip=$(read-conf ${CLUSTER_CONF} "${prefix}.ramcloud.ip" ${host_string})
177 local rc_coord_port=$(read-conf ${CLUSTER_CONF} "${prefix}.ramcloud.coordinator.port" 12246)
178 local rc_server_port=$(read-conf ${CLUSTER_CONF} "${prefix}.ramcloud.server.port" 12242)
179 local hc_hosts=`list-hc-hosts`
180
181 # creation of ONOS node config file
182 sed -i -e "s|__HOST_NAME__|${host_name}|" ${tempfile}
183 if [ -z "${host_ip}" ]; then
184 # comment out
185 sed -i -e "s|^\(.*__HOST_IP__.*\)$|#\1|" ${tempfile}
186 else
187 sed -i -e "s|__HOST_IP__|${host_ip}|" ${tempfile}
188 fi
189 sed -i -e "s|__ONOS_ROLE__|${host_role}|" ${tempfile}
190 sed -i -e "s|__BACKEND__|${CLUSTER_BACKEND}|" ${tempfile}
191 sed -i -e "s|__ZK_HOSTS__|${zk_hosts}|" ${tempfile}
192 sed -i -e "s|__RAMCLOUD_PROTOCOL__|${CLUSTER_RC_PROTOCOL}|" ${tempfile}
193 sed -i -e "s|__RAMCLOUD_IP__|${rc_ip}|" ${tempfile}
194 sed -i -e "s|__RAMCLOUD_COORD_PORT__|${rc_coord_port}|" ${tempfile}
195 sed -i -e "s|__RAMCLOUD_SERVER_PORT__|${rc_server_port}|" ${tempfile}
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700196 sed -i -e "s|__RAMCLOUD_SERVER_REPLICAS__|${CLUSTER_RC_SV_REPLICAS}|" ${tempfile}
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700197
198 if [ ${CLUSTER_HC_NETWORK} = "tcp-ip" ]; then
199 sed -i -e "s|__HAZELCAST_MEMBERS__|${hc_hosts}|" ${tempfile}
200
201 # Comment out unused parameters
202 sed -i -e "s|^\(.*__HAZELCAST_MULTICAST_GROUP__.*\)$|#\1|" ${tempfile}
203 sed -i -e "s|^\(.*__HAZELCAST_MULTICAST_PORT__.*\)$|#\1|" ${tempfile}
204 elif [ ${CLUSTER_HC_NETWORK} = "multicast" ]; then
205 sed -i -e "s|__HAZELCAST_MULTICAST_GROUP__|${CLUSTER_HC_ADDR}|" ${tempfile}
206 sed -i -e "s|__HAZELCAST_MULTICAST_PORT__|${CLUSTER_HC_PORT}|" ${tempfile}
207
208 sed -i -e "s|^\(.*__HAZELCAST_MEMBERS__.*\)$|#\1|" ${tempfile}
209 fi
210
211 end-conf-creation ${onos_conf}
Naoki Shiotab76bc652014-04-28 19:26:13 -0700212
213 echo "DONE"
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700214}
215
216# setup -f : force overwrite existing files
217function setup {
218 if [ "${1}" = "-f" ]; then
219 create-pssh-conf
220
221 for host in ${CLUSTER_HOSTS}; do
222 create-onos-conf ${host}
223 done
224 else
225 create-conf-interactive ${PSSH_CONF} create-pssh-conf
226
227 for host in ${CLUSTER_HOSTS}; do
228 local filename="${CLUSTER_CONF_DIR}/onos_node.${host}.conf"
229 create-conf-interactive ${filename} create-onos-conf ${host}
230 done
231 fi
232}
233
234############################################
235
236
237############ Deploy functions ##############
238
239function deploy {
240 if [ ! -f ${PSSH_CONF} ]; then
241 echo "[ERROR] ${PSSH_CONF} not found"
242 local command=`basename ${0}`
243 echo "[ERROR] Try \"${command} setup\" to create files."
244 exit 1
245 fi
246
Naoki Shiota05721b32014-04-29 14:41:12 -0700247 mkdir -p ${CLUSTER_LOGDIR}
248
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700249 for host in ${CLUSTER_HOSTS}; do
250 local conf=${CLUSTER_CONF_DIR}/onos_node.${host}.conf
251 if [ ! -f ${conf} ]; then
252 echo "[ERROR] ${conf} not found"
253 local command=`basename ${0}`
254 echo "[ERROR] Try \"${command} setup\" to create files."
255 exit 1
256 fi
Naoki Shiota05721b32014-04-29 14:41:12 -0700257
258 local filename=`basename ${conf}`
259 echo -n "Copying ${filename} to ${host} ... "
260
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700261 local user=$(read-conf ${CLUSTER_CONF} "remote.${host}.ssh.user")
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700262 if [ -z "${user}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700263 # falling back to common setting
264 user=$(read-conf ${CLUSTER_CONF} "remote.common.ssh.user")
265 fi
Naoki Shiotab76bc652014-04-28 19:26:13 -0700266
Naoki Shiota05721b32014-04-29 14:41:12 -0700267 local login=
Naoki Shiotab76bc652014-04-28 19:26:13 -0700268 if [ -z "${user}" ]; then
269 user=`whoami`
270 fi
271
Naoki Shiota05721b32014-04-29 14:41:12 -0700272 ${SCP} ${conf} ${user}@${host}:${REMOTE_ONOS_CONF_DIR} &> ${CLUSTER_LOGDIR}/deploy.${host}.log
273 echo "DONE"
274
275 echo -n "Configuring ${host} ... "
276 ${SSH} ${user}@${host} "cd ${REMOTE_ONOS_HOME}; ./onos.sh setup -f; ./build-ramcloud-java-bindings.sh" &>> ${CLUSTER_LOGDIR}/deploy.${host}.log
277 echo "DONE"
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700278 done
279
280# TODO: Replacing per-host ssh command with pssh command below.
281# Need to solve concurrency problem when ONOS directory is shared among hosts.
282# ${PSSH} -i -h ${PSSH_CONF} "cd ${REMOTE_ONOS_HOME}; ./onos.sh setup -f"
283}
284############################################
285
286
287############# Start functions ##############
288
289function start {
290 if [ ! -f ${PSSH_CONF} ]; then
291 echo "[ERROR] ${PSSH_CONF} not found"
292 local command=`basename ${0}`
293 echo "[ERROR] Try \"${command} setup\" to create files."
294 exit 1
295 fi
296
Naoki Shiotab76bc652014-04-28 19:26:13 -0700297 echo "Starting ONOS cluster"
298 ${PSSH} -i -h ${PSSH_CONF} "cd ${REMOTE_ONOS_HOME}; ./onos.sh start 2>&1"
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700299}
300
301############################################
302
303
304############# Stop functions $##############
305
306function stop {
307 if [ ! -f ${PSSH_CONF} ]; then
308 echo "[ERROR] ${PSSH_CONF} not found"
309 local command=`basename ${0}`
310 echo "[ERROR] Try \"${command} setup\" to create files."
311 exit 1
312 fi
313
Naoki Shiotab76bc652014-04-28 19:26:13 -0700314 echo "Stopping ONOS cluster"
315 ${PSSH} -i -h ${PSSH_CONF} "cd ${REMOTE_ONOS_HOME}; ./onos.sh stop 2>&1"
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700316}
317
318############################################
319
320
321############ Status functions ##############
322
323function status {
324 if [ ! -f ${PSSH_CONF} ]; then
325 echo "[ERROR] ${PSSH_CONF} not found"
326 local command=`basename ${0}`
327 echo "[ERROR] Try \"${command} setup\" to create files."
328 exit 1
329 fi
330
Naoki Shiotab76bc652014-04-28 19:26:13 -0700331 ${PSSH} -i -h ${PSSH_CONF} "cd ${REMOTE_ONOS_HOME}; ./onos.sh status 2>&1"
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700332}
333
334############################################
335
336
Naoki Shiota05721b32014-04-29 14:41:12 -0700337############## Cmd functions ###############
338
339function do-cmd {
340 local cmd=$*
341
342 if [ -z "${cmd}" ]; then
343 print-usage
344 else
345 ${PSSH} -i -h ${PSSH_CONF} "${cmd}"
346 fi
347}
348
349############################################
350
351
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700352################## Main ####################
353case "$1" in
354 setup)
355 setup $2
356 ;;
357 deploy)
358 deploy
359 ;;
360 start)
361 start
362 ;;
363 stop)
364 stop
365 ;;
366 stat*) # <- status
367 status
368 ;;
Naoki Shiota05721b32014-04-29 14:41:12 -0700369 cmd)
370 array=("$@")
371 unset array[0]
372 do-cmd ${array[@]}
373 ;;
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700374 *)
Naoki Shiota05721b32014-04-29 14:41:12 -0700375 print-usage
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700376 exit 1
377esac