blob: 6c0891c72f3a9a5d26faff07e399b5bc2d07f7b7 [file] [log] [blame]
Naoki Shiota4e463182014-03-21 15:13:24 -07001#!/bin/bash
Naoki Shiota4e463182014-03-21 15:13:24 -07002
Naoki Shiota590c18d2014-03-31 17:52:59 -07003### Env vars used by this script. (default value) ###
4# $ONOS_HOME : path of root directory of ONOS repository (this script's dir)
5# $ONOS_CONF_DIR : path of ONOS config directory (~/ONOS/conf)
Naoki Shiota9a1e6d12014-04-03 14:47:12 -07006# $ONOS_CONF : path of ONOS node config file (~/ONOS/conf/onos_node.`hostname`.conf or onos_node.conf)
Naoki Shiota590c18d2014-03-31 17:52:59 -07007# $ONOS_PROPS : path of ONOS properties file (~/ONOS/conf/onos.properties)
8# $ONOS_LOGBACK : path of logback config file (~/ONOS/conf/logback.`hostname`.xml)
Naoki Shiotad8ea71a2014-04-23 17:04:51 -07009# $ONOS_LOGDIR : path of log output directory (~/ONOS/onos-logs)
10# $ONOS_LOGBASE : base name of log output file (onos.`hostname`)
Naoki Shiota590c18d2014-03-31 17:52:59 -070011# $RAMCLOUD_HOME : path of root directory of RAMCloud repository (~/ramcloud)
12# $RAMCLOUD_BRANCH : branch name of RAMCloud to use (master)
Naoki Shiota72209722014-04-08 14:32:17 -070013# $ZK_HOME : path of root directory of ZooKeeper (~/zookeeper-3.4.5)
14# $ZK_LIB_DIR : path of ZooKeeper library (/var/lib/zookeeper)
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070015# $ZK_LOG_DIR : path of ZooKeeper log output directory (~/ONOS/onos-logs/zk-`hostname`)
Naoki Shiota590c18d2014-03-31 17:52:59 -070016# $JVM_OPTS : JVM options ONOS starts with
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070017# $ZK_CONF : path of ZooKeeper config file (~/ONOS/conf/zoo.cfg)
18# $HC_CONF : path of Hazelcast config file (~/ONOS/conf/hazelcast.xml)
19# $RAMCLOUD_CONF : path of RAMCloud config file (~/ONOS/conf/ramcloud.conf)
Naoki Shiota590c18d2014-03-31 17:52:59 -070020#####################################################
21
Naoki Shiota590c18d2014-03-31 17:52:59 -070022ONOS_HOME=${ONOS_HOME:-$(cd `dirname $0`; pwd)}
Naoki Shiota9df15d32014-03-27 14:26:20 -070023ONOS_CONF_DIR=${ONOS_CONF_DIR:-${ONOS_HOME}/conf}
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070024ONOS_CONF=${ONOS_CONF:-${ONOS_CONF_DIR}/onos_node.`hostname`.conf}
Naoki Shiota590c18d2014-03-31 17:52:59 -070025
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070026source ${ONOS_HOME}/scripts/common/utils.sh
27
Yuta HIGUCHIe748ecc2014-05-28 13:55:03 -070028confirm-if-root
29
Naoki Shiota590c18d2014-03-31 17:52:59 -070030if [ ! -f ${ONOS_CONF} ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070031 # falling back to default config file
32 ONOS_CONF=${ONOS_CONF_DIR}/onos_node.conf
33 if [ ! -f ${ONOS_CONF} ]; then
34 echo "${ONOS_CONF} not found."
35 exit 1
36 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -070037fi
Naoki Shiota4e928512014-04-03 15:49:35 -070038
Naoki Shiota4e928512014-04-03 15:49:35 -070039### Variables read from ONOS config file ###
40ONOS_HOST_NAME=$(read-conf ${ONOS_CONF} host.name `hostname`)
41ONOS_HOST_IP=$(read-conf ${ONOS_CONF} host.ip)
42ONOS_HOST_ROLE=$(read-conf ${ONOS_CONF} host.role)
43ONOS_HOST_BACKEND=$(read-conf ${ONOS_CONF} host.backend)
44ZK_HOSTS=$(read-conf ${ONOS_CONF} zookeeper.hosts ${ONOS_HOST_NAME})
45RC_COORD_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.coordinator.protocol "fast+udp")
46RC_COORD_IP=$(read-conf ${ONOS_CONF} ramcloud.coordinator.ip ${ONOS_HOST_IP})
47RC_COORD_PORT=$(read-conf ${ONOS_CONF} ramcloud.coordinator.port 12246)
48RC_SERVER_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.server.protocol "fast+udp")
49RC_SERVER_IP=$(read-conf ${ONOS_CONF} ramcloud.server.ip ${ONOS_HOST_IP})
50RC_SERVER_PORT=$(read-conf ${ONOS_CONF} ramcloud.server.port 12242)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070051HC_HOST_PORT=$(read-conf ${ONOS_CONF} hazelcast.host.port 5701)
Naoki Shiota4e928512014-04-03 15:49:35 -070052HC_TCPIP_MEMBERS=$(read-conf ${ONOS_CONF} hazelcast.tcp-ip.members)
53HC_MULTICAST_GROUP=$(read-conf ${ONOS_CONF} hazelcast.multicast.group "224.2.2.3")
54HC_MULTICAST_PORT=$(read-conf ${ONOS_CONF} hazelcast.multicast.port 54327)
Naoki Shiota590c18d2014-03-31 17:52:59 -070055############################################
56
57
58############## Other variables #############
Naoki Shiota4e928512014-04-03 15:49:35 -070059ONOS_TEMPLATE_DIR=${ONOS_CONF_DIR}/template
60
Naoki Shiota4e463182014-03-21 15:13:24 -070061LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
62
Naoki Shiota72209722014-04-08 14:32:17 -070063ZK_HOME=${ZK_HOME:-~/zookeeper-3.4.5}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070064ZK_CONF=${ZK_CONF:-${ONOS_CONF_DIR}/zoo.cfg}
Naoki Shiota4e928512014-04-03 15:49:35 -070065ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070066# Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070067ZK_LOG_DIR=${ZK_LOG_DIR:-${ONOS_HOME}/onos-logs/zk-${ONOS_HOST_NAME}}
Naoki Shiota72209722014-04-08 14:32:17 -070068ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070069ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070070
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070071HC_CONF=${HC_CONF:-${ONOS_CONF_DIR}/hazelcast.xml}
Naoki Shiota4e928512014-04-03 15:49:35 -070072HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
73
Naoki Shiota4e463182014-03-21 15:13:24 -070074RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070075RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
76RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070077RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070078RAMCLOUD_CONF=${RAMCLOUD_CONF:-${ONOS_CONF_DIR}/ramcloud.conf}
Naoki Shiota4e463182014-03-21 15:13:24 -070079
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070080export LD_LIBRARY_PATH=${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}:$LD_LIBRARY_PATH
Naoki Shiota4e463182014-03-21 15:13:24 -070081
82## Because the script change dir to $ONOS_HOME, we can set ONOS_LOGBACK and LOGDIR relative to $ONOS_HOME
Naoki Shiota590c18d2014-03-31 17:52:59 -070083ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070084ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070085ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070086LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070087LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070088ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
89PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
90LOGS="$ONOS_LOG $PCAP_LOG"
91
Naoki Shiota9df15d32014-03-27 14:26:20 -070092ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -070093JMX_PORT=${JMX_PORT:-7189}
94
95# Set JVM options
96JVM_OPTS="${JVM_OPTS:-}"
Naoki Shiota4e463182014-03-21 15:13:24 -070097JVM_OPTS="$JVM_OPTS -server -d64"
98#JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -070099
100# Uncomment or specify appropriate value as JVM_OPTS environment variables.
101#JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
Naoki Shiota4e463182014-03-21 15:13:24 -0700102#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
103#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700104
105#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC"
106JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
107JVM_OPTS="$JVM_OPTS -XX:+AggressiveOpts"
108
109# We may want to remove UseFastAccessorMethods option: http://bugs.java.com/view_bug.do?bug_id=6385687
110JVM_OPTS="$JVM_OPTS -XX:+UseFastAccessorMethods"
111
112JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192"
113JVM_OPTS="$JVM_OPTS -XX:FreqInlineSize=8192"
114JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500"
115
Naoki Shiota4e463182014-03-21 15:13:24 -0700116JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700117
118# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
119JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
120
121JVM_OPTS="$JVM_OPTS -XX:+UseCompressedOops"
122
123JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
124JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
125JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
126
Naoki Shiota4e463182014-03-21 15:13:24 -0700127JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
128
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700129# Uncomment to dump final JVM flags to stdout
130#JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
131
Naoki Shiota4e463182014-03-21 15:13:24 -0700132# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700133MAIN_CLASS="net.onrc.onos.core.main.Main"
Naoki Shiota4e463182014-03-21 15:13:24 -0700134
135MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700136############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700137
Naoki Shiota9df15d32014-03-27 14:26:20 -0700138
Naoki Shiota590c18d2014-03-31 17:52:59 -0700139############# Common functions #############
140function print_usage {
Naoki Shiota05721b32014-04-29 14:41:12 -0700141 local scriptname=`basename $0`
Naoki Shiota590c18d2014-03-31 17:52:59 -0700142 local filename=`basename ${ONOS_CONF}`
143 local usage="Usage: setup/start/stop ONOS on this server.
Naoki Shiota05721b32014-04-29 14:41:12 -0700144 \$ ${scriptname} setup [-f]
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700145 Set up ONOS node using ${ONOS_CONF} .
Naoki Shiota590c18d2014-03-31 17:52:59 -0700146 - generate and replace config file of ZooKeeper.
147 - create myid in ZooKeeper datadir.
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700148 - generate and replace config file for Hazelcast.
149 - generate and replace config file for RAMCloud.
Naoki Shiota590c18d2014-03-31 17:52:59 -0700150 - generate and replace logback.${ONOS_HOST_NAME}.xml
151 If -f option is used, all existing files will be overwritten without confirmation.
Naoki Shiota05721b32014-04-29 14:41:12 -0700152 \$ ${scriptname} start [single-node|coord-node|server-node|coord-and-server-node]
Naoki Shiota590c18d2014-03-31 17:52:59 -0700153 Start ONOS node with specific RAMCloud entities
154 - single-node: start ONOS with stand-alone RAMCloud
155 - coord-node : start ONOS with RAMCloud coordinator
156 - server-node: start ONOS with RAMCloud server
157 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
158 * Default behavior can be defined by ${filename}
Naoki Shiota05721b32014-04-29 14:41:12 -0700159 \$ ${scriptname} stop
Naoki Shiota590c18d2014-03-31 17:52:59 -0700160 Stop all ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700161 \$ ${scriptname} restart
Naoki Shiota590c18d2014-03-31 17:52:59 -0700162 Stop and start currently running ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700163 \$ ${scriptname} status
Naoki Shiota590c18d2014-03-31 17:52:59 -0700164 Show status of ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700165 \$ ${scriptname} {zk|rc-coord|rc-server|core} {start|stop|restart|status}
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700166 Control specific ONOS-related process
167 \$ ${scriptname} rc deldb
168 Delete data in RAMCloud"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700169
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700170 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700171}
172
173function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700174 local logfile=$1
175 local nr_max=${2:-10}
176 if [ -f $logfile ]; then
177 for i in `seq $(expr $nr_max - 1) -1 1`; do
178 if [ -f ${logfile}.${i} ]; then
179 mv -f ${logfile}.${i} ${logfile}.`expr $i + 1`
180 fi
181 done
182 mv $logfile $logfile.1
183 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700184}
185
186# kill-processes {module-name} {array of pids}
187function kill-processes {
188 # Kill the existing processes
189 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700190 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700191 echo -n "Stopping $1 ... "
192 fi
193 for p in ${pids}; do
194 if [ x$p != "x" ]; then
Naoki Shiota4b355762014-05-27 11:46:43 -0700195 # Check if target process is accesible from current user
196 kill -0 $p
197 if [ "$?" -ne 0 ]; then
198 # Error exit code here means "failed to send signal".
199 # Supposedly because of permission error.
200 echo "Failed to kill process (pid: $p)."
201 echo "Check if current user is the same as process owner."
202 exit 1
203 fi
204
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700205 (
206 # Ask process with SIGTERM first, if that did not kill the process
207 # wait 1s and if process still exist, force process to be killed.
208 kill -TERM $p && kill -0 $p && sleep 1 && kill -0 $p && kill -KILL $p
209 ) 2> /dev/null
Naoki Shiota4b355762014-05-27 11:46:43 -0700210
211 # Ensure process is killed.
212 kill -0 $p 2> /dev/null
213 if [ "$?" -ne 0 ]; then
214 # Error exit code here means "process not found", i.e. "kill succeeded".
215 echo "Killed existing process (pid: $p)"
216 else
217 # Process still exists. Some unexpected error occurs.
218 echo "Failed to kill process (pid: $p)."
219 echo "Unexpected error occurs during process termination."
220 exit 1
221 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700222 fi
223 done
224}
225
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700226function handle-error {
227 set -e
228
229 revert-confs
230
231 set +e
232
233 exit 1
234}
235
Naoki Shiota4e928512014-04-03 15:49:35 -0700236# revert-confs [error message]
237function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700238 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700239
Naoki Shiota4e928512014-04-03 15:49:35 -0700240 revert-file `basename ${ZK_CONF}`
241 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700242
243 echo "EXIT"
244
245 if [ ! -z "$1" ]; then
246 echo $1
247 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700248}
249
Naoki Shiota590c18d2014-03-31 17:52:59 -0700250function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700251 echo -n "Creating ${ZK_CONF} ... "
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700252
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700253 # Create the ZooKeeper lib directory
254 if [ ! -d ${ZK_LIB_DIR} ]; then
Naoki Shiota7f495cf2014-05-21 17:23:25 -0700255 local SUDO=${SUDO:-sudo}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700256 local whoami=`whoami`
257 {
258 ${SUDO} mkdir ${ZK_LIB_DIR}
259 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
260 } || {
261 echo "FAILED"
262 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
263 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
264 exit 1
265 }
266 fi
267
268 # creation of ZooKeeper config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700269 local temp_zk=`begin-conf-creation ${ZK_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700270
Naoki Shiota4e928512014-04-03 15:49:35 -0700271 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700272
273 local i=1
274 local myid=
275 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700276 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700277 myid=$i
278 break
279 fi
280 i=`expr $i + 1`
281 done
282
283 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700284 local filename=`basename ${ONOS_CONF}`
Naoki Shiota35f8d5c2014-04-08 11:13:18 -0700285 revert-confs "[ERROR] In ${filename}, zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700286 fi
287
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700288 if [ -f "${ZK_MY_ID}" ]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700289 # sudo will be needed if ZK_MY_ID is already created by other (old) script
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700290 local SUDO=${SUDO:-}
291 {
292 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
293 } || {
294 echo "FAILED"
295 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700296 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700297 exit 1
298 }
299 fi
300
Naoki Shiota9df15d32014-03-27 14:26:20 -0700301 echo ${myid} > ${ZK_MY_ID}
302
303 echo -n "myid is assigned to ${myid} ... "
304
305 while read line; do
306 if [[ $line =~ ^__HOSTS__$ ]]; then
307 i=1
308 for host in ${hostarr}; do
309 # TODO: ports might be configurable
310 local hostline="server.${i}=${host}:2888:3888"
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700311 echo $hostline
Naoki Shiota9df15d32014-03-27 14:26:20 -0700312 i=`expr $i + 1`
313 done
314 elif [[ $line =~ __DATADIR__ ]]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700315 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700316 else
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700317 echo $line
Naoki Shiota9df15d32014-03-27 14:26:20 -0700318 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700319 done < ${ZK_CONF_TEMPLATE} > ${temp_zk}
320
321 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700322
323 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700324}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700325
Naoki Shiota4e928512014-04-03 15:49:35 -0700326function create-hazelcast-conf {
327 echo -n "Creating ${HC_CONF} ... "
328
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700329 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700330
331 # To keep indent of XML file, change IFS
332 local IFS=''
333 while read line; do
334 if [[ $line =~ __HC_NETWORK__ ]]; then
335 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
336 # temporary change
337 IFS=' '
338 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
339 echo '<multicast enabled="false" />'
340 echo '<tcp-ip enabled="true">'
341 for member in ${memberarr}; do
342 echo " <member>${member}</member>"
343 done
344 echo '</tcp-ip>'
345 IFS=''
346 else
347 echo '<multicast enabled="true">'
348 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
349 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
350 echo '</multicast>'
351 echo '<tcp-ip enabled="false" />'
352 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700353 elif [[ $line =~ __HC_PORT__ ]]; then
354 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700355 else
356 echo "${line}"
357 fi
358 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
359
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700360 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700361
362 echo "DONE"
363}
364
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700365function create-ramcloud-conf {
366 echo -n "Creating ${RAMCLOUD_CONF} ... "
367
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700368 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700369
370 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
371
372 # TODO make ZooKeeper address configurable.
373 echo "ramcloud.locator=zk:localhost:2181" > ${temp_rc}
374 echo "ramcloud.clusterName=${rc_cluster_name}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700375
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700376 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700377
378 echo "DONE"
379}
380
Naoki Shiota590c18d2014-03-31 17:52:59 -0700381function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700382 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700383
Naoki Shiota9df15d32014-03-27 14:26:20 -0700384 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700385 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
386
387 sed -e "s|__FILENAME__|${ONOS_LOG}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
388
389 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700390
Naoki Shiota9df15d32014-03-27 14:26:20 -0700391 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700392}
393
Naoki Shiota4e928512014-04-03 15:49:35 -0700394function create-confs {
395 local key
396 local filename
397
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700398 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700399
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700400 echo "Config file : ${ONOS_CONF}"
401
Naoki Shiota4e928512014-04-03 15:49:35 -0700402 if [ "$1" == "-f" ]; then
403 create-zk-conf
404 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700405 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700406 create-logback-conf
407 else
408 create-conf-interactive ${ZK_CONF} create-zk-conf
409 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700410 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700411 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
412 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700413
414 trap - ERR
415}
416############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700417
Naoki Shiota590c18d2014-03-31 17:52:59 -0700418
419###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700420function zk {
421 case "$1" in
422 start)
423 start-zk
424 ;;
425 stop)
426 stop-zk
427 ;;
428 stat*) # <- status
429 status-zk
430 ;;
431 re*) # <- restart
432 stop-zk
433 start-zk
434 ;;
435 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700436 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700437 exit 1
438 esac
439}
440
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700441function load-zk-cfg {
442 if [ -f "${ZK_CONF}" ]; then
443 local filename=`basename ${ZK_CONF}`
444 local dirname=`dirname ${ZK_CONF}`
445
446 # Run ZooKeeper with our configuration
447 export ZOOCFG=${filename}
448 export ZOOCFGDIR=${dirname}
449 fi
450}
451
Naoki Shiota4e463182014-03-21 15:13:24 -0700452function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700453 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700454
Naoki Shiota4e928512014-04-03 15:49:35 -0700455 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700456 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700457
458 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700459
Naoki Shiota72209722014-04-08 14:32:17 -0700460 ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700461}
462
463function stop-zk {
464 kill-processes "ZooKeeper" `jps -l | grep org.apache.zookeeper.server | awk '{print $1}'`
465}
466
467function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700468 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700469
Naoki Shiota72209722014-04-08 14:32:17 -0700470 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700471}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700472
473function check-zk {
474 # assumption here is that ZK status script is the last command in status-zk.
475 status-zk &> /dev/null
476 local zk_status=$?
477 if [ "$zk_status" -ne 0 ]; then
478 return 1;
479 fi
480 return 0
481}
482
483# wait-zk-or-die {timeout-sec}
484function wait-zk-or-die {
485 local retries=${1:-1}
486 # do-while retries >= 0
487 while true; do
488 check-zk
489 local zk_status=$?
490 if [ "$zk_status" -eq 0 ]; then
491 return 0
492 fi
493 sleep 1;
494 ((retries -= 1))
495 (( retries >= 0 )) || break
496 done
497 echo "ZooKeeper is not running."
498 exit 1
499}
500
Naoki Shiota590c18d2014-03-31 17:52:59 -0700501############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700502
503
Naoki Shiota590c18d2014-03-31 17:52:59 -0700504####### Functions related to RAMCloud ######
Naoki Shiota9df15d32014-03-27 14:26:20 -0700505function start-backend {
506 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700507 if [ $1 == "coord" ]; then
508 rc-coord startifdown
509 elif [ $1 == "server" ]; then
510 rc-server startifdown
511 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700512 fi
513}
514
515function stop-backend {
516 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
517 if [ $rcsn != 0 ]; then
518 rc-server stop
519 fi
520
Naoki Shiota590c18d2014-03-31 17:52:59 -0700521 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700522 if [ $rccn != 0 ]; then
523 rc-coord stop
524 fi
525}
526
Naoki Shiota590c18d2014-03-31 17:52:59 -0700527
Naoki Shiota4e463182014-03-21 15:13:24 -0700528### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700529function rc-coord-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700530 local host=${RC_COORD_IP}
531 if [ -z "${host}" ]; then
532 # falling back to 0.0.0.0
533 host="0.0.0.0"
534 fi
535 echo "${RC_COORD_PROTOCOL}:host=${host},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700536}
537
538function rc-server-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700539 local host=${RC_SERVER_IP}
540 if [ -z "${host}" ]; then
541 # falling back to 0.0.0.0
542 host="0.0.0.0"
543 fi
544 echo "${RC_SERVER_PROTOCOL}:host=${host},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700545}
546
Naoki Shiota4e463182014-03-21 15:13:24 -0700547function rc-coord {
548 case "$1" in
549 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700550 stop-coord
551 start-coord
552 ;;
553 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700554 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700555 if [ $n == 0 ]; then
556 start-coord
557 else
558 echo "$n instance of RAMCloud coordinator running"
559 fi
560 ;;
561 stop)
562 stop-coord
563 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700564 deldb)
565 stop-backend
566 del-coord-info
567 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700568 stat*) # <- status
569 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
570 echo "$n RAMCloud coordinator running"
571 ;;
572 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700573 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700574 exit 1
575 esac
576}
577
578function start-coord {
Yuta HIGUCHI71703322014-05-06 09:17:29 -0700579 wait-zk-or-die 2
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700580
Naoki Shiota4e463182014-03-21 15:13:24 -0700581 if [ ! -d ${LOGDIR} ]; then
582 mkdir -p ${LOGDIR}
583 fi
584 if [ -f $RAMCLOUD_COORD_LOG ]; then
585 rotate-log $RAMCLOUD_COORD_LOG
586 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700587
588 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700589
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700590 # TODO Configuration for ZK address, port
591 local zk_addr="localhost:2181"
592 # RAMCloud cluster name
593 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700594 # RAMCloud transport timeout
595 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700596 # RAMCloud option deadServerTimeout
597 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
598 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
599
600 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
601 # but actual RAMCloud code only uses -C argument now.
602 # (FYI: -C is documented to be deprecated in the document)
603
604 local coord_args="-C ${coord_addr}"
605 coord_args="${coord_args} --externalStorage zk:${zk_addr}"
606 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700607 coord_args="${coord_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700608 coord_args="${coord_args} --deadServerTimeout ${rc_coord_deadServerTimeout}"
609
610 # Read environment variables if set
611 coord_args="${coord_args} ${RC_COORDINATOR_OPTS}"
612
613 if [ "${ONOS_HOST_ROLE}" == "single-node" ]; then
614 # Note: Following reset is required, since RC restart is considered node failure,
615 # and tries recovery of server, which will never succeed after restart.
616 echo "Role configured to single-node mode. RAMCloud cluster will be reset on each start-up."
617 coord_args="${coord_args} --reset"
618 fi
619
Naoki Shiota4e463182014-03-21 15:13:24 -0700620 # Run ramcloud
621 echo -n "Starting RAMCloud coordinator ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700622 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700623 echo "STARTED"
624}
625
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700626function del-coord-info {
627 wait-zk-or-die 1
628
629 if [ ! -d ${LOGDIR} ]; then
630 mkdir -p ${LOGDIR}
631 fi
632 if [ -f $RAMCLOUD_COORD_LOG ]; then
633 rotate-log $RAMCLOUD_COORD_LOG
634 fi
635
636 local coord_addr=`rc-coord-addr`
637
638 # TODO Configuration for ZK address, port
639 local zk_addr="localhost:2181"
640 # RAMCloud cluster name
641 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
642 # RAMCloud option deadServerTimeout
643 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
644 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
645
646 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
647 # but actual RAMCloud code only uses -C argument now.
648 # (FYI: -C is documented to be deprecated in the document)
649
650 local coord_args="-C ${coord_addr}"
651 coord_args="${coord_args} --externalStorage zk:${zk_addr}"
652 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
653
654 # Note: --reset will reset ZK stored info and start running as acoordinator.
655 echo -n "Deleting RAMCloud cluster coordination info ... "
656 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} --reset &> $RAMCLOUD_COORD_LOG &
657
Yuta HIGUCHIcd44eb32014-05-24 16:51:31 -0700658 # TODO Assuming 5 sec is enough. To be sure monitor log?
659 sleep 5
660 # kill coordinator
Yuta HIGUCHIce9f3ee2014-05-14 09:48:40 -0700661 (pkill -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator &> /dev/null)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700662
663 echo "DONE"
664}
Naoki Shiota4e463182014-03-21 15:13:24 -0700665
666function stop-coord {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700667 kill-processes "RAMCloud coordinator" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700668}
669
Naoki Shiota4e463182014-03-21 15:13:24 -0700670### Functions related to RAMCloud server
671function rc-server {
672 case "$1" in
673 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700674 stop-server
675 start-server
676 ;;
677 startifdown)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700678 local n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700679 if [ $n == 0 ]; then
680 start-server
681 else
682 echo "$n instance of RAMCloud server running"
683 fi
684 ;;
685 stop)
686 stop-server
687 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700688 deldb)
689 stop-server
690 del-server-backup
691 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700692 stat*) # <- status
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700693 n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700694 echo "$n RAMCloud server running"
695 ;;
696 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700697 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700698 exit 1
699 esac
700}
701
702function start-server {
Yuta HIGUCHI71703322014-05-06 09:17:29 -0700703 wait-zk-or-die 2
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700704
Naoki Shiota4e463182014-03-21 15:13:24 -0700705 if [ ! -d ${LOGDIR} ]; then
706 mkdir -p ${LOGDIR}
707 fi
708 if [ -f $RAMCLOUD_SERVER_LOG ]; then
709 rotate-log $RAMCLOUD_SERVER_LOG
710 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700711
712 local coord_addr=`rc-coord-addr`
713 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700714
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700715 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.server.masterServiceThreads 5)
716 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.server.logCleanerThreads 1)
Yuta HIGUCHI52efcbb2014-05-16 09:30:33 -0700717 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.server.detectFailures 0)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700718
719 # TODO Configuration for ZK address, port
720 local zk_addr="localhost:2181"
721 # RAMCloud cluster name
722 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700723 # RAMCloud transport timeout
724 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700725 # replication factor (-r) config
726 local rc_replicas=$(read-conf ${ONOS_CONF} ramcloud.server.replicas 0)
727 # backup file path (-f) config
728 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
729 mkdir -p `dirname ${rc_datafile}`
730
731 local server_args="-L ${server_addr}"
732 server_args="${server_args} --externalStorage zk:${zk_addr}"
733 server_args="${server_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700734 server_args="${server_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700735 server_args="${server_args} --masterServiceThreads ${masterServiceThreads}"
736 server_args="${server_args} --logCleanerThreads ${logCleanerThreads}"
737 server_args="${server_args} --detectFailures ${detectFailures}"
738 server_args="${server_args} --replicas ${rc_replicas}"
739 server_args="${server_args} --file ${rc_datafile}"
740
741 # Read environment variables if set
742 server_args="${server_args} ${RC_SERVER_OPTS}"
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700743
Naoki Shiota4e463182014-03-21 15:13:24 -0700744 # Run ramcloud
745 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700746 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server ${server_args} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700747 echo "STARTED"
748}
749
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700750function del-server-backup {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700751 echo -n "Delete RAMCloud backup server data [y/N]? "
752 while [ 1 ]; do
753 read key
754 if [ "${key}" == "Y" -o "${key}" == "y" ]; then
755 break
756 elif [ -z "${key}" -o "${key}" == "N" -o "${key}" == "n" ]; then
757 echo "Cancelled."
758 return
759 fi
760 echo "[y/N]? "
761 done
762
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700763 echo -n "Removing RAMCloud backup server data ... "
764 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
765 rm -f ${rc_datafile}
766 echo "DONE"
767}
768
Naoki Shiota4e463182014-03-21 15:13:24 -0700769function stop-server {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700770 kill-processes "RAMCloud server" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700771}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700772############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700773
774
Naoki Shiota590c18d2014-03-31 17:52:59 -0700775## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700776function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700777 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700778 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700779 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700780 ${MVN} -f ${ONOS_HOME}/pom.xml compile
781 fi
782 JAVA_CP=`cat ${CPFILE}`
783 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
784
785 case "$1" in
786 start)
787 stop-onos
788 start-onos
789 ;;
790 startnokill)
791 start-onos
792 ;;
793 startifdown)
794 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
795 if [ $n == 0 ]; then
796 start-onos
797 else
798 echo "$n instance of onos running"
799 fi
800 ;;
801 stop)
802 stop-onos
803 ;;
804 stat*) # <- status
805 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
806 echo "$n instance of onos running"
807 ;;
808 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700809 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700810 exit 1
811 esac
812}
813
814function start-onos {
815 if [ ! -d ${LOGDIR} ]; then
816 mkdir -p ${LOGDIR}
817 fi
818 # Backup log files
819 for log in ${LOGS}; do
820 if [ -f ${log} ]; then
821 rotate-log ${log}
822 fi
823 done
Naoki Shiota9df15d32014-03-27 14:26:20 -0700824
Naoki Shiota4e463182014-03-21 15:13:24 -0700825 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700826 echo "[WARNING] ${ONOS_LOGBACK} not found."
827 echo " Run \"\$ $0 setup\" to create."
828 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700829 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700830
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700831 if [ ! -f ${HC_CONF} ]; then
832 echo "[WARNING] ${HC_CONF} not found."
833 echo " Run \"\$ $0 setup\" to create."
834 exit 1
835 fi
836
837 # specify hazelcast.xml to datagrid
838 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
839
840 # specify backend config
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700841 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700842 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
843 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700844 elif [ "${ONOS_HOST_BACKEND}" = "hazelcast" ]; then
845 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.hazelcast.baseConfig=${HC_CONF}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700846 fi
847
Naoki Shiota9df15d32014-03-27 14:26:20 -0700848 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -0700849
850 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
851 cd ${ONOS_HOME}
852
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700853 echo -n "Starting ONOS controller ... "
Naoki Shiota4e463182014-03-21 15:13:24 -0700854 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
855
856 # We need to wait a bit to find out whether starting the ONOS process succeeded
857 sleep 1
858
859 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700860 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700861 echo " STARTED"
862 else
863 echo " FAILED"
864 fi
865
866# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
867# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
868}
869
870function stop-onos {
871 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
872# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
873}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700874############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700875
876
Naoki Shiota590c18d2014-03-31 17:52:59 -0700877################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -0700878case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -0700879 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -0700880 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700881 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700882 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -0700883 mode_parameter=${ONOS_HOST_ROLE}
884 if [ ! -z "$2" ]; then
885 mode_parameter=$2
886 fi
887
888 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -0700889 single-node)
890 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700891 start-backend coord
892 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700893 onos startifdown
894 ;;
895 coord-node)
896 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700897 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -0700898 onos startifdown
899 ;;
900 server-node)
901 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700902 start-backend server
903 onos startifdown
904 ;;
905 coord-and-server-node)
906 zk start
907 start-backend coord
908 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700909 onos startifdown
910 ;;
911 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700912 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700913 ;;
914 esac
915 echo
916 ;;
917 stop)
918 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
919 if [ $on != 0 ]; then
920 onos stop
921 fi
922
Naoki Shiota9df15d32014-03-27 14:26:20 -0700923 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -0700924
925 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
926 if [ $zkn != 0 ]; then
927 zk stop
928 fi
929 echo
930 ;;
931 restart)
932 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
933 if [ $on != 0 ]; then
934 onos stop
935 fi
936
937 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
938 if [ $rcsn != 0 ]; then
939 rc-server stop
940 fi
941
Naoki Shiota590c18d2014-03-31 17:52:59 -0700942 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700943 if [ $rccn != 0 ]; then
944 rc-coord stop
945 fi
946
947 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
948 if [ $zkn != 0 ]; then
949 zk restart
950 fi
951
952 if [ $rccn != 0 ]; then
953 rc-coord startifdown
954 fi
955
956 if [ $rcsn != 0 ]; then
957 rc-server startifdown
958 fi
959
960 if [ $on != 0 ]; then
961 onos startifdown
962 fi
963 echo
964 ;;
965 stat*) # <- status
966 echo '[ZooKeeper]'
967 zk status
968 echo
969 echo '[RAMCloud coordinator]'
970 rc-coord status
971 echo
972 echo '[RAMCloud server]'
973 rc-server status
974 echo
975 echo '[ONOS core]'
976 onos status
977 echo
978 ;;
979 zk)
980 zk $2
981 ;;
982 rc-c*) # <- rc-coordinator
983 rc-coord $2
984 ;;
985 rc-s*) # <- rc-server
986 rc-server $2
987 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700988 rc)
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700989 # TODO make deldb command more organized (clarify when it can work)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700990 rc-coord $2
991 rc-server $2
992 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700993 core)
994 onos $2
995 ;;
996 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700997 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700998 exit 1
999esac
Naoki Shiota590c18d2014-03-31 17:52:59 -07001000