blob: beb874f65a4fca8f98be7a9d3a4e2eab7ca6abf1 [file] [log] [blame]
Naoki Shiota4e463182014-03-21 15:13:24 -07001#!/bin/bash
Naoki Shiota4e463182014-03-21 15:13:24 -07002
Naoki Shiota590c18d2014-03-31 17:52:59 -07003### Env vars used by this script. (default value) ###
4# $ONOS_HOME : path of root directory of ONOS repository (this script's dir)
5# $ONOS_CONF_DIR : path of ONOS config directory (~/ONOS/conf)
Naoki Shiota9a1e6d12014-04-03 14:47:12 -07006# $ONOS_CONF : path of ONOS node config file (~/ONOS/conf/onos_node.`hostname`.conf or onos_node.conf)
Naoki Shiota590c18d2014-03-31 17:52:59 -07007# $ONOS_PROPS : path of ONOS properties file (~/ONOS/conf/onos.properties)
8# $ONOS_LOGBACK : path of logback config file (~/ONOS/conf/logback.`hostname`.xml)
Naoki Shiotad8ea71a2014-04-23 17:04:51 -07009# $ONOS_LOGDIR : path of log output directory (~/ONOS/onos-logs)
10# $ONOS_LOGBASE : base name of log output file (onos.`hostname`)
Naoki Shiota590c18d2014-03-31 17:52:59 -070011# $RAMCLOUD_HOME : path of root directory of RAMCloud repository (~/ramcloud)
12# $RAMCLOUD_BRANCH : branch name of RAMCloud to use (master)
Naoki Shiota72209722014-04-08 14:32:17 -070013# $ZK_HOME : path of root directory of ZooKeeper (~/zookeeper-3.4.5)
14# $ZK_LIB_DIR : path of ZooKeeper library (/var/lib/zookeeper)
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070015# $ZK_LOG_DIR : path of ZooKeeper log output directory (~/ONOS/onos-logs/zk-`hostname`)
Naoki Shiota590c18d2014-03-31 17:52:59 -070016# $JVM_OPTS : JVM options ONOS starts with
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070017# $ZK_CONF : path of ZooKeeper config file (~/ONOS/conf/zoo.cfg)
18# $HC_CONF : path of Hazelcast config file (~/ONOS/conf/hazelcast.xml)
19# $RAMCLOUD_CONF : path of RAMCloud config file (~/ONOS/conf/ramcloud.conf)
Naoki Shiota590c18d2014-03-31 17:52:59 -070020#####################################################
21
Naoki Shiota590c18d2014-03-31 17:52:59 -070022ONOS_HOME=${ONOS_HOME:-$(cd `dirname $0`; pwd)}
Naoki Shiota9df15d32014-03-27 14:26:20 -070023ONOS_CONF_DIR=${ONOS_CONF_DIR:-${ONOS_HOME}/conf}
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070024ONOS_CONF=${ONOS_CONF:-${ONOS_CONF_DIR}/onos_node.`hostname`.conf}
Naoki Shiota590c18d2014-03-31 17:52:59 -070025
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070026source ${ONOS_HOME}/scripts/common/utils.sh
27
Naoki Shiota590c18d2014-03-31 17:52:59 -070028if [ ! -f ${ONOS_CONF} ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070029 # falling back to default config file
30 ONOS_CONF=${ONOS_CONF_DIR}/onos_node.conf
31 if [ ! -f ${ONOS_CONF} ]; then
32 echo "${ONOS_CONF} not found."
33 exit 1
34 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -070035fi
Naoki Shiota4e928512014-04-03 15:49:35 -070036
37
38### Variables read from ONOS config file ###
39ONOS_HOST_NAME=$(read-conf ${ONOS_CONF} host.name `hostname`)
40ONOS_HOST_IP=$(read-conf ${ONOS_CONF} host.ip)
41ONOS_HOST_ROLE=$(read-conf ${ONOS_CONF} host.role)
42ONOS_HOST_BACKEND=$(read-conf ${ONOS_CONF} host.backend)
43ZK_HOSTS=$(read-conf ${ONOS_CONF} zookeeper.hosts ${ONOS_HOST_NAME})
44RC_COORD_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.coordinator.protocol "fast+udp")
45RC_COORD_IP=$(read-conf ${ONOS_CONF} ramcloud.coordinator.ip ${ONOS_HOST_IP})
46RC_COORD_PORT=$(read-conf ${ONOS_CONF} ramcloud.coordinator.port 12246)
47RC_SERVER_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.server.protocol "fast+udp")
48RC_SERVER_IP=$(read-conf ${ONOS_CONF} ramcloud.server.ip ${ONOS_HOST_IP})
49RC_SERVER_PORT=$(read-conf ${ONOS_CONF} ramcloud.server.port 12242)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070050HC_HOST_PORT=$(read-conf ${ONOS_CONF} hazelcast.host.port 5701)
Naoki Shiota4e928512014-04-03 15:49:35 -070051HC_TCPIP_MEMBERS=$(read-conf ${ONOS_CONF} hazelcast.tcp-ip.members)
52HC_MULTICAST_GROUP=$(read-conf ${ONOS_CONF} hazelcast.multicast.group "224.2.2.3")
53HC_MULTICAST_PORT=$(read-conf ${ONOS_CONF} hazelcast.multicast.port 54327)
Naoki Shiota590c18d2014-03-31 17:52:59 -070054############################################
55
56
57############## Other variables #############
Naoki Shiota4e928512014-04-03 15:49:35 -070058ONOS_TEMPLATE_DIR=${ONOS_CONF_DIR}/template
59
Naoki Shiota4e463182014-03-21 15:13:24 -070060LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
61
Naoki Shiota72209722014-04-08 14:32:17 -070062ZK_HOME=${ZK_HOME:-~/zookeeper-3.4.5}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070063ZK_CONF=${ZK_CONF:-${ONOS_CONF_DIR}/zoo.cfg}
Naoki Shiota4e928512014-04-03 15:49:35 -070064ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070065# Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070066ZK_LOG_DIR=${ZK_LOG_DIR:-${ONOS_HOME}/onos-logs/zk-${ONOS_HOST_NAME}}
Naoki Shiota72209722014-04-08 14:32:17 -070067ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070068ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070069
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070070HC_CONF=${HC_CONF:-${ONOS_CONF_DIR}/hazelcast.xml}
Naoki Shiota4e928512014-04-03 15:49:35 -070071HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
72
Naoki Shiota4e463182014-03-21 15:13:24 -070073RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070074RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
75RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070076RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070077RAMCLOUD_CONF=${RAMCLOUD_CONF:-${ONOS_CONF_DIR}/ramcloud.conf}
Naoki Shiota4e463182014-03-21 15:13:24 -070078
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070079export LD_LIBRARY_PATH=${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}:$LD_LIBRARY_PATH
Naoki Shiota4e463182014-03-21 15:13:24 -070080
81## Because the script change dir to $ONOS_HOME, we can set ONOS_LOGBACK and LOGDIR relative to $ONOS_HOME
Naoki Shiota590c18d2014-03-31 17:52:59 -070082ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070083ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070084ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070085LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070086LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070087ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
88PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
89LOGS="$ONOS_LOG $PCAP_LOG"
90
Naoki Shiota9df15d32014-03-27 14:26:20 -070091ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -070092JMX_PORT=${JMX_PORT:-7189}
93
94# Set JVM options
95JVM_OPTS="${JVM_OPTS:-}"
Naoki Shiota4e463182014-03-21 15:13:24 -070096JVM_OPTS="$JVM_OPTS -server -d64"
97#JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -070098
99# Uncomment or specify appropriate value as JVM_OPTS environment variables.
100#JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
Naoki Shiota4e463182014-03-21 15:13:24 -0700101#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
102#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700103
104#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC"
105JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
106JVM_OPTS="$JVM_OPTS -XX:+AggressiveOpts"
107
108# We may want to remove UseFastAccessorMethods option: http://bugs.java.com/view_bug.do?bug_id=6385687
109JVM_OPTS="$JVM_OPTS -XX:+UseFastAccessorMethods"
110
111JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192"
112JVM_OPTS="$JVM_OPTS -XX:FreqInlineSize=8192"
113JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500"
114
Naoki Shiota4e463182014-03-21 15:13:24 -0700115JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700116
117# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
118JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
119
120JVM_OPTS="$JVM_OPTS -XX:+UseCompressedOops"
121
122JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
123JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
124JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
125
Naoki Shiota4e463182014-03-21 15:13:24 -0700126JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
127
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700128# Uncomment to dump final JVM flags to stdout
129#JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
130
Naoki Shiota4e463182014-03-21 15:13:24 -0700131# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700132MAIN_CLASS="net.onrc.onos.core.main.Main"
Naoki Shiota4e463182014-03-21 15:13:24 -0700133
134MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700135############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700136
Naoki Shiota9df15d32014-03-27 14:26:20 -0700137
Naoki Shiota590c18d2014-03-31 17:52:59 -0700138############# Common functions #############
139function print_usage {
Naoki Shiota05721b32014-04-29 14:41:12 -0700140 local scriptname=`basename $0`
Naoki Shiota590c18d2014-03-31 17:52:59 -0700141 local filename=`basename ${ONOS_CONF}`
142 local usage="Usage: setup/start/stop ONOS on this server.
Naoki Shiota05721b32014-04-29 14:41:12 -0700143 \$ ${scriptname} setup [-f]
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700144 Set up ONOS node using ${ONOS_CONF} .
Naoki Shiota590c18d2014-03-31 17:52:59 -0700145 - generate and replace config file of ZooKeeper.
146 - create myid in ZooKeeper datadir.
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700147 - generate and replace config file for Hazelcast.
148 - generate and replace config file for RAMCloud.
Naoki Shiota590c18d2014-03-31 17:52:59 -0700149 - generate and replace logback.${ONOS_HOST_NAME}.xml
150 If -f option is used, all existing files will be overwritten without confirmation.
Naoki Shiota05721b32014-04-29 14:41:12 -0700151 \$ ${scriptname} start [single-node|coord-node|server-node|coord-and-server-node]
Naoki Shiota590c18d2014-03-31 17:52:59 -0700152 Start ONOS node with specific RAMCloud entities
153 - single-node: start ONOS with stand-alone RAMCloud
154 - coord-node : start ONOS with RAMCloud coordinator
155 - server-node: start ONOS with RAMCloud server
156 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
157 * Default behavior can be defined by ${filename}
Naoki Shiota05721b32014-04-29 14:41:12 -0700158 \$ ${scriptname} stop
Naoki Shiota590c18d2014-03-31 17:52:59 -0700159 Stop all ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700160 \$ ${scriptname} restart
Naoki Shiota590c18d2014-03-31 17:52:59 -0700161 Stop and start currently running ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700162 \$ ${scriptname} status
Naoki Shiota590c18d2014-03-31 17:52:59 -0700163 Show status of ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700164 \$ ${scriptname} {zk|rc-coord|rc-server|core} {start|stop|restart|status}
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700165 Control specific ONOS-related process
166 \$ ${scriptname} rc deldb
167 Delete data in RAMCloud"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700168
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700169 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700170}
171
172function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700173 local logfile=$1
174 local nr_max=${2:-10}
175 if [ -f $logfile ]; then
176 for i in `seq $(expr $nr_max - 1) -1 1`; do
177 if [ -f ${logfile}.${i} ]; then
178 mv -f ${logfile}.${i} ${logfile}.`expr $i + 1`
179 fi
180 done
181 mv $logfile $logfile.1
182 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700183}
184
185# kill-processes {module-name} {array of pids}
186function kill-processes {
187 # Kill the existing processes
188 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700189 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700190 echo -n "Stopping $1 ... "
191 fi
192 for p in ${pids}; do
193 if [ x$p != "x" ]; then
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700194 (
195 # Ask process with SIGTERM first, if that did not kill the process
196 # wait 1s and if process still exist, force process to be killed.
197 kill -TERM $p && kill -0 $p && sleep 1 && kill -0 $p && kill -KILL $p
198 ) 2> /dev/null
Naoki Shiota4e463182014-03-21 15:13:24 -0700199 echo "Killed existing process (pid: $p)"
200 fi
201 done
202}
203
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700204function handle-error {
205 set -e
206
207 revert-confs
208
209 set +e
210
211 exit 1
212}
213
Naoki Shiota4e928512014-04-03 15:49:35 -0700214# revert-confs [error message]
215function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700216 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700217
Naoki Shiota4e928512014-04-03 15:49:35 -0700218 revert-file `basename ${ZK_CONF}`
219 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700220
221 echo "EXIT"
222
223 if [ ! -z "$1" ]; then
224 echo $1
225 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700226}
227
Naoki Shiota590c18d2014-03-31 17:52:59 -0700228function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700229 echo -n "Creating ${ZK_CONF} ... "
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700230
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700231 # Create the ZooKeeper lib directory
232 if [ ! -d ${ZK_LIB_DIR} ]; then
233 local SUDO=${SUDO:-}
234 local whoami=`whoami`
235 {
236 ${SUDO} mkdir ${ZK_LIB_DIR}
237 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
238 } || {
239 echo "FAILED"
240 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
241 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
242 exit 1
243 }
244 fi
245
246 # creation of ZooKeeper config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700247 local temp_zk=`begin-conf-creation ${ZK_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700248
Naoki Shiota4e928512014-04-03 15:49:35 -0700249 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700250
251 local i=1
252 local myid=
253 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700254 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700255 myid=$i
256 break
257 fi
258 i=`expr $i + 1`
259 done
260
261 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700262 local filename=`basename ${ONOS_CONF}`
Naoki Shiota35f8d5c2014-04-08 11:13:18 -0700263 revert-confs "[ERROR] In ${filename}, zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700264 fi
265
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700266 if [ -f "${ZK_MY_ID}" ]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700267 # sudo will be needed if ZK_MY_ID is already created by other (old) script
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700268 local SUDO=${SUDO:-}
269 {
270 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
271 } || {
272 echo "FAILED"
273 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700274 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700275 exit 1
276 }
277 fi
278
Naoki Shiota9df15d32014-03-27 14:26:20 -0700279 echo ${myid} > ${ZK_MY_ID}
280
281 echo -n "myid is assigned to ${myid} ... "
282
283 while read line; do
284 if [[ $line =~ ^__HOSTS__$ ]]; then
285 i=1
286 for host in ${hostarr}; do
287 # TODO: ports might be configurable
288 local hostline="server.${i}=${host}:2888:3888"
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700289 echo $hostline
Naoki Shiota9df15d32014-03-27 14:26:20 -0700290 i=`expr $i + 1`
291 done
292 elif [[ $line =~ __DATADIR__ ]]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700293 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700294 else
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700295 echo $line
Naoki Shiota9df15d32014-03-27 14:26:20 -0700296 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700297 done < ${ZK_CONF_TEMPLATE} > ${temp_zk}
298
299 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700300
301 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700302}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700303
Naoki Shiota4e928512014-04-03 15:49:35 -0700304function create-hazelcast-conf {
305 echo -n "Creating ${HC_CONF} ... "
306
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700307 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700308
309 # To keep indent of XML file, change IFS
310 local IFS=''
311 while read line; do
312 if [[ $line =~ __HC_NETWORK__ ]]; then
313 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
314 # temporary change
315 IFS=' '
316 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
317 echo '<multicast enabled="false" />'
318 echo '<tcp-ip enabled="true">'
319 for member in ${memberarr}; do
320 echo " <member>${member}</member>"
321 done
322 echo '</tcp-ip>'
323 IFS=''
324 else
325 echo '<multicast enabled="true">'
326 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
327 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
328 echo '</multicast>'
329 echo '<tcp-ip enabled="false" />'
330 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700331 elif [[ $line =~ __HC_PORT__ ]]; then
332 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700333 else
334 echo "${line}"
335 fi
336 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
337
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700338 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700339
340 echo "DONE"
341}
342
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700343function create-ramcloud-conf {
344 echo -n "Creating ${RAMCLOUD_CONF} ... "
345
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700346 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700347
348 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
349
350 # TODO make ZooKeeper address configurable.
351 echo "ramcloud.locator=zk:localhost:2181" > ${temp_rc}
352 echo "ramcloud.clusterName=${rc_cluster_name}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700353
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700354 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700355
356 echo "DONE"
357}
358
Naoki Shiota590c18d2014-03-31 17:52:59 -0700359function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700360 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700361
Naoki Shiota9df15d32014-03-27 14:26:20 -0700362 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700363 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
364
365 sed -e "s|__FILENAME__|${ONOS_LOG}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
366
367 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700368
Naoki Shiota9df15d32014-03-27 14:26:20 -0700369 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700370}
371
Naoki Shiota4e928512014-04-03 15:49:35 -0700372function create-confs {
373 local key
374 local filename
375
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700376 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700377
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700378 echo "Config file : ${ONOS_CONF}"
379
Naoki Shiota4e928512014-04-03 15:49:35 -0700380 if [ "$1" == "-f" ]; then
381 create-zk-conf
382 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700383 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700384 create-logback-conf
385 else
386 create-conf-interactive ${ZK_CONF} create-zk-conf
387 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700388 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700389 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
390 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700391
392 trap - ERR
393}
394############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700395
Naoki Shiota590c18d2014-03-31 17:52:59 -0700396
397###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700398function zk {
399 case "$1" in
400 start)
401 start-zk
402 ;;
403 stop)
404 stop-zk
405 ;;
406 stat*) # <- status
407 status-zk
408 ;;
409 re*) # <- restart
410 stop-zk
411 start-zk
412 ;;
413 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700414 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700415 exit 1
416 esac
417}
418
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700419function load-zk-cfg {
420 if [ -f "${ZK_CONF}" ]; then
421 local filename=`basename ${ZK_CONF}`
422 local dirname=`dirname ${ZK_CONF}`
423
424 # Run ZooKeeper with our configuration
425 export ZOOCFG=${filename}
426 export ZOOCFGDIR=${dirname}
427 fi
428}
429
Naoki Shiota4e463182014-03-21 15:13:24 -0700430function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700431 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700432
Naoki Shiota4e928512014-04-03 15:49:35 -0700433 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700434 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700435
436 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700437
Naoki Shiota72209722014-04-08 14:32:17 -0700438 ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700439}
440
441function stop-zk {
442 kill-processes "ZooKeeper" `jps -l | grep org.apache.zookeeper.server | awk '{print $1}'`
443}
444
445function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700446 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700447
Naoki Shiota72209722014-04-08 14:32:17 -0700448 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700449}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700450
451function check-zk {
452 # assumption here is that ZK status script is the last command in status-zk.
453 status-zk &> /dev/null
454 local zk_status=$?
455 if [ "$zk_status" -ne 0 ]; then
456 return 1;
457 fi
458 return 0
459}
460
461# wait-zk-or-die {timeout-sec}
462function wait-zk-or-die {
463 local retries=${1:-1}
464 # do-while retries >= 0
465 while true; do
466 check-zk
467 local zk_status=$?
468 if [ "$zk_status" -eq 0 ]; then
469 return 0
470 fi
471 sleep 1;
472 ((retries -= 1))
473 (( retries >= 0 )) || break
474 done
475 echo "ZooKeeper is not running."
476 exit 1
477}
478
Naoki Shiota590c18d2014-03-31 17:52:59 -0700479############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700480
481
Naoki Shiota590c18d2014-03-31 17:52:59 -0700482####### Functions related to RAMCloud ######
Naoki Shiota9df15d32014-03-27 14:26:20 -0700483function start-backend {
484 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700485 if [ $1 == "coord" ]; then
486 rc-coord startifdown
487 elif [ $1 == "server" ]; then
488 rc-server startifdown
489 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700490 fi
491}
492
493function stop-backend {
494 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
495 if [ $rcsn != 0 ]; then
496 rc-server stop
497 fi
498
Naoki Shiota590c18d2014-03-31 17:52:59 -0700499 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700500 if [ $rccn != 0 ]; then
501 rc-coord stop
502 fi
503}
504
Naoki Shiota590c18d2014-03-31 17:52:59 -0700505
Naoki Shiota4e463182014-03-21 15:13:24 -0700506### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700507function rc-coord-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700508 local host=${RC_COORD_IP}
509 if [ -z "${host}" ]; then
510 # falling back to 0.0.0.0
511 host="0.0.0.0"
512 fi
513 echo "${RC_COORD_PROTOCOL}:host=${host},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700514}
515
516function rc-server-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700517 local host=${RC_SERVER_IP}
518 if [ -z "${host}" ]; then
519 # falling back to 0.0.0.0
520 host="0.0.0.0"
521 fi
522 echo "${RC_SERVER_PROTOCOL}:host=${host},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700523}
524
Naoki Shiota4e463182014-03-21 15:13:24 -0700525function rc-coord {
526 case "$1" in
527 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700528 stop-coord
529 start-coord
530 ;;
531 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700532 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700533 if [ $n == 0 ]; then
534 start-coord
535 else
536 echo "$n instance of RAMCloud coordinator running"
537 fi
538 ;;
539 stop)
540 stop-coord
541 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700542 deldb)
543 stop-backend
544 del-coord-info
545 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700546 stat*) # <- status
547 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
548 echo "$n RAMCloud coordinator running"
549 ;;
550 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700551 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700552 exit 1
553 esac
554}
555
556function start-coord {
Yuta HIGUCHI71703322014-05-06 09:17:29 -0700557 wait-zk-or-die 2
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700558
Naoki Shiota4e463182014-03-21 15:13:24 -0700559 if [ ! -d ${LOGDIR} ]; then
560 mkdir -p ${LOGDIR}
561 fi
562 if [ -f $RAMCLOUD_COORD_LOG ]; then
563 rotate-log $RAMCLOUD_COORD_LOG
564 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700565
566 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700567
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700568 # TODO Configuration for ZK address, port
569 local zk_addr="localhost:2181"
570 # RAMCloud cluster name
571 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700572 # RAMCloud transport timeout
573 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700574 # RAMCloud option deadServerTimeout
575 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
576 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
577
578 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
579 # but actual RAMCloud code only uses -C argument now.
580 # (FYI: -C is documented to be deprecated in the document)
581
582 local coord_args="-C ${coord_addr}"
583 coord_args="${coord_args} --externalStorage zk:${zk_addr}"
584 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700585 coord_args="${coord_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700586 coord_args="${coord_args} --deadServerTimeout ${rc_coord_deadServerTimeout}"
587
588 # Read environment variables if set
589 coord_args="${coord_args} ${RC_COORDINATOR_OPTS}"
590
591 if [ "${ONOS_HOST_ROLE}" == "single-node" ]; then
592 # Note: Following reset is required, since RC restart is considered node failure,
593 # and tries recovery of server, which will never succeed after restart.
594 echo "Role configured to single-node mode. RAMCloud cluster will be reset on each start-up."
595 coord_args="${coord_args} --reset"
596 fi
597
Naoki Shiota4e463182014-03-21 15:13:24 -0700598 # Run ramcloud
599 echo -n "Starting RAMCloud coordinator ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700600 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700601 echo "STARTED"
602}
603
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700604function del-coord-info {
605 wait-zk-or-die 1
606
607 if [ ! -d ${LOGDIR} ]; then
608 mkdir -p ${LOGDIR}
609 fi
610 if [ -f $RAMCLOUD_COORD_LOG ]; then
611 rotate-log $RAMCLOUD_COORD_LOG
612 fi
613
614 local coord_addr=`rc-coord-addr`
615
616 # TODO Configuration for ZK address, port
617 local zk_addr="localhost:2181"
618 # RAMCloud cluster name
619 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
620 # RAMCloud option deadServerTimeout
621 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
622 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
623
624 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
625 # but actual RAMCloud code only uses -C argument now.
626 # (FYI: -C is documented to be deprecated in the document)
627
628 local coord_args="-C ${coord_addr}"
629 coord_args="${coord_args} --externalStorage zk:${zk_addr}"
630 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
631
632 # Note: --reset will reset ZK stored info and start running as acoordinator.
633 echo -n "Deleting RAMCloud cluster coordination info ... "
634 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} --reset &> $RAMCLOUD_COORD_LOG &
635
636 # TODO Assuming 1sec is enough. To be sure monitor log?
637 sleep 1
638 # Silently kill coordinator
Yuta HIGUCHIce9f3ee2014-05-14 09:48:40 -0700639 (pkill -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator &> /dev/null)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700640
641 echo "DONE"
642}
Naoki Shiota4e463182014-03-21 15:13:24 -0700643
644function stop-coord {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700645 kill-processes "RAMCloud coordinator" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700646}
647
Naoki Shiota4e463182014-03-21 15:13:24 -0700648### Functions related to RAMCloud server
649function rc-server {
650 case "$1" in
651 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700652 stop-server
653 start-server
654 ;;
655 startifdown)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700656 local n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700657 if [ $n == 0 ]; then
658 start-server
659 else
660 echo "$n instance of RAMCloud server running"
661 fi
662 ;;
663 stop)
664 stop-server
665 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700666 deldb)
667 stop-server
668 del-server-backup
669 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700670 stat*) # <- status
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700671 n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700672 echo "$n RAMCloud server running"
673 ;;
674 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700675 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700676 exit 1
677 esac
678}
679
680function start-server {
Yuta HIGUCHI71703322014-05-06 09:17:29 -0700681 wait-zk-or-die 2
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700682
Naoki Shiota4e463182014-03-21 15:13:24 -0700683 if [ ! -d ${LOGDIR} ]; then
684 mkdir -p ${LOGDIR}
685 fi
686 if [ -f $RAMCLOUD_SERVER_LOG ]; then
687 rotate-log $RAMCLOUD_SERVER_LOG
688 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700689
690 local coord_addr=`rc-coord-addr`
691 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700692
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700693 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.server.masterServiceThreads 5)
694 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.server.logCleanerThreads 1)
Yuta HIGUCHI52efcbb2014-05-16 09:30:33 -0700695 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.server.detectFailures 0)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700696
697 # TODO Configuration for ZK address, port
698 local zk_addr="localhost:2181"
699 # RAMCloud cluster name
700 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700701 # RAMCloud transport timeout
702 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700703 # replication factor (-r) config
704 local rc_replicas=$(read-conf ${ONOS_CONF} ramcloud.server.replicas 0)
705 # backup file path (-f) config
706 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
707 mkdir -p `dirname ${rc_datafile}`
708
709 local server_args="-L ${server_addr}"
710 server_args="${server_args} --externalStorage zk:${zk_addr}"
711 server_args="${server_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700712 server_args="${server_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700713 server_args="${server_args} --masterServiceThreads ${masterServiceThreads}"
714 server_args="${server_args} --logCleanerThreads ${logCleanerThreads}"
715 server_args="${server_args} --detectFailures ${detectFailures}"
716 server_args="${server_args} --replicas ${rc_replicas}"
717 server_args="${server_args} --file ${rc_datafile}"
718
719 # Read environment variables if set
720 server_args="${server_args} ${RC_SERVER_OPTS}"
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700721
Naoki Shiota4e463182014-03-21 15:13:24 -0700722 # Run ramcloud
723 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700724 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server ${server_args} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700725 echo "STARTED"
726}
727
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700728function del-server-backup {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700729 echo -n "Delete RAMCloud backup server data [y/N]? "
730 while [ 1 ]; do
731 read key
732 if [ "${key}" == "Y" -o "${key}" == "y" ]; then
733 break
734 elif [ -z "${key}" -o "${key}" == "N" -o "${key}" == "n" ]; then
735 echo "Cancelled."
736 return
737 fi
738 echo "[y/N]? "
739 done
740
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700741 echo -n "Removing RAMCloud backup server data ... "
742 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
743 rm -f ${rc_datafile}
744 echo "DONE"
745}
746
Naoki Shiota4e463182014-03-21 15:13:24 -0700747function stop-server {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700748 kill-processes "RAMCloud server" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700749}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700750############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700751
752
Naoki Shiota590c18d2014-03-31 17:52:59 -0700753## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700754function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700755 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700756 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700757 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700758 ${MVN} -f ${ONOS_HOME}/pom.xml compile
759 fi
760 JAVA_CP=`cat ${CPFILE}`
761 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
762
763 case "$1" in
764 start)
765 stop-onos
766 start-onos
767 ;;
768 startnokill)
769 start-onos
770 ;;
771 startifdown)
772 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
773 if [ $n == 0 ]; then
774 start-onos
775 else
776 echo "$n instance of onos running"
777 fi
778 ;;
779 stop)
780 stop-onos
781 ;;
782 stat*) # <- status
783 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
784 echo "$n instance of onos running"
785 ;;
786 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700787 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700788 exit 1
789 esac
790}
791
792function start-onos {
793 if [ ! -d ${LOGDIR} ]; then
794 mkdir -p ${LOGDIR}
795 fi
796 # Backup log files
797 for log in ${LOGS}; do
798 if [ -f ${log} ]; then
799 rotate-log ${log}
800 fi
801 done
Naoki Shiota9df15d32014-03-27 14:26:20 -0700802
Naoki Shiota4e463182014-03-21 15:13:24 -0700803 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700804 echo "[WARNING] ${ONOS_LOGBACK} not found."
805 echo " Run \"\$ $0 setup\" to create."
806 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700807 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700808
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700809 if [ ! -f ${HC_CONF} ]; then
810 echo "[WARNING] ${HC_CONF} not found."
811 echo " Run \"\$ $0 setup\" to create."
812 exit 1
813 fi
814
815 # specify hazelcast.xml to datagrid
816 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
817
818 # specify backend config
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700819 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700820 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
821 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700822 elif [ "${ONOS_HOST_BACKEND}" = "hazelcast" ]; then
823 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.hazelcast.baseConfig=${HC_CONF}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700824 fi
825
Naoki Shiota9df15d32014-03-27 14:26:20 -0700826 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -0700827
828 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
829 cd ${ONOS_HOME}
830
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700831 echo -n "Starting ONOS controller ... "
Naoki Shiota4e463182014-03-21 15:13:24 -0700832 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
833
834 # We need to wait a bit to find out whether starting the ONOS process succeeded
835 sleep 1
836
837 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700838 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700839 echo " STARTED"
840 else
841 echo " FAILED"
842 fi
843
844# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
845# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
846}
847
848function stop-onos {
849 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
850# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
851}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700852############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700853
854
Naoki Shiota590c18d2014-03-31 17:52:59 -0700855################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -0700856case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -0700857 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -0700858 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700859 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700860 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -0700861 mode_parameter=${ONOS_HOST_ROLE}
862 if [ ! -z "$2" ]; then
863 mode_parameter=$2
864 fi
865
866 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -0700867 single-node)
868 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700869 start-backend coord
870 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700871 onos startifdown
872 ;;
873 coord-node)
874 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700875 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -0700876 onos startifdown
877 ;;
878 server-node)
879 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700880 start-backend server
881 onos startifdown
882 ;;
883 coord-and-server-node)
884 zk start
885 start-backend coord
886 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700887 onos startifdown
888 ;;
889 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700890 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700891 ;;
892 esac
893 echo
894 ;;
895 stop)
896 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
897 if [ $on != 0 ]; then
898 onos stop
899 fi
900
Naoki Shiota9df15d32014-03-27 14:26:20 -0700901 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -0700902
903 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
904 if [ $zkn != 0 ]; then
905 zk stop
906 fi
907 echo
908 ;;
909 restart)
910 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
911 if [ $on != 0 ]; then
912 onos stop
913 fi
914
915 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
916 if [ $rcsn != 0 ]; then
917 rc-server stop
918 fi
919
Naoki Shiota590c18d2014-03-31 17:52:59 -0700920 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700921 if [ $rccn != 0 ]; then
922 rc-coord stop
923 fi
924
925 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
926 if [ $zkn != 0 ]; then
927 zk restart
928 fi
929
930 if [ $rccn != 0 ]; then
931 rc-coord startifdown
932 fi
933
934 if [ $rcsn != 0 ]; then
935 rc-server startifdown
936 fi
937
938 if [ $on != 0 ]; then
939 onos startifdown
940 fi
941 echo
942 ;;
943 stat*) # <- status
944 echo '[ZooKeeper]'
945 zk status
946 echo
947 echo '[RAMCloud coordinator]'
948 rc-coord status
949 echo
950 echo '[RAMCloud server]'
951 rc-server status
952 echo
953 echo '[ONOS core]'
954 onos status
955 echo
956 ;;
957 zk)
958 zk $2
959 ;;
960 rc-c*) # <- rc-coordinator
961 rc-coord $2
962 ;;
963 rc-s*) # <- rc-server
964 rc-server $2
965 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700966 rc)
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700967 # TODO make deldb command more organized (clarify when it can work)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700968 rc-coord $2
969 rc-server $2
970 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700971 core)
972 onos $2
973 ;;
974 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700975 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700976 exit 1
977esac
Naoki Shiota590c18d2014-03-31 17:52:59 -0700978