blob: e674bafcd09e16483d65d0e4640cd9f6ca0b511d [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})
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -070048ZK_CLIENTPORT=$(read-conf ${ONOS_CONF} zookeeper.clientPort 2181)
49ZK_PORTS=$(read-conf ${ONOS_CONF} zookeeper.ports 2888:3888)
Naoki Shiota4e928512014-04-03 15:49:35 -070050RC_COORD_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.coordinator.protocol "fast+udp")
51RC_COORD_IP=$(read-conf ${ONOS_CONF} ramcloud.coordinator.ip ${ONOS_HOST_IP})
52RC_COORD_PORT=$(read-conf ${ONOS_CONF} ramcloud.coordinator.port 12246)
53RC_SERVER_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.server.protocol "fast+udp")
54RC_SERVER_IP=$(read-conf ${ONOS_CONF} ramcloud.server.ip ${ONOS_HOST_IP})
55RC_SERVER_PORT=$(read-conf ${ONOS_CONF} ramcloud.server.port 12242)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070056HC_HOST_PORT=$(read-conf ${ONOS_CONF} hazelcast.host.port 5701)
Naoki Shiota4e928512014-04-03 15:49:35 -070057HC_TCPIP_MEMBERS=$(read-conf ${ONOS_CONF} hazelcast.tcp-ip.members)
58HC_MULTICAST_GROUP=$(read-conf ${ONOS_CONF} hazelcast.multicast.group "224.2.2.3")
59HC_MULTICAST_PORT=$(read-conf ${ONOS_CONF} hazelcast.multicast.port 54327)
Naoki Shiota590c18d2014-03-31 17:52:59 -070060############################################
61
62
63############## Other variables #############
Naoki Shiota4e928512014-04-03 15:49:35 -070064ONOS_TEMPLATE_DIR=${ONOS_CONF_DIR}/template
65
Naoki Shiota4e463182014-03-21 15:13:24 -070066LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
67
Yuta HIGUCHIfb1905a2014-06-09 14:07:34 -070068ZK_HOME=${ZK_HOME:-~/zookeeper-3.4.6}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070069ZK_CONF=${ZK_CONF:-${ONOS_CONF_DIR}/zoo.cfg}
Naoki Shiota4e928512014-04-03 15:49:35 -070070ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070071# Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070072ZK_LOG_DIR=${ZK_LOG_DIR:-${ONOS_HOME}/onos-logs/zk-${ONOS_HOST_NAME}}
Naoki Shiota72209722014-04-08 14:32:17 -070073ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070074ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070075
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070076HC_CONF=${HC_CONF:-${ONOS_CONF_DIR}/hazelcast.xml}
Naoki Shiota4e928512014-04-03 15:49:35 -070077HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
78
Naoki Shiota4e463182014-03-21 15:13:24 -070079RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070080RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
81RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070082RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070083RAMCLOUD_CONF=${RAMCLOUD_CONF:-${ONOS_CONF_DIR}/ramcloud.conf}
Naoki Shiota4e463182014-03-21 15:13:24 -070084
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070085export LD_LIBRARY_PATH=${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}:$LD_LIBRARY_PATH
Naoki Shiota4e463182014-03-21 15:13:24 -070086
87## 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 -070088ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070089ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070090ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070091LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070092LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070093ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070094ONOS_LOG_ROLLING_PATTERN="${LOGDIR}/${LOGBASE}.%i.log.gz"
95ONOS_STDOUT_LOG="${LOGDIR}/${LOGBASE}.stdout"
96ONOS_STDERR_LOG="${LOGDIR}/${LOGBASE}.stderr"
Naoki Shiota4e463182014-03-21 15:13:24 -070097PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070098LOGS="$ONOS_LOG $ONOS_STDOUT_LOG $ONOS_STDERR_LOG $PCAP_LOG"
Naoki Shiota4e463182014-03-21 15:13:24 -070099
Naoki Shiota9df15d32014-03-27 14:26:20 -0700100ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -0700101JMX_PORT=${JMX_PORT:-7189}
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -0700102JVM_DEBUG_PORT=${JVM_DEBUG_PORT:-22007}
Naoki Shiota4e463182014-03-21 15:13:24 -0700103
104# Set JVM options
105JVM_OPTS="${JVM_OPTS:-}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700106JVM_OPTS="$JVM_OPTS -server -d64"
107#JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700108
109# Uncomment or specify appropriate value as JVM_OPTS environment variables.
110#JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
Naoki Shiota4e463182014-03-21 15:13:24 -0700111#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
112#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700113
114#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC"
115JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
116JVM_OPTS="$JVM_OPTS -XX:+AggressiveOpts"
117
118# We may want to remove UseFastAccessorMethods option: http://bugs.java.com/view_bug.do?bug_id=6385687
119JVM_OPTS="$JVM_OPTS -XX:+UseFastAccessorMethods"
120
121JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192"
122JVM_OPTS="$JVM_OPTS -XX:FreqInlineSize=8192"
123JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500"
124
Naoki Shiota4e463182014-03-21 15:13:24 -0700125JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700126
Praseed Balakrishnan4fe80812014-07-24 11:29:19 -0700127# This option tells the VM to generate a heap dump when memory allocation cannot be satisfied.
128# http://www.oracle.com/technetwork/java/javase/clopts-139448.html#gbzrr
129JVM_OPTS="$JVM_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${ONOS_HOME}"
130
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700131# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
132JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
133
134JVM_OPTS="$JVM_OPTS -XX:+UseCompressedOops"
135
136JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
137JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
138JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
139
Naoki Shiota4e463182014-03-21 15:13:24 -0700140JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
141
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700142# Uncomment to dump final JVM flags to stdout
143#JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
144
Naoki Shiota4e463182014-03-21 15:13:24 -0700145# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700146MAIN_CLASS="net.onrc.onos.core.main.Main"
Naoki Shiota4e463182014-03-21 15:13:24 -0700147
148MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700149############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700150
Naoki Shiota9df15d32014-03-27 14:26:20 -0700151
Naoki Shiota590c18d2014-03-31 17:52:59 -0700152############# Common functions #############
153function print_usage {
Naoki Shiota05721b32014-04-29 14:41:12 -0700154 local scriptname=`basename $0`
Naoki Shiota590c18d2014-03-31 17:52:59 -0700155 local filename=`basename ${ONOS_CONF}`
156 local usage="Usage: setup/start/stop ONOS on this server.
Naoki Shiota05721b32014-04-29 14:41:12 -0700157 \$ ${scriptname} setup [-f]
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700158 Set up ONOS node using ${ONOS_CONF} .
Naoki Shiota590c18d2014-03-31 17:52:59 -0700159 - generate and replace config file of ZooKeeper.
160 - create myid in ZooKeeper datadir.
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700161 - generate and replace config file for Hazelcast.
162 - generate and replace config file for RAMCloud.
Naoki Shiota590c18d2014-03-31 17:52:59 -0700163 - generate and replace logback.${ONOS_HOST_NAME}.xml
164 If -f option is used, all existing files will be overwritten without confirmation.
Naoki Shiota05721b32014-04-29 14:41:12 -0700165 \$ ${scriptname} start [single-node|coord-node|server-node|coord-and-server-node]
Naoki Shiota590c18d2014-03-31 17:52:59 -0700166 Start ONOS node with specific RAMCloud entities
167 - single-node: start ONOS with stand-alone RAMCloud
168 - coord-node : start ONOS with RAMCloud coordinator
169 - server-node: start ONOS with RAMCloud server
170 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
171 * Default behavior can be defined by ${filename}
Naoki Shiota05721b32014-04-29 14:41:12 -0700172 \$ ${scriptname} stop
Naoki Shiota590c18d2014-03-31 17:52:59 -0700173 Stop all ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700174 \$ ${scriptname} restart
Naoki Shiota590c18d2014-03-31 17:52:59 -0700175 Stop and start currently running ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700176 \$ ${scriptname} status
Naoki Shiota590c18d2014-03-31 17:52:59 -0700177 Show status of ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700178 \$ ${scriptname} {zk|rc-coord|rc-server|core} {start|stop|restart|status}
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700179 Control specific ONOS-related process
180 \$ ${scriptname} rc deldb
181 Delete data in RAMCloud"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700182
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700183 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700184}
185
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700186# rotate-log [log-filename] [max rotations]
187# Example:
188# foobar.log -> foobar.log.1
189# foobar.log.1 -> foobar.log.2
190# foobar.log.gz -> foobar.log.1.gz
Naoki Shiota4e463182014-03-21 15:13:24 -0700191function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700192 local logfile=$1
193 local nr_max=${2:-10}
194 if [ -f $logfile ]; then
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700195 # TODO treating only .gz now. probably want more generic solution
196 local basename=${logfile%%.gz}
197 local append=""
198 if [ "$basename" != "$logfile" ]; then
199 append=".gz"
200 fi
Naoki Shiota4e928512014-04-03 15:49:35 -0700201 for i in `seq $(expr $nr_max - 1) -1 1`; do
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700202 if [ -f ${basename}.${i}${append} ]; then
203 mv -f ${basename}.${i}${append} ${basename}.`expr $i + 1`${append}
Naoki Shiota4e928512014-04-03 15:49:35 -0700204 fi
205 done
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700206 mv ${basename}${append} ${basename}.1${append}
Naoki Shiota4e928512014-04-03 15:49:35 -0700207 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700208}
209
210# kill-processes {module-name} {array of pids}
211function kill-processes {
212 # Kill the existing processes
213 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700214 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700215 echo -n "Stopping $1 ... "
216 fi
217 for p in ${pids}; do
218 if [ x$p != "x" ]; then
Naoki Shiota4b355762014-05-27 11:46:43 -0700219 # Check if target process is accesible from current user
220 kill -0 $p
221 if [ "$?" -ne 0 ]; then
222 # Error exit code here means "failed to send signal".
223 # Supposedly because of permission error.
224 echo "Failed to kill process (pid: $p)."
225 echo "Check if current user is the same as process owner."
226 exit 1
227 fi
228
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700229 (
230 # Ask process with SIGTERM first, if that did not kill the process
231 # wait 1s and if process still exist, force process to be killed.
232 kill -TERM $p && kill -0 $p && sleep 1 && kill -0 $p && kill -KILL $p
233 ) 2> /dev/null
Naoki Shiota4b355762014-05-27 11:46:43 -0700234
235 # Ensure process is killed.
236 kill -0 $p 2> /dev/null
237 if [ "$?" -ne 0 ]; then
238 # Error exit code here means "process not found", i.e. "kill succeeded".
239 echo "Killed existing process (pid: $p)"
240 else
241 # Process still exists. Some unexpected error occurs.
242 echo "Failed to kill process (pid: $p)."
243 echo "Unexpected error occurs during process termination."
244 exit 1
245 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700246 fi
247 done
248}
249
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700250function handle-error {
251 set -e
252
253 revert-confs
254
255 set +e
256
257 exit 1
258}
259
Naoki Shiota4e928512014-04-03 15:49:35 -0700260# revert-confs [error message]
261function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700262 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700263
Naoki Shiota4e928512014-04-03 15:49:35 -0700264 revert-file `basename ${ZK_CONF}`
265 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700266
267 echo "EXIT"
268
269 if [ ! -z "$1" ]; then
270 echo $1
271 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700272}
273
Naoki Shiota590c18d2014-03-31 17:52:59 -0700274function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700275 echo -n "Creating ${ZK_CONF} ... "
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700276
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700277 # Create the ZooKeeper lib directory
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700278 if [[ ! ( -w ${ZK_LIB_DIR} && -d ${ZK_LIB_DIR} ) ]]; then
Naoki Shiota7f495cf2014-05-21 17:23:25 -0700279 local SUDO=${SUDO:-sudo}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700280 local whoami=`whoami`
281 {
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700282 ${SUDO} mkdir -p ${ZK_LIB_DIR}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700283 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
284 } || {
285 echo "FAILED"
286 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
287 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
288 exit 1
289 }
290 fi
291
292 # creation of ZooKeeper config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700293 local temp_zk=`begin-conf-creation ${ZK_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700294
Naoki Shiota4e928512014-04-03 15:49:35 -0700295 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700296
297 local i=1
298 local myid=
299 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700300 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700301 myid=$i
302 break
303 fi
304 i=`expr $i + 1`
305 done
306
307 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700308 local filename=`basename ${ONOS_CONF}`
Naoki Shiota35f8d5c2014-04-08 11:13:18 -0700309 revert-confs "[ERROR] In ${filename}, zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700310 fi
311
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700312 if [ -f "${ZK_MY_ID}" ]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700313 # sudo will be needed if ZK_MY_ID is already created by other (old) script
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700314 local SUDO=${SUDO:-}
315 {
316 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
317 } || {
318 echo "FAILED"
319 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700320 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700321 exit 1
322 }
323 fi
324
Naoki Shiota9df15d32014-03-27 14:26:20 -0700325 echo ${myid} > ${ZK_MY_ID}
326
327 echo -n "myid is assigned to ${myid} ... "
328
329 while read line; do
330 if [[ $line =~ ^__HOSTS__$ ]]; then
331 i=1
332 for host in ${hostarr}; do
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700333 # TODO: ZK ports should be configurable per host
334 local hostline="server.${i}=${host}:${ZK_PORTS}"
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700335 echo $hostline
Naoki Shiota9df15d32014-03-27 14:26:20 -0700336 i=`expr $i + 1`
337 done
338 elif [[ $line =~ __DATADIR__ ]]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700339 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700340 else
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700341 echo $line
Naoki Shiota9df15d32014-03-27 14:26:20 -0700342 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700343 done < ${ZK_CONF_TEMPLATE} > ${temp_zk}
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700344 sed -e "s|__CLIENTPORT__|${ZK_CLIENTPORT}|" -i "" ${temp_zk}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700345
346 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700347
348 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700349}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700350
Naoki Shiota4e928512014-04-03 15:49:35 -0700351function create-hazelcast-conf {
352 echo -n "Creating ${HC_CONF} ... "
353
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700354 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700355
356 # To keep indent of XML file, change IFS
357 local IFS=''
358 while read line; do
359 if [[ $line =~ __HC_NETWORK__ ]]; then
360 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
361 # temporary change
362 IFS=' '
363 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
364 echo '<multicast enabled="false" />'
365 echo '<tcp-ip enabled="true">'
366 for member in ${memberarr}; do
367 echo " <member>${member}</member>"
368 done
369 echo '</tcp-ip>'
370 IFS=''
371 else
372 echo '<multicast enabled="true">'
373 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
374 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
375 echo '</multicast>'
376 echo '<tcp-ip enabled="false" />'
377 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700378 elif [[ $line =~ __HC_PORT__ ]]; then
379 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700380 elif [[ $line =~ __HC_CLUSTER__ ]]; then
381 echo $line | sed -e "s|__HC_CLUSTER__|${ONOS_CLUSTER_NAME}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700382 else
383 echo "${line}"
384 fi
385 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
386
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700387 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700388
389 echo "DONE"
390}
391
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700392function create-ramcloud-conf {
393 echo -n "Creating ${RAMCLOUD_CONF} ... "
394
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700395 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700396
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700397 local rc_locator=`echo "${ZK_HOSTS}" | sed -e "s/,/:${ZK_CLIENTPORT},/g"`
398 rc_locator+=":${ZK_CLIENTPORT}"
399
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700400 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700401
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700402 echo "ramcloud.locator=zk:localhost:2181,${rc_locator}" > ${temp_rc}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700403 echo "ramcloud.clusterName=${rc_cluster_name}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700404
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700405 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700406
407 echo "DONE"
408}
409
Naoki Shiota590c18d2014-03-31 17:52:59 -0700410function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700411 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700412
Naoki Shiota9df15d32014-03-27 14:26:20 -0700413 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700414 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700415
416 sed -e "s|__FILENAME__|${ONOS_LOG}|" \
417 -e "s|__ROLLING_PATTERN__|${ONOS_LOG_ROLLING_PATTERN}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700418
419 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700420
Naoki Shiota9df15d32014-03-27 14:26:20 -0700421 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700422}
423
Naoki Shiota4e928512014-04-03 15:49:35 -0700424function create-confs {
425 local key
426 local filename
427
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700428 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700429
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700430 echo "Config file : ${ONOS_CONF}"
431
Naoki Shiota4e928512014-04-03 15:49:35 -0700432 if [ "$1" == "-f" ]; then
433 create-zk-conf
434 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700435 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700436 create-logback-conf
437 else
438 create-conf-interactive ${ZK_CONF} create-zk-conf
439 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700440 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700441 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
442 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700443
444 trap - ERR
445}
446############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700447
Naoki Shiota590c18d2014-03-31 17:52:59 -0700448
449###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700450function zk {
451 case "$1" in
452 start)
453 start-zk
454 ;;
455 stop)
456 stop-zk
457 ;;
458 stat*) # <- status
459 status-zk
460 ;;
461 re*) # <- restart
462 stop-zk
463 start-zk
464 ;;
465 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700466 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700467 exit 1
468 esac
469}
470
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700471function load-zk-cfg {
472 if [ -f "${ZK_CONF}" ]; then
473 local filename=`basename ${ZK_CONF}`
474 local dirname=`dirname ${ZK_CONF}`
475
476 # Run ZooKeeper with our configuration
477 export ZOOCFG=${filename}
478 export ZOOCFGDIR=${dirname}
479 fi
480}
481
Naoki Shiota4e463182014-03-21 15:13:24 -0700482function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700483 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700484
Naoki Shiota4e928512014-04-03 15:49:35 -0700485 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700486 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700487
488 load-zk-cfg
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700489
490 # log4j.properties is read from classpath if not found in CWD.
491 # Using the ZooKeeper supplied default in ZooKeeper conf dir.
492 # TODO: To explicitly specify our customized log4j config file:
493 # export SERVER_JVMFLAGS="-Dlog4j.configuration=${ZK_LOG4J}"
494 env CLASSPATH="${ZK_HOME}/conf:${CLASSPATH}" ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700495}
496
497function stop-zk {
Yuta HIGUCHI7c708362014-06-02 23:15:13 -0700498 echo -n "Stopping ZooKeeper ... "
499 load-zk-cfg
500
501 ${ZK_HOME}/bin/zkServer.sh stop
Naoki Shiota4e463182014-03-21 15:13:24 -0700502}
503
504function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700505 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700506
Naoki Shiota72209722014-04-08 14:32:17 -0700507 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700508}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700509
510function check-zk {
511 # assumption here is that ZK status script is the last command in status-zk.
512 status-zk &> /dev/null
513 local zk_status=$?
514 if [ "$zk_status" -ne 0 ]; then
515 return 1;
516 fi
517 return 0
518}
519
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700520function check-and-start-zk {
521 check-zk
522 local zk_status=$?
523 if [ "$zk_status" -ne 0 ]; then
524 start-zk
525 fi
526}
527
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700528# wait-zk-or-die {timeout-sec}
529function wait-zk-or-die {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700530 local retries=${1:-5}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700531 # do-while retries >= 0
532 while true; do
533 check-zk
534 local zk_status=$?
535 if [ "$zk_status" -eq 0 ]; then
536 return 0
537 fi
538 sleep 1;
539 ((retries -= 1))
540 (( retries >= 0 )) || break
541 done
542 echo "ZooKeeper is not running."
543 exit 1
544}
545
Naoki Shiota590c18d2014-03-31 17:52:59 -0700546############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700547
548
Naoki Shiota590c18d2014-03-31 17:52:59 -0700549####### Functions related to RAMCloud ######
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -0700550
551# Check if this host should be running RAMCloud coordinator:
552function is-coord-role {
553 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
554 case "${ONOS_HOST_ROLE}" in
555 single-node)
556 echo "true"
557 return 0
558 ;;
559 coord-node)
560 echo "true"
561 return 0
562 ;;
563 server-node)
564 echo "false"
565 return 1
566 ;;
567 coord-and-server-node)
568 echo "true"
569 return 0
570 ;;
571 *)
572 echo "false"
573 return 1
574 ;;
575 esac
576 else
577 echo "false"
578 return 1
579 fi
580}
581
582# Check if this host should be running RAMCloud server:
583function is-server-role {
584 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
585 case "${ONOS_HOST_ROLE}" in
586 single-node)
587 echo "true"
588 return 0
589 ;;
590 coord-node)
591 echo "false"
592 return 1
593 ;;
594 server-node)
595 echo "true"
596 return 0
597 ;;
598 coord-and-server-node)
599 echo "true"
600 return 0
601 ;;
602 *)
603 echo "false"
604 return 1
605 ;;
606 esac
607 else
608 echo "false"
609 return 1
610 fi
611}
612
Naoki Shiota9df15d32014-03-27 14:26:20 -0700613function start-backend {
614 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700615 if [ $1 == "coord" ]; then
616 rc-coord startifdown
617 elif [ $1 == "server" ]; then
618 rc-server startifdown
619 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700620 fi
621}
622
623function stop-backend {
624 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
625 if [ $rcsn != 0 ]; then
626 rc-server stop
627 fi
628
Naoki Shiota590c18d2014-03-31 17:52:59 -0700629 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700630 if [ $rccn != 0 ]; then
631 rc-coord stop
632 fi
633}
634
Naoki Shiota590c18d2014-03-31 17:52:59 -0700635
Naoki Shiota4e463182014-03-21 15:13:24 -0700636### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700637function rc-coord-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700638 local host=${RC_COORD_IP}
639 if [ -z "${host}" ]; then
640 # falling back to 0.0.0.0
641 host="0.0.0.0"
642 fi
643 echo "${RC_COORD_PROTOCOL}:host=${host},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700644}
645
646function rc-server-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700647 local host=${RC_SERVER_IP}
648 if [ -z "${host}" ]; then
649 # falling back to 0.0.0.0
650 host="0.0.0.0"
651 fi
652 echo "${RC_SERVER_PROTOCOL}:host=${host},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700653}
654
Naoki Shiota4e463182014-03-21 15:13:24 -0700655function rc-coord {
656 case "$1" in
657 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700658 stop-coord
659 start-coord
660 ;;
661 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700662 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700663 if [ $n == 0 ]; then
664 start-coord
665 else
666 echo "$n instance of RAMCloud coordinator running"
667 fi
668 ;;
669 stop)
670 stop-coord
671 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700672 deldb)
673 stop-backend
674 del-coord-info
675 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700676 stat*) # <- status
677 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
678 echo "$n RAMCloud coordinator running"
679 ;;
680 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700681 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700682 exit 1
683 esac
684}
685
686function start-coord {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700687 check-and-start-zk
688 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700689
Naoki Shiota4e463182014-03-21 15:13:24 -0700690 if [ ! -d ${LOGDIR} ]; then
691 mkdir -p ${LOGDIR}
692 fi
693 if [ -f $RAMCLOUD_COORD_LOG ]; then
694 rotate-log $RAMCLOUD_COORD_LOG
695 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700696
697 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700698
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700699 # TODO Make ONOS_CONF readable from ONOS process then eliminate RAMCLOUD_CONF
700
701 # Configuration for ZK address, port
702 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
703
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700704 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700705 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700706
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700707 # RAMCloud transport timeout
708 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700709 # RAMCloud option deadServerTimeout
710 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
711 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
712
713 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
714 # but actual RAMCloud code only uses -C argument now.
715 # (FYI: -C is documented to be deprecated in the document)
716
717 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700718 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700719 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700720 coord_args="${coord_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700721 coord_args="${coord_args} --deadServerTimeout ${rc_coord_deadServerTimeout}"
722
723 # Read environment variables if set
724 coord_args="${coord_args} ${RC_COORDINATOR_OPTS}"
725
726 if [ "${ONOS_HOST_ROLE}" == "single-node" ]; then
727 # Note: Following reset is required, since RC restart is considered node failure,
728 # and tries recovery of server, which will never succeed after restart.
729 echo "Role configured to single-node mode. RAMCloud cluster will be reset on each start-up."
730 coord_args="${coord_args} --reset"
731 fi
732
Naoki Shiota4e463182014-03-21 15:13:24 -0700733 # Run ramcloud
734 echo -n "Starting RAMCloud coordinator ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700735 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700736 echo "STARTED"
737}
738
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700739function del-coord-info {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700740 check-and-start-zk
741 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700742
743 if [ ! -d ${LOGDIR} ]; then
744 mkdir -p ${LOGDIR}
745 fi
746 if [ -f $RAMCLOUD_COORD_LOG ]; then
747 rotate-log $RAMCLOUD_COORD_LOG
748 fi
749
750 local coord_addr=`rc-coord-addr`
751
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700752 # Configuration for ZK address, port
753 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700754 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700755 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700756 # RAMCloud option deadServerTimeout
757 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
758 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
759
760 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
761 # but actual RAMCloud code only uses -C argument now.
762 # (FYI: -C is documented to be deprecated in the document)
763
764 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700765 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700766 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
767
768 # Note: --reset will reset ZK stored info and start running as acoordinator.
769 echo -n "Deleting RAMCloud cluster coordination info ... "
770 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} --reset &> $RAMCLOUD_COORD_LOG &
771
Yuta HIGUCHIcd44eb32014-05-24 16:51:31 -0700772 # TODO Assuming 5 sec is enough. To be sure monitor log?
773 sleep 5
774 # kill coordinator
Yuta HIGUCHIce9f3ee2014-05-14 09:48:40 -0700775 (pkill -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator &> /dev/null)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700776
777 echo "DONE"
778}
Naoki Shiota4e463182014-03-21 15:13:24 -0700779
780function stop-coord {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700781 kill-processes "RAMCloud coordinator" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700782}
783
Naoki Shiota4e463182014-03-21 15:13:24 -0700784### Functions related to RAMCloud server
785function rc-server {
786 case "$1" in
787 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700788 stop-server
789 start-server
790 ;;
791 startifdown)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700792 local n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700793 if [ $n == 0 ]; then
794 start-server
795 else
796 echo "$n instance of RAMCloud server running"
797 fi
798 ;;
799 stop)
800 stop-server
801 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700802 deldb)
803 stop-server
804 del-server-backup
805 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700806 stat*) # <- status
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700807 n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700808 echo "$n RAMCloud server running"
809 ;;
810 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700811 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700812 exit 1
813 esac
814}
815
816function start-server {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700817 check-and-start-zk
818 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700819
Naoki Shiota4e463182014-03-21 15:13:24 -0700820 if [ ! -d ${LOGDIR} ]; then
821 mkdir -p ${LOGDIR}
822 fi
823 if [ -f $RAMCLOUD_SERVER_LOG ]; then
824 rotate-log $RAMCLOUD_SERVER_LOG
825 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700826
827 local coord_addr=`rc-coord-addr`
828 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700829
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700830 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.server.masterServiceThreads 5)
831 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.server.logCleanerThreads 1)
Yuta HIGUCHI52efcbb2014-05-16 09:30:33 -0700832 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.server.detectFailures 0)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700833
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700834 # Configuration for ZK address, port
835 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700836 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700837 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700838 # RAMCloud transport timeout
839 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700840 # replication factor (-r) config
841 local rc_replicas=$(read-conf ${ONOS_CONF} ramcloud.server.replicas 0)
842 # backup file path (-f) config
843 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
844 mkdir -p `dirname ${rc_datafile}`
845
846 local server_args="-L ${server_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700847 server_args="${server_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700848 server_args="${server_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700849 server_args="${server_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700850 server_args="${server_args} --masterServiceThreads ${masterServiceThreads}"
851 server_args="${server_args} --logCleanerThreads ${logCleanerThreads}"
852 server_args="${server_args} --detectFailures ${detectFailures}"
853 server_args="${server_args} --replicas ${rc_replicas}"
854 server_args="${server_args} --file ${rc_datafile}"
855
856 # Read environment variables if set
857 server_args="${server_args} ${RC_SERVER_OPTS}"
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700858
Naoki Shiota4e463182014-03-21 15:13:24 -0700859 # Run ramcloud
860 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700861 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server ${server_args} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700862 echo "STARTED"
863}
864
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700865function del-server-backup {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700866 echo -n "Delete RAMCloud backup server data [y/N]? "
867 while [ 1 ]; do
868 read key
869 if [ "${key}" == "Y" -o "${key}" == "y" ]; then
870 break
871 elif [ -z "${key}" -o "${key}" == "N" -o "${key}" == "n" ]; then
872 echo "Cancelled."
873 return
874 fi
875 echo "[y/N]? "
876 done
877
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700878 echo -n "Removing RAMCloud backup server data ... "
879 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
880 rm -f ${rc_datafile}
881 echo "DONE"
882}
883
Naoki Shiota4e463182014-03-21 15:13:24 -0700884function stop-server {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700885 kill-processes "RAMCloud server" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700886}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700887############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700888
889
Naoki Shiota590c18d2014-03-31 17:52:59 -0700890## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700891function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700892 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700893 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700894 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700895 ${MVN} -f ${ONOS_HOME}/pom.xml compile
896 fi
897 JAVA_CP=`cat ${CPFILE}`
898 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
899
900 case "$1" in
901 start)
902 stop-onos
903 start-onos
904 ;;
905 startnokill)
906 start-onos
907 ;;
908 startifdown)
909 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
910 if [ $n == 0 ]; then
911 start-onos
912 else
913 echo "$n instance of onos running"
914 fi
915 ;;
916 stop)
917 stop-onos
918 ;;
919 stat*) # <- status
920 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
921 echo "$n instance of onos running"
922 ;;
923 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700924 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700925 exit 1
926 esac
927}
928
929function start-onos {
930 if [ ! -d ${LOGDIR} ]; then
931 mkdir -p ${LOGDIR}
932 fi
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700933 # Rotate log files
Naoki Shiota4e463182014-03-21 15:13:24 -0700934 for log in ${LOGS}; do
935 if [ -f ${log} ]; then
936 rotate-log ${log}
937 fi
938 done
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700939
940 # Rotate logs rolled at runtime.
941 local rolled_log_shellpat=`echo ${ONOS_LOG_ROLLING_PATTERN} | sed -e "s/%i/\\*/"`
942 for rolled_log in ${rolled_log_shellpat}; do
943 if [ -f ${rolled_log} ]; then
944 rotate-log ${rolled_log}
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700945 fi
946 done
947
Naoki Shiota4e463182014-03-21 15:13:24 -0700948 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700949 echo "[WARNING] ${ONOS_LOGBACK} not found."
950 echo " Run \"\$ $0 setup\" to create."
951 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700952 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700953
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700954 if [ ! -f ${HC_CONF} ]; then
955 echo "[WARNING] ${HC_CONF} not found."
956 echo " Run \"\$ $0 setup\" to create."
957 exit 1
958 fi
959
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700960 # specify ZooKeeper(curator) namespace
961 JVM_OPTS="${JVM_OPTS} -Dzookeeper.namespace=${ONOS_CLUSTER_NAME}"
962
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700963 # specify ZooKeeper connectionString
964 local zk_locator=`echo "${ZK_HOSTS}" | sed -e "s/,/:${ZK_CLIENTPORT},/g"`
965 zk_locator+=":${ZK_CLIENTPORT}"
966 zk_locator="localhost:${ZK_CLIENTPORT},${zk_locator}"
967
968 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.registry.ZookeeperRegistry.connectionString=${zk_locator}"
969
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700970 # specify hazelcast.xml to datagrid
971 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
972
973 # specify backend config
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700974 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700975 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
976 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700977 elif [ "${ONOS_HOST_BACKEND}" = "hazelcast" ]; then
978 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.hazelcast.baseConfig=${HC_CONF}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700979 fi
980
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -0700981 # check for jvm debug flag
982 if [ "${ONOS_DEBUG}" = "true" ]; then
983 JVM_OPTS="${JVM_OPTS} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${JVM_DEBUG_PORT}"
984 fi
985
Naoki Shiota9df15d32014-03-27 14:26:20 -0700986 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -0700987
988 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
989 cd ${ONOS_HOME}
990
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700991 echo -n "Starting ONOS controller ... "
Naoki Shiota4e463182014-03-21 15:13:24 -0700992 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
993
994 # We need to wait a bit to find out whether starting the ONOS process succeeded
995 sleep 1
996
997 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700998 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700999 echo " STARTED"
1000 else
1001 echo " FAILED"
1002 fi
1003
1004# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
1005# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
1006}
1007
1008function stop-onos {
1009 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
1010# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
1011}
Naoki Shiota590c18d2014-03-31 17:52:59 -07001012############################################
Naoki Shiota4e463182014-03-21 15:13:24 -07001013
1014
Naoki Shiota590c18d2014-03-31 17:52:59 -07001015################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -07001016case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -07001017 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -07001018 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -07001019 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001020 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -07001021 mode_parameter=${ONOS_HOST_ROLE}
1022 if [ ! -z "$2" ]; then
1023 mode_parameter=$2
1024 fi
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -07001025
Naoki Shiota9df15d32014-03-27 14:26:20 -07001026 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -07001027 single-node)
1028 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001029 start-backend coord
1030 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001031 onos startifdown
1032 ;;
1033 coord-node)
1034 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001035 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -07001036 onos startifdown
1037 ;;
1038 server-node)
1039 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001040 start-backend server
1041 onos startifdown
1042 ;;
1043 coord-and-server-node)
1044 zk start
1045 start-backend coord
1046 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001047 onos startifdown
1048 ;;
1049 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001050 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001051 ;;
1052 esac
1053 echo
1054 ;;
1055 stop)
1056 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1057 if [ $on != 0 ]; then
1058 onos stop
1059 fi
1060
Naoki Shiota9df15d32014-03-27 14:26:20 -07001061 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -07001062
1063 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1064 if [ $zkn != 0 ]; then
1065 zk stop
1066 fi
1067 echo
1068 ;;
1069 restart)
1070 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1071 if [ $on != 0 ]; then
1072 onos stop
1073 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001074
Naoki Shiota4e463182014-03-21 15:13:24 -07001075 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
1076 if [ $rcsn != 0 ]; then
1077 rc-server stop
1078 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001079
Naoki Shiota590c18d2014-03-31 17:52:59 -07001080 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -07001081 if [ $rccn != 0 ]; then
1082 rc-coord stop
1083 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001084
Naoki Shiota4e463182014-03-21 15:13:24 -07001085 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1086 if [ $zkn != 0 ]; then
1087 zk restart
1088 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001089
Naoki Shiota4e463182014-03-21 15:13:24 -07001090 if [ $rccn != 0 ]; then
1091 rc-coord startifdown
1092 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001093
Naoki Shiota4e463182014-03-21 15:13:24 -07001094 if [ $rcsn != 0 ]; then
1095 rc-server startifdown
1096 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001097
Naoki Shiota4e463182014-03-21 15:13:24 -07001098 if [ $on != 0 ]; then
1099 onos startifdown
1100 fi
1101 echo
1102 ;;
1103 stat*) # <- status
1104 echo '[ZooKeeper]'
1105 zk status
1106 echo
1107 echo '[RAMCloud coordinator]'
1108 rc-coord status
1109 echo
1110 echo '[RAMCloud server]'
1111 rc-server status
1112 echo
1113 echo '[ONOS core]'
1114 onos status
1115 echo
1116 ;;
1117 zk)
1118 zk $2
1119 ;;
1120 rc-c*) # <- rc-coordinator
1121 rc-coord $2
1122 ;;
1123 rc-s*) # <- rc-server
1124 rc-server $2
1125 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001126 rc)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001127 # XXX rc command ignores if the host is not configured to have those roles,
1128 # while rc-s or rc-c executes the task regardless of role definition.
1129 # This is a workaround since TestON does not take role in account
1130 # and always issue rc deldb right now.
1131
Naoki Shiota9109a1e2014-05-13 11:11:01 -07001132 # TODO make deldb command more organized (clarify when it can work)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001133 if [ "`is-coord-role`" == "true" ]; then
1134 rc-coord $2
1135 fi
1136 if [ "`is-server-role`" == "true" ]; then
1137 rc-server $2
1138 fi
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001139 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001140 core)
1141 onos $2
1142 ;;
1143 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001144 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001145 exit 1
1146esac
Naoki Shiota590c18d2014-03-31 17:52:59 -07001147