blob: 147ed749641178004d3fbc3b0eeea45f68d15fdd [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}
Naoki Shiota590c18d2014-03-31 17:52:59 -070027
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070028source ${ONOS_HOME}/scripts/common/utils.sh
29
Yuta HIGUCHIe748ecc2014-05-28 13:55:03 -070030confirm-if-root
31
Naoki Shiota590c18d2014-03-31 17:52:59 -070032if [ ! -f ${ONOS_CONF} ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070033 # falling back to default config file
34 ONOS_CONF=${ONOS_CONF_DIR}/onos_node.conf
35 if [ ! -f ${ONOS_CONF} ]; then
36 echo "${ONOS_CONF} not found."
37 exit 1
38 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -070039fi
Naoki Shiota4e928512014-04-03 15:49:35 -070040
Naoki Shiota4e928512014-04-03 15:49:35 -070041### Variables read from ONOS config file ###
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -070042ONOS_CLUSTER_NAME=$(read-conf ${ONOS_CONF} onos.cluster.name "onos")
Naoki Shiota4e928512014-04-03 15:49:35 -070043ONOS_HOST_NAME=$(read-conf ${ONOS_CONF} host.name `hostname`)
44ONOS_HOST_IP=$(read-conf ${ONOS_CONF} host.ip)
45ONOS_HOST_ROLE=$(read-conf ${ONOS_CONF} host.role)
46ONOS_HOST_BACKEND=$(read-conf ${ONOS_CONF} host.backend)
47ZK_HOSTS=$(read-conf ${ONOS_CONF} zookeeper.hosts ${ONOS_HOST_NAME})
48RC_COORD_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.coordinator.protocol "fast+udp")
49RC_COORD_IP=$(read-conf ${ONOS_CONF} ramcloud.coordinator.ip ${ONOS_HOST_IP})
50RC_COORD_PORT=$(read-conf ${ONOS_CONF} ramcloud.coordinator.port 12246)
51RC_SERVER_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.server.protocol "fast+udp")
52RC_SERVER_IP=$(read-conf ${ONOS_CONF} ramcloud.server.ip ${ONOS_HOST_IP})
53RC_SERVER_PORT=$(read-conf ${ONOS_CONF} ramcloud.server.port 12242)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070054HC_HOST_PORT=$(read-conf ${ONOS_CONF} hazelcast.host.port 5701)
Naoki Shiota4e928512014-04-03 15:49:35 -070055HC_TCPIP_MEMBERS=$(read-conf ${ONOS_CONF} hazelcast.tcp-ip.members)
56HC_MULTICAST_GROUP=$(read-conf ${ONOS_CONF} hazelcast.multicast.group "224.2.2.3")
57HC_MULTICAST_PORT=$(read-conf ${ONOS_CONF} hazelcast.multicast.port 54327)
Naoki Shiota590c18d2014-03-31 17:52:59 -070058############################################
59
60
61############## Other variables #############
Naoki Shiota4e928512014-04-03 15:49:35 -070062ONOS_TEMPLATE_DIR=${ONOS_CONF_DIR}/template
63
Naoki Shiota4e463182014-03-21 15:13:24 -070064LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
65
Yuta HIGUCHIfb1905a2014-06-09 14:07:34 -070066ZK_HOME=${ZK_HOME:-~/zookeeper-3.4.6}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070067ZK_CONF=${ZK_CONF:-${ONOS_CONF_DIR}/zoo.cfg}
Naoki Shiota4e928512014-04-03 15:49:35 -070068ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070069# Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070070ZK_LOG_DIR=${ZK_LOG_DIR:-${ONOS_HOME}/onos-logs/zk-${ONOS_HOST_NAME}}
Naoki Shiota72209722014-04-08 14:32:17 -070071ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070072ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070073
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070074HC_CONF=${HC_CONF:-${ONOS_CONF_DIR}/hazelcast.xml}
Naoki Shiota4e928512014-04-03 15:49:35 -070075HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
76
Naoki Shiota4e463182014-03-21 15:13:24 -070077RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070078RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
79RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070080RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070081RAMCLOUD_CONF=${RAMCLOUD_CONF:-${ONOS_CONF_DIR}/ramcloud.conf}
Naoki Shiota4e463182014-03-21 15:13:24 -070082
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070083export LD_LIBRARY_PATH=${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}:$LD_LIBRARY_PATH
Naoki Shiota4e463182014-03-21 15:13:24 -070084
85## 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 -070086ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070087ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070088ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070089LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070090LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070091ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070092ONOS_LOG_ROLLING_PATTERN="${LOGDIR}/${LOGBASE}.%i.log.gz"
93ONOS_STDOUT_LOG="${LOGDIR}/${LOGBASE}.stdout"
94ONOS_STDERR_LOG="${LOGDIR}/${LOGBASE}.stderr"
Naoki Shiota4e463182014-03-21 15:13:24 -070095PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070096LOGS="$ONOS_LOG $ONOS_STDOUT_LOG $ONOS_STDERR_LOG $PCAP_LOG"
Naoki Shiota4e463182014-03-21 15:13:24 -070097
Naoki Shiota9df15d32014-03-27 14:26:20 -070098ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -070099JMX_PORT=${JMX_PORT:-7189}
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -0700100JVM_DEBUG_PORT=${JVM_DEBUG_PORT:-22007}
Naoki Shiota4e463182014-03-21 15:13:24 -0700101
102# Set JVM options
103JVM_OPTS="${JVM_OPTS:-}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700104JVM_OPTS="$JVM_OPTS -server -d64"
105#JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700106
107# Uncomment or specify appropriate value as JVM_OPTS environment variables.
108#JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
Naoki Shiota4e463182014-03-21 15:13:24 -0700109#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
110#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700111
112#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC"
113JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
114JVM_OPTS="$JVM_OPTS -XX:+AggressiveOpts"
115
116# We may want to remove UseFastAccessorMethods option: http://bugs.java.com/view_bug.do?bug_id=6385687
117JVM_OPTS="$JVM_OPTS -XX:+UseFastAccessorMethods"
118
119JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192"
120JVM_OPTS="$JVM_OPTS -XX:FreqInlineSize=8192"
121JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500"
122
Naoki Shiota4e463182014-03-21 15:13:24 -0700123JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700124
Praseed Balakrishnan4fe80812014-07-24 11:29:19 -0700125# This option tells the VM to generate a heap dump when memory allocation cannot be satisfied.
126# http://www.oracle.com/technetwork/java/javase/clopts-139448.html#gbzrr
127JVM_OPTS="$JVM_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${ONOS_HOME}"
128
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700129# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
130JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
131
132JVM_OPTS="$JVM_OPTS -XX:+UseCompressedOops"
133
134JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
135JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
136JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
137
Naoki Shiota4e463182014-03-21 15:13:24 -0700138JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
139
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700140# Uncomment to dump final JVM flags to stdout
141#JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
142
Naoki Shiota4e463182014-03-21 15:13:24 -0700143# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700144MAIN_CLASS="net.onrc.onos.core.main.Main"
Naoki Shiota4e463182014-03-21 15:13:24 -0700145
146MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700147############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700148
Naoki Shiota9df15d32014-03-27 14:26:20 -0700149
Naoki Shiota590c18d2014-03-31 17:52:59 -0700150############# Common functions #############
151function print_usage {
Naoki Shiota05721b32014-04-29 14:41:12 -0700152 local scriptname=`basename $0`
Naoki Shiota590c18d2014-03-31 17:52:59 -0700153 local filename=`basename ${ONOS_CONF}`
154 local usage="Usage: setup/start/stop ONOS on this server.
Naoki Shiota05721b32014-04-29 14:41:12 -0700155 \$ ${scriptname} setup [-f]
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700156 Set up ONOS node using ${ONOS_CONF} .
Naoki Shiota590c18d2014-03-31 17:52:59 -0700157 - generate and replace config file of ZooKeeper.
158 - create myid in ZooKeeper datadir.
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700159 - generate and replace config file for Hazelcast.
160 - generate and replace config file for RAMCloud.
Naoki Shiota590c18d2014-03-31 17:52:59 -0700161 - generate and replace logback.${ONOS_HOST_NAME}.xml
162 If -f option is used, all existing files will be overwritten without confirmation.
Naoki Shiota05721b32014-04-29 14:41:12 -0700163 \$ ${scriptname} start [single-node|coord-node|server-node|coord-and-server-node]
Naoki Shiota590c18d2014-03-31 17:52:59 -0700164 Start ONOS node with specific RAMCloud entities
165 - single-node: start ONOS with stand-alone RAMCloud
166 - coord-node : start ONOS with RAMCloud coordinator
167 - server-node: start ONOS with RAMCloud server
168 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
169 * Default behavior can be defined by ${filename}
Naoki Shiota05721b32014-04-29 14:41:12 -0700170 \$ ${scriptname} stop
Naoki Shiota590c18d2014-03-31 17:52:59 -0700171 Stop all ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700172 \$ ${scriptname} restart
Naoki Shiota590c18d2014-03-31 17:52:59 -0700173 Stop and start currently running ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700174 \$ ${scriptname} status
Naoki Shiota590c18d2014-03-31 17:52:59 -0700175 Show status of ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700176 \$ ${scriptname} {zk|rc-coord|rc-server|core} {start|stop|restart|status}
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700177 Control specific ONOS-related process
178 \$ ${scriptname} rc deldb
179 Delete data in RAMCloud"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700180
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700181 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700182}
183
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700184# rotate-log [log-filename] [max rotations]
185# Example:
186# foobar.log -> foobar.log.1
187# foobar.log.1 -> foobar.log.2
188# foobar.log.gz -> foobar.log.1.gz
Naoki Shiota4e463182014-03-21 15:13:24 -0700189function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700190 local logfile=$1
191 local nr_max=${2:-10}
192 if [ -f $logfile ]; then
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700193 # TODO treating only .gz now. probably want more generic solution
194 local basename=${logfile%%.gz}
195 local append=""
196 if [ "$basename" != "$logfile" ]; then
197 append=".gz"
198 fi
Naoki Shiota4e928512014-04-03 15:49:35 -0700199 for i in `seq $(expr $nr_max - 1) -1 1`; do
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700200 if [ -f ${basename}.${i}${append} ]; then
201 mv -f ${basename}.${i}${append} ${basename}.`expr $i + 1`${append}
Naoki Shiota4e928512014-04-03 15:49:35 -0700202 fi
203 done
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700204 mv ${basename}${append} ${basename}.1${append}
Naoki Shiota4e928512014-04-03 15:49:35 -0700205 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700206}
207
208# kill-processes {module-name} {array of pids}
209function kill-processes {
210 # Kill the existing processes
211 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700212 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700213 echo -n "Stopping $1 ... "
214 fi
215 for p in ${pids}; do
216 if [ x$p != "x" ]; then
Naoki Shiota4b355762014-05-27 11:46:43 -0700217 # Check if target process is accesible from current user
218 kill -0 $p
219 if [ "$?" -ne 0 ]; then
220 # Error exit code here means "failed to send signal".
221 # Supposedly because of permission error.
222 echo "Failed to kill process (pid: $p)."
223 echo "Check if current user is the same as process owner."
224 exit 1
225 fi
226
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700227 (
228 # Ask process with SIGTERM first, if that did not kill the process
229 # wait 1s and if process still exist, force process to be killed.
230 kill -TERM $p && kill -0 $p && sleep 1 && kill -0 $p && kill -KILL $p
231 ) 2> /dev/null
Naoki Shiota4b355762014-05-27 11:46:43 -0700232
233 # Ensure process is killed.
234 kill -0 $p 2> /dev/null
235 if [ "$?" -ne 0 ]; then
236 # Error exit code here means "process not found", i.e. "kill succeeded".
237 echo "Killed existing process (pid: $p)"
238 else
239 # Process still exists. Some unexpected error occurs.
240 echo "Failed to kill process (pid: $p)."
241 echo "Unexpected error occurs during process termination."
242 exit 1
243 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700244 fi
245 done
246}
247
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700248function handle-error {
249 set -e
250
251 revert-confs
252
253 set +e
254
255 exit 1
256}
257
Naoki Shiota4e928512014-04-03 15:49:35 -0700258# revert-confs [error message]
259function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700260 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700261
Naoki Shiota4e928512014-04-03 15:49:35 -0700262 revert-file `basename ${ZK_CONF}`
263 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700264
265 echo "EXIT"
266
267 if [ ! -z "$1" ]; then
268 echo $1
269 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700270}
271
Naoki Shiota590c18d2014-03-31 17:52:59 -0700272function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700273 echo -n "Creating ${ZK_CONF} ... "
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700274
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700275 # Create the ZooKeeper lib directory
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700276 if [[ ! ( -w ${ZK_LIB_DIR} && -d ${ZK_LIB_DIR} ) ]]; then
Naoki Shiota7f495cf2014-05-21 17:23:25 -0700277 local SUDO=${SUDO:-sudo}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700278 local whoami=`whoami`
279 {
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700280 ${SUDO} mkdir -p ${ZK_LIB_DIR}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700281 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
282 } || {
283 echo "FAILED"
284 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
285 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
286 exit 1
287 }
288 fi
289
290 # creation of ZooKeeper config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700291 local temp_zk=`begin-conf-creation ${ZK_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700292
Naoki Shiota4e928512014-04-03 15:49:35 -0700293 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700294
295 local i=1
296 local myid=
297 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700298 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700299 myid=$i
300 break
301 fi
302 i=`expr $i + 1`
303 done
304
305 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700306 local filename=`basename ${ONOS_CONF}`
Naoki Shiota35f8d5c2014-04-08 11:13:18 -0700307 revert-confs "[ERROR] In ${filename}, zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700308 fi
309
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700310 if [ -f "${ZK_MY_ID}" ]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700311 # sudo will be needed if ZK_MY_ID is already created by other (old) script
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700312 local SUDO=${SUDO:-}
313 {
314 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
315 } || {
316 echo "FAILED"
317 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700318 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700319 exit 1
320 }
321 fi
322
Naoki Shiota9df15d32014-03-27 14:26:20 -0700323 echo ${myid} > ${ZK_MY_ID}
324
325 echo -n "myid is assigned to ${myid} ... "
326
327 while read line; do
328 if [[ $line =~ ^__HOSTS__$ ]]; then
329 i=1
330 for host in ${hostarr}; do
331 # TODO: ports might be configurable
332 local hostline="server.${i}=${host}:2888:3888"
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700333 echo $hostline
Naoki Shiota9df15d32014-03-27 14:26:20 -0700334 i=`expr $i + 1`
335 done
336 elif [[ $line =~ __DATADIR__ ]]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700337 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700338 else
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700339 echo $line
Naoki Shiota9df15d32014-03-27 14:26:20 -0700340 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700341 done < ${ZK_CONF_TEMPLATE} > ${temp_zk}
342
343 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700344
345 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700346}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700347
Naoki Shiota4e928512014-04-03 15:49:35 -0700348function create-hazelcast-conf {
349 echo -n "Creating ${HC_CONF} ... "
350
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700351 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700352
353 # To keep indent of XML file, change IFS
354 local IFS=''
355 while read line; do
356 if [[ $line =~ __HC_NETWORK__ ]]; then
357 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
358 # temporary change
359 IFS=' '
360 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
361 echo '<multicast enabled="false" />'
362 echo '<tcp-ip enabled="true">'
363 for member in ${memberarr}; do
364 echo " <member>${member}</member>"
365 done
366 echo '</tcp-ip>'
367 IFS=''
368 else
369 echo '<multicast enabled="true">'
370 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
371 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
372 echo '</multicast>'
373 echo '<tcp-ip enabled="false" />'
374 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700375 elif [[ $line =~ __HC_PORT__ ]]; then
376 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700377 elif [[ $line =~ __HC_CLUSTER__ ]]; then
378 echo $line | sed -e "s|__HC_CLUSTER__|${ONOS_CLUSTER_NAME}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700379 else
380 echo "${line}"
381 fi
382 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
383
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700384 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700385
386 echo "DONE"
387}
388
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700389function create-ramcloud-conf {
390 echo -n "Creating ${RAMCLOUD_CONF} ... "
391
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700392 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700393
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700394 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700395
396 # TODO make ZooKeeper address configurable.
397 echo "ramcloud.locator=zk:localhost:2181" > ${temp_rc}
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700398 echo "#ramcloud.locator=zk:localhost:2181,otherhost:2181" >> ${temp_rc}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700399 echo "ramcloud.clusterName=${rc_cluster_name}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700400
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700401 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700402
403 echo "DONE"
404}
405
Naoki Shiota590c18d2014-03-31 17:52:59 -0700406function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700407 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700408
Naoki Shiota9df15d32014-03-27 14:26:20 -0700409 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700410 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700411
412 sed -e "s|__FILENAME__|${ONOS_LOG}|" \
413 -e "s|__ROLLING_PATTERN__|${ONOS_LOG_ROLLING_PATTERN}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700414
415 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700416
Naoki Shiota9df15d32014-03-27 14:26:20 -0700417 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700418}
419
Naoki Shiota4e928512014-04-03 15:49:35 -0700420function create-confs {
421 local key
422 local filename
423
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700424 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700425
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700426 echo "Config file : ${ONOS_CONF}"
427
Naoki Shiota4e928512014-04-03 15:49:35 -0700428 if [ "$1" == "-f" ]; then
429 create-zk-conf
430 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700431 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700432 create-logback-conf
433 else
434 create-conf-interactive ${ZK_CONF} create-zk-conf
435 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700436 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700437 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
438 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700439
440 trap - ERR
441}
442############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700443
Naoki Shiota590c18d2014-03-31 17:52:59 -0700444
445###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700446function zk {
447 case "$1" in
448 start)
449 start-zk
450 ;;
451 stop)
452 stop-zk
453 ;;
454 stat*) # <- status
455 status-zk
456 ;;
457 re*) # <- restart
458 stop-zk
459 start-zk
460 ;;
461 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700462 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700463 exit 1
464 esac
465}
466
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700467function load-zk-cfg {
468 if [ -f "${ZK_CONF}" ]; then
469 local filename=`basename ${ZK_CONF}`
470 local dirname=`dirname ${ZK_CONF}`
471
472 # Run ZooKeeper with our configuration
473 export ZOOCFG=${filename}
474 export ZOOCFGDIR=${dirname}
475 fi
476}
477
Naoki Shiota4e463182014-03-21 15:13:24 -0700478function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700479 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700480
Naoki Shiota4e928512014-04-03 15:49:35 -0700481 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700482 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700483
484 load-zk-cfg
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700485
486 # log4j.properties is read from classpath if not found in CWD.
487 # Using the ZooKeeper supplied default in ZooKeeper conf dir.
488 # TODO: To explicitly specify our customized log4j config file:
489 # export SERVER_JVMFLAGS="-Dlog4j.configuration=${ZK_LOG4J}"
490 env CLASSPATH="${ZK_HOME}/conf:${CLASSPATH}" ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700491}
492
493function stop-zk {
Yuta HIGUCHI7c708362014-06-02 23:15:13 -0700494 echo -n "Stopping ZooKeeper ... "
495 load-zk-cfg
496
497 ${ZK_HOME}/bin/zkServer.sh stop
Naoki Shiota4e463182014-03-21 15:13:24 -0700498}
499
500function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700501 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700502
Naoki Shiota72209722014-04-08 14:32:17 -0700503 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700504}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700505
506function check-zk {
507 # assumption here is that ZK status script is the last command in status-zk.
508 status-zk &> /dev/null
509 local zk_status=$?
510 if [ "$zk_status" -ne 0 ]; then
511 return 1;
512 fi
513 return 0
514}
515
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700516function check-and-start-zk {
517 check-zk
518 local zk_status=$?
519 if [ "$zk_status" -ne 0 ]; then
520 start-zk
521 fi
522}
523
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700524# wait-zk-or-die {timeout-sec}
525function wait-zk-or-die {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700526 local retries=${1:-5}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700527 # do-while retries >= 0
528 while true; do
529 check-zk
530 local zk_status=$?
531 if [ "$zk_status" -eq 0 ]; then
532 return 0
533 fi
534 sleep 1;
535 ((retries -= 1))
536 (( retries >= 0 )) || break
537 done
538 echo "ZooKeeper is not running."
539 exit 1
540}
541
Naoki Shiota590c18d2014-03-31 17:52:59 -0700542############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700543
544
Naoki Shiota590c18d2014-03-31 17:52:59 -0700545####### Functions related to RAMCloud ######
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -0700546
547# Check if this host should be running RAMCloud coordinator:
548function is-coord-role {
549 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
550 case "${ONOS_HOST_ROLE}" in
551 single-node)
552 echo "true"
553 return 0
554 ;;
555 coord-node)
556 echo "true"
557 return 0
558 ;;
559 server-node)
560 echo "false"
561 return 1
562 ;;
563 coord-and-server-node)
564 echo "true"
565 return 0
566 ;;
567 *)
568 echo "false"
569 return 1
570 ;;
571 esac
572 else
573 echo "false"
574 return 1
575 fi
576}
577
578# Check if this host should be running RAMCloud server:
579function is-server-role {
580 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
581 case "${ONOS_HOST_ROLE}" in
582 single-node)
583 echo "true"
584 return 0
585 ;;
586 coord-node)
587 echo "false"
588 return 1
589 ;;
590 server-node)
591 echo "true"
592 return 0
593 ;;
594 coord-and-server-node)
595 echo "true"
596 return 0
597 ;;
598 *)
599 echo "false"
600 return 1
601 ;;
602 esac
603 else
604 echo "false"
605 return 1
606 fi
607}
608
Naoki Shiota9df15d32014-03-27 14:26:20 -0700609function start-backend {
610 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700611 if [ $1 == "coord" ]; then
612 rc-coord startifdown
613 elif [ $1 == "server" ]; then
614 rc-server startifdown
615 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700616 fi
617}
618
619function stop-backend {
620 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
621 if [ $rcsn != 0 ]; then
622 rc-server stop
623 fi
624
Naoki Shiota590c18d2014-03-31 17:52:59 -0700625 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700626 if [ $rccn != 0 ]; then
627 rc-coord stop
628 fi
629}
630
Naoki Shiota590c18d2014-03-31 17:52:59 -0700631
Naoki Shiota4e463182014-03-21 15:13:24 -0700632### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700633function rc-coord-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700634 local host=${RC_COORD_IP}
635 if [ -z "${host}" ]; then
636 # falling back to 0.0.0.0
637 host="0.0.0.0"
638 fi
639 echo "${RC_COORD_PROTOCOL}:host=${host},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700640}
641
642function rc-server-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700643 local host=${RC_SERVER_IP}
644 if [ -z "${host}" ]; then
645 # falling back to 0.0.0.0
646 host="0.0.0.0"
647 fi
648 echo "${RC_SERVER_PROTOCOL}:host=${host},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700649}
650
Naoki Shiota4e463182014-03-21 15:13:24 -0700651function rc-coord {
652 case "$1" in
653 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700654 stop-coord
655 start-coord
656 ;;
657 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700658 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700659 if [ $n == 0 ]; then
660 start-coord
661 else
662 echo "$n instance of RAMCloud coordinator running"
663 fi
664 ;;
665 stop)
666 stop-coord
667 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700668 deldb)
669 stop-backend
670 del-coord-info
671 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700672 stat*) # <- status
673 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
674 echo "$n RAMCloud coordinator running"
675 ;;
676 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700677 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700678 exit 1
679 esac
680}
681
682function start-coord {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700683 check-and-start-zk
684 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700685
Naoki Shiota4e463182014-03-21 15:13:24 -0700686 if [ ! -d ${LOGDIR} ]; then
687 mkdir -p ${LOGDIR}
688 fi
689 if [ -f $RAMCLOUD_COORD_LOG ]; then
690 rotate-log $RAMCLOUD_COORD_LOG
691 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700692
693 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700694
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700695 # TODO Make ONOS_CONF readable from ONOS process then eliminate RAMCLOUD_CONF
696
697 # Configuration for ZK address, port
698 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
699
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700700 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700701 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700702
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700703 # RAMCloud transport timeout
704 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700705 # RAMCloud option deadServerTimeout
706 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
707 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
708
709 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
710 # but actual RAMCloud code only uses -C argument now.
711 # (FYI: -C is documented to be deprecated in the document)
712
713 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700714 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700715 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700716 coord_args="${coord_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700717 coord_args="${coord_args} --deadServerTimeout ${rc_coord_deadServerTimeout}"
718
719 # Read environment variables if set
720 coord_args="${coord_args} ${RC_COORDINATOR_OPTS}"
721
722 if [ "${ONOS_HOST_ROLE}" == "single-node" ]; then
723 # Note: Following reset is required, since RC restart is considered node failure,
724 # and tries recovery of server, which will never succeed after restart.
725 echo "Role configured to single-node mode. RAMCloud cluster will be reset on each start-up."
726 coord_args="${coord_args} --reset"
727 fi
728
Naoki Shiota4e463182014-03-21 15:13:24 -0700729 # Run ramcloud
730 echo -n "Starting RAMCloud coordinator ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700731 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700732 echo "STARTED"
733}
734
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700735function del-coord-info {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700736 check-and-start-zk
737 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700738
739 if [ ! -d ${LOGDIR} ]; then
740 mkdir -p ${LOGDIR}
741 fi
742 if [ -f $RAMCLOUD_COORD_LOG ]; then
743 rotate-log $RAMCLOUD_COORD_LOG
744 fi
745
746 local coord_addr=`rc-coord-addr`
747
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700748 # Configuration for ZK address, port
749 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700750 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700751 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700752 # RAMCloud option deadServerTimeout
753 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
754 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
755
756 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
757 # but actual RAMCloud code only uses -C argument now.
758 # (FYI: -C is documented to be deprecated in the document)
759
760 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700761 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700762 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
763
764 # Note: --reset will reset ZK stored info and start running as acoordinator.
765 echo -n "Deleting RAMCloud cluster coordination info ... "
766 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} --reset &> $RAMCLOUD_COORD_LOG &
767
Yuta HIGUCHIcd44eb32014-05-24 16:51:31 -0700768 # TODO Assuming 5 sec is enough. To be sure monitor log?
769 sleep 5
770 # kill coordinator
Yuta HIGUCHIce9f3ee2014-05-14 09:48:40 -0700771 (pkill -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator &> /dev/null)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700772
773 echo "DONE"
774}
Naoki Shiota4e463182014-03-21 15:13:24 -0700775
776function stop-coord {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700777 kill-processes "RAMCloud coordinator" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700778}
779
Naoki Shiota4e463182014-03-21 15:13:24 -0700780### Functions related to RAMCloud server
781function rc-server {
782 case "$1" in
783 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700784 stop-server
785 start-server
786 ;;
787 startifdown)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700788 local n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700789 if [ $n == 0 ]; then
790 start-server
791 else
792 echo "$n instance of RAMCloud server running"
793 fi
794 ;;
795 stop)
796 stop-server
797 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700798 deldb)
799 stop-server
800 del-server-backup
801 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700802 stat*) # <- status
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700803 n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700804 echo "$n RAMCloud server running"
805 ;;
806 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700807 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700808 exit 1
809 esac
810}
811
812function start-server {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700813 check-and-start-zk
814 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700815
Naoki Shiota4e463182014-03-21 15:13:24 -0700816 if [ ! -d ${LOGDIR} ]; then
817 mkdir -p ${LOGDIR}
818 fi
819 if [ -f $RAMCLOUD_SERVER_LOG ]; then
820 rotate-log $RAMCLOUD_SERVER_LOG
821 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700822
823 local coord_addr=`rc-coord-addr`
824 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700825
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700826 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.server.masterServiceThreads 5)
827 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.server.logCleanerThreads 1)
Yuta HIGUCHI52efcbb2014-05-16 09:30:33 -0700828 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.server.detectFailures 0)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700829
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700830 # Configuration for ZK address, port
831 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700832 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700833 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700834 # RAMCloud transport timeout
835 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700836 # replication factor (-r) config
837 local rc_replicas=$(read-conf ${ONOS_CONF} ramcloud.server.replicas 0)
838 # backup file path (-f) config
839 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
840 mkdir -p `dirname ${rc_datafile}`
841
842 local server_args="-L ${server_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700843 server_args="${server_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700844 server_args="${server_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700845 server_args="${server_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700846 server_args="${server_args} --masterServiceThreads ${masterServiceThreads}"
847 server_args="${server_args} --logCleanerThreads ${logCleanerThreads}"
848 server_args="${server_args} --detectFailures ${detectFailures}"
849 server_args="${server_args} --replicas ${rc_replicas}"
850 server_args="${server_args} --file ${rc_datafile}"
851
852 # Read environment variables if set
853 server_args="${server_args} ${RC_SERVER_OPTS}"
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700854
Naoki Shiota4e463182014-03-21 15:13:24 -0700855 # Run ramcloud
856 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700857 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server ${server_args} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700858 echo "STARTED"
859}
860
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700861function del-server-backup {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700862 echo -n "Delete RAMCloud backup server data [y/N]? "
863 while [ 1 ]; do
864 read key
865 if [ "${key}" == "Y" -o "${key}" == "y" ]; then
866 break
867 elif [ -z "${key}" -o "${key}" == "N" -o "${key}" == "n" ]; then
868 echo "Cancelled."
869 return
870 fi
871 echo "[y/N]? "
872 done
873
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700874 echo -n "Removing RAMCloud backup server data ... "
875 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
876 rm -f ${rc_datafile}
877 echo "DONE"
878}
879
Naoki Shiota4e463182014-03-21 15:13:24 -0700880function stop-server {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700881 kill-processes "RAMCloud server" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700882}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700883############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700884
885
Naoki Shiota590c18d2014-03-31 17:52:59 -0700886## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700887function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700888 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700889 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700890 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700891 ${MVN} -f ${ONOS_HOME}/pom.xml compile
892 fi
893 JAVA_CP=`cat ${CPFILE}`
894 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
895
896 case "$1" in
897 start)
898 stop-onos
899 start-onos
900 ;;
901 startnokill)
902 start-onos
903 ;;
904 startifdown)
905 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
906 if [ $n == 0 ]; then
907 start-onos
908 else
909 echo "$n instance of onos running"
910 fi
911 ;;
912 stop)
913 stop-onos
914 ;;
915 stat*) # <- status
916 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
917 echo "$n instance of onos running"
918 ;;
919 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700920 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700921 exit 1
922 esac
923}
924
925function start-onos {
926 if [ ! -d ${LOGDIR} ]; then
927 mkdir -p ${LOGDIR}
928 fi
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700929 # Rotate log files
Naoki Shiota4e463182014-03-21 15:13:24 -0700930 for log in ${LOGS}; do
931 if [ -f ${log} ]; then
932 rotate-log ${log}
933 fi
934 done
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700935
936 # Rotate logs rolled at runtime.
937 local rolled_log_shellpat=`echo ${ONOS_LOG_ROLLING_PATTERN} | sed -e "s/%i/\\*/"`
938 for rolled_log in ${rolled_log_shellpat}; do
939 if [ -f ${rolled_log} ]; then
940 rotate-log ${rolled_log}
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700941 fi
942 done
943
Naoki Shiota4e463182014-03-21 15:13:24 -0700944 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700945 echo "[WARNING] ${ONOS_LOGBACK} not found."
946 echo " Run \"\$ $0 setup\" to create."
947 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700948 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700949
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700950 if [ ! -f ${HC_CONF} ]; then
951 echo "[WARNING] ${HC_CONF} not found."
952 echo " Run \"\$ $0 setup\" to create."
953 exit 1
954 fi
955
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700956 # specify ZooKeeper(curator) namespace
957 JVM_OPTS="${JVM_OPTS} -Dzookeeper.namespace=${ONOS_CLUSTER_NAME}"
958
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700959 # specify hazelcast.xml to datagrid
960 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
961
962 # specify backend config
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700963 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700964 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
965 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700966 elif [ "${ONOS_HOST_BACKEND}" = "hazelcast" ]; then
967 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.hazelcast.baseConfig=${HC_CONF}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700968 fi
969
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -0700970 # check for jvm debug flag
971 if [ "${ONOS_DEBUG}" = "true" ]; then
972 JVM_OPTS="${JVM_OPTS} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${JVM_DEBUG_PORT}"
973 fi
974
Naoki Shiota9df15d32014-03-27 14:26:20 -0700975 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -0700976
977 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
978 cd ${ONOS_HOME}
979
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700980 echo -n "Starting ONOS controller ... "
Naoki Shiota4e463182014-03-21 15:13:24 -0700981 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
982
983 # We need to wait a bit to find out whether starting the ONOS process succeeded
984 sleep 1
985
986 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700987 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700988 echo " STARTED"
989 else
990 echo " FAILED"
991 fi
992
993# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
994# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
995}
996
997function stop-onos {
998 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
999# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
1000}
Naoki Shiota590c18d2014-03-31 17:52:59 -07001001############################################
Naoki Shiota4e463182014-03-21 15:13:24 -07001002
1003
Naoki Shiota590c18d2014-03-31 17:52:59 -07001004################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -07001005case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -07001006 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -07001007 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -07001008 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001009 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -07001010 mode_parameter=${ONOS_HOST_ROLE}
1011 if [ ! -z "$2" ]; then
1012 mode_parameter=$2
1013 fi
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -07001014
Naoki Shiota9df15d32014-03-27 14:26:20 -07001015 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -07001016 single-node)
1017 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001018 start-backend coord
1019 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001020 onos startifdown
1021 ;;
1022 coord-node)
1023 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001024 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -07001025 onos startifdown
1026 ;;
1027 server-node)
1028 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001029 start-backend server
1030 onos startifdown
1031 ;;
1032 coord-and-server-node)
1033 zk start
1034 start-backend coord
1035 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001036 onos startifdown
1037 ;;
1038 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001039 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001040 ;;
1041 esac
1042 echo
1043 ;;
1044 stop)
1045 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1046 if [ $on != 0 ]; then
1047 onos stop
1048 fi
1049
Naoki Shiota9df15d32014-03-27 14:26:20 -07001050 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -07001051
1052 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1053 if [ $zkn != 0 ]; then
1054 zk stop
1055 fi
1056 echo
1057 ;;
1058 restart)
1059 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1060 if [ $on != 0 ]; then
1061 onos stop
1062 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001063
Naoki Shiota4e463182014-03-21 15:13:24 -07001064 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
1065 if [ $rcsn != 0 ]; then
1066 rc-server stop
1067 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001068
Naoki Shiota590c18d2014-03-31 17:52:59 -07001069 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -07001070 if [ $rccn != 0 ]; then
1071 rc-coord stop
1072 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001073
Naoki Shiota4e463182014-03-21 15:13:24 -07001074 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1075 if [ $zkn != 0 ]; then
1076 zk restart
1077 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001078
Naoki Shiota4e463182014-03-21 15:13:24 -07001079 if [ $rccn != 0 ]; then
1080 rc-coord startifdown
1081 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001082
Naoki Shiota4e463182014-03-21 15:13:24 -07001083 if [ $rcsn != 0 ]; then
1084 rc-server startifdown
1085 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001086
Naoki Shiota4e463182014-03-21 15:13:24 -07001087 if [ $on != 0 ]; then
1088 onos startifdown
1089 fi
1090 echo
1091 ;;
1092 stat*) # <- status
1093 echo '[ZooKeeper]'
1094 zk status
1095 echo
1096 echo '[RAMCloud coordinator]'
1097 rc-coord status
1098 echo
1099 echo '[RAMCloud server]'
1100 rc-server status
1101 echo
1102 echo '[ONOS core]'
1103 onos status
1104 echo
1105 ;;
1106 zk)
1107 zk $2
1108 ;;
1109 rc-c*) # <- rc-coordinator
1110 rc-coord $2
1111 ;;
1112 rc-s*) # <- rc-server
1113 rc-server $2
1114 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001115 rc)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001116 # XXX rc command ignores if the host is not configured to have those roles,
1117 # while rc-s or rc-c executes the task regardless of role definition.
1118 # This is a workaround since TestON does not take role in account
1119 # and always issue rc deldb right now.
1120
Naoki Shiota9109a1e2014-05-13 11:11:01 -07001121 # TODO make deldb command more organized (clarify when it can work)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001122 if [ "`is-coord-role`" == "true" ]; then
1123 rc-coord $2
1124 fi
1125 if [ "`is-server-role`" == "true" ]; then
1126 rc-server $2
1127 fi
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001128 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001129 core)
1130 onos $2
1131 ;;
1132 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001133 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001134 exit 1
1135esac
Naoki Shiota590c18d2014-03-31 17:52:59 -07001136