blob: 869be4421a3102062f42116bed477e02b28f017b [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`)
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -070011# $ONOS_DEBUG : option to enable debugger for ONOS (false)
Naoki Shiota590c18d2014-03-31 17:52:59 -070012# $RAMCLOUD_HOME : path of root directory of RAMCloud repository (~/ramcloud)
13# $RAMCLOUD_BRANCH : branch name of RAMCloud to use (master)
Yuta HIGUCHIfb1905a2014-06-09 14:07:34 -070014# $ZK_HOME : path of root directory of ZooKeeper (~/zookeeper-3.4.6)
Naoki Shiota72209722014-04-08 14:32:17 -070015# $ZK_LIB_DIR : path of ZooKeeper library (/var/lib/zookeeper)
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070016# $ZK_LOG_DIR : path of ZooKeeper log output directory (~/ONOS/onos-logs/zk-`hostname`)
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -070017# $JVM_OPTS : JVM options to add when starting ONOS
18# $JVM_DEBUG_PORT : port to use for remote debugging ONOS process, when $ONOS_DEBUG is enabled (22007)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070019# $ZK_CONF : path of ZooKeeper config file (~/ONOS/conf/zoo.cfg)
20# $HC_CONF : path of Hazelcast config file (~/ONOS/conf/hazelcast.xml)
21# $RAMCLOUD_CONF : path of RAMCloud config file (~/ONOS/conf/ramcloud.conf)
Naoki Shiota590c18d2014-03-31 17:52:59 -070022#####################################################
23
Naoki Shiota590c18d2014-03-31 17:52:59 -070024ONOS_HOME=${ONOS_HOME:-$(cd `dirname $0`; pwd)}
Naoki Shiota9df15d32014-03-27 14:26:20 -070025ONOS_CONF_DIR=${ONOS_CONF_DIR:-${ONOS_HOME}/conf}
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070026ONOS_CONF=${ONOS_CONF:-${ONOS_CONF_DIR}/onos_node.`hostname`.conf}
Yuta HIGUCHIec920702014-08-01 17:54:30 -070027ONOS_DUMP_DIR=${ONOS_DUMP_DIR:-${ONOS_HOME}}
Naoki Shiota590c18d2014-03-31 17:52:59 -070028
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070029source ${ONOS_HOME}/scripts/common/utils.sh
30
Yuta HIGUCHIe748ecc2014-05-28 13:55:03 -070031confirm-if-root
32
Naoki Shiota590c18d2014-03-31 17:52:59 -070033if [ ! -f ${ONOS_CONF} ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070034 # falling back to default config file
35 ONOS_CONF=${ONOS_CONF_DIR}/onos_node.conf
36 if [ ! -f ${ONOS_CONF} ]; then
37 echo "${ONOS_CONF} not found."
38 exit 1
39 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -070040fi
Naoki Shiota4e928512014-04-03 15:49:35 -070041
Naoki Shiota4e928512014-04-03 15:49:35 -070042### Variables read from ONOS config file ###
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -070043ONOS_CLUSTER_NAME=$(read-conf ${ONOS_CONF} onos.cluster.name "onos")
Naoki Shiota4e928512014-04-03 15:49:35 -070044ONOS_HOST_NAME=$(read-conf ${ONOS_CONF} host.name `hostname`)
45ONOS_HOST_IP=$(read-conf ${ONOS_CONF} host.ip)
46ONOS_HOST_ROLE=$(read-conf ${ONOS_CONF} host.role)
47ONOS_HOST_BACKEND=$(read-conf ${ONOS_CONF} host.backend)
48ZK_HOSTS=$(read-conf ${ONOS_CONF} zookeeper.hosts ${ONOS_HOST_NAME})
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -070049ZK_CLIENTPORT=$(read-conf ${ONOS_CONF} zookeeper.clientPort 2181)
50ZK_PORTS=$(read-conf ${ONOS_CONF} zookeeper.ports 2888:3888)
Naoki Shiota4e928512014-04-03 15:49:35 -070051RC_COORD_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.coordinator.protocol "fast+udp")
52RC_COORD_IP=$(read-conf ${ONOS_CONF} ramcloud.coordinator.ip ${ONOS_HOST_IP})
53RC_COORD_PORT=$(read-conf ${ONOS_CONF} ramcloud.coordinator.port 12246)
54RC_SERVER_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.server.protocol "fast+udp")
55RC_SERVER_IP=$(read-conf ${ONOS_CONF} ramcloud.server.ip ${ONOS_HOST_IP})
56RC_SERVER_PORT=$(read-conf ${ONOS_CONF} ramcloud.server.port 12242)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070057HC_HOST_PORT=$(read-conf ${ONOS_CONF} hazelcast.host.port 5701)
Naoki Shiota4e928512014-04-03 15:49:35 -070058HC_TCPIP_MEMBERS=$(read-conf ${ONOS_CONF} hazelcast.tcp-ip.members)
59HC_MULTICAST_GROUP=$(read-conf ${ONOS_CONF} hazelcast.multicast.group "224.2.2.3")
60HC_MULTICAST_PORT=$(read-conf ${ONOS_CONF} hazelcast.multicast.port 54327)
Naoki Shiota590c18d2014-03-31 17:52:59 -070061############################################
62
63
64############## Other variables #############
Naoki Shiota4e928512014-04-03 15:49:35 -070065ONOS_TEMPLATE_DIR=${ONOS_CONF_DIR}/template
66
Naoki Shiota4e463182014-03-21 15:13:24 -070067LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
68
Yuta HIGUCHIfb1905a2014-06-09 14:07:34 -070069ZK_HOME=${ZK_HOME:-~/zookeeper-3.4.6}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070070ZK_CONF=${ZK_CONF:-${ONOS_CONF_DIR}/zoo.cfg}
Naoki Shiota4e928512014-04-03 15:49:35 -070071ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070072# Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070073ZK_LOG_DIR=${ZK_LOG_DIR:-${ONOS_HOME}/onos-logs/zk-${ONOS_HOST_NAME}}
Naoki Shiota72209722014-04-08 14:32:17 -070074ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070075ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070076
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070077HC_CONF=${HC_CONF:-${ONOS_CONF_DIR}/hazelcast.xml}
Naoki Shiota4e928512014-04-03 15:49:35 -070078HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
79
Naoki Shiota4e463182014-03-21 15:13:24 -070080RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070081RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
82RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070083RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070084RAMCLOUD_CONF=${RAMCLOUD_CONF:-${ONOS_CONF_DIR}/ramcloud.conf}
Naoki Shiota4e463182014-03-21 15:13:24 -070085
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070086export LD_LIBRARY_PATH=${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}:$LD_LIBRARY_PATH
Naoki Shiota4e463182014-03-21 15:13:24 -070087
88## 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 -070089ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070090ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070091ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070092LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070093LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070094ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070095ONOS_LOG_ROLLING_PATTERN="${LOGDIR}/${LOGBASE}.%i.log.gz"
96ONOS_STDOUT_LOG="${LOGDIR}/${LOGBASE}.stdout"
97ONOS_STDERR_LOG="${LOGDIR}/${LOGBASE}.stderr"
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -070098ONOS_COMPILATION_LOG="${LOGDIR}/compilation.${ONOS_HOST_NAME}.log"
99ONOS_GC_LOG="${LOGDIR}/gc.${ONOS_HOST_NAME}.log"
Naoki Shiota4e463182014-03-21 15:13:24 -0700100PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700101LOGS="$ONOS_LOG $ONOS_STDOUT_LOG $ONOS_STDERR_LOG $ONOS_COMPILATION_LOG $ONOS_GC_LOG $PCAP_LOG"
Naoki Shiota4e463182014-03-21 15:13:24 -0700102
Naoki Shiota9df15d32014-03-27 14:26:20 -0700103ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -0700104JMX_PORT=${JMX_PORT:-7189}
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -0700105JVM_DEBUG_PORT=${JVM_DEBUG_PORT:-22007}
Naoki Shiota4e463182014-03-21 15:13:24 -0700106
107# Set JVM options
108JVM_OPTS="${JVM_OPTS:-}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700109JVM_OPTS="$JVM_OPTS -server -d64"
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700110
111# TODO Revisit appropriate CodeCache size
112JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700113
114# Uncomment or specify appropriate value as JVM_OPTS environment variables.
115#JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
Naoki Shiota4e463182014-03-21 15:13:24 -0700116#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
117#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700118
119#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC"
120JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700121#JVM_OPTS="$JVM_OPTS -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
122# Note: shoult NOT specify -Xmn when using G1GC
123# http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/G1GettingStarted/index.html
124
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700125JVM_OPTS="$JVM_OPTS -XX:+AggressiveOpts"
126
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700127# TODO We may want to remove UseFastAccessorMethods option: http://bugs.java.com/view_bug.do?bug_id=6385687
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700128JVM_OPTS="$JVM_OPTS -XX:+UseFastAccessorMethods"
129
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700130# TODO revisit these derived from floodlight
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700131JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192"
132JVM_OPTS="$JVM_OPTS -XX:FreqInlineSize=8192"
133JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500"
134
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700135# FIXME Specify appropriate cmd to handle fatal crash
136# http://www.oracle.com/technetwork/java/javase/clopts-139448.html#gbmum
Naoki Shiota4e463182014-03-21 15:13:24 -0700137JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700138#JVM_OPTS="$JVM_OPTS -XX:+ShowMessageBoxOnError"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700139
Praseed Balakrishnan4fe80812014-07-24 11:29:19 -0700140# This option tells the VM to generate a heap dump when memory allocation cannot be satisfied.
141# http://www.oracle.com/technetwork/java/javase/clopts-139448.html#gbzrr
Yuta HIGUCHIec920702014-08-01 17:54:30 -0700142JVM_OPTS="$JVM_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${ONOS_DUMP_DIR}"
Praseed Balakrishnan4fe80812014-07-24 11:29:19 -0700143
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700144# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
145JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
146
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700147JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
148JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
149JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
150
Yuta HIGUCHI6bcaf982014-07-30 15:49:58 -0700151JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j -Dhazelcast.jmx=true -Dhazelcast.jmx.detailed=true"
Naoki Shiota4e463182014-03-21 15:13:24 -0700152
Yuta HIGUCHIc248d0d2014-07-30 16:14:18 -0700153# TODO revisit HZ related magic numbers
154# HZ Default:1000000
155JVM_OPTS="$JVM_OPTS -Dhazelcast.event.queue.capacity=4000000"
156# HZ Default:32 (KB)
157JVM_OPTS="$JVM_OPTS -Dhazelcast.socket.receive.buffer.size=4096"
158# HZ Default:32 (KB)
159JVM_OPTS="$JVM_OPTS -Dhazelcast.socket.send.buffer.size=4096"
160
161
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700162if [ "${ONOS_DEBUG}" == "true" ]; then
163 # Compilation diagnosis
164 JVM_OPTS="$JVM_OPTS -XX:+UnlockDiagnosticVMOptions"
165 JVM_OPTS="$JVM_OPTS -XX:+LogCompilation"
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700166 JVM_OPTS="$JVM_OPTS -XX:LogFile=${ONOS_COMPILATION_LOG}"
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700167
168 # GC diagnosis
169 JVM_OPTS="$JVM_OPTS -verbose:gc"
170 JVM_OPTS="$JVM_OPTS -verbose:sizes"
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700171 JVM_OPTS="$JVM_OPTS -Xloggc:${ONOS_GC_LOG}"
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700172 JVM_OPTS="$JVM_OPTS -XX:GCLogFileSize=1G"
173 JVM_OPTS="$JVM_OPTS -XX:+PrintGCDetails"
174 JVM_OPTS="$JVM_OPTS -XX:+PrintGCTimeStamps"
175 JVM_OPTS="$JVM_OPTS -XX:+PrintGCDateStamps"
176 JVM_OPTS="$JVM_OPTS -XX:+PrintTenuringDistribution"
177 JVM_OPTS="$JVM_OPTS -XX:+PrintCMSInitiationStatistics"
178fi
179
180# dump final JVM flags to stdout
181JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700182
Naoki Shiota4e463182014-03-21 15:13:24 -0700183# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700184MAIN_CLASS="net.onrc.onos.core.main.Main"
Naoki Shiota4e463182014-03-21 15:13:24 -0700185
186MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700187############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700188
Naoki Shiota9df15d32014-03-27 14:26:20 -0700189
Naoki Shiota590c18d2014-03-31 17:52:59 -0700190############# Common functions #############
191function print_usage {
Naoki Shiota05721b32014-04-29 14:41:12 -0700192 local scriptname=`basename $0`
Naoki Shiota590c18d2014-03-31 17:52:59 -0700193 local filename=`basename ${ONOS_CONF}`
194 local usage="Usage: setup/start/stop ONOS on this server.
Naoki Shiota05721b32014-04-29 14:41:12 -0700195 \$ ${scriptname} setup [-f]
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700196 Set up ONOS node using ${ONOS_CONF} .
Naoki Shiota590c18d2014-03-31 17:52:59 -0700197 - generate and replace config file of ZooKeeper.
198 - create myid in ZooKeeper datadir.
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700199 - generate and replace config file for Hazelcast.
200 - generate and replace config file for RAMCloud.
Naoki Shiota590c18d2014-03-31 17:52:59 -0700201 - generate and replace logback.${ONOS_HOST_NAME}.xml
202 If -f option is used, all existing files will be overwritten without confirmation.
Naoki Shiota05721b32014-04-29 14:41:12 -0700203 \$ ${scriptname} start [single-node|coord-node|server-node|coord-and-server-node]
Naoki Shiota590c18d2014-03-31 17:52:59 -0700204 Start ONOS node with specific RAMCloud entities
205 - single-node: start ONOS with stand-alone RAMCloud
206 - coord-node : start ONOS with RAMCloud coordinator
207 - server-node: start ONOS with RAMCloud server
208 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
209 * Default behavior can be defined by ${filename}
Naoki Shiota05721b32014-04-29 14:41:12 -0700210 \$ ${scriptname} stop
Naoki Shiota590c18d2014-03-31 17:52:59 -0700211 Stop all ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700212 \$ ${scriptname} restart
Naoki Shiota590c18d2014-03-31 17:52:59 -0700213 Stop and start currently running ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700214 \$ ${scriptname} status
Naoki Shiota590c18d2014-03-31 17:52:59 -0700215 Show status of ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700216 \$ ${scriptname} {zk|rc-coord|rc-server|core} {start|stop|restart|status}
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700217 Control specific ONOS-related process
218 \$ ${scriptname} rc deldb
219 Delete data in RAMCloud"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700220
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700221 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700222}
223
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700224# rotate-log [log-filename] [max rotations]
225# Example:
226# foobar.log -> foobar.log.1
227# foobar.log.1 -> foobar.log.2
228# foobar.log.gz -> foobar.log.1.gz
Naoki Shiota4e463182014-03-21 15:13:24 -0700229function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700230 local logfile=$1
231 local nr_max=${2:-10}
232 if [ -f $logfile ]; then
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700233 # TODO treating only .gz now. probably want more generic solution
234 local basename=${logfile%%.gz}
235 local append=""
236 if [ "$basename" != "$logfile" ]; then
237 append=".gz"
238 fi
Naoki Shiota4e928512014-04-03 15:49:35 -0700239 for i in `seq $(expr $nr_max - 1) -1 1`; do
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700240 if [ -f ${basename}.${i}${append} ]; then
241 mv -f ${basename}.${i}${append} ${basename}.`expr $i + 1`${append}
Naoki Shiota4e928512014-04-03 15:49:35 -0700242 fi
243 done
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700244 mv ${basename}${append} ${basename}.1${append}
Naoki Shiota4e928512014-04-03 15:49:35 -0700245 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700246}
247
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700248# pack-rotate-log [packname] "[log-filenames]" [max rotations]
249# Note: [packname] and all the log-files specified by [log-filenames]
250# must reside in same dir
251# Example:
252# pack="/foo/bar/testlogs"
253# logfiles="/foo/bar/test1.log /foo/bar/test*.log"
254# pack-rotate-log $pack "$logfiles" 5
255# => testlogs.tar.bz2 (contain test1.log test2.log ...)
256# testlogs.tar.bz2 -> testlogs.1.tar.bz2
257# testlogs.1.tar.bz2 -> testlogs.2.tar.bz2
258# ...
259function pack-rotate-log {
260 local packname=$1
261 local logfiles=$2
262 local nr_max=${3:-10}
263 local suffix=".tar.bz2"
264
265 # rotate
266 for i in `seq $(expr $nr_max - 1) -1 1`; do
267 if [ -f ${packname}.${i}${suffix} ]; then
268 mv -f -- ${packname}.${i}${suffix} ${packname}.`expr $i + 1`${suffix}
269 fi
270 done
271 if [ -f ${packname}${suffix} ]; then
272 mv -- ${packname}${suffix} ${packname}.1${suffix}
273 fi
274
275 # pack
276 local existing_logfiles=$( ls -1 $logfiles | xargs basename 2>/dev/null )
277 if [ ! -z "${existing_logfiles}" ]; then
278 tar cjf ${packname}${suffix} -C `dirname ${packname}` -- ${existing_logfiles}
279 rm -- `dirname ${packname}`/${existing_logfiles}
280 fi
281}
282
Naoki Shiota4e463182014-03-21 15:13:24 -0700283# kill-processes {module-name} {array of pids}
284function kill-processes {
285 # Kill the existing processes
286 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700287 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700288 echo -n "Stopping $1 ... "
289 fi
290 for p in ${pids}; do
291 if [ x$p != "x" ]; then
Naoki Shiota4b355762014-05-27 11:46:43 -0700292 # Check if target process is accesible from current user
293 kill -0 $p
294 if [ "$?" -ne 0 ]; then
295 # Error exit code here means "failed to send signal".
296 # Supposedly because of permission error.
297 echo "Failed to kill process (pid: $p)."
298 echo "Check if current user is the same as process owner."
299 exit 1
300 fi
301
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700302 (
303 # Ask process with SIGTERM first, if that did not kill the process
304 # wait 1s and if process still exist, force process to be killed.
305 kill -TERM $p && kill -0 $p && sleep 1 && kill -0 $p && kill -KILL $p
306 ) 2> /dev/null
Naoki Shiota4b355762014-05-27 11:46:43 -0700307
308 # Ensure process is killed.
309 kill -0 $p 2> /dev/null
310 if [ "$?" -ne 0 ]; then
311 # Error exit code here means "process not found", i.e. "kill succeeded".
312 echo "Killed existing process (pid: $p)"
313 else
314 # Process still exists. Some unexpected error occurs.
315 echo "Failed to kill process (pid: $p)."
316 echo "Unexpected error occurs during process termination."
317 exit 1
318 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700319 fi
320 done
321}
322
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700323function handle-error {
324 set -e
325
326 revert-confs
327
328 set +e
329
330 exit 1
331}
332
Naoki Shiota4e928512014-04-03 15:49:35 -0700333# revert-confs [error message]
334function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700335 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700336
Naoki Shiota4e928512014-04-03 15:49:35 -0700337 revert-file `basename ${ZK_CONF}`
338 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700339
340 echo "EXIT"
341
342 if [ ! -z "$1" ]; then
343 echo $1
344 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700345}
346
Naoki Shiota590c18d2014-03-31 17:52:59 -0700347function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700348 echo -n "Creating ${ZK_CONF} ... "
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700349
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700350 # Create the ZooKeeper lib directory
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700351 if [[ ! ( -w ${ZK_LIB_DIR} && -d ${ZK_LIB_DIR} ) ]]; then
Naoki Shiota7f495cf2014-05-21 17:23:25 -0700352 local SUDO=${SUDO:-sudo}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700353 local whoami=`whoami`
354 {
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700355 ${SUDO} mkdir -p ${ZK_LIB_DIR}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700356 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
357 } || {
358 echo "FAILED"
359 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
360 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
361 exit 1
362 }
363 fi
364
365 # creation of ZooKeeper config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700366 local temp_zk=`begin-conf-creation ${ZK_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700367
Naoki Shiota4e928512014-04-03 15:49:35 -0700368 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700369
370 local i=1
371 local myid=
372 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700373 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700374 myid=$i
375 break
376 fi
377 i=`expr $i + 1`
378 done
379
380 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700381 local filename=`basename ${ONOS_CONF}`
Naoki Shiota35f8d5c2014-04-08 11:13:18 -0700382 revert-confs "[ERROR] In ${filename}, zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700383 fi
384
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700385 if [ -f "${ZK_MY_ID}" ]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700386 # sudo will be needed if ZK_MY_ID is already created by other (old) script
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700387 local SUDO=${SUDO:-}
388 {
389 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
390 } || {
391 echo "FAILED"
392 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700393 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700394 exit 1
395 }
396 fi
397
Naoki Shiota9df15d32014-03-27 14:26:20 -0700398 echo ${myid} > ${ZK_MY_ID}
399
400 echo -n "myid is assigned to ${myid} ... "
401
402 while read line; do
403 if [[ $line =~ ^__HOSTS__$ ]]; then
404 i=1
405 for host in ${hostarr}; do
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700406 # TODO: ZK ports should be configurable per host
407 local hostline="server.${i}=${host}:${ZK_PORTS}"
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700408 echo $hostline
Naoki Shiota9df15d32014-03-27 14:26:20 -0700409 i=`expr $i + 1`
410 done
411 elif [[ $line =~ __DATADIR__ ]]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700412 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700413 else
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700414 echo $line
Naoki Shiota9df15d32014-03-27 14:26:20 -0700415 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700416 done < ${ZK_CONF_TEMPLATE} > ${temp_zk}
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700417 sed -e "s|__CLIENTPORT__|${ZK_CLIENTPORT}|" -i "" ${temp_zk}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700418
419 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700420
421 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700422}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700423
Naoki Shiota4e928512014-04-03 15:49:35 -0700424function create-hazelcast-conf {
425 echo -n "Creating ${HC_CONF} ... "
426
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700427 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700428
429 # To keep indent of XML file, change IFS
430 local IFS=''
431 while read line; do
432 if [[ $line =~ __HC_NETWORK__ ]]; then
433 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
434 # temporary change
435 IFS=' '
436 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
437 echo '<multicast enabled="false" />'
438 echo '<tcp-ip enabled="true">'
439 for member in ${memberarr}; do
440 echo " <member>${member}</member>"
441 done
442 echo '</tcp-ip>'
443 IFS=''
444 else
445 echo '<multicast enabled="true">'
446 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
447 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
448 echo '</multicast>'
449 echo '<tcp-ip enabled="false" />'
450 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700451 elif [[ $line =~ __HC_PORT__ ]]; then
452 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700453 elif [[ $line =~ __HC_CLUSTER__ ]]; then
454 echo $line | sed -e "s|__HC_CLUSTER__|${ONOS_CLUSTER_NAME}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700455 else
456 echo "${line}"
457 fi
458 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
459
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700460 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700461
462 echo "DONE"
463}
464
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700465function create-ramcloud-conf {
466 echo -n "Creating ${RAMCLOUD_CONF} ... "
467
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700468 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700469
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700470 local rc_locator=`echo "${ZK_HOSTS}" | sed -e "s/,/:${ZK_CLIENTPORT},/g"`
471 rc_locator+=":${ZK_CLIENTPORT}"
472
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700473 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700474
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700475 echo "ramcloud.locator=zk:localhost:2181,${rc_locator}" > ${temp_rc}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700476 echo "ramcloud.clusterName=${rc_cluster_name}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700477
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700478 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700479
480 echo "DONE"
481}
482
Naoki Shiota590c18d2014-03-31 17:52:59 -0700483function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700484 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700485
Naoki Shiota9df15d32014-03-27 14:26:20 -0700486 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700487 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700488
489 sed -e "s|__FILENAME__|${ONOS_LOG}|" \
490 -e "s|__ROLLING_PATTERN__|${ONOS_LOG_ROLLING_PATTERN}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700491
492 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700493
Naoki Shiota9df15d32014-03-27 14:26:20 -0700494 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700495}
496
Naoki Shiota4e928512014-04-03 15:49:35 -0700497function create-confs {
498 local key
499 local filename
500
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700501 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700502
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700503 echo "Config file : ${ONOS_CONF}"
504
Naoki Shiota4e928512014-04-03 15:49:35 -0700505 if [ "$1" == "-f" ]; then
506 create-zk-conf
507 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700508 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700509 create-logback-conf
510 else
511 create-conf-interactive ${ZK_CONF} create-zk-conf
512 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700513 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700514 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
515 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700516
517 trap - ERR
518}
519############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700520
Naoki Shiota590c18d2014-03-31 17:52:59 -0700521
522###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700523function zk {
524 case "$1" in
525 start)
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700526 check-and-start-zk
Naoki Shiota4e463182014-03-21 15:13:24 -0700527 ;;
528 stop)
529 stop-zk
530 ;;
531 stat*) # <- status
532 status-zk
533 ;;
534 re*) # <- restart
535 stop-zk
536 start-zk
537 ;;
538 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700539 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700540 exit 1
541 esac
542}
543
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700544function load-zk-cfg {
545 if [ -f "${ZK_CONF}" ]; then
546 local filename=`basename ${ZK_CONF}`
547 local dirname=`dirname ${ZK_CONF}`
548
549 # Run ZooKeeper with our configuration
550 export ZOOCFG=${filename}
551 export ZOOCFGDIR=${dirname}
552 fi
553}
554
Naoki Shiota4e463182014-03-21 15:13:24 -0700555function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700556 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700557
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700558 # CONSOLE output dir
Naoki Shiota4e928512014-04-03 15:49:35 -0700559 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700560 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700561
562 load-zk-cfg
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700563
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700564 # level,appender
565 export ZOO_LOG4J_PROP="INFO,ROLLINGFILE"
566 # ROLLINGFILE : ${zookeeper.log.dir}/${zookeeper.log.file}
567 local server_jvm="-Dzookeeper.log.dir=${LOGDIR} -Dzookeeper.log.file=zk.${ONOS_HOST_NAME}.log"
568
569 pack-rotate-log "${LOGDIR}/zk.${ONOS_HOST_NAME}" "${LOGDIR}/zk.${ONOS_HOST_NAME}.log*"
570
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700571 # log4j.properties is read from classpath if not found in CWD.
572 # Using the ZooKeeper supplied default in ZooKeeper conf dir.
573 # TODO: To explicitly specify our customized log4j config file:
574 # export SERVER_JVMFLAGS="-Dlog4j.configuration=${ZK_LOG4J}"
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700575 env CLASSPATH="${ZK_HOME}/conf:${CLASSPATH}" SERVER_JVMFLAGS="${server_jvm}" ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700576}
577
578function stop-zk {
Yuta HIGUCHI7c708362014-06-02 23:15:13 -0700579 echo -n "Stopping ZooKeeper ... "
580 load-zk-cfg
581
582 ${ZK_HOME}/bin/zkServer.sh stop
Naoki Shiota4e463182014-03-21 15:13:24 -0700583}
584
585function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700586 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700587
Naoki Shiota72209722014-04-08 14:32:17 -0700588 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700589}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700590
591function check-zk {
592 # assumption here is that ZK status script is the last command in status-zk.
593 status-zk &> /dev/null
594 local zk_status=$?
595 if [ "$zk_status" -ne 0 ]; then
596 return 1;
597 fi
598 return 0
599}
600
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700601function check-and-start-zk {
602 check-zk
603 local zk_status=$?
604 if [ "$zk_status" -ne 0 ]; then
605 start-zk
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700606 else
607 echo "ZooKeeper already running"
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700608 fi
609}
610
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700611# wait-zk-or-die {timeout-sec}
612function wait-zk-or-die {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700613 local retries=${1:-5}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700614 # do-while retries >= 0
615 while true; do
616 check-zk
617 local zk_status=$?
618 if [ "$zk_status" -eq 0 ]; then
619 return 0
620 fi
621 sleep 1;
622 ((retries -= 1))
623 (( retries >= 0 )) || break
624 done
625 echo "ZooKeeper is not running."
626 exit 1
627}
628
Naoki Shiota590c18d2014-03-31 17:52:59 -0700629############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700630
631
Naoki Shiota590c18d2014-03-31 17:52:59 -0700632####### Functions related to RAMCloud ######
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -0700633
634# Check if this host should be running RAMCloud coordinator:
635function is-coord-role {
636 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
637 case "${ONOS_HOST_ROLE}" in
638 single-node)
639 echo "true"
640 return 0
641 ;;
642 coord-node)
643 echo "true"
644 return 0
645 ;;
646 server-node)
647 echo "false"
648 return 1
649 ;;
650 coord-and-server-node)
651 echo "true"
652 return 0
653 ;;
654 *)
655 echo "false"
656 return 1
657 ;;
658 esac
659 else
660 echo "false"
661 return 1
662 fi
663}
664
665# Check if this host should be running RAMCloud server:
666function is-server-role {
667 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
668 case "${ONOS_HOST_ROLE}" in
669 single-node)
670 echo "true"
671 return 0
672 ;;
673 coord-node)
674 echo "false"
675 return 1
676 ;;
677 server-node)
678 echo "true"
679 return 0
680 ;;
681 coord-and-server-node)
682 echo "true"
683 return 0
684 ;;
685 *)
686 echo "false"
687 return 1
688 ;;
689 esac
690 else
691 echo "false"
692 return 1
693 fi
694}
695
Naoki Shiota9df15d32014-03-27 14:26:20 -0700696function start-backend {
697 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700698 if [ $1 == "coord" ]; then
699 rc-coord startifdown
700 elif [ $1 == "server" ]; then
701 rc-server startifdown
702 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700703 fi
704}
705
706function stop-backend {
707 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
708 if [ $rcsn != 0 ]; then
709 rc-server stop
710 fi
711
Naoki Shiota590c18d2014-03-31 17:52:59 -0700712 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700713 if [ $rccn != 0 ]; then
714 rc-coord stop
715 fi
716}
717
Naoki Shiota590c18d2014-03-31 17:52:59 -0700718
Naoki Shiota4e463182014-03-21 15:13:24 -0700719### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700720function rc-coord-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700721 local host=${RC_COORD_IP}
722 if [ -z "${host}" ]; then
723 # falling back to 0.0.0.0
724 host="0.0.0.0"
725 fi
726 echo "${RC_COORD_PROTOCOL}:host=${host},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700727}
728
729function rc-server-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700730 local host=${RC_SERVER_IP}
731 if [ -z "${host}" ]; then
732 # falling back to 0.0.0.0
733 host="0.0.0.0"
734 fi
735 echo "${RC_SERVER_PROTOCOL}:host=${host},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700736}
737
Naoki Shiota4e463182014-03-21 15:13:24 -0700738function rc-coord {
739 case "$1" in
740 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700741 stop-coord
742 start-coord
743 ;;
744 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700745 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700746 if [ $n == 0 ]; then
747 start-coord
748 else
749 echo "$n instance of RAMCloud coordinator running"
750 fi
751 ;;
752 stop)
753 stop-coord
754 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700755 deldb)
756 stop-backend
757 del-coord-info
758 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700759 stat*) # <- status
760 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
761 echo "$n RAMCloud coordinator running"
762 ;;
763 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700764 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700765 exit 1
766 esac
767}
768
769function start-coord {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700770 check-and-start-zk
771 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700772
Naoki Shiota4e463182014-03-21 15:13:24 -0700773 if [ ! -d ${LOGDIR} ]; then
774 mkdir -p ${LOGDIR}
775 fi
776 if [ -f $RAMCLOUD_COORD_LOG ]; then
777 rotate-log $RAMCLOUD_COORD_LOG
778 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700779
780 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700781
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700782 # TODO Make ONOS_CONF readable from ONOS process then eliminate RAMCLOUD_CONF
783
784 # Configuration for ZK address, port
785 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
786
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700787 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700788 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700789
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700790 # RAMCloud transport timeout
791 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700792 # RAMCloud option deadServerTimeout
793 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
794 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
795
796 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
797 # but actual RAMCloud code only uses -C argument now.
798 # (FYI: -C is documented to be deprecated in the document)
799
800 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700801 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700802 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700803 coord_args="${coord_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700804 coord_args="${coord_args} --deadServerTimeout ${rc_coord_deadServerTimeout}"
805
806 # Read environment variables if set
807 coord_args="${coord_args} ${RC_COORDINATOR_OPTS}"
808
809 if [ "${ONOS_HOST_ROLE}" == "single-node" ]; then
810 # Note: Following reset is required, since RC restart is considered node failure,
811 # and tries recovery of server, which will never succeed after restart.
812 echo "Role configured to single-node mode. RAMCloud cluster will be reset on each start-up."
813 coord_args="${coord_args} --reset"
814 fi
815
Naoki Shiota4e463182014-03-21 15:13:24 -0700816 # Run ramcloud
817 echo -n "Starting RAMCloud coordinator ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700818 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700819 echo "STARTED"
820}
821
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700822function del-coord-info {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700823 check-and-start-zk
824 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700825
826 if [ ! -d ${LOGDIR} ]; then
827 mkdir -p ${LOGDIR}
828 fi
829 if [ -f $RAMCLOUD_COORD_LOG ]; then
830 rotate-log $RAMCLOUD_COORD_LOG
831 fi
832
833 local coord_addr=`rc-coord-addr`
834
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700835 # Configuration for ZK address, port
836 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700837 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700838 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700839 # RAMCloud option deadServerTimeout
840 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
841 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
842
843 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
844 # but actual RAMCloud code only uses -C argument now.
845 # (FYI: -C is documented to be deprecated in the document)
846
847 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700848 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700849 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
850
851 # Note: --reset will reset ZK stored info and start running as acoordinator.
852 echo -n "Deleting RAMCloud cluster coordination info ... "
853 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} --reset &> $RAMCLOUD_COORD_LOG &
854
Yuta HIGUCHIcd44eb32014-05-24 16:51:31 -0700855 # TODO Assuming 5 sec is enough. To be sure monitor log?
856 sleep 5
857 # kill coordinator
Yuta HIGUCHIce9f3ee2014-05-14 09:48:40 -0700858 (pkill -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator &> /dev/null)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700859
860 echo "DONE"
861}
Naoki Shiota4e463182014-03-21 15:13:24 -0700862
863function stop-coord {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700864 kill-processes "RAMCloud coordinator" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700865}
866
Naoki Shiota4e463182014-03-21 15:13:24 -0700867### Functions related to RAMCloud server
868function rc-server {
869 case "$1" in
870 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700871 stop-server
872 start-server
873 ;;
874 startifdown)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700875 local n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700876 if [ $n == 0 ]; then
877 start-server
878 else
879 echo "$n instance of RAMCloud server running"
880 fi
881 ;;
882 stop)
883 stop-server
884 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700885 deldb)
886 stop-server
887 del-server-backup
888 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700889 stat*) # <- status
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700890 n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700891 echo "$n RAMCloud server running"
892 ;;
893 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700894 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700895 exit 1
896 esac
897}
898
899function start-server {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700900 check-and-start-zk
901 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700902
Naoki Shiota4e463182014-03-21 15:13:24 -0700903 if [ ! -d ${LOGDIR} ]; then
904 mkdir -p ${LOGDIR}
905 fi
906 if [ -f $RAMCLOUD_SERVER_LOG ]; then
907 rotate-log $RAMCLOUD_SERVER_LOG
908 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700909
910 local coord_addr=`rc-coord-addr`
911 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700912
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700913 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.server.masterServiceThreads 5)
914 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.server.logCleanerThreads 1)
Yuta HIGUCHI52efcbb2014-05-16 09:30:33 -0700915 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.server.detectFailures 0)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700916
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700917 # Configuration for ZK address, port
918 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700919 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700920 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700921 # RAMCloud transport timeout
922 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700923 # replication factor (-r) config
924 local rc_replicas=$(read-conf ${ONOS_CONF} ramcloud.server.replicas 0)
925 # backup file path (-f) config
926 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
927 mkdir -p `dirname ${rc_datafile}`
928
929 local server_args="-L ${server_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700930 server_args="${server_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700931 server_args="${server_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700932 server_args="${server_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700933 server_args="${server_args} --masterServiceThreads ${masterServiceThreads}"
934 server_args="${server_args} --logCleanerThreads ${logCleanerThreads}"
935 server_args="${server_args} --detectFailures ${detectFailures}"
936 server_args="${server_args} --replicas ${rc_replicas}"
937 server_args="${server_args} --file ${rc_datafile}"
938
939 # Read environment variables if set
940 server_args="${server_args} ${RC_SERVER_OPTS}"
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700941
Naoki Shiota4e463182014-03-21 15:13:24 -0700942 # Run ramcloud
943 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700944 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server ${server_args} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700945 echo "STARTED"
946}
947
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700948function del-server-backup {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700949 echo -n "Delete RAMCloud backup server data [y/N]? "
950 while [ 1 ]; do
951 read key
952 if [ "${key}" == "Y" -o "${key}" == "y" ]; then
953 break
954 elif [ -z "${key}" -o "${key}" == "N" -o "${key}" == "n" ]; then
955 echo "Cancelled."
956 return
957 fi
958 echo "[y/N]? "
959 done
960
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700961 echo -n "Removing RAMCloud backup server data ... "
962 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
963 rm -f ${rc_datafile}
964 echo "DONE"
965}
966
Naoki Shiota4e463182014-03-21 15:13:24 -0700967function stop-server {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700968 kill-processes "RAMCloud server" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700969}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700970############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700971
972
Naoki Shiota590c18d2014-03-31 17:52:59 -0700973## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700974function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700975 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700976 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700977 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700978 ${MVN} -f ${ONOS_HOME}/pom.xml compile
979 fi
980 JAVA_CP=`cat ${CPFILE}`
981 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
982
983 case "$1" in
984 start)
985 stop-onos
986 start-onos
987 ;;
988 startnokill)
989 start-onos
990 ;;
991 startifdown)
992 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
993 if [ $n == 0 ]; then
994 start-onos
995 else
996 echo "$n instance of onos running"
997 fi
998 ;;
999 stop)
1000 stop-onos
1001 ;;
1002 stat*) # <- status
1003 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1004 echo "$n instance of onos running"
1005 ;;
1006 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001007 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001008 exit 1
1009 esac
1010}
1011
1012function start-onos {
1013 if [ ! -d ${LOGDIR} ]; then
1014 mkdir -p ${LOGDIR}
1015 fi
Yuta HIGUCHI04713972014-05-30 11:01:17 -07001016 # Rotate log files
Naoki Shiota4e463182014-03-21 15:13:24 -07001017 for log in ${LOGS}; do
1018 if [ -f ${log} ]; then
1019 rotate-log ${log}
1020 fi
1021 done
Yuta HIGUCHI04713972014-05-30 11:01:17 -07001022
1023 # Rotate logs rolled at runtime.
1024 local rolled_log_shellpat=`echo ${ONOS_LOG_ROLLING_PATTERN} | sed -e "s/%i/\\*/"`
1025 for rolled_log in ${rolled_log_shellpat}; do
1026 if [ -f ${rolled_log} ]; then
1027 rotate-log ${rolled_log}
Yuta HIGUCHI04713972014-05-30 11:01:17 -07001028 fi
1029 done
1030
Naoki Shiota4e463182014-03-21 15:13:24 -07001031 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -07001032 echo "[WARNING] ${ONOS_LOGBACK} not found."
1033 echo " Run \"\$ $0 setup\" to create."
1034 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -07001035 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -07001036
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -07001037 if [ ! -f ${HC_CONF} ]; then
1038 echo "[WARNING] ${HC_CONF} not found."
1039 echo " Run \"\$ $0 setup\" to create."
1040 exit 1
1041 fi
1042
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -07001043 # specify ZooKeeper(curator) namespace
1044 JVM_OPTS="${JVM_OPTS} -Dzookeeper.namespace=${ONOS_CLUSTER_NAME}"
1045
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -07001046 # specify ZooKeeper connectionString
1047 local zk_locator=`echo "${ZK_HOSTS}" | sed -e "s/,/:${ZK_CLIENTPORT},/g"`
1048 zk_locator+=":${ZK_CLIENTPORT}"
1049 zk_locator="localhost:${ZK_CLIENTPORT},${zk_locator}"
1050
1051 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.registry.ZookeeperRegistry.connectionString=${zk_locator}"
1052
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -07001053 # specify hazelcast.xml to datagrid
1054 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
1055
1056 # specify backend config
Jonathan Hartef3dc1a2014-04-03 11:39:50 -07001057 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -07001058 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
1059 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -07001060 elif [ "${ONOS_HOST_BACKEND}" = "hazelcast" ]; then
1061 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.hazelcast.baseConfig=${HC_CONF}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -07001062 fi
1063
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -07001064 # check for jvm debug flag
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -07001065 if [ "${ONOS_DEBUG}" == "true" ]; then
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -07001066 JVM_OPTS="${JVM_OPTS} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${JVM_DEBUG_PORT}"
1067 fi
1068
Naoki Shiota9df15d32014-03-27 14:26:20 -07001069 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -07001070
1071 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
1072 cd ${ONOS_HOME}
1073
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001074 echo -n "Starting ONOS controller ... "
Naoki Shiota4e463182014-03-21 15:13:24 -07001075 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
1076
1077 # We need to wait a bit to find out whether starting the ONOS process succeeded
1078 sleep 1
1079
1080 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -07001081 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -07001082 echo " STARTED"
1083 else
1084 echo " FAILED"
1085 fi
1086
1087# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
1088# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
1089}
1090
1091function stop-onos {
1092 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
1093# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
1094}
Naoki Shiota590c18d2014-03-31 17:52:59 -07001095############################################
Naoki Shiota4e463182014-03-21 15:13:24 -07001096
1097
Naoki Shiota590c18d2014-03-31 17:52:59 -07001098################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -07001099case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -07001100 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -07001101 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -07001102 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001103 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -07001104 mode_parameter=${ONOS_HOST_ROLE}
1105 if [ ! -z "$2" ]; then
1106 mode_parameter=$2
1107 fi
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -07001108
Naoki Shiota9df15d32014-03-27 14:26:20 -07001109 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -07001110 single-node)
1111 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001112 start-backend coord
1113 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001114 onos startifdown
1115 ;;
1116 coord-node)
1117 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001118 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -07001119 onos startifdown
1120 ;;
1121 server-node)
1122 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001123 start-backend server
1124 onos startifdown
1125 ;;
1126 coord-and-server-node)
1127 zk start
1128 start-backend coord
1129 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001130 onos startifdown
1131 ;;
1132 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001133 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001134 ;;
1135 esac
1136 echo
1137 ;;
1138 stop)
1139 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1140 if [ $on != 0 ]; then
1141 onos stop
1142 fi
1143
Naoki Shiota9df15d32014-03-27 14:26:20 -07001144 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -07001145
1146 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1147 if [ $zkn != 0 ]; then
1148 zk stop
1149 fi
1150 echo
1151 ;;
1152 restart)
1153 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1154 if [ $on != 0 ]; then
1155 onos stop
1156 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001157
Naoki Shiota4e463182014-03-21 15:13:24 -07001158 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
1159 if [ $rcsn != 0 ]; then
1160 rc-server stop
1161 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001162
Naoki Shiota590c18d2014-03-31 17:52:59 -07001163 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -07001164 if [ $rccn != 0 ]; then
1165 rc-coord stop
1166 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001167
Naoki Shiota4e463182014-03-21 15:13:24 -07001168 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1169 if [ $zkn != 0 ]; then
1170 zk restart
1171 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001172
Naoki Shiota4e463182014-03-21 15:13:24 -07001173 if [ $rccn != 0 ]; then
1174 rc-coord startifdown
1175 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001176
Naoki Shiota4e463182014-03-21 15:13:24 -07001177 if [ $rcsn != 0 ]; then
1178 rc-server startifdown
1179 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001180
Naoki Shiota4e463182014-03-21 15:13:24 -07001181 if [ $on != 0 ]; then
1182 onos startifdown
1183 fi
1184 echo
1185 ;;
1186 stat*) # <- status
1187 echo '[ZooKeeper]'
1188 zk status
1189 echo
1190 echo '[RAMCloud coordinator]'
1191 rc-coord status
1192 echo
1193 echo '[RAMCloud server]'
1194 rc-server status
1195 echo
1196 echo '[ONOS core]'
1197 onos status
1198 echo
1199 ;;
1200 zk)
1201 zk $2
1202 ;;
1203 rc-c*) # <- rc-coordinator
1204 rc-coord $2
1205 ;;
1206 rc-s*) # <- rc-server
1207 rc-server $2
1208 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001209 rc)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001210 # XXX rc command ignores if the host is not configured to have those roles,
1211 # while rc-s or rc-c executes the task regardless of role definition.
1212 # This is a workaround since TestON does not take role in account
1213 # and always issue rc deldb right now.
1214
Naoki Shiota9109a1e2014-05-13 11:11:01 -07001215 # TODO make deldb command more organized (clarify when it can work)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001216 if [ "`is-coord-role`" == "true" ]; then
1217 rc-coord $2
1218 fi
1219 if [ "`is-server-role`" == "true" ]; then
1220 rc-server $2
1221 fi
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001222 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001223 core)
1224 onos $2
1225 ;;
1226 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001227 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001228 exit 1
1229esac
Naoki Shiota590c18d2014-03-31 17:52:59 -07001230