blob: 67c10707c07e93c7d9027302f897db744606ea8e [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 ###
40ONOS_HOST_NAME=$(read-conf ${ONOS_CONF} host.name `hostname`)
41ONOS_HOST_IP=$(read-conf ${ONOS_CONF} host.ip)
42ONOS_HOST_ROLE=$(read-conf ${ONOS_CONF} host.role)
43ONOS_HOST_BACKEND=$(read-conf ${ONOS_CONF} host.backend)
44ZK_HOSTS=$(read-conf ${ONOS_CONF} zookeeper.hosts ${ONOS_HOST_NAME})
45RC_COORD_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.coordinator.protocol "fast+udp")
46RC_COORD_IP=$(read-conf ${ONOS_CONF} ramcloud.coordinator.ip ${ONOS_HOST_IP})
47RC_COORD_PORT=$(read-conf ${ONOS_CONF} ramcloud.coordinator.port 12246)
48RC_SERVER_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.server.protocol "fast+udp")
49RC_SERVER_IP=$(read-conf ${ONOS_CONF} ramcloud.server.ip ${ONOS_HOST_IP})
50RC_SERVER_PORT=$(read-conf ${ONOS_CONF} ramcloud.server.port 12242)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070051HC_HOST_PORT=$(read-conf ${ONOS_CONF} hazelcast.host.port 5701)
Naoki Shiota4e928512014-04-03 15:49:35 -070052HC_TCPIP_MEMBERS=$(read-conf ${ONOS_CONF} hazelcast.tcp-ip.members)
53HC_MULTICAST_GROUP=$(read-conf ${ONOS_CONF} hazelcast.multicast.group "224.2.2.3")
54HC_MULTICAST_PORT=$(read-conf ${ONOS_CONF} hazelcast.multicast.port 54327)
Naoki Shiota590c18d2014-03-31 17:52:59 -070055############################################
56
57
58############## Other variables #############
Naoki Shiota4e928512014-04-03 15:49:35 -070059ONOS_TEMPLATE_DIR=${ONOS_CONF_DIR}/template
60
Naoki Shiota4e463182014-03-21 15:13:24 -070061LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
62
Yuta HIGUCHIfb1905a2014-06-09 14:07:34 -070063ZK_HOME=${ZK_HOME:-~/zookeeper-3.4.6}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070064ZK_CONF=${ZK_CONF:-${ONOS_CONF_DIR}/zoo.cfg}
Naoki Shiota4e928512014-04-03 15:49:35 -070065ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070066# Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070067ZK_LOG_DIR=${ZK_LOG_DIR:-${ONOS_HOME}/onos-logs/zk-${ONOS_HOST_NAME}}
Naoki Shiota72209722014-04-08 14:32:17 -070068ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070069ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070070
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070071HC_CONF=${HC_CONF:-${ONOS_CONF_DIR}/hazelcast.xml}
Naoki Shiota4e928512014-04-03 15:49:35 -070072HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
73
Naoki Shiota4e463182014-03-21 15:13:24 -070074RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070075RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
76RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070077RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070078RAMCLOUD_CONF=${RAMCLOUD_CONF:-${ONOS_CONF_DIR}/ramcloud.conf}
Naoki Shiota4e463182014-03-21 15:13:24 -070079
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070080export LD_LIBRARY_PATH=${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}:$LD_LIBRARY_PATH
Naoki Shiota4e463182014-03-21 15:13:24 -070081
82## 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 -070083ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070084ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070085ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070086LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070087LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070088ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070089ONOS_LOG_ROLLING_PATTERN="${LOGDIR}/${LOGBASE}.%i.log.gz"
90ONOS_STDOUT_LOG="${LOGDIR}/${LOGBASE}.stdout"
91ONOS_STDERR_LOG="${LOGDIR}/${LOGBASE}.stderr"
Naoki Shiota4e463182014-03-21 15:13:24 -070092PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070093LOGS="$ONOS_LOG $ONOS_STDOUT_LOG $ONOS_STDERR_LOG $PCAP_LOG"
Naoki Shiota4e463182014-03-21 15:13:24 -070094
Naoki Shiota9df15d32014-03-27 14:26:20 -070095ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -070096JMX_PORT=${JMX_PORT:-7189}
97
98# Set JVM options
99JVM_OPTS="${JVM_OPTS:-}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700100JVM_OPTS="$JVM_OPTS -server -d64"
101#JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700102
103# Uncomment or specify appropriate value as JVM_OPTS environment variables.
104#JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
Naoki Shiota4e463182014-03-21 15:13:24 -0700105#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
106#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700107
108#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC"
109JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
110JVM_OPTS="$JVM_OPTS -XX:+AggressiveOpts"
111
112# We may want to remove UseFastAccessorMethods option: http://bugs.java.com/view_bug.do?bug_id=6385687
113JVM_OPTS="$JVM_OPTS -XX:+UseFastAccessorMethods"
114
115JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192"
116JVM_OPTS="$JVM_OPTS -XX:FreqInlineSize=8192"
117JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500"
118
Naoki Shiota4e463182014-03-21 15:13:24 -0700119JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700120
121# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
122JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
123
124JVM_OPTS="$JVM_OPTS -XX:+UseCompressedOops"
125
126JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
127JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
128JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
129
Naoki Shiota4e463182014-03-21 15:13:24 -0700130JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
131
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700132# Uncomment to dump final JVM flags to stdout
133#JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
134
Naoki Shiota4e463182014-03-21 15:13:24 -0700135# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700136MAIN_CLASS="net.onrc.onos.core.main.Main"
Naoki Shiota4e463182014-03-21 15:13:24 -0700137
138MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700139############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700140
Naoki Shiota9df15d32014-03-27 14:26:20 -0700141
Naoki Shiota590c18d2014-03-31 17:52:59 -0700142############# Common functions #############
143function print_usage {
Naoki Shiota05721b32014-04-29 14:41:12 -0700144 local scriptname=`basename $0`
Naoki Shiota590c18d2014-03-31 17:52:59 -0700145 local filename=`basename ${ONOS_CONF}`
146 local usage="Usage: setup/start/stop ONOS on this server.
Naoki Shiota05721b32014-04-29 14:41:12 -0700147 \$ ${scriptname} setup [-f]
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700148 Set up ONOS node using ${ONOS_CONF} .
Naoki Shiota590c18d2014-03-31 17:52:59 -0700149 - generate and replace config file of ZooKeeper.
150 - create myid in ZooKeeper datadir.
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700151 - generate and replace config file for Hazelcast.
152 - generate and replace config file for RAMCloud.
Naoki Shiota590c18d2014-03-31 17:52:59 -0700153 - generate and replace logback.${ONOS_HOST_NAME}.xml
154 If -f option is used, all existing files will be overwritten without confirmation.
Naoki Shiota05721b32014-04-29 14:41:12 -0700155 \$ ${scriptname} start [single-node|coord-node|server-node|coord-and-server-node]
Naoki Shiota590c18d2014-03-31 17:52:59 -0700156 Start ONOS node with specific RAMCloud entities
157 - single-node: start ONOS with stand-alone RAMCloud
158 - coord-node : start ONOS with RAMCloud coordinator
159 - server-node: start ONOS with RAMCloud server
160 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
161 * Default behavior can be defined by ${filename}
Naoki Shiota05721b32014-04-29 14:41:12 -0700162 \$ ${scriptname} stop
Naoki Shiota590c18d2014-03-31 17:52:59 -0700163 Stop all ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700164 \$ ${scriptname} restart
Naoki Shiota590c18d2014-03-31 17:52:59 -0700165 Stop and start currently running ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700166 \$ ${scriptname} status
Naoki Shiota590c18d2014-03-31 17:52:59 -0700167 Show status of ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700168 \$ ${scriptname} {zk|rc-coord|rc-server|core} {start|stop|restart|status}
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700169 Control specific ONOS-related process
170 \$ ${scriptname} rc deldb
171 Delete data in RAMCloud"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700172
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700173 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700174}
175
176function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700177 local logfile=$1
178 local nr_max=${2:-10}
179 if [ -f $logfile ]; then
180 for i in `seq $(expr $nr_max - 1) -1 1`; do
181 if [ -f ${logfile}.${i} ]; then
182 mv -f ${logfile}.${i} ${logfile}.`expr $i + 1`
183 fi
184 done
185 mv $logfile $logfile.1
186 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700187}
188
189# kill-processes {module-name} {array of pids}
190function kill-processes {
191 # Kill the existing processes
192 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700193 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700194 echo -n "Stopping $1 ... "
195 fi
196 for p in ${pids}; do
197 if [ x$p != "x" ]; then
Naoki Shiota4b355762014-05-27 11:46:43 -0700198 # Check if target process is accesible from current user
199 kill -0 $p
200 if [ "$?" -ne 0 ]; then
201 # Error exit code here means "failed to send signal".
202 # Supposedly because of permission error.
203 echo "Failed to kill process (pid: $p)."
204 echo "Check if current user is the same as process owner."
205 exit 1
206 fi
207
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700208 (
209 # Ask process with SIGTERM first, if that did not kill the process
210 # wait 1s and if process still exist, force process to be killed.
211 kill -TERM $p && kill -0 $p && sleep 1 && kill -0 $p && kill -KILL $p
212 ) 2> /dev/null
Naoki Shiota4b355762014-05-27 11:46:43 -0700213
214 # Ensure process is killed.
215 kill -0 $p 2> /dev/null
216 if [ "$?" -ne 0 ]; then
217 # Error exit code here means "process not found", i.e. "kill succeeded".
218 echo "Killed existing process (pid: $p)"
219 else
220 # Process still exists. Some unexpected error occurs.
221 echo "Failed to kill process (pid: $p)."
222 echo "Unexpected error occurs during process termination."
223 exit 1
224 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700225 fi
226 done
227}
228
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700229function handle-error {
230 set -e
231
232 revert-confs
233
234 set +e
235
236 exit 1
237}
238
Naoki Shiota4e928512014-04-03 15:49:35 -0700239# revert-confs [error message]
240function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700241 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700242
Naoki Shiota4e928512014-04-03 15:49:35 -0700243 revert-file `basename ${ZK_CONF}`
244 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700245
246 echo "EXIT"
247
248 if [ ! -z "$1" ]; then
249 echo $1
250 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700251}
252
Naoki Shiota590c18d2014-03-31 17:52:59 -0700253function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700254 echo -n "Creating ${ZK_CONF} ... "
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700255
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700256 # Create the ZooKeeper lib directory
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700257 if [[ ! ( -w ${ZK_LIB_DIR} && -d ${ZK_LIB_DIR} ) ]]; then
Naoki Shiota7f495cf2014-05-21 17:23:25 -0700258 local SUDO=${SUDO:-sudo}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700259 local whoami=`whoami`
260 {
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700261 ${SUDO} mkdir -p ${ZK_LIB_DIR}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700262 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
263 } || {
264 echo "FAILED"
265 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
266 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
267 exit 1
268 }
269 fi
270
271 # creation of ZooKeeper config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700272 local temp_zk=`begin-conf-creation ${ZK_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700273
Naoki Shiota4e928512014-04-03 15:49:35 -0700274 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700275
276 local i=1
277 local myid=
278 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700279 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700280 myid=$i
281 break
282 fi
283 i=`expr $i + 1`
284 done
285
286 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700287 local filename=`basename ${ONOS_CONF}`
Naoki Shiota35f8d5c2014-04-08 11:13:18 -0700288 revert-confs "[ERROR] In ${filename}, zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700289 fi
290
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700291 if [ -f "${ZK_MY_ID}" ]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700292 # sudo will be needed if ZK_MY_ID is already created by other (old) script
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700293 local SUDO=${SUDO:-}
294 {
295 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
296 } || {
297 echo "FAILED"
298 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700299 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700300 exit 1
301 }
302 fi
303
Naoki Shiota9df15d32014-03-27 14:26:20 -0700304 echo ${myid} > ${ZK_MY_ID}
305
306 echo -n "myid is assigned to ${myid} ... "
307
308 while read line; do
309 if [[ $line =~ ^__HOSTS__$ ]]; then
310 i=1
311 for host in ${hostarr}; do
312 # TODO: ports might be configurable
313 local hostline="server.${i}=${host}:2888:3888"
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700314 echo $hostline
Naoki Shiota9df15d32014-03-27 14:26:20 -0700315 i=`expr $i + 1`
316 done
317 elif [[ $line =~ __DATADIR__ ]]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700318 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700319 else
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700320 echo $line
Naoki Shiota9df15d32014-03-27 14:26:20 -0700321 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700322 done < ${ZK_CONF_TEMPLATE} > ${temp_zk}
323
324 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700325
326 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700327}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700328
Naoki Shiota4e928512014-04-03 15:49:35 -0700329function create-hazelcast-conf {
330 echo -n "Creating ${HC_CONF} ... "
331
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700332 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700333
334 # To keep indent of XML file, change IFS
335 local IFS=''
336 while read line; do
337 if [[ $line =~ __HC_NETWORK__ ]]; then
338 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
339 # temporary change
340 IFS=' '
341 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
342 echo '<multicast enabled="false" />'
343 echo '<tcp-ip enabled="true">'
344 for member in ${memberarr}; do
345 echo " <member>${member}</member>"
346 done
347 echo '</tcp-ip>'
348 IFS=''
349 else
350 echo '<multicast enabled="true">'
351 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
352 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
353 echo '</multicast>'
354 echo '<tcp-ip enabled="false" />'
355 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700356 elif [[ $line =~ __HC_PORT__ ]]; then
357 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700358 else
359 echo "${line}"
360 fi
361 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
362
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700363 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700364
365 echo "DONE"
366}
367
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700368function create-ramcloud-conf {
369 echo -n "Creating ${RAMCLOUD_CONF} ... "
370
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700371 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700372
373 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
374
375 # TODO make ZooKeeper address configurable.
376 echo "ramcloud.locator=zk:localhost:2181" > ${temp_rc}
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700377 echo "#ramcloud.locator=zk:localhost:2181,otherhost:2181" >> ${temp_rc}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700378 echo "ramcloud.clusterName=${rc_cluster_name}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700379
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700380 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700381
382 echo "DONE"
383}
384
Naoki Shiota590c18d2014-03-31 17:52:59 -0700385function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700386 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700387
Naoki Shiota9df15d32014-03-27 14:26:20 -0700388 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700389 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700390
391 sed -e "s|__FILENAME__|${ONOS_LOG}|" \
392 -e "s|__ROLLING_PATTERN__|${ONOS_LOG_ROLLING_PATTERN}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700393
394 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700395
Naoki Shiota9df15d32014-03-27 14:26:20 -0700396 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700397}
398
Naoki Shiota4e928512014-04-03 15:49:35 -0700399function create-confs {
400 local key
401 local filename
402
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700403 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700404
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700405 echo "Config file : ${ONOS_CONF}"
406
Naoki Shiota4e928512014-04-03 15:49:35 -0700407 if [ "$1" == "-f" ]; then
408 create-zk-conf
409 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700410 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700411 create-logback-conf
412 else
413 create-conf-interactive ${ZK_CONF} create-zk-conf
414 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700415 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700416 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
417 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700418
419 trap - ERR
420}
421############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700422
Naoki Shiota590c18d2014-03-31 17:52:59 -0700423
424###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700425function zk {
426 case "$1" in
427 start)
428 start-zk
429 ;;
430 stop)
431 stop-zk
432 ;;
433 stat*) # <- status
434 status-zk
435 ;;
436 re*) # <- restart
437 stop-zk
438 start-zk
439 ;;
440 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700441 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700442 exit 1
443 esac
444}
445
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700446function load-zk-cfg {
447 if [ -f "${ZK_CONF}" ]; then
448 local filename=`basename ${ZK_CONF}`
449 local dirname=`dirname ${ZK_CONF}`
450
451 # Run ZooKeeper with our configuration
452 export ZOOCFG=${filename}
453 export ZOOCFGDIR=${dirname}
454 fi
455}
456
Naoki Shiota4e463182014-03-21 15:13:24 -0700457function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700458 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700459
Naoki Shiota4e928512014-04-03 15:49:35 -0700460 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700461 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700462
463 load-zk-cfg
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700464
465 # log4j.properties is read from classpath if not found in CWD.
466 # Using the ZooKeeper supplied default in ZooKeeper conf dir.
467 # TODO: To explicitly specify our customized log4j config file:
468 # export SERVER_JVMFLAGS="-Dlog4j.configuration=${ZK_LOG4J}"
469 env CLASSPATH="${ZK_HOME}/conf:${CLASSPATH}" ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700470}
471
472function stop-zk {
Yuta HIGUCHI7c708362014-06-02 23:15:13 -0700473 echo -n "Stopping ZooKeeper ... "
474 load-zk-cfg
475
476 ${ZK_HOME}/bin/zkServer.sh stop
Naoki Shiota4e463182014-03-21 15:13:24 -0700477}
478
479function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700480 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700481
Naoki Shiota72209722014-04-08 14:32:17 -0700482 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700483}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700484
485function check-zk {
486 # assumption here is that ZK status script is the last command in status-zk.
487 status-zk &> /dev/null
488 local zk_status=$?
489 if [ "$zk_status" -ne 0 ]; then
490 return 1;
491 fi
492 return 0
493}
494
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700495function check-and-start-zk {
496 check-zk
497 local zk_status=$?
498 if [ "$zk_status" -ne 0 ]; then
499 start-zk
500 fi
501}
502
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700503# wait-zk-or-die {timeout-sec}
504function wait-zk-or-die {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700505 local retries=${1:-5}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700506 # do-while retries >= 0
507 while true; do
508 check-zk
509 local zk_status=$?
510 if [ "$zk_status" -eq 0 ]; then
511 return 0
512 fi
513 sleep 1;
514 ((retries -= 1))
515 (( retries >= 0 )) || break
516 done
517 echo "ZooKeeper is not running."
518 exit 1
519}
520
Naoki Shiota590c18d2014-03-31 17:52:59 -0700521############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700522
523
Naoki Shiota590c18d2014-03-31 17:52:59 -0700524####### Functions related to RAMCloud ######
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -0700525
526# Check if this host should be running RAMCloud coordinator:
527function is-coord-role {
528 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
529 case "${ONOS_HOST_ROLE}" in
530 single-node)
531 echo "true"
532 return 0
533 ;;
534 coord-node)
535 echo "true"
536 return 0
537 ;;
538 server-node)
539 echo "false"
540 return 1
541 ;;
542 coord-and-server-node)
543 echo "true"
544 return 0
545 ;;
546 *)
547 echo "false"
548 return 1
549 ;;
550 esac
551 else
552 echo "false"
553 return 1
554 fi
555}
556
557# Check if this host should be running RAMCloud server:
558function is-server-role {
559 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
560 case "${ONOS_HOST_ROLE}" in
561 single-node)
562 echo "true"
563 return 0
564 ;;
565 coord-node)
566 echo "false"
567 return 1
568 ;;
569 server-node)
570 echo "true"
571 return 0
572 ;;
573 coord-and-server-node)
574 echo "true"
575 return 0
576 ;;
577 *)
578 echo "false"
579 return 1
580 ;;
581 esac
582 else
583 echo "false"
584 return 1
585 fi
586}
587
Naoki Shiota9df15d32014-03-27 14:26:20 -0700588function start-backend {
589 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700590 if [ $1 == "coord" ]; then
591 rc-coord startifdown
592 elif [ $1 == "server" ]; then
593 rc-server startifdown
594 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700595 fi
596}
597
598function stop-backend {
599 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
600 if [ $rcsn != 0 ]; then
601 rc-server stop
602 fi
603
Naoki Shiota590c18d2014-03-31 17:52:59 -0700604 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700605 if [ $rccn != 0 ]; then
606 rc-coord stop
607 fi
608}
609
Naoki Shiota590c18d2014-03-31 17:52:59 -0700610
Naoki Shiota4e463182014-03-21 15:13:24 -0700611### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700612function rc-coord-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700613 local host=${RC_COORD_IP}
614 if [ -z "${host}" ]; then
615 # falling back to 0.0.0.0
616 host="0.0.0.0"
617 fi
618 echo "${RC_COORD_PROTOCOL}:host=${host},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700619}
620
621function rc-server-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700622 local host=${RC_SERVER_IP}
623 if [ -z "${host}" ]; then
624 # falling back to 0.0.0.0
625 host="0.0.0.0"
626 fi
627 echo "${RC_SERVER_PROTOCOL}:host=${host},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700628}
629
Naoki Shiota4e463182014-03-21 15:13:24 -0700630function rc-coord {
631 case "$1" in
632 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700633 stop-coord
634 start-coord
635 ;;
636 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700637 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700638 if [ $n == 0 ]; then
639 start-coord
640 else
641 echo "$n instance of RAMCloud coordinator running"
642 fi
643 ;;
644 stop)
645 stop-coord
646 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700647 deldb)
648 stop-backend
649 del-coord-info
650 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700651 stat*) # <- status
652 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
653 echo "$n RAMCloud coordinator running"
654 ;;
655 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700656 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700657 exit 1
658 esac
659}
660
661function start-coord {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700662 check-and-start-zk
663 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700664
Naoki Shiota4e463182014-03-21 15:13:24 -0700665 if [ ! -d ${LOGDIR} ]; then
666 mkdir -p ${LOGDIR}
667 fi
668 if [ -f $RAMCLOUD_COORD_LOG ]; then
669 rotate-log $RAMCLOUD_COORD_LOG
670 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700671
672 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700673
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700674 # TODO Make ONOS_CONF readable from ONOS process then eliminate RAMCLOUD_CONF
675
676 # Configuration for ZK address, port
677 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
678
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700679 # RAMCloud cluster name
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700680 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName "ONOS-RC")
681
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700682 # RAMCloud transport timeout
683 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700684 # RAMCloud option deadServerTimeout
685 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
686 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
687
688 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
689 # but actual RAMCloud code only uses -C argument now.
690 # (FYI: -C is documented to be deprecated in the document)
691
692 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700693 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700694 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700695 coord_args="${coord_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700696 coord_args="${coord_args} --deadServerTimeout ${rc_coord_deadServerTimeout}"
697
698 # Read environment variables if set
699 coord_args="${coord_args} ${RC_COORDINATOR_OPTS}"
700
701 if [ "${ONOS_HOST_ROLE}" == "single-node" ]; then
702 # Note: Following reset is required, since RC restart is considered node failure,
703 # and tries recovery of server, which will never succeed after restart.
704 echo "Role configured to single-node mode. RAMCloud cluster will be reset on each start-up."
705 coord_args="${coord_args} --reset"
706 fi
707
Naoki Shiota4e463182014-03-21 15:13:24 -0700708 # Run ramcloud
709 echo -n "Starting RAMCloud coordinator ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700710 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700711 echo "STARTED"
712}
713
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700714function del-coord-info {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700715 check-and-start-zk
716 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700717
718 if [ ! -d ${LOGDIR} ]; then
719 mkdir -p ${LOGDIR}
720 fi
721 if [ -f $RAMCLOUD_COORD_LOG ]; then
722 rotate-log $RAMCLOUD_COORD_LOG
723 fi
724
725 local coord_addr=`rc-coord-addr`
726
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700727 # Configuration for ZK address, port
728 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700729 # RAMCloud cluster name
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700730 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName "ONOS-RC")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700731 # RAMCloud option deadServerTimeout
732 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
733 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
734
735 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
736 # but actual RAMCloud code only uses -C argument now.
737 # (FYI: -C is documented to be deprecated in the document)
738
739 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700740 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700741 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
742
743 # Note: --reset will reset ZK stored info and start running as acoordinator.
744 echo -n "Deleting RAMCloud cluster coordination info ... "
745 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} --reset &> $RAMCLOUD_COORD_LOG &
746
Yuta HIGUCHIcd44eb32014-05-24 16:51:31 -0700747 # TODO Assuming 5 sec is enough. To be sure monitor log?
748 sleep 5
749 # kill coordinator
Yuta HIGUCHIce9f3ee2014-05-14 09:48:40 -0700750 (pkill -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator &> /dev/null)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700751
752 echo "DONE"
753}
Naoki Shiota4e463182014-03-21 15:13:24 -0700754
755function stop-coord {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700756 kill-processes "RAMCloud coordinator" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700757}
758
Naoki Shiota4e463182014-03-21 15:13:24 -0700759### Functions related to RAMCloud server
760function rc-server {
761 case "$1" in
762 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700763 stop-server
764 start-server
765 ;;
766 startifdown)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700767 local n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700768 if [ $n == 0 ]; then
769 start-server
770 else
771 echo "$n instance of RAMCloud server running"
772 fi
773 ;;
774 stop)
775 stop-server
776 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700777 deldb)
778 stop-server
779 del-server-backup
780 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700781 stat*) # <- status
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700782 n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700783 echo "$n RAMCloud server running"
784 ;;
785 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700786 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700787 exit 1
788 esac
789}
790
791function start-server {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700792 check-and-start-zk
793 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700794
Naoki Shiota4e463182014-03-21 15:13:24 -0700795 if [ ! -d ${LOGDIR} ]; then
796 mkdir -p ${LOGDIR}
797 fi
798 if [ -f $RAMCLOUD_SERVER_LOG ]; then
799 rotate-log $RAMCLOUD_SERVER_LOG
800 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700801
802 local coord_addr=`rc-coord-addr`
803 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700804
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700805 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.server.masterServiceThreads 5)
806 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.server.logCleanerThreads 1)
Yuta HIGUCHI52efcbb2014-05-16 09:30:33 -0700807 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.server.detectFailures 0)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700808
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700809 # Configuration for ZK address, port
810 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700811 # RAMCloud cluster name
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700812 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName "ONOS-RC")
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700813 # RAMCloud transport timeout
814 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700815 # replication factor (-r) config
816 local rc_replicas=$(read-conf ${ONOS_CONF} ramcloud.server.replicas 0)
817 # backup file path (-f) config
818 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
819 mkdir -p `dirname ${rc_datafile}`
820
821 local server_args="-L ${server_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700822 server_args="${server_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700823 server_args="${server_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700824 server_args="${server_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700825 server_args="${server_args} --masterServiceThreads ${masterServiceThreads}"
826 server_args="${server_args} --logCleanerThreads ${logCleanerThreads}"
827 server_args="${server_args} --detectFailures ${detectFailures}"
828 server_args="${server_args} --replicas ${rc_replicas}"
829 server_args="${server_args} --file ${rc_datafile}"
830
831 # Read environment variables if set
832 server_args="${server_args} ${RC_SERVER_OPTS}"
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700833
Naoki Shiota4e463182014-03-21 15:13:24 -0700834 # Run ramcloud
835 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700836 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server ${server_args} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700837 echo "STARTED"
838}
839
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700840function del-server-backup {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700841 echo -n "Delete RAMCloud backup server data [y/N]? "
842 while [ 1 ]; do
843 read key
844 if [ "${key}" == "Y" -o "${key}" == "y" ]; then
845 break
846 elif [ -z "${key}" -o "${key}" == "N" -o "${key}" == "n" ]; then
847 echo "Cancelled."
848 return
849 fi
850 echo "[y/N]? "
851 done
852
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700853 echo -n "Removing RAMCloud backup server data ... "
854 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
855 rm -f ${rc_datafile}
856 echo "DONE"
857}
858
Naoki Shiota4e463182014-03-21 15:13:24 -0700859function stop-server {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700860 kill-processes "RAMCloud server" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700861}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700862############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700863
864
Naoki Shiota590c18d2014-03-31 17:52:59 -0700865## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700866function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700867 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700868 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700869 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700870 ${MVN} -f ${ONOS_HOME}/pom.xml compile
871 fi
872 JAVA_CP=`cat ${CPFILE}`
873 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
874
875 case "$1" in
876 start)
877 stop-onos
878 start-onos
879 ;;
880 startnokill)
881 start-onos
882 ;;
883 startifdown)
884 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
885 if [ $n == 0 ]; then
886 start-onos
887 else
888 echo "$n instance of onos running"
889 fi
890 ;;
891 stop)
892 stop-onos
893 ;;
894 stat*) # <- status
895 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
896 echo "$n instance of onos running"
897 ;;
898 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700899 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700900 exit 1
901 esac
902}
903
904function start-onos {
905 if [ ! -d ${LOGDIR} ]; then
906 mkdir -p ${LOGDIR}
907 fi
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700908 # Rotate log files
Naoki Shiota4e463182014-03-21 15:13:24 -0700909 for log in ${LOGS}; do
910 if [ -f ${log} ]; then
911 rotate-log ${log}
912 fi
913 done
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700914
915 # Rotate logs rolled at runtime.
916 local rolled_log_shellpat=`echo ${ONOS_LOG_ROLLING_PATTERN} | sed -e "s/%i/\\*/"`
917 for rolled_log in ${rolled_log_shellpat}; do
918 if [ -f ${rolled_log} ]; then
919 rotate-log ${rolled_log}
920 # NOTE: renamed file will end up with an extension like .log.gz.1
921 fi
922 done
923
Naoki Shiota4e463182014-03-21 15:13:24 -0700924 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700925 echo "[WARNING] ${ONOS_LOGBACK} not found."
926 echo " Run \"\$ $0 setup\" to create."
927 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700928 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700929
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700930 if [ ! -f ${HC_CONF} ]; then
931 echo "[WARNING] ${HC_CONF} not found."
932 echo " Run \"\$ $0 setup\" to create."
933 exit 1
934 fi
935
936 # specify hazelcast.xml to datagrid
937 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
938
939 # specify backend config
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700940 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700941 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
942 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700943 elif [ "${ONOS_HOST_BACKEND}" = "hazelcast" ]; then
944 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.hazelcast.baseConfig=${HC_CONF}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700945 fi
946
Naoki Shiota9df15d32014-03-27 14:26:20 -0700947 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -0700948
949 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
950 cd ${ONOS_HOME}
951
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700952 echo -n "Starting ONOS controller ... "
Naoki Shiota4e463182014-03-21 15:13:24 -0700953 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
954
955 # We need to wait a bit to find out whether starting the ONOS process succeeded
956 sleep 1
957
958 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700959 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700960 echo " STARTED"
961 else
962 echo " FAILED"
963 fi
964
965# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
966# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
967}
968
969function stop-onos {
970 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
971# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
972}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700973############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700974
975
Naoki Shiota590c18d2014-03-31 17:52:59 -0700976################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -0700977case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -0700978 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -0700979 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700980 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700981 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -0700982 mode_parameter=${ONOS_HOST_ROLE}
983 if [ ! -z "$2" ]; then
984 mode_parameter=$2
985 fi
986
987 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -0700988 single-node)
989 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700990 start-backend coord
991 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700992 onos startifdown
993 ;;
994 coord-node)
995 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700996 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -0700997 onos startifdown
998 ;;
999 server-node)
1000 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001001 start-backend server
1002 onos startifdown
1003 ;;
1004 coord-and-server-node)
1005 zk start
1006 start-backend coord
1007 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001008 onos startifdown
1009 ;;
1010 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001011 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001012 ;;
1013 esac
1014 echo
1015 ;;
1016 stop)
1017 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1018 if [ $on != 0 ]; then
1019 onos stop
1020 fi
1021
Naoki Shiota9df15d32014-03-27 14:26:20 -07001022 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -07001023
1024 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1025 if [ $zkn != 0 ]; then
1026 zk stop
1027 fi
1028 echo
1029 ;;
1030 restart)
1031 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1032 if [ $on != 0 ]; then
1033 onos stop
1034 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001035
Naoki Shiota4e463182014-03-21 15:13:24 -07001036 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
1037 if [ $rcsn != 0 ]; then
1038 rc-server stop
1039 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001040
Naoki Shiota590c18d2014-03-31 17:52:59 -07001041 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -07001042 if [ $rccn != 0 ]; then
1043 rc-coord stop
1044 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001045
Naoki Shiota4e463182014-03-21 15:13:24 -07001046 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1047 if [ $zkn != 0 ]; then
1048 zk restart
1049 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001050
Naoki Shiota4e463182014-03-21 15:13:24 -07001051 if [ $rccn != 0 ]; then
1052 rc-coord startifdown
1053 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001054
Naoki Shiota4e463182014-03-21 15:13:24 -07001055 if [ $rcsn != 0 ]; then
1056 rc-server startifdown
1057 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001058
Naoki Shiota4e463182014-03-21 15:13:24 -07001059 if [ $on != 0 ]; then
1060 onos startifdown
1061 fi
1062 echo
1063 ;;
1064 stat*) # <- status
1065 echo '[ZooKeeper]'
1066 zk status
1067 echo
1068 echo '[RAMCloud coordinator]'
1069 rc-coord status
1070 echo
1071 echo '[RAMCloud server]'
1072 rc-server status
1073 echo
1074 echo '[ONOS core]'
1075 onos status
1076 echo
1077 ;;
1078 zk)
1079 zk $2
1080 ;;
1081 rc-c*) # <- rc-coordinator
1082 rc-coord $2
1083 ;;
1084 rc-s*) # <- rc-server
1085 rc-server $2
1086 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001087 rc)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001088 # XXX rc command ignores if the host is not configured to have those roles,
1089 # while rc-s or rc-c executes the task regardless of role definition.
1090 # This is a workaround since TestON does not take role in account
1091 # and always issue rc deldb right now.
1092
Naoki Shiota9109a1e2014-05-13 11:11:01 -07001093 # TODO make deldb command more organized (clarify when it can work)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001094 if [ "`is-coord-role`" == "true" ]; then
1095 rc-coord $2
1096 fi
1097 if [ "`is-server-role`" == "true" ]; then
1098 rc-server $2
1099 fi
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001100 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001101 core)
1102 onos $2
1103 ;;
1104 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001105 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001106 exit 1
1107esac
Naoki Shiota590c18d2014-03-31 17:52:59 -07001108