blob: 1c89b4f9f1ec7d2f0a1613dcde3f36e32e247d8a [file] [log] [blame]
Naoki Shiota4e463182014-03-21 15:13:24 -07001#!/bin/bash
Naoki Shiota4e463182014-03-21 15:13:24 -07002
Naoki Shiota590c18d2014-03-31 17:52:59 -07003### Env vars used by this script. (default value) ###
4# $ONOS_HOME : path of root directory of ONOS repository (this script's dir)
5# $ONOS_CONF_DIR : path of ONOS config directory (~/ONOS/conf)
Naoki Shiota9a1e6d12014-04-03 14:47:12 -07006# $ONOS_CONF : path of ONOS node config file (~/ONOS/conf/onos_node.`hostname`.conf or onos_node.conf)
Naoki Shiota590c18d2014-03-31 17:52:59 -07007# $ONOS_PROPS : path of ONOS properties file (~/ONOS/conf/onos.properties)
8# $ONOS_LOGBACK : path of logback config file (~/ONOS/conf/logback.`hostname`.xml)
Naoki Shiotad8ea71a2014-04-23 17:04:51 -07009# $ONOS_LOGDIR : path of log output directory (~/ONOS/onos-logs)
10# $ONOS_LOGBASE : base name of log output file (onos.`hostname`)
Naoki Shiota590c18d2014-03-31 17:52:59 -070011# $RAMCLOUD_HOME : path of root directory of RAMCloud repository (~/ramcloud)
12# $RAMCLOUD_BRANCH : branch name of RAMCloud to use (master)
Yuta HIGUCHIfb1905a2014-06-09 14:07:34 -070013# $ZK_HOME : path of root directory of ZooKeeper (~/zookeeper-3.4.6)
Naoki Shiota72209722014-04-08 14:32:17 -070014# $ZK_LIB_DIR : path of ZooKeeper library (/var/lib/zookeeper)
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070015# $ZK_LOG_DIR : path of ZooKeeper log output directory (~/ONOS/onos-logs/zk-`hostname`)
Naoki Shiota590c18d2014-03-31 17:52:59 -070016# $JVM_OPTS : JVM options ONOS starts with
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070017# $ZK_CONF : path of ZooKeeper config file (~/ONOS/conf/zoo.cfg)
18# $HC_CONF : path of Hazelcast config file (~/ONOS/conf/hazelcast.xml)
19# $RAMCLOUD_CONF : path of RAMCloud config file (~/ONOS/conf/ramcloud.conf)
Naoki Shiota590c18d2014-03-31 17:52:59 -070020#####################################################
21
Naoki Shiota590c18d2014-03-31 17:52:59 -070022ONOS_HOME=${ONOS_HOME:-$(cd `dirname $0`; pwd)}
Naoki Shiota9df15d32014-03-27 14:26:20 -070023ONOS_CONF_DIR=${ONOS_CONF_DIR:-${ONOS_HOME}/conf}
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070024ONOS_CONF=${ONOS_CONF:-${ONOS_CONF_DIR}/onos_node.`hostname`.conf}
Naoki Shiota590c18d2014-03-31 17:52:59 -070025
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070026source ${ONOS_HOME}/scripts/common/utils.sh
27
Yuta HIGUCHIe748ecc2014-05-28 13:55:03 -070028confirm-if-root
29
Naoki Shiota590c18d2014-03-31 17:52:59 -070030if [ ! -f ${ONOS_CONF} ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070031 # falling back to default config file
32 ONOS_CONF=${ONOS_CONF_DIR}/onos_node.conf
33 if [ ! -f ${ONOS_CONF} ]; then
34 echo "${ONOS_CONF} not found."
35 exit 1
36 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -070037fi
Naoki Shiota4e928512014-04-03 15:49:35 -070038
Naoki Shiota4e928512014-04-03 15:49:35 -070039### Variables read from ONOS config file ###
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -070040ONOS_CLUSTER_NAME=$(read-conf ${ONOS_CONF} onos.cluster.name "onos")
Naoki Shiota4e928512014-04-03 15:49:35 -070041ONOS_HOST_NAME=$(read-conf ${ONOS_CONF} host.name `hostname`)
42ONOS_HOST_IP=$(read-conf ${ONOS_CONF} host.ip)
43ONOS_HOST_ROLE=$(read-conf ${ONOS_CONF} host.role)
44ONOS_HOST_BACKEND=$(read-conf ${ONOS_CONF} host.backend)
45ZK_HOSTS=$(read-conf ${ONOS_CONF} zookeeper.hosts ${ONOS_HOST_NAME})
46RC_COORD_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.coordinator.protocol "fast+udp")
47RC_COORD_IP=$(read-conf ${ONOS_CONF} ramcloud.coordinator.ip ${ONOS_HOST_IP})
48RC_COORD_PORT=$(read-conf ${ONOS_CONF} ramcloud.coordinator.port 12246)
49RC_SERVER_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.server.protocol "fast+udp")
50RC_SERVER_IP=$(read-conf ${ONOS_CONF} ramcloud.server.ip ${ONOS_HOST_IP})
51RC_SERVER_PORT=$(read-conf ${ONOS_CONF} ramcloud.server.port 12242)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070052HC_HOST_PORT=$(read-conf ${ONOS_CONF} hazelcast.host.port 5701)
Naoki Shiota4e928512014-04-03 15:49:35 -070053HC_TCPIP_MEMBERS=$(read-conf ${ONOS_CONF} hazelcast.tcp-ip.members)
54HC_MULTICAST_GROUP=$(read-conf ${ONOS_CONF} hazelcast.multicast.group "224.2.2.3")
55HC_MULTICAST_PORT=$(read-conf ${ONOS_CONF} hazelcast.multicast.port 54327)
Naoki Shiota590c18d2014-03-31 17:52:59 -070056############################################
57
58
59############## Other variables #############
Naoki Shiota4e928512014-04-03 15:49:35 -070060ONOS_TEMPLATE_DIR=${ONOS_CONF_DIR}/template
61
Naoki Shiota4e463182014-03-21 15:13:24 -070062LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
63
Yuta HIGUCHIfb1905a2014-06-09 14:07:34 -070064ZK_HOME=${ZK_HOME:-~/zookeeper-3.4.6}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070065ZK_CONF=${ZK_CONF:-${ONOS_CONF_DIR}/zoo.cfg}
Naoki Shiota4e928512014-04-03 15:49:35 -070066ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070067# Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070068ZK_LOG_DIR=${ZK_LOG_DIR:-${ONOS_HOME}/onos-logs/zk-${ONOS_HOST_NAME}}
Naoki Shiota72209722014-04-08 14:32:17 -070069ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070070ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070071
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070072HC_CONF=${HC_CONF:-${ONOS_CONF_DIR}/hazelcast.xml}
Naoki Shiota4e928512014-04-03 15:49:35 -070073HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
74
Naoki Shiota4e463182014-03-21 15:13:24 -070075RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070076RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
77RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070078RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070079RAMCLOUD_CONF=${RAMCLOUD_CONF:-${ONOS_CONF_DIR}/ramcloud.conf}
Naoki Shiota4e463182014-03-21 15:13:24 -070080
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070081export LD_LIBRARY_PATH=${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}:$LD_LIBRARY_PATH
Naoki Shiota4e463182014-03-21 15:13:24 -070082
83## 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 -070084ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070085ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070086ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070087LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070088LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070089ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070090ONOS_LOG_ROLLING_PATTERN="${LOGDIR}/${LOGBASE}.%i.log.gz"
91ONOS_STDOUT_LOG="${LOGDIR}/${LOGBASE}.stdout"
92ONOS_STDERR_LOG="${LOGDIR}/${LOGBASE}.stderr"
Naoki Shiota4e463182014-03-21 15:13:24 -070093PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070094LOGS="$ONOS_LOG $ONOS_STDOUT_LOG $ONOS_STDERR_LOG $PCAP_LOG"
Naoki Shiota4e463182014-03-21 15:13:24 -070095
Naoki Shiota9df15d32014-03-27 14:26:20 -070096ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -070097JMX_PORT=${JMX_PORT:-7189}
98
99# Set JVM options
100JVM_OPTS="${JVM_OPTS:-}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700101JVM_OPTS="$JVM_OPTS -server -d64"
102#JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700103
104# Uncomment or specify appropriate value as JVM_OPTS environment variables.
105#JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
Naoki Shiota4e463182014-03-21 15:13:24 -0700106#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
107#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700108
109#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC"
110JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
111JVM_OPTS="$JVM_OPTS -XX:+AggressiveOpts"
112
113# We may want to remove UseFastAccessorMethods option: http://bugs.java.com/view_bug.do?bug_id=6385687
114JVM_OPTS="$JVM_OPTS -XX:+UseFastAccessorMethods"
115
116JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192"
117JVM_OPTS="$JVM_OPTS -XX:FreqInlineSize=8192"
118JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500"
119
Naoki Shiota4e463182014-03-21 15:13:24 -0700120JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700121
122# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
123JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
124
125JVM_OPTS="$JVM_OPTS -XX:+UseCompressedOops"
126
127JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
128JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
129JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
130
Naoki Shiota4e463182014-03-21 15:13:24 -0700131JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
132
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700133# Uncomment to dump final JVM flags to stdout
134#JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
135
Naoki Shiota4e463182014-03-21 15:13:24 -0700136# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700137MAIN_CLASS="net.onrc.onos.core.main.Main"
Naoki Shiota4e463182014-03-21 15:13:24 -0700138
139MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700140############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700141
Naoki Shiota9df15d32014-03-27 14:26:20 -0700142
Naoki Shiota590c18d2014-03-31 17:52:59 -0700143############# Common functions #############
144function print_usage {
Naoki Shiota05721b32014-04-29 14:41:12 -0700145 local scriptname=`basename $0`
Naoki Shiota590c18d2014-03-31 17:52:59 -0700146 local filename=`basename ${ONOS_CONF}`
147 local usage="Usage: setup/start/stop ONOS on this server.
Naoki Shiota05721b32014-04-29 14:41:12 -0700148 \$ ${scriptname} setup [-f]
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700149 Set up ONOS node using ${ONOS_CONF} .
Naoki Shiota590c18d2014-03-31 17:52:59 -0700150 - generate and replace config file of ZooKeeper.
151 - create myid in ZooKeeper datadir.
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700152 - generate and replace config file for Hazelcast.
153 - generate and replace config file for RAMCloud.
Naoki Shiota590c18d2014-03-31 17:52:59 -0700154 - generate and replace logback.${ONOS_HOST_NAME}.xml
155 If -f option is used, all existing files will be overwritten without confirmation.
Naoki Shiota05721b32014-04-29 14:41:12 -0700156 \$ ${scriptname} start [single-node|coord-node|server-node|coord-and-server-node]
Naoki Shiota590c18d2014-03-31 17:52:59 -0700157 Start ONOS node with specific RAMCloud entities
158 - single-node: start ONOS with stand-alone RAMCloud
159 - coord-node : start ONOS with RAMCloud coordinator
160 - server-node: start ONOS with RAMCloud server
161 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
162 * Default behavior can be defined by ${filename}
Naoki Shiota05721b32014-04-29 14:41:12 -0700163 \$ ${scriptname} stop
Naoki Shiota590c18d2014-03-31 17:52:59 -0700164 Stop all ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700165 \$ ${scriptname} restart
Naoki Shiota590c18d2014-03-31 17:52:59 -0700166 Stop and start currently running ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700167 \$ ${scriptname} status
Naoki Shiota590c18d2014-03-31 17:52:59 -0700168 Show status of ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700169 \$ ${scriptname} {zk|rc-coord|rc-server|core} {start|stop|restart|status}
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700170 Control specific ONOS-related process
171 \$ ${scriptname} rc deldb
172 Delete data in RAMCloud"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700173
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700174 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700175}
176
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700177# rotate-log [log-filename] [max rotations]
178# Example:
179# foobar.log -> foobar.log.1
180# foobar.log.1 -> foobar.log.2
181# foobar.log.gz -> foobar.log.1.gz
Naoki Shiota4e463182014-03-21 15:13:24 -0700182function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700183 local logfile=$1
184 local nr_max=${2:-10}
185 if [ -f $logfile ]; then
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700186 # TODO treating only .gz now. probably want more generic solution
187 local basename=${logfile%%.gz}
188 local append=""
189 if [ "$basename" != "$logfile" ]; then
190 append=".gz"
191 fi
Naoki Shiota4e928512014-04-03 15:49:35 -0700192 for i in `seq $(expr $nr_max - 1) -1 1`; do
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700193 if [ -f ${basename}.${i}${append} ]; then
194 mv -f ${basename}.${i}${append} ${basename}.`expr $i + 1`${append}
Naoki Shiota4e928512014-04-03 15:49:35 -0700195 fi
196 done
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700197 mv ${basename}${append} ${basename}.1${append}
Naoki Shiota4e928512014-04-03 15:49:35 -0700198 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700199}
200
201# kill-processes {module-name} {array of pids}
202function kill-processes {
203 # Kill the existing processes
204 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700205 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700206 echo -n "Stopping $1 ... "
207 fi
208 for p in ${pids}; do
209 if [ x$p != "x" ]; then
Naoki Shiota4b355762014-05-27 11:46:43 -0700210 # Check if target process is accesible from current user
211 kill -0 $p
212 if [ "$?" -ne 0 ]; then
213 # Error exit code here means "failed to send signal".
214 # Supposedly because of permission error.
215 echo "Failed to kill process (pid: $p)."
216 echo "Check if current user is the same as process owner."
217 exit 1
218 fi
219
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700220 (
221 # Ask process with SIGTERM first, if that did not kill the process
222 # wait 1s and if process still exist, force process to be killed.
223 kill -TERM $p && kill -0 $p && sleep 1 && kill -0 $p && kill -KILL $p
224 ) 2> /dev/null
Naoki Shiota4b355762014-05-27 11:46:43 -0700225
226 # Ensure process is killed.
227 kill -0 $p 2> /dev/null
228 if [ "$?" -ne 0 ]; then
229 # Error exit code here means "process not found", i.e. "kill succeeded".
230 echo "Killed existing process (pid: $p)"
231 else
232 # Process still exists. Some unexpected error occurs.
233 echo "Failed to kill process (pid: $p)."
234 echo "Unexpected error occurs during process termination."
235 exit 1
236 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700237 fi
238 done
239}
240
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700241function handle-error {
242 set -e
243
244 revert-confs
245
246 set +e
247
248 exit 1
249}
250
Naoki Shiota4e928512014-04-03 15:49:35 -0700251# revert-confs [error message]
252function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700253 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700254
Naoki Shiota4e928512014-04-03 15:49:35 -0700255 revert-file `basename ${ZK_CONF}`
256 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700257
258 echo "EXIT"
259
260 if [ ! -z "$1" ]; then
261 echo $1
262 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700263}
264
Naoki Shiota590c18d2014-03-31 17:52:59 -0700265function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700266 echo -n "Creating ${ZK_CONF} ... "
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700267
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700268 # Create the ZooKeeper lib directory
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700269 if [[ ! ( -w ${ZK_LIB_DIR} && -d ${ZK_LIB_DIR} ) ]]; then
Naoki Shiota7f495cf2014-05-21 17:23:25 -0700270 local SUDO=${SUDO:-sudo}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700271 local whoami=`whoami`
272 {
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700273 ${SUDO} mkdir -p ${ZK_LIB_DIR}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700274 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
275 } || {
276 echo "FAILED"
277 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
278 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
279 exit 1
280 }
281 fi
282
283 # creation of ZooKeeper config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700284 local temp_zk=`begin-conf-creation ${ZK_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700285
Naoki Shiota4e928512014-04-03 15:49:35 -0700286 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700287
288 local i=1
289 local myid=
290 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700291 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700292 myid=$i
293 break
294 fi
295 i=`expr $i + 1`
296 done
297
298 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700299 local filename=`basename ${ONOS_CONF}`
Naoki Shiota35f8d5c2014-04-08 11:13:18 -0700300 revert-confs "[ERROR] In ${filename}, zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700301 fi
302
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700303 if [ -f "${ZK_MY_ID}" ]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700304 # sudo will be needed if ZK_MY_ID is already created by other (old) script
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700305 local SUDO=${SUDO:-}
306 {
307 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
308 } || {
309 echo "FAILED"
310 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700311 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700312 exit 1
313 }
314 fi
315
Naoki Shiota9df15d32014-03-27 14:26:20 -0700316 echo ${myid} > ${ZK_MY_ID}
317
318 echo -n "myid is assigned to ${myid} ... "
319
320 while read line; do
321 if [[ $line =~ ^__HOSTS__$ ]]; then
322 i=1
323 for host in ${hostarr}; do
324 # TODO: ports might be configurable
325 local hostline="server.${i}=${host}:2888:3888"
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700326 echo $hostline
Naoki Shiota9df15d32014-03-27 14:26:20 -0700327 i=`expr $i + 1`
328 done
329 elif [[ $line =~ __DATADIR__ ]]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700330 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700331 else
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700332 echo $line
Naoki Shiota9df15d32014-03-27 14:26:20 -0700333 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700334 done < ${ZK_CONF_TEMPLATE} > ${temp_zk}
335
336 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700337
338 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700339}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700340
Naoki Shiota4e928512014-04-03 15:49:35 -0700341function create-hazelcast-conf {
342 echo -n "Creating ${HC_CONF} ... "
343
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700344 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700345
346 # To keep indent of XML file, change IFS
347 local IFS=''
348 while read line; do
349 if [[ $line =~ __HC_NETWORK__ ]]; then
350 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
351 # temporary change
352 IFS=' '
353 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
354 echo '<multicast enabled="false" />'
355 echo '<tcp-ip enabled="true">'
356 for member in ${memberarr}; do
357 echo " <member>${member}</member>"
358 done
359 echo '</tcp-ip>'
360 IFS=''
361 else
362 echo '<multicast enabled="true">'
363 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
364 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
365 echo '</multicast>'
366 echo '<tcp-ip enabled="false" />'
367 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700368 elif [[ $line =~ __HC_PORT__ ]]; then
369 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700370 elif [[ $line =~ __HC_CLUSTER__ ]]; then
371 echo $line | sed -e "s|__HC_CLUSTER__|${ONOS_CLUSTER_NAME}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700372 else
373 echo "${line}"
374 fi
375 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
376
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700377 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700378
379 echo "DONE"
380}
381
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700382function create-ramcloud-conf {
383 echo -n "Creating ${RAMCLOUD_CONF} ... "
384
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700385 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700386
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700387 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700388
389 # TODO make ZooKeeper address configurable.
390 echo "ramcloud.locator=zk:localhost:2181" > ${temp_rc}
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700391 echo "#ramcloud.locator=zk:localhost:2181,otherhost:2181" >> ${temp_rc}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700392 echo "ramcloud.clusterName=${rc_cluster_name}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700393
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700394 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700395
396 echo "DONE"
397}
398
Naoki Shiota590c18d2014-03-31 17:52:59 -0700399function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700400 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700401
Naoki Shiota9df15d32014-03-27 14:26:20 -0700402 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700403 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700404
405 sed -e "s|__FILENAME__|${ONOS_LOG}|" \
406 -e "s|__ROLLING_PATTERN__|${ONOS_LOG_ROLLING_PATTERN}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700407
408 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700409
Naoki Shiota9df15d32014-03-27 14:26:20 -0700410 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700411}
412
Naoki Shiota4e928512014-04-03 15:49:35 -0700413function create-confs {
414 local key
415 local filename
416
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700417 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700418
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700419 echo "Config file : ${ONOS_CONF}"
420
Naoki Shiota4e928512014-04-03 15:49:35 -0700421 if [ "$1" == "-f" ]; then
422 create-zk-conf
423 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700424 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700425 create-logback-conf
426 else
427 create-conf-interactive ${ZK_CONF} create-zk-conf
428 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700429 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700430 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
431 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700432
433 trap - ERR
434}
435############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700436
Naoki Shiota590c18d2014-03-31 17:52:59 -0700437
438###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700439function zk {
440 case "$1" in
441 start)
442 start-zk
443 ;;
444 stop)
445 stop-zk
446 ;;
447 stat*) # <- status
448 status-zk
449 ;;
450 re*) # <- restart
451 stop-zk
452 start-zk
453 ;;
454 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700455 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700456 exit 1
457 esac
458}
459
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700460function load-zk-cfg {
461 if [ -f "${ZK_CONF}" ]; then
462 local filename=`basename ${ZK_CONF}`
463 local dirname=`dirname ${ZK_CONF}`
464
465 # Run ZooKeeper with our configuration
466 export ZOOCFG=${filename}
467 export ZOOCFGDIR=${dirname}
468 fi
469}
470
Naoki Shiota4e463182014-03-21 15:13:24 -0700471function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700472 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700473
Naoki Shiota4e928512014-04-03 15:49:35 -0700474 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700475 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700476
477 load-zk-cfg
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700478
479 # log4j.properties is read from classpath if not found in CWD.
480 # Using the ZooKeeper supplied default in ZooKeeper conf dir.
481 # TODO: To explicitly specify our customized log4j config file:
482 # export SERVER_JVMFLAGS="-Dlog4j.configuration=${ZK_LOG4J}"
483 env CLASSPATH="${ZK_HOME}/conf:${CLASSPATH}" ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700484}
485
486function stop-zk {
Yuta HIGUCHI7c708362014-06-02 23:15:13 -0700487 echo -n "Stopping ZooKeeper ... "
488 load-zk-cfg
489
490 ${ZK_HOME}/bin/zkServer.sh stop
Naoki Shiota4e463182014-03-21 15:13:24 -0700491}
492
493function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700494 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700495
Naoki Shiota72209722014-04-08 14:32:17 -0700496 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700497}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700498
499function check-zk {
500 # assumption here is that ZK status script is the last command in status-zk.
501 status-zk &> /dev/null
502 local zk_status=$?
503 if [ "$zk_status" -ne 0 ]; then
504 return 1;
505 fi
506 return 0
507}
508
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700509function check-and-start-zk {
510 check-zk
511 local zk_status=$?
512 if [ "$zk_status" -ne 0 ]; then
513 start-zk
514 fi
515}
516
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700517# wait-zk-or-die {timeout-sec}
518function wait-zk-or-die {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700519 local retries=${1:-5}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700520 # do-while retries >= 0
521 while true; do
522 check-zk
523 local zk_status=$?
524 if [ "$zk_status" -eq 0 ]; then
525 return 0
526 fi
527 sleep 1;
528 ((retries -= 1))
529 (( retries >= 0 )) || break
530 done
531 echo "ZooKeeper is not running."
532 exit 1
533}
534
Naoki Shiota590c18d2014-03-31 17:52:59 -0700535############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700536
537
Naoki Shiota590c18d2014-03-31 17:52:59 -0700538####### Functions related to RAMCloud ######
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -0700539
540# Check if this host should be running RAMCloud coordinator:
541function is-coord-role {
542 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
543 case "${ONOS_HOST_ROLE}" in
544 single-node)
545 echo "true"
546 return 0
547 ;;
548 coord-node)
549 echo "true"
550 return 0
551 ;;
552 server-node)
553 echo "false"
554 return 1
555 ;;
556 coord-and-server-node)
557 echo "true"
558 return 0
559 ;;
560 *)
561 echo "false"
562 return 1
563 ;;
564 esac
565 else
566 echo "false"
567 return 1
568 fi
569}
570
571# Check if this host should be running RAMCloud server:
572function is-server-role {
573 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
574 case "${ONOS_HOST_ROLE}" in
575 single-node)
576 echo "true"
577 return 0
578 ;;
579 coord-node)
580 echo "false"
581 return 1
582 ;;
583 server-node)
584 echo "true"
585 return 0
586 ;;
587 coord-and-server-node)
588 echo "true"
589 return 0
590 ;;
591 *)
592 echo "false"
593 return 1
594 ;;
595 esac
596 else
597 echo "false"
598 return 1
599 fi
600}
601
Naoki Shiota9df15d32014-03-27 14:26:20 -0700602function start-backend {
603 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700604 if [ $1 == "coord" ]; then
605 rc-coord startifdown
606 elif [ $1 == "server" ]; then
607 rc-server startifdown
608 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700609 fi
610}
611
612function stop-backend {
613 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
614 if [ $rcsn != 0 ]; then
615 rc-server stop
616 fi
617
Naoki Shiota590c18d2014-03-31 17:52:59 -0700618 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700619 if [ $rccn != 0 ]; then
620 rc-coord stop
621 fi
622}
623
Naoki Shiota590c18d2014-03-31 17:52:59 -0700624
Naoki Shiota4e463182014-03-21 15:13:24 -0700625### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700626function rc-coord-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700627 local host=${RC_COORD_IP}
628 if [ -z "${host}" ]; then
629 # falling back to 0.0.0.0
630 host="0.0.0.0"
631 fi
632 echo "${RC_COORD_PROTOCOL}:host=${host},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700633}
634
635function rc-server-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700636 local host=${RC_SERVER_IP}
637 if [ -z "${host}" ]; then
638 # falling back to 0.0.0.0
639 host="0.0.0.0"
640 fi
641 echo "${RC_SERVER_PROTOCOL}:host=${host},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700642}
643
Naoki Shiota4e463182014-03-21 15:13:24 -0700644function rc-coord {
645 case "$1" in
646 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700647 stop-coord
648 start-coord
649 ;;
650 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700651 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700652 if [ $n == 0 ]; then
653 start-coord
654 else
655 echo "$n instance of RAMCloud coordinator running"
656 fi
657 ;;
658 stop)
659 stop-coord
660 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700661 deldb)
662 stop-backend
663 del-coord-info
664 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700665 stat*) # <- status
666 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
667 echo "$n RAMCloud coordinator running"
668 ;;
669 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700670 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700671 exit 1
672 esac
673}
674
675function start-coord {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700676 check-and-start-zk
677 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700678
Naoki Shiota4e463182014-03-21 15:13:24 -0700679 if [ ! -d ${LOGDIR} ]; then
680 mkdir -p ${LOGDIR}
681 fi
682 if [ -f $RAMCLOUD_COORD_LOG ]; then
683 rotate-log $RAMCLOUD_COORD_LOG
684 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700685
686 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700687
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700688 # TODO Make ONOS_CONF readable from ONOS process then eliminate RAMCLOUD_CONF
689
690 # Configuration for ZK address, port
691 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
692
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700693 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700694 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700695
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700696 # RAMCloud transport timeout
697 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700698 # RAMCloud option deadServerTimeout
699 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
700 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
701
702 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
703 # but actual RAMCloud code only uses -C argument now.
704 # (FYI: -C is documented to be deprecated in the document)
705
706 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700707 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700708 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700709 coord_args="${coord_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700710 coord_args="${coord_args} --deadServerTimeout ${rc_coord_deadServerTimeout}"
711
712 # Read environment variables if set
713 coord_args="${coord_args} ${RC_COORDINATOR_OPTS}"
714
715 if [ "${ONOS_HOST_ROLE}" == "single-node" ]; then
716 # Note: Following reset is required, since RC restart is considered node failure,
717 # and tries recovery of server, which will never succeed after restart.
718 echo "Role configured to single-node mode. RAMCloud cluster will be reset on each start-up."
719 coord_args="${coord_args} --reset"
720 fi
721
Naoki Shiota4e463182014-03-21 15:13:24 -0700722 # Run ramcloud
723 echo -n "Starting RAMCloud coordinator ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700724 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700725 echo "STARTED"
726}
727
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700728function del-coord-info {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700729 check-and-start-zk
730 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700731
732 if [ ! -d ${LOGDIR} ]; then
733 mkdir -p ${LOGDIR}
734 fi
735 if [ -f $RAMCLOUD_COORD_LOG ]; then
736 rotate-log $RAMCLOUD_COORD_LOG
737 fi
738
739 local coord_addr=`rc-coord-addr`
740
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700741 # Configuration for ZK address, port
742 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700743 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700744 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700745 # RAMCloud option deadServerTimeout
746 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
747 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
748
749 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
750 # but actual RAMCloud code only uses -C argument now.
751 # (FYI: -C is documented to be deprecated in the document)
752
753 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700754 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700755 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
756
757 # Note: --reset will reset ZK stored info and start running as acoordinator.
758 echo -n "Deleting RAMCloud cluster coordination info ... "
759 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} --reset &> $RAMCLOUD_COORD_LOG &
760
Yuta HIGUCHIcd44eb32014-05-24 16:51:31 -0700761 # TODO Assuming 5 sec is enough. To be sure monitor log?
762 sleep 5
763 # kill coordinator
Yuta HIGUCHIce9f3ee2014-05-14 09:48:40 -0700764 (pkill -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator &> /dev/null)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700765
766 echo "DONE"
767}
Naoki Shiota4e463182014-03-21 15:13:24 -0700768
769function stop-coord {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700770 kill-processes "RAMCloud coordinator" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700771}
772
Naoki Shiota4e463182014-03-21 15:13:24 -0700773### Functions related to RAMCloud server
774function rc-server {
775 case "$1" in
776 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700777 stop-server
778 start-server
779 ;;
780 startifdown)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700781 local n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700782 if [ $n == 0 ]; then
783 start-server
784 else
785 echo "$n instance of RAMCloud server running"
786 fi
787 ;;
788 stop)
789 stop-server
790 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700791 deldb)
792 stop-server
793 del-server-backup
794 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700795 stat*) # <- status
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700796 n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700797 echo "$n RAMCloud server running"
798 ;;
799 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700800 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700801 exit 1
802 esac
803}
804
805function start-server {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700806 check-and-start-zk
807 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700808
Naoki Shiota4e463182014-03-21 15:13:24 -0700809 if [ ! -d ${LOGDIR} ]; then
810 mkdir -p ${LOGDIR}
811 fi
812 if [ -f $RAMCLOUD_SERVER_LOG ]; then
813 rotate-log $RAMCLOUD_SERVER_LOG
814 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700815
816 local coord_addr=`rc-coord-addr`
817 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700818
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700819 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.server.masterServiceThreads 5)
820 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.server.logCleanerThreads 1)
Yuta HIGUCHI52efcbb2014-05-16 09:30:33 -0700821 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.server.detectFailures 0)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700822
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700823 # Configuration for ZK address, port
824 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700825 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700826 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700827 # RAMCloud transport timeout
828 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700829 # replication factor (-r) config
830 local rc_replicas=$(read-conf ${ONOS_CONF} ramcloud.server.replicas 0)
831 # backup file path (-f) config
832 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
833 mkdir -p `dirname ${rc_datafile}`
834
835 local server_args="-L ${server_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700836 server_args="${server_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700837 server_args="${server_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700838 server_args="${server_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700839 server_args="${server_args} --masterServiceThreads ${masterServiceThreads}"
840 server_args="${server_args} --logCleanerThreads ${logCleanerThreads}"
841 server_args="${server_args} --detectFailures ${detectFailures}"
842 server_args="${server_args} --replicas ${rc_replicas}"
843 server_args="${server_args} --file ${rc_datafile}"
844
845 # Read environment variables if set
846 server_args="${server_args} ${RC_SERVER_OPTS}"
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700847
Naoki Shiota4e463182014-03-21 15:13:24 -0700848 # Run ramcloud
849 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700850 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server ${server_args} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700851 echo "STARTED"
852}
853
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700854function del-server-backup {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700855 echo -n "Delete RAMCloud backup server data [y/N]? "
856 while [ 1 ]; do
857 read key
858 if [ "${key}" == "Y" -o "${key}" == "y" ]; then
859 break
860 elif [ -z "${key}" -o "${key}" == "N" -o "${key}" == "n" ]; then
861 echo "Cancelled."
862 return
863 fi
864 echo "[y/N]? "
865 done
866
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700867 echo -n "Removing RAMCloud backup server data ... "
868 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
869 rm -f ${rc_datafile}
870 echo "DONE"
871}
872
Naoki Shiota4e463182014-03-21 15:13:24 -0700873function stop-server {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700874 kill-processes "RAMCloud server" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700875}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700876############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700877
878
Naoki Shiota590c18d2014-03-31 17:52:59 -0700879## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700880function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700881 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700882 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700883 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700884 ${MVN} -f ${ONOS_HOME}/pom.xml compile
885 fi
886 JAVA_CP=`cat ${CPFILE}`
887 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
888
889 case "$1" in
890 start)
891 stop-onos
892 start-onos
893 ;;
894 startnokill)
895 start-onos
896 ;;
897 startifdown)
898 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
899 if [ $n == 0 ]; then
900 start-onos
901 else
902 echo "$n instance of onos running"
903 fi
904 ;;
905 stop)
906 stop-onos
907 ;;
908 stat*) # <- status
909 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
910 echo "$n instance of onos running"
911 ;;
912 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700913 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700914 exit 1
915 esac
916}
917
918function start-onos {
919 if [ ! -d ${LOGDIR} ]; then
920 mkdir -p ${LOGDIR}
921 fi
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700922 # Rotate log files
Naoki Shiota4e463182014-03-21 15:13:24 -0700923 for log in ${LOGS}; do
924 if [ -f ${log} ]; then
925 rotate-log ${log}
926 fi
927 done
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700928
929 # Rotate logs rolled at runtime.
930 local rolled_log_shellpat=`echo ${ONOS_LOG_ROLLING_PATTERN} | sed -e "s/%i/\\*/"`
931 for rolled_log in ${rolled_log_shellpat}; do
932 if [ -f ${rolled_log} ]; then
933 rotate-log ${rolled_log}
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700934 fi
935 done
936
Naoki Shiota4e463182014-03-21 15:13:24 -0700937 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700938 echo "[WARNING] ${ONOS_LOGBACK} not found."
939 echo " Run \"\$ $0 setup\" to create."
940 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700941 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700942
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700943 if [ ! -f ${HC_CONF} ]; then
944 echo "[WARNING] ${HC_CONF} not found."
945 echo " Run \"\$ $0 setup\" to create."
946 exit 1
947 fi
948
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700949 # specify ZooKeeper(curator) namespace
950 JVM_OPTS="${JVM_OPTS} -Dzookeeper.namespace=${ONOS_CLUSTER_NAME}"
951
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700952 # specify hazelcast.xml to datagrid
953 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
954
955 # specify backend config
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700956 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700957 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
958 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700959 elif [ "${ONOS_HOST_BACKEND}" = "hazelcast" ]; then
960 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.hazelcast.baseConfig=${HC_CONF}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700961 fi
962
Naoki Shiota9df15d32014-03-27 14:26:20 -0700963 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -0700964
965 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
966 cd ${ONOS_HOME}
967
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700968 echo -n "Starting ONOS controller ... "
Naoki Shiota4e463182014-03-21 15:13:24 -0700969 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
970
971 # We need to wait a bit to find out whether starting the ONOS process succeeded
972 sleep 1
973
974 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700975 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700976 echo " STARTED"
977 else
978 echo " FAILED"
979 fi
980
981# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
982# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
983}
984
985function stop-onos {
986 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
987# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
988}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700989############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700990
991
Naoki Shiota590c18d2014-03-31 17:52:59 -0700992################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -0700993case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -0700994 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -0700995 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700996 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700997 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -0700998 mode_parameter=${ONOS_HOST_ROLE}
999 if [ ! -z "$2" ]; then
1000 mode_parameter=$2
1001 fi
1002
1003 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -07001004 single-node)
1005 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001006 start-backend coord
1007 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001008 onos startifdown
1009 ;;
1010 coord-node)
1011 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001012 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -07001013 onos startifdown
1014 ;;
1015 server-node)
1016 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001017 start-backend server
1018 onos startifdown
1019 ;;
1020 coord-and-server-node)
1021 zk start
1022 start-backend coord
1023 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001024 onos startifdown
1025 ;;
1026 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001027 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001028 ;;
1029 esac
1030 echo
1031 ;;
1032 stop)
1033 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1034 if [ $on != 0 ]; then
1035 onos stop
1036 fi
1037
Naoki Shiota9df15d32014-03-27 14:26:20 -07001038 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -07001039
1040 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1041 if [ $zkn != 0 ]; then
1042 zk stop
1043 fi
1044 echo
1045 ;;
1046 restart)
1047 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1048 if [ $on != 0 ]; then
1049 onos stop
1050 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001051
Naoki Shiota4e463182014-03-21 15:13:24 -07001052 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
1053 if [ $rcsn != 0 ]; then
1054 rc-server stop
1055 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001056
Naoki Shiota590c18d2014-03-31 17:52:59 -07001057 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -07001058 if [ $rccn != 0 ]; then
1059 rc-coord stop
1060 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001061
Naoki Shiota4e463182014-03-21 15:13:24 -07001062 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1063 if [ $zkn != 0 ]; then
1064 zk restart
1065 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001066
Naoki Shiota4e463182014-03-21 15:13:24 -07001067 if [ $rccn != 0 ]; then
1068 rc-coord startifdown
1069 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001070
Naoki Shiota4e463182014-03-21 15:13:24 -07001071 if [ $rcsn != 0 ]; then
1072 rc-server startifdown
1073 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001074
Naoki Shiota4e463182014-03-21 15:13:24 -07001075 if [ $on != 0 ]; then
1076 onos startifdown
1077 fi
1078 echo
1079 ;;
1080 stat*) # <- status
1081 echo '[ZooKeeper]'
1082 zk status
1083 echo
1084 echo '[RAMCloud coordinator]'
1085 rc-coord status
1086 echo
1087 echo '[RAMCloud server]'
1088 rc-server status
1089 echo
1090 echo '[ONOS core]'
1091 onos status
1092 echo
1093 ;;
1094 zk)
1095 zk $2
1096 ;;
1097 rc-c*) # <- rc-coordinator
1098 rc-coord $2
1099 ;;
1100 rc-s*) # <- rc-server
1101 rc-server $2
1102 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001103 rc)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001104 # XXX rc command ignores if the host is not configured to have those roles,
1105 # while rc-s or rc-c executes the task regardless of role definition.
1106 # This is a workaround since TestON does not take role in account
1107 # and always issue rc deldb right now.
1108
Naoki Shiota9109a1e2014-05-13 11:11:01 -07001109 # TODO make deldb command more organized (clarify when it can work)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001110 if [ "`is-coord-role`" == "true" ]; then
1111 rc-coord $2
1112 fi
1113 if [ "`is-server-role`" == "true" ]; then
1114 rc-server $2
1115 fi
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001116 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001117 core)
1118 onos $2
1119 ;;
1120 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001121 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001122 exit 1
1123esac
Naoki Shiota590c18d2014-03-31 17:52:59 -07001124