blob: 9bc7a3d5affd55cb20618c8c81b3ed0224738f45 [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
Naoki Shiota590c18d2014-03-31 17:52:59 -070028if [ ! -f ${ONOS_CONF} ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070029 # falling back to default config file
30 ONOS_CONF=${ONOS_CONF_DIR}/onos_node.conf
31 if [ ! -f ${ONOS_CONF} ]; then
32 echo "${ONOS_CONF} not found."
33 exit 1
34 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -070035fi
Naoki Shiota4e928512014-04-03 15:49:35 -070036
37
38### Variables read from ONOS config file ###
39ONOS_HOST_NAME=$(read-conf ${ONOS_CONF} host.name `hostname`)
40ONOS_HOST_IP=$(read-conf ${ONOS_CONF} host.ip)
41ONOS_HOST_ROLE=$(read-conf ${ONOS_CONF} host.role)
42ONOS_HOST_BACKEND=$(read-conf ${ONOS_CONF} host.backend)
43ZK_HOSTS=$(read-conf ${ONOS_CONF} zookeeper.hosts ${ONOS_HOST_NAME})
44RC_COORD_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.coordinator.protocol "fast+udp")
45RC_COORD_IP=$(read-conf ${ONOS_CONF} ramcloud.coordinator.ip ${ONOS_HOST_IP})
46RC_COORD_PORT=$(read-conf ${ONOS_CONF} ramcloud.coordinator.port 12246)
47RC_SERVER_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.server.protocol "fast+udp")
48RC_SERVER_IP=$(read-conf ${ONOS_CONF} ramcloud.server.ip ${ONOS_HOST_IP})
49RC_SERVER_PORT=$(read-conf ${ONOS_CONF} ramcloud.server.port 12242)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070050HC_HOST_PORT=$(read-conf ${ONOS_CONF} hazelcast.host.port 5701)
Naoki Shiota4e928512014-04-03 15:49:35 -070051HC_TCPIP_MEMBERS=$(read-conf ${ONOS_CONF} hazelcast.tcp-ip.members)
52HC_MULTICAST_GROUP=$(read-conf ${ONOS_CONF} hazelcast.multicast.group "224.2.2.3")
53HC_MULTICAST_PORT=$(read-conf ${ONOS_CONF} hazelcast.multicast.port 54327)
Naoki Shiota590c18d2014-03-31 17:52:59 -070054############################################
55
56
57############## Other variables #############
Naoki Shiota4e928512014-04-03 15:49:35 -070058ONOS_TEMPLATE_DIR=${ONOS_CONF_DIR}/template
59
Naoki Shiota4e463182014-03-21 15:13:24 -070060LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
61
Naoki Shiota72209722014-04-08 14:32:17 -070062ZK_HOME=${ZK_HOME:-~/zookeeper-3.4.5}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070063ZK_CONF=${ZK_CONF:-${ONOS_CONF_DIR}/zoo.cfg}
Naoki Shiota4e928512014-04-03 15:49:35 -070064ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070065# Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070066ZK_LOG_DIR=${ZK_LOG_DIR:-${ONOS_HOME}/onos-logs/zk-${ONOS_HOST_NAME}}
Naoki Shiota72209722014-04-08 14:32:17 -070067ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070068ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070069
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070070HC_CONF=${HC_CONF:-${ONOS_CONF_DIR}/hazelcast.xml}
Naoki Shiota4e928512014-04-03 15:49:35 -070071HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
72
Naoki Shiota4e463182014-03-21 15:13:24 -070073RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070074RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
75RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070076RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070077RAMCLOUD_CONF=${RAMCLOUD_CONF:-${ONOS_CONF_DIR}/ramcloud.conf}
Naoki Shiota4e463182014-03-21 15:13:24 -070078
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070079export LD_LIBRARY_PATH=${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}:$LD_LIBRARY_PATH
Naoki Shiota4e463182014-03-21 15:13:24 -070080
81## 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 -070082ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070083ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070084ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070085LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070086LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070087ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
88PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
89LOGS="$ONOS_LOG $PCAP_LOG"
90
Naoki Shiota9df15d32014-03-27 14:26:20 -070091ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -070092JMX_PORT=${JMX_PORT:-7189}
93
94# Set JVM options
95JVM_OPTS="${JVM_OPTS:-}"
Naoki Shiota4e463182014-03-21 15:13:24 -070096JVM_OPTS="$JVM_OPTS -server -d64"
97#JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -070098
99# Uncomment or specify appropriate value as JVM_OPTS environment variables.
100#JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
Naoki Shiota4e463182014-03-21 15:13:24 -0700101#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
102#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700103
104#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC"
105JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
106JVM_OPTS="$JVM_OPTS -XX:+AggressiveOpts"
107
108# We may want to remove UseFastAccessorMethods option: http://bugs.java.com/view_bug.do?bug_id=6385687
109JVM_OPTS="$JVM_OPTS -XX:+UseFastAccessorMethods"
110
111JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192"
112JVM_OPTS="$JVM_OPTS -XX:FreqInlineSize=8192"
113JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500"
114
Naoki Shiota4e463182014-03-21 15:13:24 -0700115JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700116
117# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
118JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
119
120JVM_OPTS="$JVM_OPTS -XX:+UseCompressedOops"
121
122JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
123JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
124JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
125
Naoki Shiota4e463182014-03-21 15:13:24 -0700126JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
127
Naoki Shiotad8ea71a2014-04-23 17:04:51 -0700128JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
129
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700130# Uncomment to dump final JVM flags to stdout
131#JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
132
Naoki Shiota4e463182014-03-21 15:13:24 -0700133# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700134MAIN_CLASS="net.onrc.onos.core.main.Main"
Naoki Shiota4e463182014-03-21 15:13:24 -0700135
136MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700137############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700138
Naoki Shiota9df15d32014-03-27 14:26:20 -0700139
Naoki Shiota590c18d2014-03-31 17:52:59 -0700140############# Common functions #############
141function print_usage {
142 local filename=`basename ${ONOS_CONF}`
143 local usage="Usage: setup/start/stop ONOS on this server.
144 \$ $0 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.
152 \$ $0 start [single-node|coord-node|server-node|coord-and-server-node]
153 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}
159 \$ $0 stop
160 Stop all ONOS-related processes
161 \$ $0 restart
162 Stop and start currently running ONOS-related processes
163 \$$0 status
164 Show status of ONOS-related processes
165 \$ $0 {zk|rc-coord|rc-server|core} {start|stop|restart|status}
166 Control specific ONOS-related process"
167
168 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700169}
170
171function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700172 local logfile=$1
173 local nr_max=${2:-10}
174 if [ -f $logfile ]; then
175 for i in `seq $(expr $nr_max - 1) -1 1`; do
176 if [ -f ${logfile}.${i} ]; then
177 mv -f ${logfile}.${i} ${logfile}.`expr $i + 1`
178 fi
179 done
180 mv $logfile $logfile.1
181 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700182}
183
184# kill-processes {module-name} {array of pids}
185function kill-processes {
186 # Kill the existing processes
187 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700188 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700189 echo -n "Stopping $1 ... "
190 fi
191 for p in ${pids}; do
192 if [ x$p != "x" ]; then
193 kill -KILL $p
194 echo "Killed existing process (pid: $p)"
195 fi
196 done
197}
198
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700199function handle-error {
200 set -e
201
202 revert-confs
203
204 set +e
205
206 exit 1
207}
208
Naoki Shiota4e928512014-04-03 15:49:35 -0700209# revert-confs [error message]
210function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700211 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700212
Naoki Shiota4e928512014-04-03 15:49:35 -0700213 revert-file `basename ${ZK_CONF}`
214 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700215
216 echo "EXIT"
217
218 if [ ! -z "$1" ]; then
219 echo $1
220 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700221}
222
Naoki Shiota590c18d2014-03-31 17:52:59 -0700223function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700224 echo -n "Creating ${ZK_CONF} ... "
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700225
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700226 # Create the ZooKeeper lib directory
227 if [ ! -d ${ZK_LIB_DIR} ]; then
228 local SUDO=${SUDO:-}
229 local whoami=`whoami`
230 {
231 ${SUDO} mkdir ${ZK_LIB_DIR}
232 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
233 } || {
234 echo "FAILED"
235 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
236 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
237 exit 1
238 }
239 fi
240
241 # creation of ZooKeeper config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700242 local temp_zk=`begin-conf-creation ${ZK_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700243
Naoki Shiota4e928512014-04-03 15:49:35 -0700244 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700245
246 local i=1
247 local myid=
248 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700249 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700250 myid=$i
251 break
252 fi
253 i=`expr $i + 1`
254 done
255
256 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700257 local filename=`basename ${ONOS_CONF}`
Naoki Shiota35f8d5c2014-04-08 11:13:18 -0700258 revert-confs "[ERROR] In ${filename}, zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700259 fi
260
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700261 if [ -f "${ZK_MY_ID}" ]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700262 # sudo will be needed if ZK_MY_ID is already created by other (old) script
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700263 local SUDO=${SUDO:-}
264 {
265 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
266 } || {
267 echo "FAILED"
268 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700269 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700270 exit 1
271 }
272 fi
273
Naoki Shiota9df15d32014-03-27 14:26:20 -0700274 echo ${myid} > ${ZK_MY_ID}
275
276 echo -n "myid is assigned to ${myid} ... "
277
278 while read line; do
279 if [[ $line =~ ^__HOSTS__$ ]]; then
280 i=1
281 for host in ${hostarr}; do
282 # TODO: ports might be configurable
283 local hostline="server.${i}=${host}:2888:3888"
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700284 echo $hostline
Naoki Shiota9df15d32014-03-27 14:26:20 -0700285 i=`expr $i + 1`
286 done
287 elif [[ $line =~ __DATADIR__ ]]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700288 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700289 else
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700290 echo $line
Naoki Shiota9df15d32014-03-27 14:26:20 -0700291 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700292 done < ${ZK_CONF_TEMPLATE} > ${temp_zk}
293
294 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700295
296 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700297}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700298
Naoki Shiota4e928512014-04-03 15:49:35 -0700299function create-hazelcast-conf {
300 echo -n "Creating ${HC_CONF} ... "
301
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700302 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700303
304 # To keep indent of XML file, change IFS
305 local IFS=''
306 while read line; do
307 if [[ $line =~ __HC_NETWORK__ ]]; then
308 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
309 # temporary change
310 IFS=' '
311 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
312 echo '<multicast enabled="false" />'
313 echo '<tcp-ip enabled="true">'
314 for member in ${memberarr}; do
315 echo " <member>${member}</member>"
316 done
317 echo '</tcp-ip>'
318 IFS=''
319 else
320 echo '<multicast enabled="true">'
321 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
322 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
323 echo '</multicast>'
324 echo '<tcp-ip enabled="false" />'
325 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700326 elif [[ $line =~ __HC_PORT__ ]]; then
327 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700328 else
329 echo "${line}"
330 fi
331 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
332
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700333 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700334
335 echo "DONE"
336}
337
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700338function create-ramcloud-conf {
339 echo -n "Creating ${RAMCLOUD_CONF} ... "
340
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700341 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
342
343 echo "ramcloud.coordinatorIp=${RC_COORD_PROTOCOL}:host=${RC_COORD_IP}" > ${temp_rc}
344 echo "ramcloud.coordinatorPort=port=${RC_COORD_PORT}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700345
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700346 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700347
348 echo "DONE"
349}
350
Naoki Shiota590c18d2014-03-31 17:52:59 -0700351function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700352 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700353
Naoki Shiota9df15d32014-03-27 14:26:20 -0700354 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700355 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
356
357 sed -e "s|__FILENAME__|${ONOS_LOG}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
358
359 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700360
Naoki Shiota9df15d32014-03-27 14:26:20 -0700361 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700362}
363
Naoki Shiota4e928512014-04-03 15:49:35 -0700364function create-confs {
365 local key
366 local filename
367
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700368 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700369
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700370 echo "Config file : ${ONOS_CONF}"
371
Naoki Shiota4e928512014-04-03 15:49:35 -0700372 if [ "$1" == "-f" ]; then
373 create-zk-conf
374 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700375 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700376 create-logback-conf
377 else
378 create-conf-interactive ${ZK_CONF} create-zk-conf
379 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700380 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700381 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
382 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700383
384 trap - ERR
385}
386############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700387
Naoki Shiota590c18d2014-03-31 17:52:59 -0700388
389###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700390function zk {
391 case "$1" in
392 start)
393 start-zk
394 ;;
395 stop)
396 stop-zk
397 ;;
398 stat*) # <- status
399 status-zk
400 ;;
401 re*) # <- restart
402 stop-zk
403 start-zk
404 ;;
405 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700406 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700407 exit 1
408 esac
409}
410
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700411function load-zk-cfg {
412 if [ -f "${ZK_CONF}" ]; then
413 local filename=`basename ${ZK_CONF}`
414 local dirname=`dirname ${ZK_CONF}`
415
416 # Run ZooKeeper with our configuration
417 export ZOOCFG=${filename}
418 export ZOOCFGDIR=${dirname}
419 fi
420}
421
Naoki Shiota4e463182014-03-21 15:13:24 -0700422function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700423 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700424
Naoki Shiota4e928512014-04-03 15:49:35 -0700425 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700426 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700427
428 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700429
Naoki Shiota72209722014-04-08 14:32:17 -0700430 ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700431}
432
433function stop-zk {
434 kill-processes "ZooKeeper" `jps -l | grep org.apache.zookeeper.server | awk '{print $1}'`
435}
436
437function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700438 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700439
Naoki Shiota72209722014-04-08 14:32:17 -0700440 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700441}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700442############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700443
444
Naoki Shiota590c18d2014-03-31 17:52:59 -0700445####### Functions related to RAMCloud ######
Naoki Shiota9df15d32014-03-27 14:26:20 -0700446function start-backend {
447 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700448 if [ $1 == "coord" ]; then
449 rc-coord startifdown
450 elif [ $1 == "server" ]; then
451 rc-server startifdown
452 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700453 fi
454}
455
456function stop-backend {
457 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
458 if [ $rcsn != 0 ]; then
459 rc-server stop
460 fi
461
Naoki Shiota590c18d2014-03-31 17:52:59 -0700462 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700463 if [ $rccn != 0 ]; then
464 rc-coord stop
465 fi
466}
467
Naoki Shiota590c18d2014-03-31 17:52:59 -0700468function deldb {
469# TODO: implement
470 return
471}
472
473
Naoki Shiota4e463182014-03-21 15:13:24 -0700474### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700475function rc-coord-addr {
Naoki Shiota4e928512014-04-03 15:49:35 -0700476 echo "${RC_COORD_PROTOCOL}:host=${RC_COORD_IP},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700477}
478
479function rc-server-addr {
Naoki Shiota4e928512014-04-03 15:49:35 -0700480 echo "${RC_SERVER_PROTOCOL}:host=${RC_SERVER_IP},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700481}
482
Naoki Shiota4e463182014-03-21 15:13:24 -0700483function rc-coord {
484 case "$1" in
485 start)
486 deldb
Naoki Shiota4e463182014-03-21 15:13:24 -0700487 stop-coord
488 start-coord
489 ;;
490 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700491 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700492 if [ $n == 0 ]; then
493 start-coord
494 else
495 echo "$n instance of RAMCloud coordinator running"
496 fi
497 ;;
498 stop)
499 stop-coord
500 ;;
501 stat*) # <- status
502 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
503 echo "$n RAMCloud coordinator running"
504 ;;
505 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700506 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700507 exit 1
508 esac
509}
510
511function start-coord {
512 if [ ! -d ${LOGDIR} ]; then
513 mkdir -p ${LOGDIR}
514 fi
515 if [ -f $RAMCLOUD_COORD_LOG ]; then
516 rotate-log $RAMCLOUD_COORD_LOG
517 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700518
519 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700520
521 # Run ramcloud
522 echo -n "Starting RAMCloud coordinator ... "
Naoki Shiota72209722014-04-08 14:32:17 -0700523 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator -L ${coord_addr} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700524 echo "STARTED"
525}
526
527
528function stop-coord {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700529 kill-processes "RAMCloud coordinator" `pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700530}
531
Naoki Shiota4e463182014-03-21 15:13:24 -0700532### Functions related to RAMCloud server
533function rc-server {
534 case "$1" in
535 start)
536 deldb
Naoki Shiota4e463182014-03-21 15:13:24 -0700537 stop-server
538 start-server
539 ;;
540 startifdown)
541 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
542 if [ $n == 0 ]; then
543 start-server
544 else
545 echo "$n instance of RAMCloud server running"
546 fi
547 ;;
548 stop)
549 stop-server
550 ;;
551# deldb)
552# deldb
553# ;;
554 stat*) # <- status
555 n=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
556 echo "$n RAMCloud server running"
557 ;;
558 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700559 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700560 exit 1
561 esac
562}
563
564function start-server {
565 if [ ! -d ${LOGDIR} ]; then
566 mkdir -p ${LOGDIR}
567 fi
568 if [ -f $RAMCLOUD_SERVER_LOG ]; then
569 rotate-log $RAMCLOUD_SERVER_LOG
570 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700571
572 local coord_addr=`rc-coord-addr`
573 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700574
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700575 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.masterServiceThreads 5)
576 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.logCleanerThreads 1)
577 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.detectFailures 0)
578
Naoki Shiota4e463182014-03-21 15:13:24 -0700579 # Run ramcloud
580 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700581 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server -M -L ${server_addr} -C ${coord_addr} --masterServiceThreads ${masterServiceThreads} --logCleanerThreads ${logCleanerThreads} --detectFailures ${detectFailures} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700582 echo "STARTED"
583}
584
585function stop-server {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700586 kill-processes "RAMCloud server" `pgrep -f obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700587}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700588############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700589
590
Naoki Shiota590c18d2014-03-31 17:52:59 -0700591## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700592function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700593 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700594 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700595 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700596 ${MVN} -f ${ONOS_HOME}/pom.xml compile
597 fi
598 JAVA_CP=`cat ${CPFILE}`
599 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
600
601 case "$1" in
602 start)
603 stop-onos
604 start-onos
605 ;;
606 startnokill)
607 start-onos
608 ;;
609 startifdown)
610 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
611 if [ $n == 0 ]; then
612 start-onos
613 else
614 echo "$n instance of onos running"
615 fi
616 ;;
617 stop)
618 stop-onos
619 ;;
620 stat*) # <- status
621 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
622 echo "$n instance of onos running"
623 ;;
624 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700625 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700626 exit 1
627 esac
628}
629
630function start-onos {
631 if [ ! -d ${LOGDIR} ]; then
632 mkdir -p ${LOGDIR}
633 fi
634 # Backup log files
635 for log in ${LOGS}; do
636 if [ -f ${log} ]; then
637 rotate-log ${log}
638 fi
639 done
Naoki Shiota9df15d32014-03-27 14:26:20 -0700640
Naoki Shiota4e463182014-03-21 15:13:24 -0700641 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700642 echo "[WARNING] ${ONOS_LOGBACK} not found."
643 echo " Run \"\$ $0 setup\" to create."
644 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700645 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700646
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700647 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700648 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
649 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
650 fi
651
Naoki Shiota9df15d32014-03-27 14:26:20 -0700652 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -0700653
654 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
655 cd ${ONOS_HOME}
656
Naoki Shiota9df15d32014-03-27 14:26:20 -0700657 echo -n "Starting ONOS controller ..."
Naoki Shiota4e463182014-03-21 15:13:24 -0700658 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
659
660 # We need to wait a bit to find out whether starting the ONOS process succeeded
661 sleep 1
662
663 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700664 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700665 echo " STARTED"
666 else
667 echo " FAILED"
668 fi
669
670# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
671# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
672}
673
674function stop-onos {
675 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
676# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
677}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700678############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700679
680
Naoki Shiota590c18d2014-03-31 17:52:59 -0700681################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -0700682case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -0700683 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -0700684 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700685 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700686 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -0700687 mode_parameter=${ONOS_HOST_ROLE}
688 if [ ! -z "$2" ]; then
689 mode_parameter=$2
690 fi
691
692 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -0700693 single-node)
694 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700695 start-backend coord
696 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700697 onos startifdown
698 ;;
699 coord-node)
700 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700701 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -0700702 onos startifdown
703 ;;
704 server-node)
705 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700706 start-backend server
707 onos startifdown
708 ;;
709 coord-and-server-node)
710 zk start
711 start-backend coord
712 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700713 onos startifdown
714 ;;
715 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700716 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700717 ;;
718 esac
719 echo
720 ;;
721 stop)
722 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
723 if [ $on != 0 ]; then
724 onos stop
725 fi
726
Naoki Shiota9df15d32014-03-27 14:26:20 -0700727 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -0700728
729 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
730 if [ $zkn != 0 ]; then
731 zk stop
732 fi
733 echo
734 ;;
735 restart)
736 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
737 if [ $on != 0 ]; then
738 onos stop
739 fi
740
741 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
742 if [ $rcsn != 0 ]; then
743 rc-server stop
744 fi
745
Naoki Shiota590c18d2014-03-31 17:52:59 -0700746 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700747 if [ $rccn != 0 ]; then
748 rc-coord stop
749 fi
750
751 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
752 if [ $zkn != 0 ]; then
753 zk restart
754 fi
755
756 if [ $rccn != 0 ]; then
757 rc-coord startifdown
758 fi
759
760 if [ $rcsn != 0 ]; then
761 rc-server startifdown
762 fi
763
764 if [ $on != 0 ]; then
765 onos startifdown
766 fi
767 echo
768 ;;
769 stat*) # <- status
770 echo '[ZooKeeper]'
771 zk status
772 echo
773 echo '[RAMCloud coordinator]'
774 rc-coord status
775 echo
776 echo '[RAMCloud server]'
777 rc-server status
778 echo
779 echo '[ONOS core]'
780 onos status
781 echo
782 ;;
783 zk)
784 zk $2
785 ;;
786 rc-c*) # <- rc-coordinator
787 rc-coord $2
788 ;;
789 rc-s*) # <- rc-server
790 rc-server $2
791 ;;
792 core)
793 onos $2
794 ;;
795 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700796 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700797 exit 1
798esac
Naoki Shiota590c18d2014-03-31 17:52:59 -0700799