blob: c1e1b2b9a9beae6ac50ac1f407795f5af57fb884 [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
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700459
460 # log4j.properties is read from classpath if not found in CWD.
461 # Using the ZooKeeper supplied default in ZooKeeper conf dir.
462 # TODO: To explicitly specify our customized log4j config file:
463 # export SERVER_JVMFLAGS="-Dlog4j.configuration=${ZK_LOG4J}"
464 env CLASSPATH="${ZK_HOME}/conf:${CLASSPATH}" ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700465}
466
467function stop-zk {
468 kill-processes "ZooKeeper" `jps -l | grep org.apache.zookeeper.server | awk '{print $1}'`
469}
470
471function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700472 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700473
Naoki Shiota72209722014-04-08 14:32:17 -0700474 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700475}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700476
477function check-zk {
478 # assumption here is that ZK status script is the last command in status-zk.
479 status-zk &> /dev/null
480 local zk_status=$?
481 if [ "$zk_status" -ne 0 ]; then
482 return 1;
483 fi
484 return 0
485}
486
487# wait-zk-or-die {timeout-sec}
488function wait-zk-or-die {
489 local retries=${1:-1}
490 # do-while retries >= 0
491 while true; do
492 check-zk
493 local zk_status=$?
494 if [ "$zk_status" -eq 0 ]; then
495 return 0
496 fi
497 sleep 1;
498 ((retries -= 1))
499 (( retries >= 0 )) || break
500 done
501 echo "ZooKeeper is not running."
502 exit 1
503}
504
Naoki Shiota590c18d2014-03-31 17:52:59 -0700505############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700506
507
Naoki Shiota590c18d2014-03-31 17:52:59 -0700508####### Functions related to RAMCloud ######
Naoki Shiota9df15d32014-03-27 14:26:20 -0700509function start-backend {
510 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700511 if [ $1 == "coord" ]; then
512 rc-coord startifdown
513 elif [ $1 == "server" ]; then
514 rc-server startifdown
515 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700516 fi
517}
518
519function stop-backend {
520 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
521 if [ $rcsn != 0 ]; then
522 rc-server stop
523 fi
524
Naoki Shiota590c18d2014-03-31 17:52:59 -0700525 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700526 if [ $rccn != 0 ]; then
527 rc-coord stop
528 fi
529}
530
Naoki Shiota590c18d2014-03-31 17:52:59 -0700531
Naoki Shiota4e463182014-03-21 15:13:24 -0700532### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700533function rc-coord-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700534 local host=${RC_COORD_IP}
535 if [ -z "${host}" ]; then
536 # falling back to 0.0.0.0
537 host="0.0.0.0"
538 fi
539 echo "${RC_COORD_PROTOCOL}:host=${host},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700540}
541
542function rc-server-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700543 local host=${RC_SERVER_IP}
544 if [ -z "${host}" ]; then
545 # falling back to 0.0.0.0
546 host="0.0.0.0"
547 fi
548 echo "${RC_SERVER_PROTOCOL}:host=${host},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700549}
550
Naoki Shiota4e463182014-03-21 15:13:24 -0700551function rc-coord {
552 case "$1" in
553 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700554 stop-coord
555 start-coord
556 ;;
557 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700558 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700559 if [ $n == 0 ]; then
560 start-coord
561 else
562 echo "$n instance of RAMCloud coordinator running"
563 fi
564 ;;
565 stop)
566 stop-coord
567 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700568 deldb)
569 stop-backend
570 del-coord-info
571 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700572 stat*) # <- status
573 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
574 echo "$n RAMCloud coordinator running"
575 ;;
576 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700577 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700578 exit 1
579 esac
580}
581
582function start-coord {
Yuta HIGUCHI71703322014-05-06 09:17:29 -0700583 wait-zk-or-die 2
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700584
Naoki Shiota4e463182014-03-21 15:13:24 -0700585 if [ ! -d ${LOGDIR} ]; then
586 mkdir -p ${LOGDIR}
587 fi
588 if [ -f $RAMCLOUD_COORD_LOG ]; then
589 rotate-log $RAMCLOUD_COORD_LOG
590 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700591
592 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700593
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700594 # TODO Configuration for ZK address, port
595 local zk_addr="localhost:2181"
596 # RAMCloud cluster name
597 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700598 # RAMCloud transport timeout
599 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700600 # RAMCloud option deadServerTimeout
601 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
602 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
603
604 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
605 # but actual RAMCloud code only uses -C argument now.
606 # (FYI: -C is documented to be deprecated in the document)
607
608 local coord_args="-C ${coord_addr}"
609 coord_args="${coord_args} --externalStorage zk:${zk_addr}"
610 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700611 coord_args="${coord_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700612 coord_args="${coord_args} --deadServerTimeout ${rc_coord_deadServerTimeout}"
613
614 # Read environment variables if set
615 coord_args="${coord_args} ${RC_COORDINATOR_OPTS}"
616
617 if [ "${ONOS_HOST_ROLE}" == "single-node" ]; then
618 # Note: Following reset is required, since RC restart is considered node failure,
619 # and tries recovery of server, which will never succeed after restart.
620 echo "Role configured to single-node mode. RAMCloud cluster will be reset on each start-up."
621 coord_args="${coord_args} --reset"
622 fi
623
Naoki Shiota4e463182014-03-21 15:13:24 -0700624 # Run ramcloud
625 echo -n "Starting RAMCloud coordinator ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700626 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700627 echo "STARTED"
628}
629
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700630function del-coord-info {
631 wait-zk-or-die 1
632
633 if [ ! -d ${LOGDIR} ]; then
634 mkdir -p ${LOGDIR}
635 fi
636 if [ -f $RAMCLOUD_COORD_LOG ]; then
637 rotate-log $RAMCLOUD_COORD_LOG
638 fi
639
640 local coord_addr=`rc-coord-addr`
641
642 # TODO Configuration for ZK address, port
643 local zk_addr="localhost:2181"
644 # RAMCloud cluster name
645 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
646 # RAMCloud option deadServerTimeout
647 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
648 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
649
650 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
651 # but actual RAMCloud code only uses -C argument now.
652 # (FYI: -C is documented to be deprecated in the document)
653
654 local coord_args="-C ${coord_addr}"
655 coord_args="${coord_args} --externalStorage zk:${zk_addr}"
656 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
657
658 # Note: --reset will reset ZK stored info and start running as acoordinator.
659 echo -n "Deleting RAMCloud cluster coordination info ... "
660 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} --reset &> $RAMCLOUD_COORD_LOG &
661
Yuta HIGUCHIcd44eb32014-05-24 16:51:31 -0700662 # TODO Assuming 5 sec is enough. To be sure monitor log?
663 sleep 5
664 # kill coordinator
Yuta HIGUCHIce9f3ee2014-05-14 09:48:40 -0700665 (pkill -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator &> /dev/null)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700666
667 echo "DONE"
668}
Naoki Shiota4e463182014-03-21 15:13:24 -0700669
670function stop-coord {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700671 kill-processes "RAMCloud coordinator" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700672}
673
Naoki Shiota4e463182014-03-21 15:13:24 -0700674### Functions related to RAMCloud server
675function rc-server {
676 case "$1" in
677 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700678 stop-server
679 start-server
680 ;;
681 startifdown)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700682 local n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700683 if [ $n == 0 ]; then
684 start-server
685 else
686 echo "$n instance of RAMCloud server running"
687 fi
688 ;;
689 stop)
690 stop-server
691 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700692 deldb)
693 stop-server
694 del-server-backup
695 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700696 stat*) # <- status
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700697 n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700698 echo "$n RAMCloud server running"
699 ;;
700 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700701 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700702 exit 1
703 esac
704}
705
706function start-server {
Yuta HIGUCHI71703322014-05-06 09:17:29 -0700707 wait-zk-or-die 2
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700708
Naoki Shiota4e463182014-03-21 15:13:24 -0700709 if [ ! -d ${LOGDIR} ]; then
710 mkdir -p ${LOGDIR}
711 fi
712 if [ -f $RAMCLOUD_SERVER_LOG ]; then
713 rotate-log $RAMCLOUD_SERVER_LOG
714 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700715
716 local coord_addr=`rc-coord-addr`
717 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700718
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700719 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.server.masterServiceThreads 5)
720 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.server.logCleanerThreads 1)
Yuta HIGUCHI52efcbb2014-05-16 09:30:33 -0700721 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.server.detectFailures 0)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700722
723 # TODO Configuration for ZK address, port
724 local zk_addr="localhost:2181"
725 # RAMCloud cluster name
726 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700727 # RAMCloud transport timeout
728 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700729 # replication factor (-r) config
730 local rc_replicas=$(read-conf ${ONOS_CONF} ramcloud.server.replicas 0)
731 # backup file path (-f) config
732 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
733 mkdir -p `dirname ${rc_datafile}`
734
735 local server_args="-L ${server_addr}"
736 server_args="${server_args} --externalStorage zk:${zk_addr}"
737 server_args="${server_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700738 server_args="${server_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700739 server_args="${server_args} --masterServiceThreads ${masterServiceThreads}"
740 server_args="${server_args} --logCleanerThreads ${logCleanerThreads}"
741 server_args="${server_args} --detectFailures ${detectFailures}"
742 server_args="${server_args} --replicas ${rc_replicas}"
743 server_args="${server_args} --file ${rc_datafile}"
744
745 # Read environment variables if set
746 server_args="${server_args} ${RC_SERVER_OPTS}"
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700747
Naoki Shiota4e463182014-03-21 15:13:24 -0700748 # Run ramcloud
749 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700750 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server ${server_args} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700751 echo "STARTED"
752}
753
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700754function del-server-backup {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700755 echo -n "Delete RAMCloud backup server data [y/N]? "
756 while [ 1 ]; do
757 read key
758 if [ "${key}" == "Y" -o "${key}" == "y" ]; then
759 break
760 elif [ -z "${key}" -o "${key}" == "N" -o "${key}" == "n" ]; then
761 echo "Cancelled."
762 return
763 fi
764 echo "[y/N]? "
765 done
766
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700767 echo -n "Removing RAMCloud backup server data ... "
768 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
769 rm -f ${rc_datafile}
770 echo "DONE"
771}
772
Naoki Shiota4e463182014-03-21 15:13:24 -0700773function stop-server {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700774 kill-processes "RAMCloud server" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700775}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700776############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700777
778
Naoki Shiota590c18d2014-03-31 17:52:59 -0700779## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700780function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700781 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700782 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700783 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700784 ${MVN} -f ${ONOS_HOME}/pom.xml compile
785 fi
786 JAVA_CP=`cat ${CPFILE}`
787 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
788
789 case "$1" in
790 start)
791 stop-onos
792 start-onos
793 ;;
794 startnokill)
795 start-onos
796 ;;
797 startifdown)
798 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
799 if [ $n == 0 ]; then
800 start-onos
801 else
802 echo "$n instance of onos running"
803 fi
804 ;;
805 stop)
806 stop-onos
807 ;;
808 stat*) # <- status
809 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
810 echo "$n instance of onos running"
811 ;;
812 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700813 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700814 exit 1
815 esac
816}
817
818function start-onos {
819 if [ ! -d ${LOGDIR} ]; then
820 mkdir -p ${LOGDIR}
821 fi
822 # Backup log files
823 for log in ${LOGS}; do
824 if [ -f ${log} ]; then
825 rotate-log ${log}
826 fi
827 done
Naoki Shiota9df15d32014-03-27 14:26:20 -0700828
Naoki Shiota4e463182014-03-21 15:13:24 -0700829 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700830 echo "[WARNING] ${ONOS_LOGBACK} not found."
831 echo " Run \"\$ $0 setup\" to create."
832 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700833 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700834
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700835 if [ ! -f ${HC_CONF} ]; then
836 echo "[WARNING] ${HC_CONF} not found."
837 echo " Run \"\$ $0 setup\" to create."
838 exit 1
839 fi
840
841 # specify hazelcast.xml to datagrid
842 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
843
844 # specify backend config
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700845 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700846 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
847 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700848 elif [ "${ONOS_HOST_BACKEND}" = "hazelcast" ]; then
849 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.hazelcast.baseConfig=${HC_CONF}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700850 fi
851
Naoki Shiota9df15d32014-03-27 14:26:20 -0700852 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -0700853
854 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
855 cd ${ONOS_HOME}
856
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700857 echo -n "Starting ONOS controller ... "
Naoki Shiota4e463182014-03-21 15:13:24 -0700858 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
859
860 # We need to wait a bit to find out whether starting the ONOS process succeeded
861 sleep 1
862
863 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700864 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700865 echo " STARTED"
866 else
867 echo " FAILED"
868 fi
869
870# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
871# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
872}
873
874function stop-onos {
875 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
876# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
877}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700878############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700879
880
Naoki Shiota590c18d2014-03-31 17:52:59 -0700881################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -0700882case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -0700883 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -0700884 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700885 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700886 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -0700887 mode_parameter=${ONOS_HOST_ROLE}
888 if [ ! -z "$2" ]; then
889 mode_parameter=$2
890 fi
891
892 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -0700893 single-node)
894 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700895 start-backend coord
896 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700897 onos startifdown
898 ;;
899 coord-node)
900 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700901 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -0700902 onos startifdown
903 ;;
904 server-node)
905 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700906 start-backend server
907 onos startifdown
908 ;;
909 coord-and-server-node)
910 zk start
911 start-backend coord
912 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700913 onos startifdown
914 ;;
915 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700916 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700917 ;;
918 esac
919 echo
920 ;;
921 stop)
922 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
923 if [ $on != 0 ]; then
924 onos stop
925 fi
926
Naoki Shiota9df15d32014-03-27 14:26:20 -0700927 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -0700928
929 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
930 if [ $zkn != 0 ]; then
931 zk stop
932 fi
933 echo
934 ;;
935 restart)
936 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
937 if [ $on != 0 ]; then
938 onos stop
939 fi
940
941 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
942 if [ $rcsn != 0 ]; then
943 rc-server stop
944 fi
945
Naoki Shiota590c18d2014-03-31 17:52:59 -0700946 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700947 if [ $rccn != 0 ]; then
948 rc-coord stop
949 fi
950
951 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
952 if [ $zkn != 0 ]; then
953 zk restart
954 fi
955
956 if [ $rccn != 0 ]; then
957 rc-coord startifdown
958 fi
959
960 if [ $rcsn != 0 ]; then
961 rc-server startifdown
962 fi
963
964 if [ $on != 0 ]; then
965 onos startifdown
966 fi
967 echo
968 ;;
969 stat*) # <- status
970 echo '[ZooKeeper]'
971 zk status
972 echo
973 echo '[RAMCloud coordinator]'
974 rc-coord status
975 echo
976 echo '[RAMCloud server]'
977 rc-server status
978 echo
979 echo '[ONOS core]'
980 onos status
981 echo
982 ;;
983 zk)
984 zk $2
985 ;;
986 rc-c*) # <- rc-coordinator
987 rc-coord $2
988 ;;
989 rc-s*) # <- rc-server
990 rc-server $2
991 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700992 rc)
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700993 # TODO make deldb command more organized (clarify when it can work)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700994 rc-coord $2
995 rc-server $2
996 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700997 core)
998 onos $2
999 ;;
1000 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001001 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001002 exit 1
1003esac
Naoki Shiota590c18d2014-03-31 17:52:59 -07001004