blob: 7930cc9ca3660b8eeb7c5d8085e83c663564eae9 [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)
Yuta HIGUCHI88ad04e2014-09-02 16:45:35 -07005# $ONOS_CONF_DIR : path of ONOS config directory (${ONOS_HOME}/conf)
6# $ONOS_CONF : path of ONOS node config file (${ONOS_CONF_DIR}/onos_node.`hostname`.conf or onos_node.conf)
7# $ONOS_PROPS : path of ONOS properties file (${ONOS_CONF_DIR}/onos.properties)
8# $ONOS_LOGBACK : path of logback config file (${ONOS_CONF_DIR}/logback.`hostname`.xml)
9# $ONOS_LOGDIR : path of log output directory (${ONOS_HOME}/onos-logs)
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070010# $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)
Yuta HIGUCHI88ad04e2014-09-02 16:45:35 -070016# $ZK_LOG_DIR : path of ZooKeeper console log output directory (${ONOS_LOGDIR}/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)
Yuta HIGUCHI88ad04e2014-09-02 16:45:35 -070019# $ZK_CONF : path of ZooKeeper config file (${ONOS_CONF_DIR}/zoo.cfg)
20# $HC_CONF : path of Hazelcast config file (${ONOS_CONF_DIR}/hazelcast.xml)
21# $RAMCLOUD_CONF : path of RAMCloud config file (${ONOS_CONF_DIR}/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
Yuta HIGUCHIb54ea172014-09-02 16:34:43 -0700419 done < ${ZK_CONF_TEMPLATE} | sed -e "s|__CLIENTPORT__|${ZK_CLIENTPORT}|" > ${temp_zk}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700420
421 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700422
423 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700424}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700425
Naoki Shiota4e928512014-04-03 15:49:35 -0700426function create-hazelcast-conf {
427 echo -n "Creating ${HC_CONF} ... "
428
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700429 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700430
431 # To keep indent of XML file, change IFS
432 local IFS=''
433 while read line; do
434 if [[ $line =~ __HC_NETWORK__ ]]; then
435 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
436 # temporary change
437 IFS=' '
438 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
439 echo '<multicast enabled="false" />'
440 echo '<tcp-ip enabled="true">'
441 for member in ${memberarr}; do
442 echo " <member>${member}</member>"
443 done
444 echo '</tcp-ip>'
445 IFS=''
446 else
447 echo '<multicast enabled="true">'
448 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
449 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
450 echo '</multicast>'
451 echo '<tcp-ip enabled="false" />'
452 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700453 elif [[ $line =~ __HC_PORT__ ]]; then
454 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700455 elif [[ $line =~ __HC_CLUSTER__ ]]; then
456 echo $line | sed -e "s|__HC_CLUSTER__|${ONOS_CLUSTER_NAME}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700457 else
458 echo "${line}"
459 fi
460 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
461
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700462 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700463
464 echo "DONE"
465}
466
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700467function create-ramcloud-conf {
468 echo -n "Creating ${RAMCLOUD_CONF} ... "
469
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700470 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700471
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700472 local rc_locator=`echo "${ZK_HOSTS}" | sed -e "s/,/:${ZK_CLIENTPORT},/g"`
473 rc_locator+=":${ZK_CLIENTPORT}"
474
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700475 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700476
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -0700477 echo "ramcloud.locator=zk:localhost:2181,${rc_locator}" > ${temp_rc}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700478 echo "ramcloud.clusterName=${rc_cluster_name}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700479
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700480 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700481
482 echo "DONE"
483}
484
Naoki Shiota590c18d2014-03-31 17:52:59 -0700485function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700486 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700487
Naoki Shiota9df15d32014-03-27 14:26:20 -0700488 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700489 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700490
491 sed -e "s|__FILENAME__|${ONOS_LOG}|" \
492 -e "s|__ROLLING_PATTERN__|${ONOS_LOG_ROLLING_PATTERN}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700493
494 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700495
Naoki Shiota9df15d32014-03-27 14:26:20 -0700496 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700497}
498
Naoki Shiota4e928512014-04-03 15:49:35 -0700499function create-confs {
500 local key
501 local filename
502
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700503 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700504
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700505 echo "Config file : ${ONOS_CONF}"
506
Naoki Shiota4e928512014-04-03 15:49:35 -0700507 if [ "$1" == "-f" ]; then
508 create-zk-conf
509 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700510 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700511 create-logback-conf
512 else
513 create-conf-interactive ${ZK_CONF} create-zk-conf
514 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700515 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700516 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
517 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700518
519 trap - ERR
520}
521############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700522
Naoki Shiota590c18d2014-03-31 17:52:59 -0700523
524###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700525function zk {
526 case "$1" in
527 start)
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700528 check-and-start-zk
Naoki Shiota4e463182014-03-21 15:13:24 -0700529 ;;
530 stop)
531 stop-zk
532 ;;
533 stat*) # <- status
534 status-zk
535 ;;
536 re*) # <- restart
537 stop-zk
538 start-zk
539 ;;
540 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700541 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700542 exit 1
543 esac
544}
545
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700546function load-zk-cfg {
547 if [ -f "${ZK_CONF}" ]; then
548 local filename=`basename ${ZK_CONF}`
549 local dirname=`dirname ${ZK_CONF}`
550
551 # Run ZooKeeper with our configuration
552 export ZOOCFG=${filename}
553 export ZOOCFGDIR=${dirname}
554 fi
555}
556
Naoki Shiota4e463182014-03-21 15:13:24 -0700557function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700558 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700559
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700560 # CONSOLE output dir
Naoki Shiota4e928512014-04-03 15:49:35 -0700561 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700562 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700563
564 load-zk-cfg
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700565
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700566 # level,appender
567 export ZOO_LOG4J_PROP="INFO,ROLLINGFILE"
568 # ROLLINGFILE : ${zookeeper.log.dir}/${zookeeper.log.file}
569 local server_jvm="-Dzookeeper.log.dir=${LOGDIR} -Dzookeeper.log.file=zk.${ONOS_HOST_NAME}.log"
570
571 pack-rotate-log "${LOGDIR}/zk.${ONOS_HOST_NAME}" "${LOGDIR}/zk.${ONOS_HOST_NAME}.log*"
572
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700573 # log4j.properties is read from classpath if not found in CWD.
574 # Using the ZooKeeper supplied default in ZooKeeper conf dir.
575 # TODO: To explicitly specify our customized log4j config file:
576 # export SERVER_JVMFLAGS="-Dlog4j.configuration=${ZK_LOG4J}"
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700577 env CLASSPATH="${ZK_HOME}/conf:${CLASSPATH}" SERVER_JVMFLAGS="${server_jvm}" ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700578}
579
580function stop-zk {
Yuta HIGUCHI7c708362014-06-02 23:15:13 -0700581 echo -n "Stopping ZooKeeper ... "
582 load-zk-cfg
583
584 ${ZK_HOME}/bin/zkServer.sh stop
Naoki Shiota4e463182014-03-21 15:13:24 -0700585}
586
587function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700588 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700589
Naoki Shiota72209722014-04-08 14:32:17 -0700590 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700591}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700592
593function check-zk {
594 # assumption here is that ZK status script is the last command in status-zk.
595 status-zk &> /dev/null
596 local zk_status=$?
597 if [ "$zk_status" -ne 0 ]; then
598 return 1;
599 fi
600 return 0
601}
602
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700603function check-and-start-zk {
604 check-zk
605 local zk_status=$?
606 if [ "$zk_status" -ne 0 ]; then
607 start-zk
Yuta HIGUCHId1941ee2014-08-06 23:48:03 -0700608 else
609 echo "ZooKeeper already running"
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700610 fi
611}
612
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700613# wait-zk-or-die {timeout-sec}
614function wait-zk-or-die {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700615 local retries=${1:-5}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700616 # do-while retries >= 0
617 while true; do
618 check-zk
619 local zk_status=$?
620 if [ "$zk_status" -eq 0 ]; then
621 return 0
622 fi
623 sleep 1;
624 ((retries -= 1))
625 (( retries >= 0 )) || break
626 done
627 echo "ZooKeeper is not running."
628 exit 1
629}
630
Naoki Shiota590c18d2014-03-31 17:52:59 -0700631############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700632
633
Naoki Shiota590c18d2014-03-31 17:52:59 -0700634####### Functions related to RAMCloud ######
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -0700635
636# Check if this host should be running RAMCloud coordinator:
637function is-coord-role {
638 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
639 case "${ONOS_HOST_ROLE}" in
640 single-node)
641 echo "true"
642 return 0
643 ;;
644 coord-node)
645 echo "true"
646 return 0
647 ;;
648 server-node)
649 echo "false"
650 return 1
651 ;;
652 coord-and-server-node)
653 echo "true"
654 return 0
655 ;;
656 *)
657 echo "false"
658 return 1
659 ;;
660 esac
661 else
662 echo "false"
663 return 1
664 fi
665}
666
667# Check if this host should be running RAMCloud server:
668function is-server-role {
669 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
670 case "${ONOS_HOST_ROLE}" in
671 single-node)
672 echo "true"
673 return 0
674 ;;
675 coord-node)
676 echo "false"
677 return 1
678 ;;
679 server-node)
680 echo "true"
681 return 0
682 ;;
683 coord-and-server-node)
684 echo "true"
685 return 0
686 ;;
687 *)
688 echo "false"
689 return 1
690 ;;
691 esac
692 else
693 echo "false"
694 return 1
695 fi
696}
697
Naoki Shiota9df15d32014-03-27 14:26:20 -0700698function start-backend {
699 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700700 if [ $1 == "coord" ]; then
701 rc-coord startifdown
702 elif [ $1 == "server" ]; then
703 rc-server startifdown
704 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700705 fi
706}
707
708function stop-backend {
709 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
710 if [ $rcsn != 0 ]; then
711 rc-server stop
712 fi
713
Naoki Shiota590c18d2014-03-31 17:52:59 -0700714 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700715 if [ $rccn != 0 ]; then
716 rc-coord stop
717 fi
718}
719
Naoki Shiota590c18d2014-03-31 17:52:59 -0700720
Naoki Shiota4e463182014-03-21 15:13:24 -0700721### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700722function rc-coord-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700723 local host=${RC_COORD_IP}
724 if [ -z "${host}" ]; then
725 # falling back to 0.0.0.0
726 host="0.0.0.0"
727 fi
728 echo "${RC_COORD_PROTOCOL}:host=${host},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700729}
730
731function rc-server-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700732 local host=${RC_SERVER_IP}
733 if [ -z "${host}" ]; then
734 # falling back to 0.0.0.0
735 host="0.0.0.0"
736 fi
737 echo "${RC_SERVER_PROTOCOL}:host=${host},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700738}
739
Naoki Shiota4e463182014-03-21 15:13:24 -0700740function rc-coord {
741 case "$1" in
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700742 start*) # <- start, startifdown
Naoki Shiota590c18d2014-03-31 17:52:59 -0700743 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700744 if [ $n == 0 ]; then
745 start-coord
746 else
747 echo "$n instance of RAMCloud coordinator running"
748 fi
749 ;;
750 stop)
751 stop-coord
752 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700753 deldb)
754 stop-backend
755 del-coord-info
756 ;;
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700757 re*) # <- restart
758 stop-coord
759 start-coord
760 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700761 stat*) # <- status
762 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
763 echo "$n RAMCloud coordinator running"
764 ;;
765 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700766 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700767 exit 1
768 esac
769}
770
771function start-coord {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700772 check-and-start-zk
773 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700774
Naoki Shiota4e463182014-03-21 15:13:24 -0700775 if [ ! -d ${LOGDIR} ]; then
776 mkdir -p ${LOGDIR}
777 fi
778 if [ -f $RAMCLOUD_COORD_LOG ]; then
779 rotate-log $RAMCLOUD_COORD_LOG
780 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700781
782 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700783
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700784 # TODO Make ONOS_CONF readable from ONOS process then eliminate RAMCLOUD_CONF
785
786 # Configuration for ZK address, port
787 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
788
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700789 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700790 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700791
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700792 # RAMCloud transport timeout
793 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700794 # RAMCloud option deadServerTimeout
795 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
796 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
797
798 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
799 # but actual RAMCloud code only uses -C argument now.
800 # (FYI: -C is documented to be deprecated in the document)
801
802 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700803 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700804 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700805 coord_args="${coord_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700806 coord_args="${coord_args} --deadServerTimeout ${rc_coord_deadServerTimeout}"
807
808 # Read environment variables if set
809 coord_args="${coord_args} ${RC_COORDINATOR_OPTS}"
810
811 if [ "${ONOS_HOST_ROLE}" == "single-node" ]; then
812 # Note: Following reset is required, since RC restart is considered node failure,
813 # and tries recovery of server, which will never succeed after restart.
814 echo "Role configured to single-node mode. RAMCloud cluster will be reset on each start-up."
815 coord_args="${coord_args} --reset"
816 fi
817
Naoki Shiota4e463182014-03-21 15:13:24 -0700818 # Run ramcloud
819 echo -n "Starting RAMCloud coordinator ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700820 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700821 echo "STARTED"
822}
823
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700824function del-coord-info {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700825 check-and-start-zk
826 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700827
828 if [ ! -d ${LOGDIR} ]; then
829 mkdir -p ${LOGDIR}
830 fi
831 if [ -f $RAMCLOUD_COORD_LOG ]; then
832 rotate-log $RAMCLOUD_COORD_LOG
833 fi
834
835 local coord_addr=`rc-coord-addr`
836
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700837 # Configuration for ZK address, port
838 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700839 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700840 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700841 # RAMCloud option deadServerTimeout
842 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
843 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
844
845 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
846 # but actual RAMCloud code only uses -C argument now.
847 # (FYI: -C is documented to be deprecated in the document)
848
849 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700850 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700851 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
852
853 # Note: --reset will reset ZK stored info and start running as acoordinator.
854 echo -n "Deleting RAMCloud cluster coordination info ... "
855 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} --reset &> $RAMCLOUD_COORD_LOG &
856
Yuta HIGUCHIcd44eb32014-05-24 16:51:31 -0700857 # TODO Assuming 5 sec is enough. To be sure monitor log?
858 sleep 5
859 # kill coordinator
Yuta HIGUCHIce9f3ee2014-05-14 09:48:40 -0700860 (pkill -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator &> /dev/null)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700861
862 echo "DONE"
863}
Naoki Shiota4e463182014-03-21 15:13:24 -0700864
865function stop-coord {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700866 kill-processes "RAMCloud coordinator" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700867}
868
Naoki Shiota4e463182014-03-21 15:13:24 -0700869### Functions related to RAMCloud server
870function rc-server {
871 case "$1" in
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700872 start*) # <- start, startifdown
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700873 local n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700874 if [ $n == 0 ]; then
875 start-server
876 else
877 echo "$n instance of RAMCloud server running"
878 fi
879 ;;
880 stop)
881 stop-server
882 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700883 deldb)
884 stop-server
885 del-server-backup
886 ;;
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700887 re*) # <- restart
888 stop-server
889 start-server
890 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700891 stat*) # <- status
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700892 n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700893 echo "$n RAMCloud server running"
894 ;;
895 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700896 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700897 exit 1
898 esac
899}
900
901function start-server {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700902 check-and-start-zk
903 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700904
Naoki Shiota4e463182014-03-21 15:13:24 -0700905 if [ ! -d ${LOGDIR} ]; then
906 mkdir -p ${LOGDIR}
907 fi
908 if [ -f $RAMCLOUD_SERVER_LOG ]; then
909 rotate-log $RAMCLOUD_SERVER_LOG
910 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700911
912 local coord_addr=`rc-coord-addr`
913 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700914
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700915 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.server.masterServiceThreads 5)
916 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.server.logCleanerThreads 1)
Yuta HIGUCHI52efcbb2014-05-16 09:30:33 -0700917 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.server.detectFailures 0)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700918
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700919 # Configuration for ZK address, port
920 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700921 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700922 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700923 # RAMCloud transport timeout
924 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700925 # replication factor (-r) config
926 local rc_replicas=$(read-conf ${ONOS_CONF} ramcloud.server.replicas 0)
927 # backup file path (-f) config
928 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
929 mkdir -p `dirname ${rc_datafile}`
930
931 local server_args="-L ${server_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700932 server_args="${server_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700933 server_args="${server_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700934 server_args="${server_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700935 server_args="${server_args} --masterServiceThreads ${masterServiceThreads}"
936 server_args="${server_args} --logCleanerThreads ${logCleanerThreads}"
937 server_args="${server_args} --detectFailures ${detectFailures}"
938 server_args="${server_args} --replicas ${rc_replicas}"
939 server_args="${server_args} --file ${rc_datafile}"
940
941 # Read environment variables if set
942 server_args="${server_args} ${RC_SERVER_OPTS}"
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700943
Naoki Shiota4e463182014-03-21 15:13:24 -0700944 # Run ramcloud
945 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700946 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server ${server_args} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700947 echo "STARTED"
948}
949
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700950function del-server-backup {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700951 echo -n "Delete RAMCloud backup server data [y/N]? "
952 while [ 1 ]; do
953 read key
954 if [ "${key}" == "Y" -o "${key}" == "y" ]; then
955 break
956 elif [ -z "${key}" -o "${key}" == "N" -o "${key}" == "n" ]; then
957 echo "Cancelled."
958 return
959 fi
960 echo "[y/N]? "
961 done
962
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700963 echo -n "Removing RAMCloud backup server data ... "
964 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
965 rm -f ${rc_datafile}
966 echo "DONE"
967}
968
Naoki Shiota4e463182014-03-21 15:13:24 -0700969function stop-server {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700970 kill-processes "RAMCloud server" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700971}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700972############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700973
974
Naoki Shiota590c18d2014-03-31 17:52:59 -0700975## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700976function onos {
Naoki Shiota4e463182014-03-21 15:13:24 -0700977
978 case "$1" in
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700979 start*) # <- start, startnokill, startifdown
Naoki Shiota4e463182014-03-21 15:13:24 -0700980 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
981 if [ $n == 0 ]; then
982 start-onos
983 else
984 echo "$n instance of onos running"
985 fi
986 ;;
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700987 unchecked-start)
988 start-onos
989 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700990 stop)
991 stop-onos
992 ;;
Yuta HIGUCHId91c6692014-08-07 09:43:46 -0700993 re*) # <- restart
994 stop-onos
995 start-onos
996 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700997 stat*) # <- status
998 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
999 echo "$n instance of onos running"
1000 ;;
1001 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001002 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001003 exit 1
1004 esac
1005}
1006
1007function start-onos {
Yuta HIGUCHI5f14b012014-07-18 19:14:04 -07001008
1009 JAVA_CP=""
1010 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
1011 if [ ! -f ${MAIN_CLASS_FILE} ]; then
1012 # No .class
1013 if [ -f ${ONOS_JAR} ]; then
1014 echo "Starting ONOS using packaged jar file"
1015 JAVA_CP="${JAVA_CP}:${ONOS_JAR}"
1016 else
1017 echo "Building ONOS from source"
1018 ${MVN} -f ${ONOS_HOME}/pom.xml compile -T 1C
1019 fi
1020 else
1021 # .class exists
1022 if [ ! -f ${CPFILE} ]; then
1023 echo "Building ONOS dependency classpath"
1024 ${MVN} -f ${ONOS_HOME}/pom.xml -T 1C dependency:go-offline -Dsilent dependency:build-classpath -Dmdep.outputFile=${CPFILE}
1025 fi
1026 fi
1027 if [ -f ${CPFILE} ]; then
1028 JAVA_CP="${JAVA_CP}:`cat ${CPFILE}`"
1029 fi
1030 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
1031
Naoki Shiota4e463182014-03-21 15:13:24 -07001032 if [ ! -d ${LOGDIR} ]; then
1033 mkdir -p ${LOGDIR}
1034 fi
Yuta HIGUCHI04713972014-05-30 11:01:17 -07001035 # Rotate log files
Naoki Shiota4e463182014-03-21 15:13:24 -07001036 for log in ${LOGS}; do
1037 if [ -f ${log} ]; then
1038 rotate-log ${log}
1039 fi
1040 done
Yuta HIGUCHI04713972014-05-30 11:01:17 -07001041
1042 # Rotate logs rolled at runtime.
1043 local rolled_log_shellpat=`echo ${ONOS_LOG_ROLLING_PATTERN} | sed -e "s/%i/\\*/"`
1044 for rolled_log in ${rolled_log_shellpat}; do
1045 if [ -f ${rolled_log} ]; then
1046 rotate-log ${rolled_log}
Yuta HIGUCHI04713972014-05-30 11:01:17 -07001047 fi
1048 done
1049
Naoki Shiota4e463182014-03-21 15:13:24 -07001050 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -07001051 echo "[WARNING] ${ONOS_LOGBACK} not found."
1052 echo " Run \"\$ $0 setup\" to create."
1053 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -07001054 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -07001055
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -07001056 if [ ! -f ${HC_CONF} ]; then
1057 echo "[WARNING] ${HC_CONF} not found."
1058 echo " Run \"\$ $0 setup\" to create."
1059 exit 1
1060 fi
1061
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -07001062 # specify ZooKeeper(curator) namespace
1063 JVM_OPTS="${JVM_OPTS} -Dzookeeper.namespace=${ONOS_CLUSTER_NAME}"
1064
Yuta HIGUCHI5f1ce1c2014-07-20 22:43:54 -07001065 # specify ZooKeeper connectionString
1066 local zk_locator=`echo "${ZK_HOSTS}" | sed -e "s/,/:${ZK_CLIENTPORT},/g"`
1067 zk_locator+=":${ZK_CLIENTPORT}"
1068 zk_locator="localhost:${ZK_CLIENTPORT},${zk_locator}"
1069
1070 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.registry.ZookeeperRegistry.connectionString=${zk_locator}"
1071
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -07001072 # specify hazelcast.xml to datagrid
1073 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
1074
1075 # specify backend config
Jonathan Hartef3dc1a2014-04-03 11:39:50 -07001076 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -07001077 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
1078 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -07001079 elif [ "${ONOS_HOST_BACKEND}" = "hazelcast" ]; then
1080 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.hazelcast.baseConfig=${HC_CONF}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -07001081 fi
1082
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -07001083 # check for jvm debug flag
Yuta HIGUCHI32532ea2014-08-01 15:52:55 -07001084 if [ "${ONOS_DEBUG}" == "true" ]; then
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -07001085 JVM_OPTS="${JVM_OPTS} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${JVM_DEBUG_PORT}"
1086 fi
1087
Naoki Shiota9df15d32014-03-27 14:26:20 -07001088 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -07001089
1090 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
1091 cd ${ONOS_HOME}
1092
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001093 echo -n "Starting ONOS controller ... "
Naoki Shiota4e463182014-03-21 15:13:24 -07001094 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
1095
1096 # We need to wait a bit to find out whether starting the ONOS process succeeded
1097 sleep 1
1098
1099 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -07001100 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -07001101 echo " STARTED"
1102 else
1103 echo " FAILED"
1104 fi
1105
1106# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
1107# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
1108}
1109
1110function stop-onos {
1111 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
1112# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
1113}
Naoki Shiota590c18d2014-03-31 17:52:59 -07001114############################################
Naoki Shiota4e463182014-03-21 15:13:24 -07001115
1116
Naoki Shiota590c18d2014-03-31 17:52:59 -07001117################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -07001118case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -07001119 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -07001120 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -07001121 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001122 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -07001123 mode_parameter=${ONOS_HOST_ROLE}
1124 if [ ! -z "$2" ]; then
1125 mode_parameter=$2
1126 fi
Praseed Balakrishnan418c7b22014-06-16 13:33:53 -07001127
Naoki Shiota9df15d32014-03-27 14:26:20 -07001128 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -07001129 single-node)
1130 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001131 start-backend coord
1132 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001133 onos startifdown
1134 ;;
1135 coord-node)
1136 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001137 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -07001138 onos startifdown
1139 ;;
1140 server-node)
1141 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001142 start-backend server
1143 onos startifdown
1144 ;;
1145 coord-and-server-node)
1146 zk start
1147 start-backend coord
1148 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001149 onos startifdown
1150 ;;
1151 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001152 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001153 ;;
1154 esac
1155 echo
1156 ;;
1157 stop)
1158 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1159 if [ $on != 0 ]; then
1160 onos stop
1161 fi
1162
Naoki Shiota9df15d32014-03-27 14:26:20 -07001163 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -07001164
1165 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1166 if [ $zkn != 0 ]; then
1167 zk stop
1168 fi
1169 echo
1170 ;;
1171 restart)
1172 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1173 if [ $on != 0 ]; then
1174 onos stop
1175 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001176
Naoki Shiota4e463182014-03-21 15:13:24 -07001177 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
1178 if [ $rcsn != 0 ]; then
1179 rc-server stop
1180 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001181
Naoki Shiota590c18d2014-03-31 17:52:59 -07001182 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -07001183 if [ $rccn != 0 ]; then
1184 rc-coord stop
1185 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001186
Naoki Shiota4e463182014-03-21 15:13:24 -07001187 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1188 if [ $zkn != 0 ]; then
1189 zk restart
1190 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001191
Naoki Shiota4e463182014-03-21 15:13:24 -07001192 if [ $rccn != 0 ]; then
1193 rc-coord startifdown
1194 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001195
Naoki Shiota4e463182014-03-21 15:13:24 -07001196 if [ $rcsn != 0 ]; then
1197 rc-server startifdown
1198 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001199
Naoki Shiota4e463182014-03-21 15:13:24 -07001200 if [ $on != 0 ]; then
1201 onos startifdown
1202 fi
1203 echo
1204 ;;
1205 stat*) # <- status
1206 echo '[ZooKeeper]'
1207 zk status
1208 echo
1209 echo '[RAMCloud coordinator]'
1210 rc-coord status
1211 echo
1212 echo '[RAMCloud server]'
1213 rc-server status
1214 echo
1215 echo '[ONOS core]'
1216 onos status
1217 echo
1218 ;;
1219 zk)
1220 zk $2
1221 ;;
1222 rc-c*) # <- rc-coordinator
1223 rc-coord $2
1224 ;;
1225 rc-s*) # <- rc-server
1226 rc-server $2
1227 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001228 rc)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001229 # XXX rc command ignores if the host is not configured to have those roles,
1230 # while rc-s or rc-c executes the task regardless of role definition.
1231 # This is a workaround since TestON does not take role in account
1232 # and always issue rc deldb right now.
1233
Naoki Shiota9109a1e2014-05-13 11:11:01 -07001234 # TODO make deldb command more organized (clarify when it can work)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001235 if [ "`is-coord-role`" == "true" ]; then
1236 rc-coord $2
1237 fi
1238 if [ "`is-server-role`" == "true" ]; then
1239 rc-server $2
1240 fi
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001241 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001242 core)
1243 onos $2
1244 ;;
1245 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001246 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001247 exit 1
1248esac
Naoki Shiota590c18d2014-03-31 17:52:59 -07001249