blob: fe03aa3ddcce96d9027697397da76d706771923c [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
125# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
126JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
127
128JVM_OPTS="$JVM_OPTS -XX:+UseCompressedOops"
129
130JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
131JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
132JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
133
Naoki Shiota4e463182014-03-21 15:13:24 -0700134JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
135
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700136# Uncomment to dump final JVM flags to stdout
137#JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
138
Naoki Shiota4e463182014-03-21 15:13:24 -0700139# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700140MAIN_CLASS="net.onrc.onos.core.main.Main"
Naoki Shiota4e463182014-03-21 15:13:24 -0700141
142MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700143############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700144
Naoki Shiota9df15d32014-03-27 14:26:20 -0700145
Naoki Shiota590c18d2014-03-31 17:52:59 -0700146############# Common functions #############
147function print_usage {
Naoki Shiota05721b32014-04-29 14:41:12 -0700148 local scriptname=`basename $0`
Naoki Shiota590c18d2014-03-31 17:52:59 -0700149 local filename=`basename ${ONOS_CONF}`
150 local usage="Usage: setup/start/stop ONOS on this server.
Naoki Shiota05721b32014-04-29 14:41:12 -0700151 \$ ${scriptname} setup [-f]
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700152 Set up ONOS node using ${ONOS_CONF} .
Naoki Shiota590c18d2014-03-31 17:52:59 -0700153 - generate and replace config file of ZooKeeper.
154 - create myid in ZooKeeper datadir.
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700155 - generate and replace config file for Hazelcast.
156 - generate and replace config file for RAMCloud.
Naoki Shiota590c18d2014-03-31 17:52:59 -0700157 - generate and replace logback.${ONOS_HOST_NAME}.xml
158 If -f option is used, all existing files will be overwritten without confirmation.
Naoki Shiota05721b32014-04-29 14:41:12 -0700159 \$ ${scriptname} start [single-node|coord-node|server-node|coord-and-server-node]
Naoki Shiota590c18d2014-03-31 17:52:59 -0700160 Start ONOS node with specific RAMCloud entities
161 - single-node: start ONOS with stand-alone RAMCloud
162 - coord-node : start ONOS with RAMCloud coordinator
163 - server-node: start ONOS with RAMCloud server
164 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
165 * Default behavior can be defined by ${filename}
Naoki Shiota05721b32014-04-29 14:41:12 -0700166 \$ ${scriptname} stop
Naoki Shiota590c18d2014-03-31 17:52:59 -0700167 Stop all ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700168 \$ ${scriptname} restart
Naoki Shiota590c18d2014-03-31 17:52:59 -0700169 Stop and start currently running ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700170 \$ ${scriptname} status
Naoki Shiota590c18d2014-03-31 17:52:59 -0700171 Show status of ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700172 \$ ${scriptname} {zk|rc-coord|rc-server|core} {start|stop|restart|status}
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700173 Control specific ONOS-related process
174 \$ ${scriptname} rc deldb
175 Delete data in RAMCloud"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700176
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700177 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700178}
179
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700180# rotate-log [log-filename] [max rotations]
181# Example:
182# foobar.log -> foobar.log.1
183# foobar.log.1 -> foobar.log.2
184# foobar.log.gz -> foobar.log.1.gz
Naoki Shiota4e463182014-03-21 15:13:24 -0700185function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700186 local logfile=$1
187 local nr_max=${2:-10}
188 if [ -f $logfile ]; then
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700189 # TODO treating only .gz now. probably want more generic solution
190 local basename=${logfile%%.gz}
191 local append=""
192 if [ "$basename" != "$logfile" ]; then
193 append=".gz"
194 fi
Naoki Shiota4e928512014-04-03 15:49:35 -0700195 for i in `seq $(expr $nr_max - 1) -1 1`; do
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700196 if [ -f ${basename}.${i}${append} ]; then
197 mv -f ${basename}.${i}${append} ${basename}.`expr $i + 1`${append}
Naoki Shiota4e928512014-04-03 15:49:35 -0700198 fi
199 done
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700200 mv ${basename}${append} ${basename}.1${append}
Naoki Shiota4e928512014-04-03 15:49:35 -0700201 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700202}
203
204# kill-processes {module-name} {array of pids}
205function kill-processes {
206 # Kill the existing processes
207 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700208 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700209 echo -n "Stopping $1 ... "
210 fi
211 for p in ${pids}; do
212 if [ x$p != "x" ]; then
Naoki Shiota4b355762014-05-27 11:46:43 -0700213 # Check if target process is accesible from current user
214 kill -0 $p
215 if [ "$?" -ne 0 ]; then
216 # Error exit code here means "failed to send signal".
217 # Supposedly because of permission error.
218 echo "Failed to kill process (pid: $p)."
219 echo "Check if current user is the same as process owner."
220 exit 1
221 fi
222
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700223 (
224 # Ask process with SIGTERM first, if that did not kill the process
225 # wait 1s and if process still exist, force process to be killed.
226 kill -TERM $p && kill -0 $p && sleep 1 && kill -0 $p && kill -KILL $p
227 ) 2> /dev/null
Naoki Shiota4b355762014-05-27 11:46:43 -0700228
229 # Ensure process is killed.
230 kill -0 $p 2> /dev/null
231 if [ "$?" -ne 0 ]; then
232 # Error exit code here means "process not found", i.e. "kill succeeded".
233 echo "Killed existing process (pid: $p)"
234 else
235 # Process still exists. Some unexpected error occurs.
236 echo "Failed to kill process (pid: $p)."
237 echo "Unexpected error occurs during process termination."
238 exit 1
239 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700240 fi
241 done
242}
243
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700244function handle-error {
245 set -e
246
247 revert-confs
248
249 set +e
250
251 exit 1
252}
253
Naoki Shiota4e928512014-04-03 15:49:35 -0700254# revert-confs [error message]
255function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700256 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700257
Naoki Shiota4e928512014-04-03 15:49:35 -0700258 revert-file `basename ${ZK_CONF}`
259 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700260
261 echo "EXIT"
262
263 if [ ! -z "$1" ]; then
264 echo $1
265 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700266}
267
Naoki Shiota590c18d2014-03-31 17:52:59 -0700268function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700269 echo -n "Creating ${ZK_CONF} ... "
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700270
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700271 # Create the ZooKeeper lib directory
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700272 if [[ ! ( -w ${ZK_LIB_DIR} && -d ${ZK_LIB_DIR} ) ]]; then
Naoki Shiota7f495cf2014-05-21 17:23:25 -0700273 local SUDO=${SUDO:-sudo}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700274 local whoami=`whoami`
275 {
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700276 ${SUDO} mkdir -p ${ZK_LIB_DIR}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700277 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
278 } || {
279 echo "FAILED"
280 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
281 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
282 exit 1
283 }
284 fi
285
286 # creation of ZooKeeper config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700287 local temp_zk=`begin-conf-creation ${ZK_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700288
Naoki Shiota4e928512014-04-03 15:49:35 -0700289 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700290
291 local i=1
292 local myid=
293 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700294 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700295 myid=$i
296 break
297 fi
298 i=`expr $i + 1`
299 done
300
301 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700302 local filename=`basename ${ONOS_CONF}`
Naoki Shiota35f8d5c2014-04-08 11:13:18 -0700303 revert-confs "[ERROR] In ${filename}, zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700304 fi
305
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700306 if [ -f "${ZK_MY_ID}" ]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700307 # sudo will be needed if ZK_MY_ID is already created by other (old) script
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700308 local SUDO=${SUDO:-}
309 {
310 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
311 } || {
312 echo "FAILED"
313 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700314 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700315 exit 1
316 }
317 fi
318
Naoki Shiota9df15d32014-03-27 14:26:20 -0700319 echo ${myid} > ${ZK_MY_ID}
320
321 echo -n "myid is assigned to ${myid} ... "
322
323 while read line; do
324 if [[ $line =~ ^__HOSTS__$ ]]; then
325 i=1
326 for host in ${hostarr}; do
327 # TODO: ports might be configurable
328 local hostline="server.${i}=${host}:2888:3888"
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700329 echo $hostline
Naoki Shiota9df15d32014-03-27 14:26:20 -0700330 i=`expr $i + 1`
331 done
332 elif [[ $line =~ __DATADIR__ ]]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700333 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700334 else
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700335 echo $line
Naoki Shiota9df15d32014-03-27 14:26:20 -0700336 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700337 done < ${ZK_CONF_TEMPLATE} > ${temp_zk}
338
339 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700340
341 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700342}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700343
Naoki Shiota4e928512014-04-03 15:49:35 -0700344function create-hazelcast-conf {
345 echo -n "Creating ${HC_CONF} ... "
346
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700347 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700348
349 # To keep indent of XML file, change IFS
350 local IFS=''
351 while read line; do
352 if [[ $line =~ __HC_NETWORK__ ]]; then
353 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
354 # temporary change
355 IFS=' '
356 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
357 echo '<multicast enabled="false" />'
358 echo '<tcp-ip enabled="true">'
359 for member in ${memberarr}; do
360 echo " <member>${member}</member>"
361 done
362 echo '</tcp-ip>'
363 IFS=''
364 else
365 echo '<multicast enabled="true">'
366 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
367 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
368 echo '</multicast>'
369 echo '<tcp-ip enabled="false" />'
370 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700371 elif [[ $line =~ __HC_PORT__ ]]; then
372 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700373 elif [[ $line =~ __HC_CLUSTER__ ]]; then
374 echo $line | sed -e "s|__HC_CLUSTER__|${ONOS_CLUSTER_NAME}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700375 else
376 echo "${line}"
377 fi
378 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
379
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700380 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700381
382 echo "DONE"
383}
384
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700385function create-ramcloud-conf {
386 echo -n "Creating ${RAMCLOUD_CONF} ... "
387
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700388 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700389
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700390 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700391
392 # TODO make ZooKeeper address configurable.
393 echo "ramcloud.locator=zk:localhost:2181" > ${temp_rc}
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700394 echo "#ramcloud.locator=zk:localhost:2181,otherhost:2181" >> ${temp_rc}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700395 echo "ramcloud.clusterName=${rc_cluster_name}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700396
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700397 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700398
399 echo "DONE"
400}
401
Naoki Shiota590c18d2014-03-31 17:52:59 -0700402function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700403 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700404
Naoki Shiota9df15d32014-03-27 14:26:20 -0700405 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700406 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700407
408 sed -e "s|__FILENAME__|${ONOS_LOG}|" \
409 -e "s|__ROLLING_PATTERN__|${ONOS_LOG_ROLLING_PATTERN}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700410
411 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700412
Naoki Shiota9df15d32014-03-27 14:26:20 -0700413 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700414}
415
Naoki Shiota4e928512014-04-03 15:49:35 -0700416function create-confs {
417 local key
418 local filename
419
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700420 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700421
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700422 echo "Config file : ${ONOS_CONF}"
423
Naoki Shiota4e928512014-04-03 15:49:35 -0700424 if [ "$1" == "-f" ]; then
425 create-zk-conf
426 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700427 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700428 create-logback-conf
429 else
430 create-conf-interactive ${ZK_CONF} create-zk-conf
431 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700432 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700433 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
434 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700435
436 trap - ERR
437}
438############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700439
Naoki Shiota590c18d2014-03-31 17:52:59 -0700440
441###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700442function zk {
443 case "$1" in
444 start)
445 start-zk
446 ;;
447 stop)
448 stop-zk
449 ;;
450 stat*) # <- status
451 status-zk
452 ;;
453 re*) # <- restart
454 stop-zk
455 start-zk
456 ;;
457 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700458 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700459 exit 1
460 esac
461}
462
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700463function load-zk-cfg {
464 if [ -f "${ZK_CONF}" ]; then
465 local filename=`basename ${ZK_CONF}`
466 local dirname=`dirname ${ZK_CONF}`
467
468 # Run ZooKeeper with our configuration
469 export ZOOCFG=${filename}
470 export ZOOCFGDIR=${dirname}
471 fi
472}
473
Naoki Shiota4e463182014-03-21 15:13:24 -0700474function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700475 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700476
Naoki Shiota4e928512014-04-03 15:49:35 -0700477 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700478 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700479
480 load-zk-cfg
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700481
482 # log4j.properties is read from classpath if not found in CWD.
483 # Using the ZooKeeper supplied default in ZooKeeper conf dir.
484 # TODO: To explicitly specify our customized log4j config file:
485 # export SERVER_JVMFLAGS="-Dlog4j.configuration=${ZK_LOG4J}"
486 env CLASSPATH="${ZK_HOME}/conf:${CLASSPATH}" ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700487}
488
489function stop-zk {
Yuta HIGUCHI7c708362014-06-02 23:15:13 -0700490 echo -n "Stopping ZooKeeper ... "
491 load-zk-cfg
492
493 ${ZK_HOME}/bin/zkServer.sh stop
Naoki Shiota4e463182014-03-21 15:13:24 -0700494}
495
496function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700497 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700498
Naoki Shiota72209722014-04-08 14:32:17 -0700499 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700500}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700501
502function check-zk {
503 # assumption here is that ZK status script is the last command in status-zk.
504 status-zk &> /dev/null
505 local zk_status=$?
506 if [ "$zk_status" -ne 0 ]; then
507 return 1;
508 fi
509 return 0
510}
511
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700512function check-and-start-zk {
513 check-zk
514 local zk_status=$?
515 if [ "$zk_status" -ne 0 ]; then
516 start-zk
517 fi
518}
519
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700520# wait-zk-or-die {timeout-sec}
521function wait-zk-or-die {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700522 local retries=${1:-5}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700523 # do-while retries >= 0
524 while true; do
525 check-zk
526 local zk_status=$?
527 if [ "$zk_status" -eq 0 ]; then
528 return 0
529 fi
530 sleep 1;
531 ((retries -= 1))
532 (( retries >= 0 )) || break
533 done
534 echo "ZooKeeper is not running."
535 exit 1
536}
537
Naoki Shiota590c18d2014-03-31 17:52:59 -0700538############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700539
540
Naoki Shiota590c18d2014-03-31 17:52:59 -0700541####### Functions related to RAMCloud ######
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -0700542
543# Check if this host should be running RAMCloud coordinator:
544function is-coord-role {
545 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
546 case "${ONOS_HOST_ROLE}" in
547 single-node)
548 echo "true"
549 return 0
550 ;;
551 coord-node)
552 echo "true"
553 return 0
554 ;;
555 server-node)
556 echo "false"
557 return 1
558 ;;
559 coord-and-server-node)
560 echo "true"
561 return 0
562 ;;
563 *)
564 echo "false"
565 return 1
566 ;;
567 esac
568 else
569 echo "false"
570 return 1
571 fi
572}
573
574# Check if this host should be running RAMCloud server:
575function is-server-role {
576 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
577 case "${ONOS_HOST_ROLE}" in
578 single-node)
579 echo "true"
580 return 0
581 ;;
582 coord-node)
583 echo "false"
584 return 1
585 ;;
586 server-node)
587 echo "true"
588 return 0
589 ;;
590 coord-and-server-node)
591 echo "true"
592 return 0
593 ;;
594 *)
595 echo "false"
596 return 1
597 ;;
598 esac
599 else
600 echo "false"
601 return 1
602 fi
603}
604
Naoki Shiota9df15d32014-03-27 14:26:20 -0700605function start-backend {
606 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700607 if [ $1 == "coord" ]; then
608 rc-coord startifdown
609 elif [ $1 == "server" ]; then
610 rc-server startifdown
611 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700612 fi
613}
614
615function stop-backend {
616 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
617 if [ $rcsn != 0 ]; then
618 rc-server stop
619 fi
620
Naoki Shiota590c18d2014-03-31 17:52:59 -0700621 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700622 if [ $rccn != 0 ]; then
623 rc-coord stop
624 fi
625}
626
Naoki Shiota590c18d2014-03-31 17:52:59 -0700627
Naoki Shiota4e463182014-03-21 15:13:24 -0700628### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700629function rc-coord-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700630 local host=${RC_COORD_IP}
631 if [ -z "${host}" ]; then
632 # falling back to 0.0.0.0
633 host="0.0.0.0"
634 fi
635 echo "${RC_COORD_PROTOCOL}:host=${host},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700636}
637
638function rc-server-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700639 local host=${RC_SERVER_IP}
640 if [ -z "${host}" ]; then
641 # falling back to 0.0.0.0
642 host="0.0.0.0"
643 fi
644 echo "${RC_SERVER_PROTOCOL}:host=${host},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700645}
646
Naoki Shiota4e463182014-03-21 15:13:24 -0700647function rc-coord {
648 case "$1" in
649 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700650 stop-coord
651 start-coord
652 ;;
653 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700654 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700655 if [ $n == 0 ]; then
656 start-coord
657 else
658 echo "$n instance of RAMCloud coordinator running"
659 fi
660 ;;
661 stop)
662 stop-coord
663 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700664 deldb)
665 stop-backend
666 del-coord-info
667 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700668 stat*) # <- status
669 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
670 echo "$n RAMCloud coordinator running"
671 ;;
672 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700673 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700674 exit 1
675 esac
676}
677
678function start-coord {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700679 check-and-start-zk
680 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700681
Naoki Shiota4e463182014-03-21 15:13:24 -0700682 if [ ! -d ${LOGDIR} ]; then
683 mkdir -p ${LOGDIR}
684 fi
685 if [ -f $RAMCLOUD_COORD_LOG ]; then
686 rotate-log $RAMCLOUD_COORD_LOG
687 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700688
689 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700690
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700691 # TODO Make ONOS_CONF readable from ONOS process then eliminate RAMCLOUD_CONF
692
693 # Configuration for ZK address, port
694 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
695
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700696 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700697 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700698
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700699 # RAMCloud transport timeout
700 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700701 # RAMCloud option deadServerTimeout
702 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
703 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
704
705 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
706 # but actual RAMCloud code only uses -C argument now.
707 # (FYI: -C is documented to be deprecated in the document)
708
709 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700710 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700711 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700712 coord_args="${coord_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700713 coord_args="${coord_args} --deadServerTimeout ${rc_coord_deadServerTimeout}"
714
715 # Read environment variables if set
716 coord_args="${coord_args} ${RC_COORDINATOR_OPTS}"
717
718 if [ "${ONOS_HOST_ROLE}" == "single-node" ]; then
719 # Note: Following reset is required, since RC restart is considered node failure,
720 # and tries recovery of server, which will never succeed after restart.
721 echo "Role configured to single-node mode. RAMCloud cluster will be reset on each start-up."
722 coord_args="${coord_args} --reset"
723 fi
724
Naoki Shiota4e463182014-03-21 15:13:24 -0700725 # Run ramcloud
726 echo -n "Starting RAMCloud coordinator ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700727 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700728 echo "STARTED"
729}
730
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700731function del-coord-info {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700732 check-and-start-zk
733 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700734
735 if [ ! -d ${LOGDIR} ]; then
736 mkdir -p ${LOGDIR}
737 fi
738 if [ -f $RAMCLOUD_COORD_LOG ]; then
739 rotate-log $RAMCLOUD_COORD_LOG
740 fi
741
742 local coord_addr=`rc-coord-addr`
743
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700744 # Configuration for ZK address, port
745 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700746 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700747 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700748 # RAMCloud option deadServerTimeout
749 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
750 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
751
752 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
753 # but actual RAMCloud code only uses -C argument now.
754 # (FYI: -C is documented to be deprecated in the document)
755
756 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700757 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700758 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
759
760 # Note: --reset will reset ZK stored info and start running as acoordinator.
761 echo -n "Deleting RAMCloud cluster coordination info ... "
762 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} --reset &> $RAMCLOUD_COORD_LOG &
763
Yuta HIGUCHIcd44eb32014-05-24 16:51:31 -0700764 # TODO Assuming 5 sec is enough. To be sure monitor log?
765 sleep 5
766 # kill coordinator
Yuta HIGUCHIce9f3ee2014-05-14 09:48:40 -0700767 (pkill -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator &> /dev/null)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700768
769 echo "DONE"
770}
Naoki Shiota4e463182014-03-21 15:13:24 -0700771
772function stop-coord {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700773 kill-processes "RAMCloud coordinator" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700774}
775
Naoki Shiota4e463182014-03-21 15:13:24 -0700776### Functions related to RAMCloud server
777function rc-server {
778 case "$1" in
779 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700780 stop-server
781 start-server
782 ;;
783 startifdown)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700784 local n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700785 if [ $n == 0 ]; then
786 start-server
787 else
788 echo "$n instance of RAMCloud server running"
789 fi
790 ;;
791 stop)
792 stop-server
793 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700794 deldb)
795 stop-server
796 del-server-backup
797 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700798 stat*) # <- status
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700799 n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700800 echo "$n RAMCloud server running"
801 ;;
802 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700803 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700804 exit 1
805 esac
806}
807
808function start-server {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700809 check-and-start-zk
810 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700811
Naoki Shiota4e463182014-03-21 15:13:24 -0700812 if [ ! -d ${LOGDIR} ]; then
813 mkdir -p ${LOGDIR}
814 fi
815 if [ -f $RAMCLOUD_SERVER_LOG ]; then
816 rotate-log $RAMCLOUD_SERVER_LOG
817 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700818
819 local coord_addr=`rc-coord-addr`
820 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700821
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700822 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.server.masterServiceThreads 5)
823 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.server.logCleanerThreads 1)
Yuta HIGUCHI52efcbb2014-05-16 09:30:33 -0700824 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.server.detectFailures 0)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700825
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700826 # Configuration for ZK address, port
827 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700828 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700829 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700830 # RAMCloud transport timeout
831 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700832 # replication factor (-r) config
833 local rc_replicas=$(read-conf ${ONOS_CONF} ramcloud.server.replicas 0)
834 # backup file path (-f) config
835 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
836 mkdir -p `dirname ${rc_datafile}`
837
838 local server_args="-L ${server_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700839 server_args="${server_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700840 server_args="${server_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700841 server_args="${server_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700842 server_args="${server_args} --masterServiceThreads ${masterServiceThreads}"
843 server_args="${server_args} --logCleanerThreads ${logCleanerThreads}"
844 server_args="${server_args} --detectFailures ${detectFailures}"
845 server_args="${server_args} --replicas ${rc_replicas}"
846 server_args="${server_args} --file ${rc_datafile}"
847
848 # Read environment variables if set
849 server_args="${server_args} ${RC_SERVER_OPTS}"
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700850
Naoki Shiota4e463182014-03-21 15:13:24 -0700851 # Run ramcloud
852 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700853 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server ${server_args} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700854 echo "STARTED"
855}
856
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700857function del-server-backup {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700858 echo -n "Delete RAMCloud backup server data [y/N]? "
859 while [ 1 ]; do
860 read key
861 if [ "${key}" == "Y" -o "${key}" == "y" ]; then
862 break
863 elif [ -z "${key}" -o "${key}" == "N" -o "${key}" == "n" ]; then
864 echo "Cancelled."
865 return
866 fi
867 echo "[y/N]? "
868 done
869
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700870 echo -n "Removing RAMCloud backup server data ... "
871 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
872 rm -f ${rc_datafile}
873 echo "DONE"
874}
875
Naoki Shiota4e463182014-03-21 15:13:24 -0700876function stop-server {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700877 kill-processes "RAMCloud server" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700878}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700879############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700880
881
Naoki Shiota590c18d2014-03-31 17:52:59 -0700882## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700883function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700884 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700885 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700886 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700887 ${MVN} -f ${ONOS_HOME}/pom.xml compile
888 fi
889 JAVA_CP=`cat ${CPFILE}`
890 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
891
892 case "$1" in
893 start)
894 stop-onos
895 start-onos
896 ;;
897 startnokill)
898 start-onos
899 ;;
900 startifdown)
901 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
902 if [ $n == 0 ]; then
903 start-onos
904 else
905 echo "$n instance of onos running"
906 fi
907 ;;
908 stop)
909 stop-onos
910 ;;
911 stat*) # <- status
912 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
913 echo "$n instance of onos running"
914 ;;
915 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700916 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700917 exit 1
918 esac
919}
920
921function start-onos {
922 if [ ! -d ${LOGDIR} ]; then
923 mkdir -p ${LOGDIR}
924 fi
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700925 # Rotate log files
Naoki Shiota4e463182014-03-21 15:13:24 -0700926 for log in ${LOGS}; do
927 if [ -f ${log} ]; then
928 rotate-log ${log}
929 fi
930 done
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700931
932 # Rotate logs rolled at runtime.
933 local rolled_log_shellpat=`echo ${ONOS_LOG_ROLLING_PATTERN} | sed -e "s/%i/\\*/"`
934 for rolled_log in ${rolled_log_shellpat}; do
935 if [ -f ${rolled_log} ]; then
936 rotate-log ${rolled_log}
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700937 fi
938 done
939
Naoki Shiota4e463182014-03-21 15:13:24 -0700940 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700941 echo "[WARNING] ${ONOS_LOGBACK} not found."
942 echo " Run \"\$ $0 setup\" to create."
943 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700944 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700945
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700946 if [ ! -f ${HC_CONF} ]; then
947 echo "[WARNING] ${HC_CONF} not found."
948 echo " Run \"\$ $0 setup\" to create."
949 exit 1
950 fi
951
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700952 # specify ZooKeeper(curator) namespace
953 JVM_OPTS="${JVM_OPTS} -Dzookeeper.namespace=${ONOS_CLUSTER_NAME}"
954
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700955 # specify hazelcast.xml to datagrid
956 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
957
958 # specify backend config
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700959 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700960 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
961 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700962 elif [ "${ONOS_HOST_BACKEND}" = "hazelcast" ]; then
963 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.hazelcast.baseConfig=${HC_CONF}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700964 fi
965
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -0700966 # check for jvm debug flag
967 if [ "${ONOS_DEBUG}" = "true" ]; then
968 JVM_OPTS="${JVM_OPTS} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${JVM_DEBUG_PORT}"
969 fi
970
Naoki Shiota9df15d32014-03-27 14:26:20 -0700971 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -0700972
973 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
974 cd ${ONOS_HOME}
975
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700976 echo -n "Starting ONOS controller ... "
Naoki Shiota4e463182014-03-21 15:13:24 -0700977 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
978
979 # We need to wait a bit to find out whether starting the ONOS process succeeded
980 sleep 1
981
982 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700983 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700984 echo " STARTED"
985 else
986 echo " FAILED"
987 fi
988
989# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
990# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
991}
992
993function stop-onos {
994 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
995# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
996}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700997############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700998
999
Naoki Shiota590c18d2014-03-31 17:52:59 -07001000################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -07001001case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -07001002 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -07001003 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -07001004 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001005 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -07001006 mode_parameter=${ONOS_HOST_ROLE}
1007 if [ ! -z "$2" ]; then
1008 mode_parameter=$2
1009 fi
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -07001010
Naoki Shiota9df15d32014-03-27 14:26:20 -07001011 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -07001012 single-node)
1013 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001014 start-backend coord
1015 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001016 onos startifdown
1017 ;;
1018 coord-node)
1019 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001020 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -07001021 onos startifdown
1022 ;;
1023 server-node)
1024 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001025 start-backend server
1026 onos startifdown
1027 ;;
1028 coord-and-server-node)
1029 zk start
1030 start-backend coord
1031 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001032 onos startifdown
1033 ;;
1034 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001035 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001036 ;;
1037 esac
1038 echo
1039 ;;
1040 stop)
1041 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1042 if [ $on != 0 ]; then
1043 onos stop
1044 fi
1045
Naoki Shiota9df15d32014-03-27 14:26:20 -07001046 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -07001047
1048 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1049 if [ $zkn != 0 ]; then
1050 zk stop
1051 fi
1052 echo
1053 ;;
1054 restart)
1055 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1056 if [ $on != 0 ]; then
1057 onos stop
1058 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001059
Naoki Shiota4e463182014-03-21 15:13:24 -07001060 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
1061 if [ $rcsn != 0 ]; then
1062 rc-server stop
1063 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001064
Naoki Shiota590c18d2014-03-31 17:52:59 -07001065 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -07001066 if [ $rccn != 0 ]; then
1067 rc-coord stop
1068 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001069
Naoki Shiota4e463182014-03-21 15:13:24 -07001070 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1071 if [ $zkn != 0 ]; then
1072 zk restart
1073 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001074
Naoki Shiota4e463182014-03-21 15:13:24 -07001075 if [ $rccn != 0 ]; then
1076 rc-coord startifdown
1077 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001078
Naoki Shiota4e463182014-03-21 15:13:24 -07001079 if [ $rcsn != 0 ]; then
1080 rc-server startifdown
1081 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001082
Naoki Shiota4e463182014-03-21 15:13:24 -07001083 if [ $on != 0 ]; then
1084 onos startifdown
1085 fi
1086 echo
1087 ;;
1088 stat*) # <- status
1089 echo '[ZooKeeper]'
1090 zk status
1091 echo
1092 echo '[RAMCloud coordinator]'
1093 rc-coord status
1094 echo
1095 echo '[RAMCloud server]'
1096 rc-server status
1097 echo
1098 echo '[ONOS core]'
1099 onos status
1100 echo
1101 ;;
1102 zk)
1103 zk $2
1104 ;;
1105 rc-c*) # <- rc-coordinator
1106 rc-coord $2
1107 ;;
1108 rc-s*) # <- rc-server
1109 rc-server $2
1110 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001111 rc)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001112 # XXX rc command ignores if the host is not configured to have those roles,
1113 # while rc-s or rc-c executes the task regardless of role definition.
1114 # This is a workaround since TestON does not take role in account
1115 # and always issue rc deldb right now.
1116
Naoki Shiota9109a1e2014-05-13 11:11:01 -07001117 # TODO make deldb command more organized (clarify when it can work)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001118 if [ "`is-coord-role`" == "true" ]; then
1119 rc-coord $2
1120 fi
1121 if [ "`is-server-role`" == "true" ]; then
1122 rc-server $2
1123 fi
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001124 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001125 core)
1126 onos $2
1127 ;;
1128 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001129 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001130 exit 1
1131esac
Naoki Shiota590c18d2014-03-31 17:52:59 -07001132