blob: 2fdf0a837bd433d2ffa73a86e956f85b2dce43d7 [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)
6# $ONOS_CONF : path of ONOS node config file (~/ONOS/conf/onos_node.conf)
7# $ONOS_PROPS : path of ONOS properties file (~/ONOS/conf/onos.properties)
8# $ONOS_LOGBACK : path of logback config file (~/ONOS/conf/logback.`hostname`.xml)
9# $LOGDIR : path of log output directory (~/ONOS/onos-logs)
10# $LOGBASE : base name of log output file (onos.`hostname`)
11# $RAMCLOUD_HOME : path of root directory of RAMCloud repository (~/ramcloud)
12# $RAMCLOUD_BRANCH : branch name of RAMCloud to use (master)
13# $JVM_OPTS : JVM options ONOS starts with
14#####################################################
15
16
17### Variables read from ONOS config file ###
18ONOS_HOME=${ONOS_HOME:-$(cd `dirname $0`; pwd)}
Naoki Shiota9df15d32014-03-27 14:26:20 -070019ONOS_CONF_DIR=${ONOS_CONF_DIR:-${ONOS_HOME}/conf}
Naoki Shiota590c18d2014-03-31 17:52:59 -070020ONOS_CONF=${ONOS_CONF:-${ONOS_CONF_DIR}/onos_node.conf}
21
22if [ ! -f ${ONOS_CONF} ]; then
23 echo "${ONOS_CONF} not found."
24 exit 1
25fi
26ONOS_HOST_NAME=`grep ^host.name ${ONOS_CONF} | cut -d "=" -f 2 | sed -e 's/^[ \t]*//'`
27if [ -z "${ONOS_HOST_NAME}" ]; then
28 ONOS_HOST_NAME=`hostname`
29fi
30ONOS_HOST_IP=`grep ^host.ip ${ONOS_CONF} | cut -d "=" -f 2 | sed -e 's/^[ \t]*//'`
31ONOS_HOST_ROLE=`grep ^host.role ${ONOS_CONF} | cut -d "=" -f 2 | sed -e 's/^[ \t]*//'`
32ONOS_HOST_BACKEND=`grep ^host.backend ${ONOS_CONF} | cut -d "=" -f 2 | sed -e 's/^[ \t]*//'`
33ZK_HOSTS=`grep ^zookeeper.hosts ${ONOS_CONF} | cut -d "=" -f 2 | sed -e 's/^[ \t]*//'`
34RC_COORD_PROTOCOL=`grep ^ramcloud.coordinator.protocol ${ONOS_CONF} | cut -d "=" -f 2 | sed -e 's/^[ \t]*//'`
35RC_COORD_IP=`grep ^ramcloud.coordinator.ip ${ONOS_CONF} | cut -d "=" -f 2 | sed -e 's/^[ \t]*//'`
36RC_COORD_PORT=`grep ^ramcloud.coordinator.port ${ONOS_CONF} | cut -d "=" -f 2 | sed -e 's/^[ \t]*//'`
37RC_SERVER_PROTOCOL=`grep ^ramcloud.server.protocol ${ONOS_CONF} | cut -d "=" -f 2 | sed -e 's/^[ \t]*//'`
38RC_SERVER_IP=`grep ^ramcloud.server.ip ${ONOS_CONF} | cut -d "=" -f 2 | sed -e 's/^[ \t]*//'`
39RC_SERVER_PORT=`grep ^ramcloud.server.port ${ONOS_CONF} | cut -d "=" -f 2 | sed -e 's/^[ \t]*//'`
40############################################
41
42
43############## Other variables #############
Naoki Shiota4e463182014-03-21 15:13:24 -070044LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
45
46ZK_DIR=${HOME}/zookeeper-3.4.5
Naoki Shiota9df15d32014-03-27 14:26:20 -070047ZK_CONF_FILE=zoo.cfg
48ZK_CONF=${ONOS_CONF_DIR}/${ZK_CONF_FILE}
49ZK_CONF_BACKUP=${ZK_CONF}.bak
50ZK_CONF_TEMPLATE=${ONOS_CONF_DIR}/zoo.cfg.template
Naoki Shiota4e463182014-03-21 15:13:24 -070051ZOO_LOG_DIR=${ONOS_HOME}/onos-logs
Naoki Shiota9df15d32014-03-27 14:26:20 -070052ZK_LIB_DIR=/var/lib/zookeeper
53ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070054
55RAMCLOUD_DIR=${HOME}/ramcloud
56RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070057RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
58RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070059RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Naoki Shiota9df15d32014-03-27 14:26:20 -070060RAMCLOUD_COORD_PORT=12246
61RAMCLOUD_SERVER_PORT=12242
Naoki Shiota4e463182014-03-21 15:13:24 -070062
63export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}
64
65## 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 -070066ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070067ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
68ONOS_LOGBACK_TEMPLATE=${ONOS_CONF_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070069LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070070LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070071ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
72PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
73LOGS="$ONOS_LOG $PCAP_LOG"
74
Naoki Shiota9df15d32014-03-27 14:26:20 -070075ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -070076JMX_PORT=${JMX_PORT:-7189}
77
78# Set JVM options
79JVM_OPTS="${JVM_OPTS:-}"
80## If you want JaCoCo Code Coverage reports... uncomment line below
81#JVM_OPTS="$JVM_OPTS -javaagent:${ONOS_HOME}/lib/jacocoagent.jar=dumponexit=true,output=file,destfile=${LOGDIR}/jacoco.exec"
82JVM_OPTS="$JVM_OPTS -server -d64"
83#JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
84JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
85#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
86#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
87#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC -XX:+AggressiveOpts -XX:+UseFastAccessorMethods"
88JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC -XX:+UseAdaptiveSizePolicy -XX:+AggressiveOpts -XX:+UseFastAccessorMethods"
89JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192 -XX:FreqInlineSize=8192"
90JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500 -XX:PreBlockSpin=8"
91JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
92#JVM_OPTS="$JVM_OPTS -Dpython.security.respectJavaAccessibility=false"
93JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500 -XX:PreBlockSpin=8 \
94 -XX:+UseThreadPriorities \
95 -XX:ThreadPriorityPolicy=42 \
96 -XX:+UseCompressedOops \
97 -Dcom.sun.management.jmxremote.port=$JMX_PORT \
98 -Dcom.sun.management.jmxremote.ssl=false \
99 -Dbenchmark.measureBP=0 \
100 -Dbenchmark.measureRc=0 \
101 -Dbenchmark.measureONOS=0 \
102 -Dcom.sun.management.jmxremote.authenticate=false"
103JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
104
105# Set ONOS core main class
106MAIN_CLASS="net.onrc.onos.ofcontroller.core.Main"
107
108MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700109############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700110
Naoki Shiota9df15d32014-03-27 14:26:20 -0700111
Naoki Shiota590c18d2014-03-31 17:52:59 -0700112############# Common functions #############
113function print_usage {
114 local filename=`basename ${ONOS_CONF}`
115 local usage="Usage: setup/start/stop ONOS on this server.
116 \$ $0 setup [-f]
117 Set up ONOS node using ${filename}.
118 - generate and replace config file of ZooKeeper.
119 - create myid in ZooKeeper datadir.
120 - generate and replace logback.${ONOS_HOST_NAME}.xml
121 If -f option is used, all existing files will be overwritten without confirmation.
122 \$ $0 start [single-node|coord-node|server-node|coord-and-server-node]
123 Start ONOS node with specific RAMCloud entities
124 - single-node: start ONOS with stand-alone RAMCloud
125 - coord-node : start ONOS with RAMCloud coordinator
126 - server-node: start ONOS with RAMCloud server
127 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
128 * Default behavior can be defined by ${filename}
129 \$ $0 stop
130 Stop all ONOS-related processes
131 \$ $0 restart
132 Stop and start currently running ONOS-related processes
133 \$$0 status
134 Show status of ONOS-related processes
135 \$ $0 {zk|rc-coord|rc-server|core} {start|stop|restart|status}
136 Control specific ONOS-related process"
137
138 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700139}
140
141function rotate-log {
142 local logfile=$1
143 local nr_max=${2:-10}
144 if [ -f $logfile ]; then
145 for i in `seq $(expr $nr_max - 1) -1 1`; do
146 if [ -f ${logfile}.${i} ]; then
147 mv -f ${logfile}.${i} ${logfile}.`expr $i + 1`
148 fi
149 done
150 mv $logfile $logfile.1
151 fi
152}
153
154# kill-processes {module-name} {array of pids}
155function kill-processes {
156 # Kill the existing processes
157 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700158 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700159 echo -n "Stopping $1 ... "
160 fi
161 for p in ${pids}; do
162 if [ x$p != "x" ]; then
163 kill -KILL $p
164 echo "Killed existing process (pid: $p)"
165 fi
166 done
167}
168
Naoki Shiota9df15d32014-03-27 14:26:20 -0700169function revert-conf {
170 set -e
171
172 echo -n "ERROR occurred ... "
173 local temp_zk="${ZK_CONF}.tmp"
174
175 if [ -f "${temp_zk}" ]; then
176 local zk_file=`basename ${ZK_CONF}`
177 echo -n "reverting ${zk_file} ... "
178 rm ${temp_zk}
179 mv ${ZK_CONF_BACKUP} ${ZK_CONF}
180 fi
181
182 echo "EXIT"
183
184 if [ ! -z "$1" ]; then
185 echo $1
186 fi
187
188 set +e
189
190 exit 1
191}
192
Naoki Shiota590c18d2014-03-31 17:52:59 -0700193function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700194 echo -n "Creating ${ZK_CONF} ... "
195
Naoki Shiota590c18d2014-03-31 17:52:59 -0700196 # creation of zookeeper config
Naoki Shiota9df15d32014-03-27 14:26:20 -0700197
Naoki Shiota590c18d2014-03-31 17:52:59 -0700198 local temp_zk="${ZK_CONF}.tmp"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700199 touch ${temp_zk}
200
201 if [ -f ${ZK_CONF} ]; then
202 mv ${ZK_CONF} ${ZK_CONF_BACKUP}
203 local backup_file=`basename ${ZK_CONF_BACKUP}`
204 echo -n "backup old file to ${backup_file} ... "
205 fi
206
207 local hosts=${ZK_HOSTS}
208 if [ -z "${hosts}" ]; then
209 # assume single-node mode
210 hosts=${ONOS_HOST_NAME}
211 fi
212
213 hostarr=`echo ${hosts} | tr "," " "`
214
215 local i=1
216 local myid=
217 for host in ${hostarr}; do
218 if [ ${host} = ${ONOS_HOST_NAME} ]; then
219 myid=$i
220 break
221 fi
222 i=`expr $i + 1`
223 done
224
225 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700226 local filename=`basename ${ONOS_CONF}`
227 revert-conf "[ERROR in ${filename}] zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\""
Naoki Shiota9df15d32014-03-27 14:26:20 -0700228 fi
229
230 # TODO: Remove sudo.
231 # This is temporary code for the sake of compatibility with old code (which creates myid with root user).
232 sudo mv ${ZK_MY_ID} ${ZK_MY_ID}.old
233 echo ${myid} > ${ZK_MY_ID}
234
235 echo -n "myid is assigned to ${myid} ... "
236
237 while read line; do
238 if [[ $line =~ ^__HOSTS__$ ]]; then
239 i=1
240 for host in ${hostarr}; do
241 # TODO: ports might be configurable
242 local hostline="server.${i}=${host}:2888:3888"
243 echo $hostline >> "${temp_zk}"
244 i=`expr $i + 1`
245 done
246 elif [[ $line =~ __DATADIR__ ]]; then
247 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|" >> ${temp_zk}
248 else
249 echo $line >> ${temp_zk}
250 fi
251 done < ${ZK_CONF_TEMPLATE}
252 mv ${temp_zk} ${ZK_CONF}
253
254 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700255}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700256
Naoki Shiota590c18d2014-03-31 17:52:59 -0700257function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700258 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700259
Naoki Shiota9df15d32014-03-27 14:26:20 -0700260 # creation of logback config
261 if [ -f $ONOS_LOGBACK ]; then
262 local logback_file=`basename ${ONOS_LOGBACK}`
263 mv ${ONOS_LOGBACK} ${ONOS_LOGBACK_BACKUP}
264 local logback_back_file=`basename ${ONOS_LOGBACK_BACKUP}`
265 echo -n "backup old file to ${logback_back_file} ... "
266 fi
267 sed -e "s|__FILENAME__|${ONOS_LOG}|" ${ONOS_LOGBACK_TEMPLATE} > ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700268
Naoki Shiota9df15d32014-03-27 14:26:20 -0700269 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700270}
271
Naoki Shiota590c18d2014-03-31 17:52:59 -0700272function create-conf {
273 local key
274 local filename
275
276 trap revert-conf ERR
277
278 if [ -f ${ZK_CONF} -a "$1" != "-f" ]; then
279 # confirmation to overwrite existing config file
280 echo -n "Overwriting ${ZK_CONF_FILE} [Y/n]? "
281 while [ 1 ]; do
282 read key
283 if [ -z "${key}" -o "${key}" == "Y" ]; then
284 create-zk-conf
285 break
286 elif [ "${key}" == "n" ]; then
287 break
288 fi
289 echo "[Y/n]?"
290 done
291 else
292 create-zk-conf
293 fi
294
295 if [ -f ${ONOS_LOGBACK} -a "$1" != "-f" ]; then
296 filename=`basename ${ONOS_LOGBACK}`
297 echo -n "Overwriting ${filename} [Y/n]? "
298 while [ 1 ]; do
299 read key
300 if [ -z "${key}" -o "${key}" == "Y" ]; then
301 create-logback-conf
302 break
303 elif [ "${key}" == "n" ]; then
304 break
305 fi
306 echo "[Y/n]?"
307 done
308 else
309 create-logback-conf
310 fi
311
312 trap - ERR
313}
314############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700315
Naoki Shiota590c18d2014-03-31 17:52:59 -0700316
317###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700318function zk {
319 case "$1" in
320 start)
321 start-zk
322 ;;
323 stop)
324 stop-zk
325 ;;
326 stat*) # <- status
327 status-zk
328 ;;
329 re*) # <- restart
330 stop-zk
331 start-zk
332 ;;
333 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700334 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700335 exit 1
336 esac
337}
338
339function start-zk {
Naoki Shiota4e463182014-03-21 15:13:24 -0700340 echo -n "Starting Zookeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700341
342 if [ -f "${ZK_CONF}" ]; then
343 # Run Zookeeper with our configuration
344 export ZOOCFG=${ZK_CONF_FILE}
345 export ZOOCFGDIR=${ONOS_CONF_DIR}
346 fi
347
Naoki Shiota4e463182014-03-21 15:13:24 -0700348 $ZK_DIR/bin/zkServer.sh start
349}
350
351function stop-zk {
352 kill-processes "ZooKeeper" `jps -l | grep org.apache.zookeeper.server | awk '{print $1}'`
353}
354
355function status-zk {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700356 if [ -f ${ZK_CONF} ]; then
357 export ZOOCFG=${ZK_CONF_FILE}
358 export ZOOCFGDIR=${ONOS_CONF_DIR}
359 fi
360
Naoki Shiota4e463182014-03-21 15:13:24 -0700361 $ZK_DIR/bin/zkServer.sh status
362}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700363############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700364
365
Naoki Shiota590c18d2014-03-31 17:52:59 -0700366####### Functions related to RAMCloud ######
Naoki Shiota9df15d32014-03-27 14:26:20 -0700367function start-backend {
368 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700369 if [ $1 == "coord" ]; then
370 rc-coord startifdown
371 elif [ $1 == "server" ]; then
372 rc-server startifdown
373 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700374 fi
375}
376
377function stop-backend {
378 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
379 if [ $rcsn != 0 ]; then
380 rc-server stop
381 fi
382
Naoki Shiota590c18d2014-03-31 17:52:59 -0700383 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700384 if [ $rccn != 0 ]; then
385 rc-coord stop
386 fi
387}
388
Naoki Shiota590c18d2014-03-31 17:52:59 -0700389function deldb {
390# TODO: implement
391 return
392}
393
394
Naoki Shiota4e463182014-03-21 15:13:24 -0700395### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700396function rc-coord-addr {
397 local coordproto=${RC_COORD_PROTOCOL}
398 local coordip=${RC_COORD_IP}
399 local coordport=${RC_COORD_PORT}
400
401 if [ -z "${coordproto}" ]; then
402 coordproto='fast+udp'
403 fi
404
405 if [ -z "${coordip}" ]; then
406 # assume single-node mode
407 coordip=${ONOS_HOST_IP}
408 fi
409
410 if [ -z "${coordport}" ]; then
411 # assume default port
412 coordport=${RAMCLOUD_COORD_PORT}
413 fi
414
415 echo "${coordproto}:host=${coordip},port=${coordport}"
416}
417
418function rc-server-addr {
419 local serverproto=${RC_SERVER_PROTOCOL}
420 local serverip=${RC_SERVER_IP}
421 local serverport=${RC_SERVER_PORT}
422
423 if [ -z "${serverproto}" ]; then
424 serverproto='fast+udp'
425 fi
426
427 # Normally this parameter should be null
428 if [ -z "${serverip}" ]; then
429 serverip=${ONOS_HOST_IP}
430 fi
431
432 if [ -z "${serverport}" ]; then
433 # assume default port
434 serverport=${RAMCLOUD_SERVER_PORT}
435 fi
436
437 echo "${serverproto}:host=${serverip},port=${serverport}"
438}
439
Naoki Shiota4e463182014-03-21 15:13:24 -0700440function rc-coord {
441 case "$1" in
442 start)
443 deldb
Naoki Shiota4e463182014-03-21 15:13:24 -0700444 stop-coord
445 start-coord
446 ;;
447 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700448 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700449 if [ $n == 0 ]; then
450 start-coord
451 else
452 echo "$n instance of RAMCloud coordinator running"
453 fi
454 ;;
455 stop)
456 stop-coord
457 ;;
458 stat*) # <- status
459 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
460 echo "$n RAMCloud coordinator running"
461 ;;
462 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700463 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700464 exit 1
465 esac
466}
467
468function start-coord {
469 if [ ! -d ${LOGDIR} ]; then
470 mkdir -p ${LOGDIR}
471 fi
472 if [ -f $RAMCLOUD_COORD_LOG ]; then
473 rotate-log $RAMCLOUD_COORD_LOG
474 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700475
476 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700477
478 # Run ramcloud
479 echo -n "Starting RAMCloud coordinator ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700480 $RAMCLOUD_DIR/obj.${RAMCLOUD_BRANCH}/coordinator -L ${coord_addr} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700481 echo "STARTED"
482}
483
484
485function stop-coord {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700486 kill-processes "RAMCloud coordinator" `pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700487}
488
Naoki Shiota4e463182014-03-21 15:13:24 -0700489### Functions related to RAMCloud server
490function rc-server {
491 case "$1" in
492 start)
493 deldb
Naoki Shiota4e463182014-03-21 15:13:24 -0700494 stop-server
495 start-server
496 ;;
497 startifdown)
498 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
499 if [ $n == 0 ]; then
500 start-server
501 else
502 echo "$n instance of RAMCloud server running"
503 fi
504 ;;
505 stop)
506 stop-server
507 ;;
508# deldb)
509# deldb
510# ;;
511 stat*) # <- status
512 n=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
513 echo "$n RAMCloud server running"
514 ;;
515 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700516 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700517 exit 1
518 esac
519}
520
521function start-server {
522 if [ ! -d ${LOGDIR} ]; then
523 mkdir -p ${LOGDIR}
524 fi
525 if [ -f $RAMCLOUD_SERVER_LOG ]; then
526 rotate-log $RAMCLOUD_SERVER_LOG
527 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700528
529 local coord_addr=`rc-coord-addr`
530 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700531
532 # Run ramcloud
533 echo -n "Starting RAMCloud server ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700534 ${RAMCLOUD_DIR}/obj.${RAMCLOUD_BRANCH}/server -M -L ${server_addr} -C ${coord_addr} --masterServiceThreads 1 --logCleanerThreads 1 --detectFailures 0 > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700535 echo "STARTED"
536}
537
538function stop-server {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700539 kill-processes "RAMCloud server" `pgrep -f obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700540}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700541############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700542
543
Naoki Shiota590c18d2014-03-31 17:52:59 -0700544## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700545function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700546 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700547 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700548 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700549 ${MVN} -f ${ONOS_HOME}/pom.xml compile
550 fi
551 JAVA_CP=`cat ${CPFILE}`
552 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
553
554 case "$1" in
555 start)
556 stop-onos
557 start-onos
558 ;;
559 startnokill)
560 start-onos
561 ;;
562 startifdown)
563 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
564 if [ $n == 0 ]; then
565 start-onos
566 else
567 echo "$n instance of onos running"
568 fi
569 ;;
570 stop)
571 stop-onos
572 ;;
573 stat*) # <- status
574 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
575 echo "$n instance of onos running"
576 ;;
577 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700578 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700579 exit 1
580 esac
581}
582
583function start-onos {
584 if [ ! -d ${LOGDIR} ]; then
585 mkdir -p ${LOGDIR}
586 fi
587 # Backup log files
588 for log in ${LOGS}; do
589 if [ -f ${log} ]; then
590 rotate-log ${log}
591 fi
592 done
Naoki Shiota9df15d32014-03-27 14:26:20 -0700593
Naoki Shiota4e463182014-03-21 15:13:24 -0700594 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700595 echo "[WARNING] ${ONOS_LOGBACK} not found."
596 echo " Run \"\$ $0 setup\" to create."
597 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700598 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700599
600 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.datastore.backend=${ONOS_HOST_BACKEND}"
601
602 # Run ONOS
603
604 echo -n "Starting ONOS controller ..."
Naoki Shiota4e463182014-03-21 15:13:24 -0700605 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
606
607 # We need to wait a bit to find out whether starting the ONOS process succeeded
608 sleep 1
609
610 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700611 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700612 echo " STARTED"
613 else
614 echo " FAILED"
615 fi
616
617# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
618# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
619}
620
621function stop-onos {
622 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
623# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
624}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700625############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700626
627
Naoki Shiota590c18d2014-03-31 17:52:59 -0700628################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -0700629case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -0700630 setup)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700631 create-conf $2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700632 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700633 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -0700634 mode_parameter=${ONOS_HOST_ROLE}
635 if [ ! -z "$2" ]; then
636 mode_parameter=$2
637 fi
638
639 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -0700640 single-node)
641 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700642 start-backend coord
643 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700644 onos startifdown
645 ;;
646 coord-node)
647 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700648 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -0700649 onos startifdown
650 ;;
651 server-node)
652 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700653 start-backend server
654 onos startifdown
655 ;;
656 coord-and-server-node)
657 zk start
658 start-backend coord
659 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700660 onos startifdown
661 ;;
662 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700663 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700664 ;;
665 esac
666 echo
667 ;;
668 stop)
669 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
670 if [ $on != 0 ]; then
671 onos stop
672 fi
673
Naoki Shiota9df15d32014-03-27 14:26:20 -0700674 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -0700675
676 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
677 if [ $zkn != 0 ]; then
678 zk stop
679 fi
680 echo
681 ;;
682 restart)
683 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
684 if [ $on != 0 ]; then
685 onos stop
686 fi
687
688 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
689 if [ $rcsn != 0 ]; then
690 rc-server stop
691 fi
692
Naoki Shiota590c18d2014-03-31 17:52:59 -0700693 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700694 if [ $rccn != 0 ]; then
695 rc-coord stop
696 fi
697
698 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
699 if [ $zkn != 0 ]; then
700 zk restart
701 fi
702
703 if [ $rccn != 0 ]; then
704 rc-coord startifdown
705 fi
706
707 if [ $rcsn != 0 ]; then
708 rc-server startifdown
709 fi
710
711 if [ $on != 0 ]; then
712 onos startifdown
713 fi
714 echo
715 ;;
716 stat*) # <- status
717 echo '[ZooKeeper]'
718 zk status
719 echo
720 echo '[RAMCloud coordinator]'
721 rc-coord status
722 echo
723 echo '[RAMCloud server]'
724 rc-server status
725 echo
726 echo '[ONOS core]'
727 onos status
728 echo
729 ;;
730 zk)
731 zk $2
732 ;;
733 rc-c*) # <- rc-coordinator
734 rc-coord $2
735 ;;
736 rc-s*) # <- rc-server
737 rc-server $2
738 ;;
739 core)
740 onos $2
741 ;;
742 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700743 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700744 exit 1
745esac
Naoki Shiota590c18d2014-03-31 17:52:59 -0700746