blob: e34fe162a9b91b90b2051840daa4c704546fed51 [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) ###
6# $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# $SSH : command name to access host
10# $PSSH : command name to access hosts in parallel
11# $SCP : command name to copy config file to each host
12#####################################################
13
14
15### Variables read from ONOS config file ###
16ONOS_HOME=${ONOS_HOME:-${HOME}/ONOS}
17
18source ${ONOS_HOME}/scripts/common/utils.sh
19
20CLUSTER_HOME=${ONOS_CLUSTER_HOME:-$(cd `dirname $0`; pwd)}
21CLUSTER_CONF_DIR=${CLUSTER_HOME}/conf
22CLUSTER_CONF=${ONOS_CLUSTER_CONF:-${CLUSTER_CONF_DIR}/onos-cluster.conf}
23CLUSTER_TEMPLATE_DIR=${CLUSTER_CONF_DIR}/template
24
25REMOTE_ONOS_HOME=${REMOTE_ONOS_HOME:-ONOS}
26REMOTE_ONOS_CONF_DIR=${REMOTE_ONOS_HOME}/conf
27
28if [ ! -f ${CLUSTER_CONF} ]; then
29 echo "${CLUSTER_CONF} not found."
30 exit 1
31fi
32CLUSTER_HOSTS=$(read-conf ${CLUSTER_CONF} cluster.hosts.names `hostname` | tr ',' ' ')
33CLUSTER_BACKEND=$(read-conf ${CLUSTER_CONF} cluster.hosts.backend)
34CLUSTER_RC_PROTOCOL=$(read-conf ${CLUSTER_CONF} cluster.hosts.ramcloud.protocol "fast+udp")
35CLUSTER_HC_NETWORK=$(read-conf ${CLUSTER_CONF} cluster.hosts.hazelcast.network)
36CLUSTER_HC_ADDR=$(read-conf ${CLUSTER_CONF} cluster.hosts.hazelcast.multicast.address "224.2.2.3")
37CLUSTER_HC_PORT=$(read-conf ${CLUSTER_CONF} cluster.hosts.hazelcast.multicast.port "54327")
38############################################
39
40
41ONOS_CONF_TEMPLATE=${CLUSTER_TEMPLATE_DIR}/onos_node.conf.template
42
43
44### Parallel SSH settings ###
45SSH=${SSH:-ssh}
46PSSH=${PSSH:-parallel-ssh}
47PSSH_CONF=${CLUSTER_CONF_DIR}/pssh.hosts
48SCP=${SCP:-scp}
49#############################
50
51
52############# Common functions #############
53function print_usage {
54 local filename=`basename ${ONOS_CLUSTER_CONF}`
55 local usage="Usage: setup/deploy/start/stop/status ONOS cluster.
56 \$ $0 setup [-f]
57 Set up ONOS cluster using ${filename}.
58 If -f option is used, all existing files will be overwritten without confirmation.
59 \$ $0 deploy [-f]
60 Deliver node config files to cluster nodes.
61 If -f option is used, all existing files will be overwritten without confirmation.
62 \$ $0 start
63 Start ONOS cluster
64 \$ $0 stop
65 Stop ONOS cluster
66 \$ $0 status
67 Show status of ONOS-cluster"
68
69 echo "${usage}"
70}
71
72############################################
73
74
75############# Setup functions ##############
76
77function list-zk-hosts {
78 local list=()
79 for host in ${CLUSTER_HOSTS}; do
80 local zk_host_string=$(read-conf ${CLUSTER_CONF} "cluster.${host}.zk.host")
81
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070082 if [ -z "${zk_host_string}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070083 # falling back to ip
84 zk_host_string=$(read-conf ${CLUSTER_CONF} "cluster.${host}.ip")
85 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070086 if [ -z "${zk_host_string}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070087 # falling back to hostname
88 zk_host_string=${host}
89 fi
90
91 list=("${list[@]}" ${zk_host_string})
92 done
93
94 # join with comma
95 local IFS=,
96 echo "${list[*]}"
97}
98
99function list-hc-hosts {
100 local list=()
101 for host in ${CLUSTER_HOSTS}; do
102 local hc_host_string=$(read-conf ${CLUSTER_CONF} "cluster.${host}.hazelcast.ip")
103
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700104 if [ -z "${hc_host_string}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700105 # falling back to ip
106 hc_host_string=$(read-conf ${CLUSTER_CONF} "cluster.${host}.ip")
107 fi
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 hostname
111 hc_host_string=${host}
112 fi
113
114 list=("${list[@]}" ${hc_host_string})
115 done
116
117 local IFS=,
118 echo "${list[*]}"
119}
120
121function create-pssh-conf {
122 local tempfile=`begin-conf-creation ${PSSH_CONF}`
123
Naoki Shiotab76bc652014-04-28 19:26:13 -0700124 local filename=`basename ${PSSH_CONF}`
125 echo -n "Creating ${filename} ... "
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700126 # creation of pssh config file
127 for host in ${CLUSTER_HOSTS}; do
128 local user=$(read-conf ${CLUSTER_CONF} remote.${host}.ssh.user)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700129 if [ -z "${user}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700130 # falling back to common setting
131 user=$(read-conf ${CLUSTER_CONF} remote.common.ssh.user)
132 fi
133
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700134 if [ -z "${user}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700135 echo ${host} >> ${tempfile}
136 else
137 echo ${user}@${host} >> ${tempfile}
138 fi
139 done
140
141 end-conf-creation ${PSSH_CONF}
Naoki Shiotab76bc652014-04-28 19:26:13 -0700142 echo "DONE"
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700143}
144
145# create-onos-conf {hostname}
146function create-onos-conf {
147 local host_name=${1}
148
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700149 if [ -z "${host_name}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700150 echo "FAILED"
151 echo "[ERROR] invalid hostname ${host_name}"
152 exit 1
153 fi
154
155 local onos_conf="${CLUSTER_CONF_DIR}/onos_node.${host_name}.conf"
156 local tempfile=`begin-conf-creation ${onos_conf}`
Naoki Shiotab76bc652014-04-28 19:26:13 -0700157 local filename=`basename ${onos_conf}`
158 echo -n "Creating ${filename} ... "
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700159
160 cp ${ONOS_CONF_TEMPLATE} ${tempfile}
161
162 local prefix="cluster.${host}"
163
164 local host_ip=$(read-conf ${CLUSTER_CONF} "${prefix}.ip")
165 local host_string=${host_ip}
166 if [ -z "${host_string}" ]; then
167 host_string=${host_name}
168 fi
169 local host_role=$(read-conf ${CLUSTER_CONF} "${prefix}.role")
170 local zk_hosts=`list-zk-hosts`
171 local rc_ip=$(read-conf ${CLUSTER_CONF} "${prefix}.ramcloud.ip" ${host_string})
172 local rc_coord_port=$(read-conf ${CLUSTER_CONF} "${prefix}.ramcloud.coordinator.port" 12246)
173 local rc_server_port=$(read-conf ${CLUSTER_CONF} "${prefix}.ramcloud.server.port" 12242)
174 local hc_hosts=`list-hc-hosts`
175
176 # creation of ONOS node config file
177 sed -i -e "s|__HOST_NAME__|${host_name}|" ${tempfile}
178 if [ -z "${host_ip}" ]; then
179 # comment out
180 sed -i -e "s|^\(.*__HOST_IP__.*\)$|#\1|" ${tempfile}
181 else
182 sed -i -e "s|__HOST_IP__|${host_ip}|" ${tempfile}
183 fi
184 sed -i -e "s|__ONOS_ROLE__|${host_role}|" ${tempfile}
185 sed -i -e "s|__BACKEND__|${CLUSTER_BACKEND}|" ${tempfile}
186 sed -i -e "s|__ZK_HOSTS__|${zk_hosts}|" ${tempfile}
187 sed -i -e "s|__RAMCLOUD_PROTOCOL__|${CLUSTER_RC_PROTOCOL}|" ${tempfile}
188 sed -i -e "s|__RAMCLOUD_IP__|${rc_ip}|" ${tempfile}
189 sed -i -e "s|__RAMCLOUD_COORD_PORT__|${rc_coord_port}|" ${tempfile}
190 sed -i -e "s|__RAMCLOUD_SERVER_PORT__|${rc_server_port}|" ${tempfile}
191
192 if [ ${CLUSTER_HC_NETWORK} = "tcp-ip" ]; then
193 sed -i -e "s|__HAZELCAST_MEMBERS__|${hc_hosts}|" ${tempfile}
194
195 # Comment out unused parameters
196 sed -i -e "s|^\(.*__HAZELCAST_MULTICAST_GROUP__.*\)$|#\1|" ${tempfile}
197 sed -i -e "s|^\(.*__HAZELCAST_MULTICAST_PORT__.*\)$|#\1|" ${tempfile}
198 elif [ ${CLUSTER_HC_NETWORK} = "multicast" ]; then
199 sed -i -e "s|__HAZELCAST_MULTICAST_GROUP__|${CLUSTER_HC_ADDR}|" ${tempfile}
200 sed -i -e "s|__HAZELCAST_MULTICAST_PORT__|${CLUSTER_HC_PORT}|" ${tempfile}
201
202 sed -i -e "s|^\(.*__HAZELCAST_MEMBERS__.*\)$|#\1|" ${tempfile}
203 fi
204
205 end-conf-creation ${onos_conf}
Naoki Shiotab76bc652014-04-28 19:26:13 -0700206
207 echo "DONE"
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700208}
209
210# setup -f : force overwrite existing files
211function setup {
212 if [ "${1}" = "-f" ]; then
213 create-pssh-conf
214
215 for host in ${CLUSTER_HOSTS}; do
216 create-onos-conf ${host}
217 done
218 else
219 create-conf-interactive ${PSSH_CONF} create-pssh-conf
220
221 for host in ${CLUSTER_HOSTS}; do
222 local filename="${CLUSTER_CONF_DIR}/onos_node.${host}.conf"
223 create-conf-interactive ${filename} create-onos-conf ${host}
224 done
225 fi
226}
227
228############################################
229
230
231############ Deploy functions ##############
232
233function deploy {
234 if [ ! -f ${PSSH_CONF} ]; then
235 echo "[ERROR] ${PSSH_CONF} not found"
236 local command=`basename ${0}`
237 echo "[ERROR] Try \"${command} setup\" to create files."
238 exit 1
239 fi
240
241 for host in ${CLUSTER_HOSTS}; do
242 local conf=${CLUSTER_CONF_DIR}/onos_node.${host}.conf
243 if [ ! -f ${conf} ]; then
244 echo "[ERROR] ${conf} not found"
245 local command=`basename ${0}`
246 echo "[ERROR] Try \"${command} setup\" to create files."
247 exit 1
248 fi
249
250 local user=$(read-conf ${CLUSTER_CONF} "remote.${host}.ssh.user")
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700251 if [ -z "${user}" ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700252 # falling back to common setting
253 user=$(read-conf ${CLUSTER_CONF} "remote.common.ssh.user")
254 fi
Naoki Shiotab76bc652014-04-28 19:26:13 -0700255
256 if [ -z "${user}" ]; then
257 user=`whoami`
258 fi
259
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700260 ${SCP} ${conf} ${user}@${host}:${REMOTE_ONOS_CONF_DIR}
261 ${SSH} ${user}@${host} "cd ${REMOTE_ONOS_HOME}; ./onos.sh setup -f"
262 done
263
264# TODO: Replacing per-host ssh command with pssh command below.
265# Need to solve concurrency problem when ONOS directory is shared among hosts.
266# ${PSSH} -i -h ${PSSH_CONF} "cd ${REMOTE_ONOS_HOME}; ./onos.sh setup -f"
267}
268############################################
269
270
271############# Start functions ##############
272
273function start {
274 if [ ! -f ${PSSH_CONF} ]; then
275 echo "[ERROR] ${PSSH_CONF} not found"
276 local command=`basename ${0}`
277 echo "[ERROR] Try \"${command} setup\" to create files."
278 exit 1
279 fi
280
Naoki Shiotab76bc652014-04-28 19:26:13 -0700281 echo "Starting ONOS cluster"
282 ${PSSH} -i -h ${PSSH_CONF} "cd ${REMOTE_ONOS_HOME}; ./onos.sh start 2>&1"
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700283}
284
285############################################
286
287
288############# Stop functions $##############
289
290function stop {
291 if [ ! -f ${PSSH_CONF} ]; then
292 echo "[ERROR] ${PSSH_CONF} not found"
293 local command=`basename ${0}`
294 echo "[ERROR] Try \"${command} setup\" to create files."
295 exit 1
296 fi
297
Naoki Shiotab76bc652014-04-28 19:26:13 -0700298 echo "Stopping ONOS cluster"
299 ${PSSH} -i -h ${PSSH_CONF} "cd ${REMOTE_ONOS_HOME}; ./onos.sh stop 2>&1"
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700300}
301
302############################################
303
304
305############ Status functions ##############
306
307function status {
308 if [ ! -f ${PSSH_CONF} ]; then
309 echo "[ERROR] ${PSSH_CONF} not found"
310 local command=`basename ${0}`
311 echo "[ERROR] Try \"${command} setup\" to create files."
312 exit 1
313 fi
314
Naoki Shiotab76bc652014-04-28 19:26:13 -0700315 ${PSSH} -i -h ${PSSH_CONF} "cd ${REMOTE_ONOS_HOME}; ./onos.sh status 2>&1"
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700316}
317
318############################################
319
320
321################## Main ####################
322case "$1" in
323 setup)
324 setup $2
325 ;;
326 deploy)
327 deploy
328 ;;
329 start)
330 start
331 ;;
332 stop)
333 stop
334 ;;
335 stat*) # <- status
336 status
337 ;;
338 *)
339 print_usage
340 exit 1
341esac