blob: 0790afd842e609f0b9b013ad293588283c3224d9 [file] [log] [blame]
Naoki Shiota4e463182014-03-21 15:13:24 -07001#!/bin/bash
Naoki Shiota4e463182014-03-21 15:13:24 -07002
Naoki Shiota590c18d2014-03-31 17:52:59 -07003### Env vars used by this script. (default value) ###
4# $ONOS_HOME : path of root directory of ONOS repository (this script's dir)
5# $ONOS_CONF_DIR : path of ONOS config directory (~/ONOS/conf)
Naoki Shiota9a1e6d12014-04-03 14:47:12 -07006# $ONOS_CONF : path of ONOS node config file (~/ONOS/conf/onos_node.`hostname`.conf or onos_node.conf)
Naoki Shiota590c18d2014-03-31 17:52:59 -07007# $ONOS_PROPS : path of ONOS properties file (~/ONOS/conf/onos.properties)
8# $ONOS_LOGBACK : path of logback config file (~/ONOS/conf/logback.`hostname`.xml)
Naoki Shiotad8ea71a2014-04-23 17:04:51 -07009# $ONOS_LOGDIR : path of log output directory (~/ONOS/onos-logs)
10# $ONOS_LOGBASE : base name of log output file (onos.`hostname`)
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -070011# $ONOS_DEBUG : option to enable debugger for ONOS (false)
Naoki Shiota590c18d2014-03-31 17:52:59 -070012# $RAMCLOUD_HOME : path of root directory of RAMCloud repository (~/ramcloud)
13# $RAMCLOUD_BRANCH : branch name of RAMCloud to use (master)
Yuta HIGUCHIfb1905a2014-06-09 14:07:34 -070014# $ZK_HOME : path of root directory of ZooKeeper (~/zookeeper-3.4.6)
Naoki Shiota72209722014-04-08 14:32:17 -070015# $ZK_LIB_DIR : path of ZooKeeper library (/var/lib/zookeeper)
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070016# $ZK_LOG_DIR : path of ZooKeeper log output directory (~/ONOS/onos-logs/zk-`hostname`)
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -070017# $JVM_OPTS : JVM options to add when starting ONOS
18# $JVM_DEBUG_PORT : port to use for remote debugging ONOS process, when $ONOS_DEBUG is enabled (22007)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070019# $ZK_CONF : path of ZooKeeper config file (~/ONOS/conf/zoo.cfg)
20# $HC_CONF : path of Hazelcast config file (~/ONOS/conf/hazelcast.xml)
21# $RAMCLOUD_CONF : path of RAMCloud config file (~/ONOS/conf/ramcloud.conf)
Naoki Shiota590c18d2014-03-31 17:52:59 -070022#####################################################
23
Naoki Shiota590c18d2014-03-31 17:52:59 -070024ONOS_HOME=${ONOS_HOME:-$(cd `dirname $0`; pwd)}
Yuta HIGUCHI5f14b012014-07-18 19:14:04 -070025ONOS_VERSION=`grep -E "<version>.*</version>" ${ONOS_HOME}/pom.xml | head -n 1 | sed -e "s|<version>\(.*\)</version>|\\1|" | tr -d "[:space:]"`
Naoki Shiota9df15d32014-03-27 14:26:20 -070026ONOS_CONF_DIR=${ONOS_CONF_DIR:-${ONOS_HOME}/conf}
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070027ONOS_CONF=${ONOS_CONF:-${ONOS_CONF_DIR}/onos_node.`hostname`.conf}
Yuta HIGUCHIec920702014-08-01 17:54:30 -070028ONOS_DUMP_DIR=${ONOS_DUMP_DIR:-${ONOS_HOME}}
Yuta HIGUCHI5f14b012014-07-18 19:14:04 -070029ONOS_JAR=${ONOS_JAR:-${ONOS_HOME}/lib/onos-${ONOS_VERSION}-jar-with-dependencies.jar}
Naoki Shiota590c18d2014-03-31 17:52:59 -070030
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070031source ${ONOS_HOME}/scripts/common/utils.sh
32
Yuta HIGUCHIe748ecc2014-05-28 13:55:03 -070033confirm-if-root
34
Naoki Shiota590c18d2014-03-31 17:52:59 -070035if [ ! -f ${ONOS_CONF} ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070036 # falling back to default config file
37 ONOS_CONF=${ONOS_CONF_DIR}/onos_node.conf
38 if [ ! -f ${ONOS_CONF} ]; then
39 echo "${ONOS_CONF} not found."
40 exit 1
41 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -070042fi
Naoki Shiota4e928512014-04-03 15:49:35 -070043
Naoki Shiota4e928512014-04-03 15:49:35 -070044### Variables read from ONOS config file ###
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -070045ONOS_CLUSTER_NAME=$(read-conf ${ONOS_CONF} onos.cluster.name "onos")
Naoki Shiota4e928512014-04-03 15:49:35 -070046ONOS_HOST_NAME=$(read-conf ${ONOS_CONF} host.name `hostname`)
47ONOS_HOST_IP=$(read-conf ${ONOS_CONF} host.ip)
48ONOS_HOST_ROLE=$(read-conf ${ONOS_CONF} host.role)
49ONOS_HOST_BACKEND=$(read-conf ${ONOS_CONF} host.backend)
50ZK_HOSTS=$(read-conf ${ONOS_CONF} zookeeper.hosts ${ONOS_HOST_NAME})
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -070051ZK_CLIENTPORT=$(read-conf ${ONOS_CONF} zookeeper.clientPort 2181)
52ZK_PORTS=$(read-conf ${ONOS_CONF} zookeeper.ports 2888:3888)
Naoki Shiota4e928512014-04-03 15:49:35 -070053RC_COORD_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.coordinator.protocol "fast+udp")
54RC_COORD_IP=$(read-conf ${ONOS_CONF} ramcloud.coordinator.ip ${ONOS_HOST_IP})
55RC_COORD_PORT=$(read-conf ${ONOS_CONF} ramcloud.coordinator.port 12246)
56RC_SERVER_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.server.protocol "fast+udp")
57RC_SERVER_IP=$(read-conf ${ONOS_CONF} ramcloud.server.ip ${ONOS_HOST_IP})
58RC_SERVER_PORT=$(read-conf ${ONOS_CONF} ramcloud.server.port 12242)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070059HC_HOST_PORT=$(read-conf ${ONOS_CONF} hazelcast.host.port 5701)
Naoki Shiota4e928512014-04-03 15:49:35 -070060HC_TCPIP_MEMBERS=$(read-conf ${ONOS_CONF} hazelcast.tcp-ip.members)
61HC_MULTICAST_GROUP=$(read-conf ${ONOS_CONF} hazelcast.multicast.group "224.2.2.3")
62HC_MULTICAST_PORT=$(read-conf ${ONOS_CONF} hazelcast.multicast.port 54327)
Naoki Shiota590c18d2014-03-31 17:52:59 -070063############################################
64
65
66############## Other variables #############
Naoki Shiota4e928512014-04-03 15:49:35 -070067ONOS_TEMPLATE_DIR=${ONOS_CONF_DIR}/template
68
Naoki Shiota4e463182014-03-21 15:13:24 -070069LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
70
Yuta HIGUCHIfb1905a2014-06-09 14:07:34 -070071ZK_HOME=${ZK_HOME:-~/zookeeper-3.4.6}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070072ZK_CONF=${ZK_CONF:-${ONOS_CONF_DIR}/zoo.cfg}
Naoki Shiota4e928512014-04-03 15:49:35 -070073ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070074# Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070075ZK_LOG_DIR=${ZK_LOG_DIR:-${ONOS_HOME}/onos-logs/zk-${ONOS_HOST_NAME}}
Naoki Shiota72209722014-04-08 14:32:17 -070076ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070077ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070078
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070079HC_CONF=${HC_CONF:-${ONOS_CONF_DIR}/hazelcast.xml}
Naoki Shiota4e928512014-04-03 15:49:35 -070080HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
81
Naoki Shiota4e463182014-03-21 15:13:24 -070082RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070083RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
84RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070085RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070086RAMCLOUD_CONF=${RAMCLOUD_CONF:-${ONOS_CONF_DIR}/ramcloud.conf}
Naoki Shiota4e463182014-03-21 15:13:24 -070087
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070088export LD_LIBRARY_PATH=${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}:$LD_LIBRARY_PATH
Naoki Shiota4e463182014-03-21 15:13:24 -070089
90## 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 -070091ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070092ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070093ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070094LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070095LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070096ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070097ONOS_LOG_ROLLING_PATTERN="${LOGDIR}/${LOGBASE}.%i.log.gz"
98ONOS_STDOUT_LOG="${LOGDIR}/${LOGBASE}.stdout"
99ONOS_STDERR_LOG="${LOGDIR}/${LOGBASE}.stderr"
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700100ONOS_COMPILATION_LOG="${LOGDIR}/compilation.${ONOS_HOST_NAME}.log"
101ONOS_GC_LOG="${LOGDIR}/gc.${ONOS_HOST_NAME}.log"
Naoki Shiota4e463182014-03-21 15:13:24 -0700102PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700103LOGS="$ONOS_LOG $ONOS_STDOUT_LOG $ONOS_STDERR_LOG $ONOS_COMPILATION_LOG $ONOS_GC_LOG $PCAP_LOG"
Naoki Shiota4e463182014-03-21 15:13:24 -0700104
Naoki Shiota9df15d32014-03-27 14:26:20 -0700105ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -0700106JMX_PORT=${JMX_PORT:-7189}
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -0700107JVM_DEBUG_PORT=${JVM_DEBUG_PORT:-22007}
Naoki Shiota4e463182014-03-21 15:13:24 -0700108
109# Set JVM options
110JVM_OPTS="${JVM_OPTS:-}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700111JVM_OPTS="$JVM_OPTS -server -d64"
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700112
113# TODO Revisit appropriate CodeCache size
114JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700115
116# Uncomment or specify appropriate value as JVM_OPTS environment variables.
117#JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
Naoki Shiota4e463182014-03-21 15:13:24 -0700118#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
119#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700120
121#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC"
122JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700123#JVM_OPTS="$JVM_OPTS -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
124# Note: shoult NOT specify -Xmn when using G1GC
125# http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/G1GettingStarted/index.html
126
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700127JVM_OPTS="$JVM_OPTS -XX:+AggressiveOpts"
128
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700129# TODO We may want to remove UseFastAccessorMethods option: http://bugs.java.com/view_bug.do?bug_id=6385687
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700130JVM_OPTS="$JVM_OPTS -XX:+UseFastAccessorMethods"
131
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700132# TODO revisit these derived from floodlight
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700133JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192"
134JVM_OPTS="$JVM_OPTS -XX:FreqInlineSize=8192"
135JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500"
136
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700137# FIXME Specify appropriate cmd to handle fatal crash
138# http://www.oracle.com/technetwork/java/javase/clopts-139448.html#gbmum
Naoki Shiota4e463182014-03-21 15:13:24 -0700139JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700140#JVM_OPTS="$JVM_OPTS -XX:+ShowMessageBoxOnError"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700141
Praseed Balakrishnan4fe80812014-07-24 11:29:19 -0700142# This option tells the VM to generate a heap dump when memory allocation cannot be satisfied.
143# http://www.oracle.com/technetwork/java/javase/clopts-139448.html#gbzrr
Yuta HIGUCHIec920702014-08-01 17:54:30 -0700144JVM_OPTS="$JVM_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${ONOS_DUMP_DIR}"
Praseed Balakrishnan4fe80812014-07-24 11:29:19 -0700145
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700146# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
147JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
148
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700149JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
150JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
151JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
152
Yuta HIGUCHI6bcaf982014-07-30 15:49:58 -0700153JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j -Dhazelcast.jmx=true -Dhazelcast.jmx.detailed=true"
Naoki Shiota4e463182014-03-21 15:13:24 -0700154
Yuta HIGUCHIc248d0d2014-07-30 16:14:18 -0700155# TODO revisit HZ related magic numbers
156# HZ Default:1000000
157JVM_OPTS="$JVM_OPTS -Dhazelcast.event.queue.capacity=4000000"
158# HZ Default:32 (KB)
159JVM_OPTS="$JVM_OPTS -Dhazelcast.socket.receive.buffer.size=4096"
160# HZ Default:32 (KB)
161JVM_OPTS="$JVM_OPTS -Dhazelcast.socket.send.buffer.size=4096"
162
163
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700164if [ "${ONOS_DEBUG}" == "true" ]; then
165 # Compilation diagnosis
166 JVM_OPTS="$JVM_OPTS -XX:+UnlockDiagnosticVMOptions"
167 JVM_OPTS="$JVM_OPTS -XX:+LogCompilation"
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700168 JVM_OPTS="$JVM_OPTS -XX:LogFile=${ONOS_COMPILATION_LOG}"
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700169
170 # GC diagnosis
171 JVM_OPTS="$JVM_OPTS -verbose:gc"
172 JVM_OPTS="$JVM_OPTS -verbose:sizes"
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700173 JVM_OPTS="$JVM_OPTS -Xloggc:${ONOS_GC_LOG}"
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -0700174 JVM_OPTS="$JVM_OPTS -XX:GCLogFileSize=1G"
175 JVM_OPTS="$JVM_OPTS -XX:+PrintGCDetails"
176 JVM_OPTS="$JVM_OPTS -XX:+PrintGCTimeStamps"
177 JVM_OPTS="$JVM_OPTS -XX:+PrintGCDateStamps"
178 JVM_OPTS="$JVM_OPTS -XX:+PrintTenuringDistribution"
179 JVM_OPTS="$JVM_OPTS -XX:+PrintCMSInitiationStatistics"
180fi
181
182# dump final JVM flags to stdout
183JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700184
Naoki Shiota4e463182014-03-21 15:13:24 -0700185# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700186MAIN_CLASS="net.onrc.onos.core.main.Main"
Yuta HIGUCHI5f14b012014-07-18 19:14:04 -0700187MAIN_CLASS_FILE="${ONOS_HOME}/target/classes/net/onrc/onos/core/main/Main.class"
Naoki Shiota4e463182014-03-21 15:13:24 -0700188
Yuta HIGUCHI5f14b012014-07-18 19:14:04 -0700189MVN=${MVN:-mvn -B}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700190############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700191
Naoki Shiota9df15d32014-03-27 14:26:20 -0700192
Naoki Shiota590c18d2014-03-31 17:52:59 -0700193############# Common functions #############
194function print_usage {
Naoki Shiota05721b32014-04-29 14:41:12 -0700195 local scriptname=`basename $0`
Naoki Shiota590c18d2014-03-31 17:52:59 -0700196 local filename=`basename ${ONOS_CONF}`
197 local usage="Usage: setup/start/stop ONOS on this server.
Naoki Shiota05721b32014-04-29 14:41:12 -0700198 \$ ${scriptname} setup [-f]
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700199 Set up ONOS node using ${ONOS_CONF} .
Naoki Shiota590c18d2014-03-31 17:52:59 -0700200 - generate and replace config file of ZooKeeper.
201 - create myid in ZooKeeper datadir.
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700202 - generate and replace config file for Hazelcast.
203 - generate and replace config file for RAMCloud.
Naoki Shiota590c18d2014-03-31 17:52:59 -0700204 - generate and replace logback.${ONOS_HOST_NAME}.xml
205 If -f option is used, all existing files will be overwritten without confirmation.
Naoki Shiota05721b32014-04-29 14:41:12 -0700206 \$ ${scriptname} start [single-node|coord-node|server-node|coord-and-server-node]
Naoki Shiota590c18d2014-03-31 17:52:59 -0700207 Start ONOS node with specific RAMCloud entities
208 - single-node: start ONOS with stand-alone RAMCloud
209 - coord-node : start ONOS with RAMCloud coordinator
210 - server-node: start ONOS with RAMCloud server
211 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
212 * Default behavior can be defined by ${filename}
Naoki Shiota05721b32014-04-29 14:41:12 -0700213 \$ ${scriptname} stop
Naoki Shiota590c18d2014-03-31 17:52:59 -0700214 Stop all ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700215 \$ ${scriptname} restart
Naoki Shiota590c18d2014-03-31 17:52:59 -0700216 Stop and start currently running ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700217 \$ ${scriptname} status
Naoki Shiota590c18d2014-03-31 17:52:59 -0700218 Show status of ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700219 \$ ${scriptname} {zk|rc-coord|rc-server|core} {start|stop|restart|status}
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700220 Control specific ONOS-related process
221 \$ ${scriptname} rc deldb
222 Delete data in RAMCloud"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700223
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700224 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700225}
226
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700227# rotate-log [log-filename] [max rotations]
228# Example:
229# foobar.log -> foobar.log.1
230# foobar.log.1 -> foobar.log.2
231# foobar.log.gz -> foobar.log.1.gz
Naoki Shiota4e463182014-03-21 15:13:24 -0700232function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700233 local logfile=$1
234 local nr_max=${2:-10}
235 if [ -f $logfile ]; then
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700236 # TODO treating only .gz now. probably want more generic solution
237 local basename=${logfile%%.gz}
238 local append=""
239 if [ "$basename" != "$logfile" ]; then
240 append=".gz"
241 fi
Naoki Shiota4e928512014-04-03 15:49:35 -0700242 for i in `seq $(expr $nr_max - 1) -1 1`; do
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700243 if [ -f ${basename}.${i}${append} ]; then
244 mv -f ${basename}.${i}${append} ${basename}.`expr $i + 1`${append}
Naoki Shiota4e928512014-04-03 15:49:35 -0700245 fi
246 done
Yuta HIGUCHIb138a8c2014-06-19 11:07:20 -0700247 mv ${basename}${append} ${basename}.1${append}
Naoki Shiota4e928512014-04-03 15:49:35 -0700248 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700249}
250
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700251# pack-rotate-log [packname] "[log-filenames]" [max rotations]
252# Note: [packname] and all the log-files specified by [log-filenames]
253# must reside in same dir
254# Example:
255# pack="/foo/bar/testlogs"
256# logfiles="/foo/bar/test1.log /foo/bar/test*.log"
257# pack-rotate-log $pack "$logfiles" 5
258# => testlogs.tar.bz2 (contain test1.log test2.log ...)
259# testlogs.tar.bz2 -> testlogs.1.tar.bz2
260# testlogs.1.tar.bz2 -> testlogs.2.tar.bz2
261# ...
262function pack-rotate-log {
263 local packname=$1
264 local logfiles=$2
265 local nr_max=${3:-10}
266 local suffix=".tar.bz2"
267
268 # rotate
269 for i in `seq $(expr $nr_max - 1) -1 1`; do
270 if [ -f ${packname}.${i}${suffix} ]; then
271 mv -f -- ${packname}.${i}${suffix} ${packname}.`expr $i + 1`${suffix}
272 fi
273 done
274 if [ -f ${packname}${suffix} ]; then
275 mv -- ${packname}${suffix} ${packname}.1${suffix}
276 fi
277
278 # pack
279 local existing_logfiles=$( ls -1 $logfiles | xargs basename 2>/dev/null )
280 if [ ! -z "${existing_logfiles}" ]; then
281 tar cjf ${packname}${suffix} -C `dirname ${packname}` -- ${existing_logfiles}
282 rm -- `dirname ${packname}`/${existing_logfiles}
283 fi
284}
285
Naoki Shiota4e463182014-03-21 15:13:24 -0700286# kill-processes {module-name} {array of pids}
287function kill-processes {
288 # Kill the existing processes
289 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700290 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700291 echo -n "Stopping $1 ... "
292 fi
293 for p in ${pids}; do
294 if [ x$p != "x" ]; then
Naoki Shiota4b355762014-05-27 11:46:43 -0700295 # Check if target process is accesible from current user
296 kill -0 $p
297 if [ "$?" -ne 0 ]; then
298 # Error exit code here means "failed to send signal".
299 # Supposedly because of permission error.
300 echo "Failed to kill process (pid: $p)."
301 echo "Check if current user is the same as process owner."
302 exit 1
303 fi
304
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700305 (
306 # Ask process with SIGTERM first, if that did not kill the process
307 # wait 1s and if process still exist, force process to be killed.
308 kill -TERM $p && kill -0 $p && sleep 1 && kill -0 $p && kill -KILL $p
309 ) 2> /dev/null
Naoki Shiota4b355762014-05-27 11:46:43 -0700310
311 # Ensure process is killed.
312 kill -0 $p 2> /dev/null
313 if [ "$?" -ne 0 ]; then
314 # Error exit code here means "process not found", i.e. "kill succeeded".
315 echo "Killed existing process (pid: $p)"
316 else
317 # Process still exists. Some unexpected error occurs.
318 echo "Failed to kill process (pid: $p)."
319 echo "Unexpected error occurs during process termination."
320 exit 1
321 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700322 fi
323 done
324}
325
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700326function handle-error {
327 set -e
328
329 revert-confs
330
331 set +e
332
333 exit 1
334}
335
Naoki Shiota4e928512014-04-03 15:49:35 -0700336# revert-confs [error message]
337function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700338 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700339
Naoki Shiota4e928512014-04-03 15:49:35 -0700340 revert-file `basename ${ZK_CONF}`
341 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700342
343 echo "EXIT"
344
345 if [ ! -z "$1" ]; then
346 echo $1
347 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700348}
349
Naoki Shiota590c18d2014-03-31 17:52:59 -0700350function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700351 echo -n "Creating ${ZK_CONF} ... "
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700352
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700353 # Create the ZooKeeper lib directory
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700354 if [[ ! ( -w ${ZK_LIB_DIR} && -d ${ZK_LIB_DIR} ) ]]; then
Naoki Shiota7f495cf2014-05-21 17:23:25 -0700355 local SUDO=${SUDO:-sudo}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700356 local whoami=`whoami`
357 {
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700358 ${SUDO} mkdir -p ${ZK_LIB_DIR}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700359 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
360 } || {
361 echo "FAILED"
362 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
363 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
364 exit 1
365 }
366 fi
367
368 # creation of ZooKeeper config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700369 local temp_zk=`begin-conf-creation ${ZK_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700370
Naoki Shiota4e928512014-04-03 15:49:35 -0700371 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700372
373 local i=1
374 local myid=
375 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700376 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700377 myid=$i
378 break
379 fi
380 i=`expr $i + 1`
381 done
382
383 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700384 local filename=`basename ${ONOS_CONF}`
Naoki Shiota35f8d5c2014-04-08 11:13:18 -0700385 revert-confs "[ERROR] In ${filename}, zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700386 fi
387
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700388 if [ -f "${ZK_MY_ID}" ]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700389 # sudo will be needed if ZK_MY_ID is already created by other (old) script
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700390 local SUDO=${SUDO:-}
391 {
392 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
393 } || {
394 echo "FAILED"
395 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700396 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700397 exit 1
398 }
399 fi
400
Naoki Shiota9df15d32014-03-27 14:26:20 -0700401 echo ${myid} > ${ZK_MY_ID}
402
403 echo -n "myid is assigned to ${myid} ... "
404
405 while read line; do
406 if [[ $line =~ ^__HOSTS__$ ]]; then
407 i=1
408 for host in ${hostarr}; do
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700409 # TODO: ZK ports should be configurable per host
410 local hostline="server.${i}=${host}:${ZK_PORTS}"
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700411 echo $hostline
Naoki Shiota9df15d32014-03-27 14:26:20 -0700412 i=`expr $i + 1`
413 done
414 elif [[ $line =~ __DATADIR__ ]]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700415 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700416 else
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700417 echo $line
Naoki Shiota9df15d32014-03-27 14:26:20 -0700418 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700419 done < ${ZK_CONF_TEMPLATE} > ${temp_zk}
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700420 sed -e "s|__CLIENTPORT__|${ZK_CLIENTPORT}|" -i "" ${temp_zk}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700421
422 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700423
424 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700425}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700426
Naoki Shiota4e928512014-04-03 15:49:35 -0700427function create-hazelcast-conf {
428 echo -n "Creating ${HC_CONF} ... "
429
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700430 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700431
432 # To keep indent of XML file, change IFS
433 local IFS=''
434 while read line; do
435 if [[ $line =~ __HC_NETWORK__ ]]; then
436 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
437 # temporary change
438 IFS=' '
439 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
440 echo '<multicast enabled="false" />'
441 echo '<tcp-ip enabled="true">'
442 for member in ${memberarr}; do
443 echo " <member>${member}</member>"
444 done
445 echo '</tcp-ip>'
446 IFS=''
447 else
448 echo '<multicast enabled="true">'
449 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
450 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
451 echo '</multicast>'
452 echo '<tcp-ip enabled="false" />'
453 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700454 elif [[ $line =~ __HC_PORT__ ]]; then
455 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700456 elif [[ $line =~ __HC_CLUSTER__ ]]; then
457 echo $line | sed -e "s|__HC_CLUSTER__|${ONOS_CLUSTER_NAME}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700458 else
459 echo "${line}"
460 fi
461 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
462
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700463 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700464
465 echo "DONE"
466}
467
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700468function create-ramcloud-conf {
469 echo -n "Creating ${RAMCLOUD_CONF} ... "
470
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700471 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700472
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700473 local rc_locator=`echo "${ZK_HOSTS}" | sed -e "s/,/:${ZK_CLIENTPORT},/g"`
474 rc_locator+=":${ZK_CLIENTPORT}"
475
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700476 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700477
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700478 echo "ramcloud.locator=zk:localhost:2181,${rc_locator}" > ${temp_rc}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700479 echo "ramcloud.clusterName=${rc_cluster_name}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700480
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700481 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700482
483 echo "DONE"
484}
485
Naoki Shiota590c18d2014-03-31 17:52:59 -0700486function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700487 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700488
Naoki Shiota9df15d32014-03-27 14:26:20 -0700489 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700490 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700491
492 sed -e "s|__FILENAME__|${ONOS_LOG}|" \
493 -e "s|__ROLLING_PATTERN__|${ONOS_LOG_ROLLING_PATTERN}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700494
495 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700496
Naoki Shiota9df15d32014-03-27 14:26:20 -0700497 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700498}
499
Naoki Shiota4e928512014-04-03 15:49:35 -0700500function create-confs {
501 local key
502 local filename
503
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700504 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700505
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700506 echo "Config file : ${ONOS_CONF}"
507
Naoki Shiota4e928512014-04-03 15:49:35 -0700508 if [ "$1" == "-f" ]; then
509 create-zk-conf
510 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700511 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700512 create-logback-conf
513 else
514 create-conf-interactive ${ZK_CONF} create-zk-conf
515 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700516 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700517 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
518 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700519
520 trap - ERR
521}
522############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700523
Naoki Shiota590c18d2014-03-31 17:52:59 -0700524
525###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700526function zk {
527 case "$1" in
528 start)
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700529 check-and-start-zk
Naoki Shiota4e463182014-03-21 15:13:24 -0700530 ;;
531 stop)
532 stop-zk
533 ;;
534 stat*) # <- status
535 status-zk
536 ;;
537 re*) # <- restart
538 stop-zk
539 start-zk
540 ;;
541 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700542 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700543 exit 1
544 esac
545}
546
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700547function load-zk-cfg {
548 if [ -f "${ZK_CONF}" ]; then
549 local filename=`basename ${ZK_CONF}`
550 local dirname=`dirname ${ZK_CONF}`
551
552 # Run ZooKeeper with our configuration
553 export ZOOCFG=${filename}
554 export ZOOCFGDIR=${dirname}
555 fi
556}
557
Naoki Shiota4e463182014-03-21 15:13:24 -0700558function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700559 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700560
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700561 # CONSOLE output dir
Naoki Shiota4e928512014-04-03 15:49:35 -0700562 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700563 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700564
565 load-zk-cfg
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700566
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700567 # level,appender
568 export ZOO_LOG4J_PROP="INFO,ROLLINGFILE"
569 # ROLLINGFILE : ${zookeeper.log.dir}/${zookeeper.log.file}
570 local server_jvm="-Dzookeeper.log.dir=${LOGDIR} -Dzookeeper.log.file=zk.${ONOS_HOST_NAME}.log"
571
572 pack-rotate-log "${LOGDIR}/zk.${ONOS_HOST_NAME}" "${LOGDIR}/zk.${ONOS_HOST_NAME}.log*"
573
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700574 # log4j.properties is read from classpath if not found in CWD.
575 # Using the ZooKeeper supplied default in ZooKeeper conf dir.
576 # TODO: To explicitly specify our customized log4j config file:
577 # export SERVER_JVMFLAGS="-Dlog4j.configuration=${ZK_LOG4J}"
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700578 env CLASSPATH="${ZK_HOME}/conf:${CLASSPATH}" SERVER_JVMFLAGS="${server_jvm}" ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700579}
580
581function stop-zk {
Yuta HIGUCHI7c708362014-06-02 23:15:13 -0700582 echo -n "Stopping ZooKeeper ... "
583 load-zk-cfg
584
585 ${ZK_HOME}/bin/zkServer.sh stop
Naoki Shiota4e463182014-03-21 15:13:24 -0700586}
587
588function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700589 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700590
Naoki Shiota72209722014-04-08 14:32:17 -0700591 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700592}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700593
594function check-zk {
595 # assumption here is that ZK status script is the last command in status-zk.
596 status-zk &> /dev/null
597 local zk_status=$?
598 if [ "$zk_status" -ne 0 ]; then
599 return 1;
600 fi
601 return 0
602}
603
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700604function check-and-start-zk {
605 check-zk
606 local zk_status=$?
607 if [ "$zk_status" -ne 0 ]; then
608 start-zk
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700609 else
610 echo "ZooKeeper already running"
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700611 fi
612}
613
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700614# wait-zk-or-die {timeout-sec}
615function wait-zk-or-die {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700616 local retries=${1:-5}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700617 # do-while retries >= 0
618 while true; do
619 check-zk
620 local zk_status=$?
621 if [ "$zk_status" -eq 0 ]; then
622 return 0
623 fi
624 sleep 1;
625 ((retries -= 1))
626 (( retries >= 0 )) || break
627 done
628 echo "ZooKeeper is not running."
629 exit 1
630}
631
Naoki Shiota590c18d2014-03-31 17:52:59 -0700632############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700633
634
Naoki Shiota590c18d2014-03-31 17:52:59 -0700635####### Functions related to RAMCloud ######
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -0700636
637# Check if this host should be running RAMCloud coordinator:
638function is-coord-role {
639 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
640 case "${ONOS_HOST_ROLE}" in
641 single-node)
642 echo "true"
643 return 0
644 ;;
645 coord-node)
646 echo "true"
647 return 0
648 ;;
649 server-node)
650 echo "false"
651 return 1
652 ;;
653 coord-and-server-node)
654 echo "true"
655 return 0
656 ;;
657 *)
658 echo "false"
659 return 1
660 ;;
661 esac
662 else
663 echo "false"
664 return 1
665 fi
666}
667
668# Check if this host should be running RAMCloud server:
669function is-server-role {
670 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
671 case "${ONOS_HOST_ROLE}" in
672 single-node)
673 echo "true"
674 return 0
675 ;;
676 coord-node)
677 echo "false"
678 return 1
679 ;;
680 server-node)
681 echo "true"
682 return 0
683 ;;
684 coord-and-server-node)
685 echo "true"
686 return 0
687 ;;
688 *)
689 echo "false"
690 return 1
691 ;;
692 esac
693 else
694 echo "false"
695 return 1
696 fi
697}
698
Naoki Shiota9df15d32014-03-27 14:26:20 -0700699function start-backend {
700 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700701 if [ $1 == "coord" ]; then
702 rc-coord startifdown
703 elif [ $1 == "server" ]; then
704 rc-server startifdown
705 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700706 fi
707}
708
709function stop-backend {
710 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
711 if [ $rcsn != 0 ]; then
712 rc-server stop
713 fi
714
Naoki Shiota590c18d2014-03-31 17:52:59 -0700715 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700716 if [ $rccn != 0 ]; then
717 rc-coord stop
718 fi
719}
720
Naoki Shiota590c18d2014-03-31 17:52:59 -0700721
Naoki Shiota4e463182014-03-21 15:13:24 -0700722### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700723function rc-coord-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700724 local host=${RC_COORD_IP}
725 if [ -z "${host}" ]; then
726 # falling back to 0.0.0.0
727 host="0.0.0.0"
728 fi
729 echo "${RC_COORD_PROTOCOL}:host=${host},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700730}
731
732function rc-server-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700733 local host=${RC_SERVER_IP}
734 if [ -z "${host}" ]; then
735 # falling back to 0.0.0.0
736 host="0.0.0.0"
737 fi
738 echo "${RC_SERVER_PROTOCOL}:host=${host},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700739}
740
Naoki Shiota4e463182014-03-21 15:13:24 -0700741function rc-coord {
742 case "$1" in
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700743 start*) # <- start, startifdown
Naoki Shiota590c18d2014-03-31 17:52:59 -0700744 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700745 if [ $n == 0 ]; then
746 start-coord
747 else
748 echo "$n instance of RAMCloud coordinator running"
749 fi
750 ;;
751 stop)
752 stop-coord
753 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700754 deldb)
755 stop-backend
756 del-coord-info
757 ;;
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700758 re*) # <- restart
759 stop-coord
760 start-coord
761 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700762 stat*) # <- status
763 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
764 echo "$n RAMCloud coordinator running"
765 ;;
766 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700767 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700768 exit 1
769 esac
770}
771
772function start-coord {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700773 check-and-start-zk
774 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700775
Naoki Shiota4e463182014-03-21 15:13:24 -0700776 if [ ! -d ${LOGDIR} ]; then
777 mkdir -p ${LOGDIR}
778 fi
779 if [ -f $RAMCLOUD_COORD_LOG ]; then
780 rotate-log $RAMCLOUD_COORD_LOG
781 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700782
783 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700784
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700785 # TODO Make ONOS_CONF readable from ONOS process then eliminate RAMCLOUD_CONF
786
787 # Configuration for ZK address, port
788 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
789
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700790 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700791 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700792
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700793 # RAMCloud transport timeout
794 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700795 # RAMCloud option deadServerTimeout
796 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
797 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
798
799 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
800 # but actual RAMCloud code only uses -C argument now.
801 # (FYI: -C is documented to be deprecated in the document)
802
803 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700804 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700805 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700806 coord_args="${coord_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700807 coord_args="${coord_args} --deadServerTimeout ${rc_coord_deadServerTimeout}"
808
809 # Read environment variables if set
810 coord_args="${coord_args} ${RC_COORDINATOR_OPTS}"
811
812 if [ "${ONOS_HOST_ROLE}" == "single-node" ]; then
813 # Note: Following reset is required, since RC restart is considered node failure,
814 # and tries recovery of server, which will never succeed after restart.
815 echo "Role configured to single-node mode. RAMCloud cluster will be reset on each start-up."
816 coord_args="${coord_args} --reset"
817 fi
818
Naoki Shiota4e463182014-03-21 15:13:24 -0700819 # Run ramcloud
820 echo -n "Starting RAMCloud coordinator ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700821 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700822 echo "STARTED"
823}
824
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700825function del-coord-info {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700826 check-and-start-zk
827 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700828
829 if [ ! -d ${LOGDIR} ]; then
830 mkdir -p ${LOGDIR}
831 fi
832 if [ -f $RAMCLOUD_COORD_LOG ]; then
833 rotate-log $RAMCLOUD_COORD_LOG
834 fi
835
836 local coord_addr=`rc-coord-addr`
837
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700838 # Configuration for ZK address, port
839 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700840 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700841 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700842 # RAMCloud option deadServerTimeout
843 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
844 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
845
846 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
847 # but actual RAMCloud code only uses -C argument now.
848 # (FYI: -C is documented to be deprecated in the document)
849
850 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700851 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700852 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
853
854 # Note: --reset will reset ZK stored info and start running as acoordinator.
855 echo -n "Deleting RAMCloud cluster coordination info ... "
856 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} --reset &> $RAMCLOUD_COORD_LOG &
857
Yuta HIGUCHIcd44eb32014-05-24 16:51:31 -0700858 # TODO Assuming 5 sec is enough. To be sure monitor log?
859 sleep 5
860 # kill coordinator
Yuta HIGUCHIce9f3ee2014-05-14 09:48:40 -0700861 (pkill -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator &> /dev/null)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700862
863 echo "DONE"
864}
Naoki Shiota4e463182014-03-21 15:13:24 -0700865
866function stop-coord {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700867 kill-processes "RAMCloud coordinator" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700868}
869
Naoki Shiota4e463182014-03-21 15:13:24 -0700870### Functions related to RAMCloud server
871function rc-server {
872 case "$1" in
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700873 start*) # <- start, startifdown
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700874 local n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700875 if [ $n == 0 ]; then
876 start-server
877 else
878 echo "$n instance of RAMCloud server running"
879 fi
880 ;;
881 stop)
882 stop-server
883 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700884 deldb)
885 stop-server
886 del-server-backup
887 ;;
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700888 re*) # <- restart
889 stop-server
890 start-server
891 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700892 stat*) # <- status
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700893 n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700894 echo "$n RAMCloud server running"
895 ;;
896 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700897 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700898 exit 1
899 esac
900}
901
902function start-server {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700903 check-and-start-zk
904 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700905
Naoki Shiota4e463182014-03-21 15:13:24 -0700906 if [ ! -d ${LOGDIR} ]; then
907 mkdir -p ${LOGDIR}
908 fi
909 if [ -f $RAMCLOUD_SERVER_LOG ]; then
910 rotate-log $RAMCLOUD_SERVER_LOG
911 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700912
913 local coord_addr=`rc-coord-addr`
914 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700915
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700916 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.server.masterServiceThreads 5)
917 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.server.logCleanerThreads 1)
Yuta HIGUCHI52efcbb2014-05-16 09:30:33 -0700918 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.server.detectFailures 0)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700919
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700920 # Configuration for ZK address, port
921 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700922 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700923 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700924 # RAMCloud transport timeout
925 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700926 # replication factor (-r) config
927 local rc_replicas=$(read-conf ${ONOS_CONF} ramcloud.server.replicas 0)
928 # backup file path (-f) config
929 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
930 mkdir -p `dirname ${rc_datafile}`
931
932 local server_args="-L ${server_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700933 server_args="${server_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700934 server_args="${server_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700935 server_args="${server_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700936 server_args="${server_args} --masterServiceThreads ${masterServiceThreads}"
937 server_args="${server_args} --logCleanerThreads ${logCleanerThreads}"
938 server_args="${server_args} --detectFailures ${detectFailures}"
939 server_args="${server_args} --replicas ${rc_replicas}"
940 server_args="${server_args} --file ${rc_datafile}"
941
942 # Read environment variables if set
943 server_args="${server_args} ${RC_SERVER_OPTS}"
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700944
Naoki Shiota4e463182014-03-21 15:13:24 -0700945 # Run ramcloud
946 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700947 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server ${server_args} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700948 echo "STARTED"
949}
950
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700951function del-server-backup {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700952 echo -n "Delete RAMCloud backup server data [y/N]? "
953 while [ 1 ]; do
954 read key
955 if [ "${key}" == "Y" -o "${key}" == "y" ]; then
956 break
957 elif [ -z "${key}" -o "${key}" == "N" -o "${key}" == "n" ]; then
958 echo "Cancelled."
959 return
960 fi
961 echo "[y/N]? "
962 done
963
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700964 echo -n "Removing RAMCloud backup server data ... "
965 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
966 rm -f ${rc_datafile}
967 echo "DONE"
968}
969
Naoki Shiota4e463182014-03-21 15:13:24 -0700970function stop-server {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700971 kill-processes "RAMCloud server" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700972}
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## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700977function onos {
Naoki Shiota4e463182014-03-21 15:13:24 -0700978
979 case "$1" in
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700980 start*) # <- start, startnokill, startifdown
Naoki Shiota4e463182014-03-21 15:13:24 -0700981 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
982 if [ $n == 0 ]; then
983 start-onos
984 else
985 echo "$n instance of onos running"
986 fi
987 ;;
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700988 unchecked-start)
989 start-onos
990 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700991 stop)
992 stop-onos
993 ;;
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700994 re*) # <- restart
995 stop-onos
996 start-onos
997 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700998 stat*) # <- status
999 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1000 echo "$n instance of onos running"
1001 ;;
1002 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001003 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001004 exit 1
1005 esac
1006}
1007
1008function start-onos {
Yuta HIGUCHI5f14b012014-07-18 19:14:04 -07001009
1010 JAVA_CP=""
1011 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
1012 if [ ! -f ${MAIN_CLASS_FILE} ]; then
1013 # No .class
1014 if [ -f ${ONOS_JAR} ]; then
1015 echo "Starting ONOS using packaged jar file"
1016 JAVA_CP="${JAVA_CP}:${ONOS_JAR}"
1017 else
1018 echo "Building ONOS from source"
1019 ${MVN} -f ${ONOS_HOME}/pom.xml compile -T 1C
1020 fi
1021 else
1022 # .class exists
1023 if [ ! -f ${CPFILE} ]; then
1024 echo "Building ONOS dependency classpath"
1025 ${MVN} -f ${ONOS_HOME}/pom.xml -T 1C dependency:go-offline -Dsilent dependency:build-classpath -Dmdep.outputFile=${CPFILE}
1026 fi
1027 fi
1028 if [ -f ${CPFILE} ]; then
1029 JAVA_CP="${JAVA_CP}:`cat ${CPFILE}`"
1030 fi
1031 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
1032
Naoki Shiota4e463182014-03-21 15:13:24 -07001033 if [ ! -d ${LOGDIR} ]; then
1034 mkdir -p ${LOGDIR}
1035 fi
Yuta HIGUCHI04713972014-05-30 11:01:17 -07001036 # Rotate log files
Naoki Shiota4e463182014-03-21 15:13:24 -07001037 for log in ${LOGS}; do
1038 if [ -f ${log} ]; then
1039 rotate-log ${log}
1040 fi
1041 done
Yuta HIGUCHI04713972014-05-30 11:01:17 -07001042
1043 # Rotate logs rolled at runtime.
1044 local rolled_log_shellpat=`echo ${ONOS_LOG_ROLLING_PATTERN} | sed -e "s/%i/\\*/"`
1045 for rolled_log in ${rolled_log_shellpat}; do
1046 if [ -f ${rolled_log} ]; then
1047 rotate-log ${rolled_log}
Yuta HIGUCHI04713972014-05-30 11:01:17 -07001048 fi
1049 done
1050
Naoki Shiota4e463182014-03-21 15:13:24 -07001051 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -07001052 echo "[WARNING] ${ONOS_LOGBACK} not found."
1053 echo " Run \"\$ $0 setup\" to create."
1054 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -07001055 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -07001056
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -07001057 if [ ! -f ${HC_CONF} ]; then
1058 echo "[WARNING] ${HC_CONF} not found."
1059 echo " Run \"\$ $0 setup\" to create."
1060 exit 1
1061 fi
1062
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -07001063 # specify ZooKeeper(curator) namespace
1064 JVM_OPTS="${JVM_OPTS} -Dzookeeper.namespace=${ONOS_CLUSTER_NAME}"
1065
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -07001066 # specify ZooKeeper connectionString
1067 local zk_locator=`echo "${ZK_HOSTS}" | sed -e "s/,/:${ZK_CLIENTPORT},/g"`
1068 zk_locator+=":${ZK_CLIENTPORT}"
1069 zk_locator="localhost:${ZK_CLIENTPORT},${zk_locator}"
1070
1071 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.registry.ZookeeperRegistry.connectionString=${zk_locator}"
1072
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -07001073 # specify hazelcast.xml to datagrid
1074 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
1075
1076 # specify backend config
Jonathan Hartef3dc1a2014-04-03 11:39:50 -07001077 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -07001078 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
1079 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -07001080 elif [ "${ONOS_HOST_BACKEND}" = "hazelcast" ]; then
1081 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.hazelcast.baseConfig=${HC_CONF}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -07001082 fi
1083
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -07001084 # check for jvm debug flag
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -07001085 if [ "${ONOS_DEBUG}" == "true" ]; then
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -07001086 JVM_OPTS="${JVM_OPTS} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${JVM_DEBUG_PORT}"
1087 fi
1088
Naoki Shiota9df15d32014-03-27 14:26:20 -07001089 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -07001090
1091 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
1092 cd ${ONOS_HOME}
1093
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001094 echo -n "Starting ONOS controller ... "
Naoki Shiota4e463182014-03-21 15:13:24 -07001095 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
1096
1097 # We need to wait a bit to find out whether starting the ONOS process succeeded
1098 sleep 1
1099
1100 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -07001101 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -07001102 echo " STARTED"
1103 else
1104 echo " FAILED"
1105 fi
1106
1107# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
1108# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
1109}
1110
1111function stop-onos {
1112 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
1113# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
1114}
Naoki Shiota590c18d2014-03-31 17:52:59 -07001115############################################
Naoki Shiota4e463182014-03-21 15:13:24 -07001116
1117
Naoki Shiota590c18d2014-03-31 17:52:59 -07001118################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -07001119case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -07001120 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -07001121 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -07001122 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001123 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -07001124 mode_parameter=${ONOS_HOST_ROLE}
1125 if [ ! -z "$2" ]; then
1126 mode_parameter=$2
1127 fi
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -07001128
Naoki Shiota9df15d32014-03-27 14:26:20 -07001129 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -07001130 single-node)
1131 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001132 start-backend coord
1133 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001134 onos startifdown
1135 ;;
1136 coord-node)
1137 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001138 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -07001139 onos startifdown
1140 ;;
1141 server-node)
1142 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001143 start-backend server
1144 onos startifdown
1145 ;;
1146 coord-and-server-node)
1147 zk start
1148 start-backend coord
1149 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001150 onos startifdown
1151 ;;
1152 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001153 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001154 ;;
1155 esac
1156 echo
1157 ;;
1158 stop)
1159 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1160 if [ $on != 0 ]; then
1161 onos stop
1162 fi
1163
Naoki Shiota9df15d32014-03-27 14:26:20 -07001164 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -07001165
1166 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1167 if [ $zkn != 0 ]; then
1168 zk stop
1169 fi
1170 echo
1171 ;;
1172 restart)
1173 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1174 if [ $on != 0 ]; then
1175 onos stop
1176 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001177
Naoki Shiota4e463182014-03-21 15:13:24 -07001178 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
1179 if [ $rcsn != 0 ]; then
1180 rc-server stop
1181 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001182
Naoki Shiota590c18d2014-03-31 17:52:59 -07001183 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -07001184 if [ $rccn != 0 ]; then
1185 rc-coord stop
1186 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001187
Naoki Shiota4e463182014-03-21 15:13:24 -07001188 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1189 if [ $zkn != 0 ]; then
1190 zk restart
1191 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001192
Naoki Shiota4e463182014-03-21 15:13:24 -07001193 if [ $rccn != 0 ]; then
1194 rc-coord startifdown
1195 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001196
Naoki Shiota4e463182014-03-21 15:13:24 -07001197 if [ $rcsn != 0 ]; then
1198 rc-server startifdown
1199 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001200
Naoki Shiota4e463182014-03-21 15:13:24 -07001201 if [ $on != 0 ]; then
1202 onos startifdown
1203 fi
1204 echo
1205 ;;
1206 stat*) # <- status
1207 echo '[ZooKeeper]'
1208 zk status
1209 echo
1210 echo '[RAMCloud coordinator]'
1211 rc-coord status
1212 echo
1213 echo '[RAMCloud server]'
1214 rc-server status
1215 echo
1216 echo '[ONOS core]'
1217 onos status
1218 echo
1219 ;;
1220 zk)
1221 zk $2
1222 ;;
1223 rc-c*) # <- rc-coordinator
1224 rc-coord $2
1225 ;;
1226 rc-s*) # <- rc-server
1227 rc-server $2
1228 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001229 rc)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001230 # XXX rc command ignores if the host is not configured to have those roles,
1231 # while rc-s or rc-c executes the task regardless of role definition.
1232 # This is a workaround since TestON does not take role in account
1233 # and always issue rc deldb right now.
1234
Naoki Shiota9109a1e2014-05-13 11:11:01 -07001235 # TODO make deldb command more organized (clarify when it can work)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001236 if [ "`is-coord-role`" == "true" ]; then
1237 rc-coord $2
1238 fi
1239 if [ "`is-server-role`" == "true" ]; then
1240 rc-server $2
1241 fi
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001242 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001243 core)
1244 onos $2
1245 ;;
1246 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001247 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001248 exit 1
1249esac
Naoki Shiota590c18d2014-03-31 17:52:59 -07001250