blob: dbf2739fa712c77d113d73a3cd163a06acda84d5 [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)
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)
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 Shiota590c18d2014-03-31 17:52:59 -070015# $JVM_OPTS : JVM options ONOS starts with
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070016# $ZK_CONF : path of ZooKeeper config file (~/ONOS/conf/zoo.cfg)
17# $HC_CONF : path of Hazelcast config file (~/ONOS/conf/hazelcast.xml)
18# $RAMCLOUD_CONF : path of RAMCloud config file (~/ONOS/conf/ramcloud.conf)
Naoki Shiota590c18d2014-03-31 17:52:59 -070019#####################################################
20
Naoki Shiota590c18d2014-03-31 17:52:59 -070021ONOS_HOME=${ONOS_HOME:-$(cd `dirname $0`; pwd)}
Naoki Shiota9df15d32014-03-27 14:26:20 -070022ONOS_CONF_DIR=${ONOS_CONF_DIR:-${ONOS_HOME}/conf}
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070023ONOS_CONF=${ONOS_CONF:-${ONOS_CONF_DIR}/onos_node.`hostname`.conf}
Naoki Shiota590c18d2014-03-31 17:52:59 -070024
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070025source ${ONOS_HOME}/scripts/common/utils.sh
26
Naoki Shiota590c18d2014-03-31 17:52:59 -070027if [ ! -f ${ONOS_CONF} ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070028 # falling back to default config file
29 ONOS_CONF=${ONOS_CONF_DIR}/onos_node.conf
30 if [ ! -f ${ONOS_CONF} ]; then
31 echo "${ONOS_CONF} not found."
32 exit 1
33 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -070034fi
Naoki Shiota4e928512014-04-03 15:49:35 -070035
36
37### Variables read from ONOS config file ###
38ONOS_HOST_NAME=$(read-conf ${ONOS_CONF} host.name `hostname`)
39ONOS_HOST_IP=$(read-conf ${ONOS_CONF} host.ip)
40ONOS_HOST_ROLE=$(read-conf ${ONOS_CONF} host.role)
41ONOS_HOST_BACKEND=$(read-conf ${ONOS_CONF} host.backend)
42ZK_HOSTS=$(read-conf ${ONOS_CONF} zookeeper.hosts ${ONOS_HOST_NAME})
43RC_COORD_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.coordinator.protocol "fast+udp")
44RC_COORD_IP=$(read-conf ${ONOS_CONF} ramcloud.coordinator.ip ${ONOS_HOST_IP})
45RC_COORD_PORT=$(read-conf ${ONOS_CONF} ramcloud.coordinator.port 12246)
46RC_SERVER_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.server.protocol "fast+udp")
47RC_SERVER_IP=$(read-conf ${ONOS_CONF} ramcloud.server.ip ${ONOS_HOST_IP})
48RC_SERVER_PORT=$(read-conf ${ONOS_CONF} ramcloud.server.port 12242)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070049HC_HOST_PORT=$(read-conf ${ONOS_CONF} hazelcast.host.port 5701)
Naoki Shiota4e928512014-04-03 15:49:35 -070050HC_TCPIP_MEMBERS=$(read-conf ${ONOS_CONF} hazelcast.tcp-ip.members)
51HC_MULTICAST_GROUP=$(read-conf ${ONOS_CONF} hazelcast.multicast.group "224.2.2.3")
52HC_MULTICAST_PORT=$(read-conf ${ONOS_CONF} hazelcast.multicast.port 54327)
Naoki Shiota590c18d2014-03-31 17:52:59 -070053############################################
54
55
56############## Other variables #############
Naoki Shiota4e928512014-04-03 15:49:35 -070057ONOS_TEMPLATE_DIR=${ONOS_CONF_DIR}/template
58
Naoki Shiota4e463182014-03-21 15:13:24 -070059LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
60
Naoki Shiota72209722014-04-08 14:32:17 -070061ZK_HOME=${ZK_HOME:-~/zookeeper-3.4.5}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070062ZK_CONF=${ZK_CONF:-${ONOS_CONF_DIR}/zoo.cfg}
Naoki Shiota4e928512014-04-03 15:49:35 -070063ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070064# Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
65ZK_LOG_DIR=${ONOS_HOME}/onos-logs/${ONOS_HOST_NAME}
Naoki Shiota72209722014-04-08 14:32:17 -070066ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070067ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070068
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070069HC_CONF=${HC_CONF:-${ONOS_CONF_DIR}/hazelcast.xml}
Naoki Shiota4e928512014-04-03 15:49:35 -070070HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
71
Naoki Shiota4e463182014-03-21 15:13:24 -070072RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070073RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
74RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070075RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070076RAMCLOUD_CONF=${RAMCLOUD_CONF:-${ONOS_CONF_DIR}/ramcloud.conf}
Naoki Shiota4e463182014-03-21 15:13:24 -070077
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070078export LD_LIBRARY_PATH=${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}:$LD_LIBRARY_PATH
Naoki Shiota4e463182014-03-21 15:13:24 -070079
80## 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 -070081ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070082ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070083ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070084LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070085LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070086ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
87PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
88LOGS="$ONOS_LOG $PCAP_LOG"
89
Naoki Shiota9df15d32014-03-27 14:26:20 -070090ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -070091JMX_PORT=${JMX_PORT:-7189}
92
93# Set JVM options
94JVM_OPTS="${JVM_OPTS:-}"
Naoki Shiota4e463182014-03-21 15:13:24 -070095JVM_OPTS="$JVM_OPTS -server -d64"
96#JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -070097
98# Uncomment or specify appropriate value as JVM_OPTS environment variables.
99#JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
Naoki Shiota4e463182014-03-21 15:13:24 -0700100#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
101#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700102
103#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC"
104JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
105JVM_OPTS="$JVM_OPTS -XX:+AggressiveOpts"
106
107# We may want to remove UseFastAccessorMethods option: http://bugs.java.com/view_bug.do?bug_id=6385687
108JVM_OPTS="$JVM_OPTS -XX:+UseFastAccessorMethods"
109
110JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192"
111JVM_OPTS="$JVM_OPTS -XX:FreqInlineSize=8192"
112JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500"
113
Naoki Shiota4e463182014-03-21 15:13:24 -0700114JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700115
116# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
117JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
118
119JVM_OPTS="$JVM_OPTS -XX:+UseCompressedOops"
120
121JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
122JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
123JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
124
Naoki Shiota4e463182014-03-21 15:13:24 -0700125JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
126
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700127# Uncomment to dump final JVM flags to stdout
128#JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
129
Naoki Shiota4e463182014-03-21 15:13:24 -0700130# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700131MAIN_CLASS="net.onrc.onos.core.main.Main"
Naoki Shiota4e463182014-03-21 15:13:24 -0700132
133MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700134############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700135
Naoki Shiota9df15d32014-03-27 14:26:20 -0700136
Naoki Shiota590c18d2014-03-31 17:52:59 -0700137############# Common functions #############
138function print_usage {
139 local filename=`basename ${ONOS_CONF}`
140 local usage="Usage: setup/start/stop ONOS on this server.
141 \$ $0 setup [-f]
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700142 Set up ONOS node using ${ONOS_CONF} .
Naoki Shiota590c18d2014-03-31 17:52:59 -0700143 - generate and replace config file of ZooKeeper.
144 - create myid in ZooKeeper datadir.
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700145 - generate and replace config file for Hazelcast.
146 - generate and replace config file for RAMCloud.
Naoki Shiota590c18d2014-03-31 17:52:59 -0700147 - generate and replace logback.${ONOS_HOST_NAME}.xml
148 If -f option is used, all existing files will be overwritten without confirmation.
149 \$ $0 start [single-node|coord-node|server-node|coord-and-server-node]
150 Start ONOS node with specific RAMCloud entities
151 - single-node: start ONOS with stand-alone RAMCloud
152 - coord-node : start ONOS with RAMCloud coordinator
153 - server-node: start ONOS with RAMCloud server
154 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
155 * Default behavior can be defined by ${filename}
156 \$ $0 stop
157 Stop all ONOS-related processes
158 \$ $0 restart
159 Stop and start currently running ONOS-related processes
160 \$$0 status
161 Show status of ONOS-related processes
162 \$ $0 {zk|rc-coord|rc-server|core} {start|stop|restart|status}
163 Control specific ONOS-related process"
164
165 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700166}
167
168function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700169 local logfile=$1
170 local nr_max=${2:-10}
171 if [ -f $logfile ]; then
172 for i in `seq $(expr $nr_max - 1) -1 1`; do
173 if [ -f ${logfile}.${i} ]; then
174 mv -f ${logfile}.${i} ${logfile}.`expr $i + 1`
175 fi
176 done
177 mv $logfile $logfile.1
178 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700179}
180
181# kill-processes {module-name} {array of pids}
182function kill-processes {
183 # Kill the existing processes
184 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700185 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700186 echo -n "Stopping $1 ... "
187 fi
188 for p in ${pids}; do
189 if [ x$p != "x" ]; then
190 kill -KILL $p
191 echo "Killed existing process (pid: $p)"
192 fi
193 done
194}
195
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700196function handle-error {
197 set -e
198
199 revert-confs
200
201 set +e
202
203 exit 1
204}
205
Naoki Shiota4e928512014-04-03 15:49:35 -0700206# revert-confs [error message]
207function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700208 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700209
Naoki Shiota4e928512014-04-03 15:49:35 -0700210 revert-file `basename ${ZK_CONF}`
211 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700212
213 echo "EXIT"
214
215 if [ ! -z "$1" ]; then
216 echo $1
217 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700218}
219
Naoki Shiota590c18d2014-03-31 17:52:59 -0700220function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700221 echo -n "Creating ${ZK_CONF} ... "
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700222
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700223 # Create the ZooKeeper lib directory
224 if [ ! -d ${ZK_LIB_DIR} ]; then
225 local SUDO=${SUDO:-}
226 local whoami=`whoami`
227 {
228 ${SUDO} mkdir ${ZK_LIB_DIR}
229 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
230 } || {
231 echo "FAILED"
232 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
233 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
234 exit 1
235 }
236 fi
237
238 # creation of ZooKeeper config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700239 local temp_zk=`begin-conf-creation ${ZK_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700240
Naoki Shiota4e928512014-04-03 15:49:35 -0700241 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700242
243 local i=1
244 local myid=
245 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700246 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700247 myid=$i
248 break
249 fi
250 i=`expr $i + 1`
251 done
252
253 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700254 local filename=`basename ${ONOS_CONF}`
Naoki Shiota35f8d5c2014-04-08 11:13:18 -0700255 revert-confs "[ERROR] In ${filename}, zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700256 fi
257
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700258 if [ -f "${ZK_MY_ID}" ]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700259 # sudo will be needed if ZK_MY_ID is already created by other (old) script
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700260 local SUDO=${SUDO:-}
261 {
262 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
263 } || {
264 echo "FAILED"
265 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700266 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700267 exit 1
268 }
269 fi
270
Naoki Shiota9df15d32014-03-27 14:26:20 -0700271 echo ${myid} > ${ZK_MY_ID}
272
273 echo -n "myid is assigned to ${myid} ... "
274
275 while read line; do
276 if [[ $line =~ ^__HOSTS__$ ]]; then
277 i=1
278 for host in ${hostarr}; do
279 # TODO: ports might be configurable
280 local hostline="server.${i}=${host}:2888:3888"
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700281 echo $hostline
Naoki Shiota9df15d32014-03-27 14:26:20 -0700282 i=`expr $i + 1`
283 done
284 elif [[ $line =~ __DATADIR__ ]]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700285 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700286 else
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700287 echo $line
Naoki Shiota9df15d32014-03-27 14:26:20 -0700288 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700289 done < ${ZK_CONF_TEMPLATE} > ${temp_zk}
290
291 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700292
293 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700294}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700295
Naoki Shiota4e928512014-04-03 15:49:35 -0700296function create-hazelcast-conf {
297 echo -n "Creating ${HC_CONF} ... "
298
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700299 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700300
301 # To keep indent of XML file, change IFS
302 local IFS=''
303 while read line; do
304 if [[ $line =~ __HC_NETWORK__ ]]; then
305 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
306 # temporary change
307 IFS=' '
308 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
309 echo '<multicast enabled="false" />'
310 echo '<tcp-ip enabled="true">'
311 for member in ${memberarr}; do
312 echo " <member>${member}</member>"
313 done
314 echo '</tcp-ip>'
315 IFS=''
316 else
317 echo '<multicast enabled="true">'
318 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
319 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
320 echo '</multicast>'
321 echo '<tcp-ip enabled="false" />'
322 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700323 elif [[ $line =~ __HC_PORT__ ]]; then
324 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700325 else
326 echo "${line}"
327 fi
328 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
329
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700330 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700331
332 echo "DONE"
333}
334
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700335function create-ramcloud-conf {
336 echo -n "Creating ${RAMCLOUD_CONF} ... "
337
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700338 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
339
340 echo "ramcloud.coordinatorIp=${RC_COORD_PROTOCOL}:host=${RC_COORD_IP}" > ${temp_rc}
341 echo "ramcloud.coordinatorPort=port=${RC_COORD_PORT}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700342
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700343 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700344
345 echo "DONE"
346}
347
Naoki Shiota590c18d2014-03-31 17:52:59 -0700348function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700349 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700350
Naoki Shiota9df15d32014-03-27 14:26:20 -0700351 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700352 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
353
354 sed -e "s|__FILENAME__|${ONOS_LOG}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
355
356 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700357
Naoki Shiota9df15d32014-03-27 14:26:20 -0700358 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700359}
360
Naoki Shiota4e928512014-04-03 15:49:35 -0700361function create-confs {
362 local key
363 local filename
364
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700365 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700366
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700367 echo "Config file : ${ONOS_CONF}"
368
Naoki Shiota4e928512014-04-03 15:49:35 -0700369 if [ "$1" == "-f" ]; then
370 create-zk-conf
371 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700372 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700373 create-logback-conf
374 else
375 create-conf-interactive ${ZK_CONF} create-zk-conf
376 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700377 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700378 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
379 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700380
381 trap - ERR
382}
383############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700384
Naoki Shiota590c18d2014-03-31 17:52:59 -0700385
386###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700387function zk {
388 case "$1" in
389 start)
390 start-zk
391 ;;
392 stop)
393 stop-zk
394 ;;
395 stat*) # <- status
396 status-zk
397 ;;
398 re*) # <- restart
399 stop-zk
400 start-zk
401 ;;
402 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700403 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700404 exit 1
405 esac
406}
407
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700408function load-zk-cfg {
409 if [ -f "${ZK_CONF}" ]; then
410 local filename=`basename ${ZK_CONF}`
411 local dirname=`dirname ${ZK_CONF}`
412
413 # Run ZooKeeper with our configuration
414 export ZOOCFG=${filename}
415 export ZOOCFGDIR=${dirname}
416 fi
417}
418
Naoki Shiota4e463182014-03-21 15:13:24 -0700419function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700420 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700421
Naoki Shiota4e928512014-04-03 15:49:35 -0700422 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700423 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700424
425 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700426
Naoki Shiota72209722014-04-08 14:32:17 -0700427 ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700428}
429
430function stop-zk {
431 kill-processes "ZooKeeper" `jps -l | grep org.apache.zookeeper.server | awk '{print $1}'`
432}
433
434function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700435 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700436
Naoki Shiota72209722014-04-08 14:32:17 -0700437 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700438}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700439############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700440
441
Naoki Shiota590c18d2014-03-31 17:52:59 -0700442####### Functions related to RAMCloud ######
Naoki Shiota9df15d32014-03-27 14:26:20 -0700443function start-backend {
444 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700445 if [ $1 == "coord" ]; then
446 rc-coord startifdown
447 elif [ $1 == "server" ]; then
448 rc-server startifdown
449 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700450 fi
451}
452
453function stop-backend {
454 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
455 if [ $rcsn != 0 ]; then
456 rc-server stop
457 fi
458
Naoki Shiota590c18d2014-03-31 17:52:59 -0700459 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700460 if [ $rccn != 0 ]; then
461 rc-coord stop
462 fi
463}
464
Naoki Shiota590c18d2014-03-31 17:52:59 -0700465function deldb {
466# TODO: implement
467 return
468}
469
470
Naoki Shiota4e463182014-03-21 15:13:24 -0700471### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700472function rc-coord-addr {
Naoki Shiota4e928512014-04-03 15:49:35 -0700473 echo "${RC_COORD_PROTOCOL}:host=${RC_COORD_IP},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700474}
475
476function rc-server-addr {
Naoki Shiota4e928512014-04-03 15:49:35 -0700477 echo "${RC_SERVER_PROTOCOL}:host=${RC_SERVER_IP},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700478}
479
Naoki Shiota4e463182014-03-21 15:13:24 -0700480function rc-coord {
481 case "$1" in
482 start)
483 deldb
Naoki Shiota4e463182014-03-21 15:13:24 -0700484 stop-coord
485 start-coord
486 ;;
487 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700488 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700489 if [ $n == 0 ]; then
490 start-coord
491 else
492 echo "$n instance of RAMCloud coordinator running"
493 fi
494 ;;
495 stop)
496 stop-coord
497 ;;
498 stat*) # <- status
499 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
500 echo "$n RAMCloud coordinator running"
501 ;;
502 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700503 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700504 exit 1
505 esac
506}
507
508function start-coord {
509 if [ ! -d ${LOGDIR} ]; then
510 mkdir -p ${LOGDIR}
511 fi
512 if [ -f $RAMCLOUD_COORD_LOG ]; then
513 rotate-log $RAMCLOUD_COORD_LOG
514 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700515
516 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700517
518 # Run ramcloud
519 echo -n "Starting RAMCloud coordinator ... "
Naoki Shiota72209722014-04-08 14:32:17 -0700520 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator -L ${coord_addr} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700521 echo "STARTED"
522}
523
524
525function stop-coord {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700526 kill-processes "RAMCloud coordinator" `pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700527}
528
Naoki Shiota4e463182014-03-21 15:13:24 -0700529### Functions related to RAMCloud server
530function rc-server {
531 case "$1" in
532 start)
533 deldb
Naoki Shiota4e463182014-03-21 15:13:24 -0700534 stop-server
535 start-server
536 ;;
537 startifdown)
538 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
539 if [ $n == 0 ]; then
540 start-server
541 else
542 echo "$n instance of RAMCloud server running"
543 fi
544 ;;
545 stop)
546 stop-server
547 ;;
548# deldb)
549# deldb
550# ;;
551 stat*) # <- status
552 n=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
553 echo "$n RAMCloud server running"
554 ;;
555 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700556 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700557 exit 1
558 esac
559}
560
561function start-server {
562 if [ ! -d ${LOGDIR} ]; then
563 mkdir -p ${LOGDIR}
564 fi
565 if [ -f $RAMCLOUD_SERVER_LOG ]; then
566 rotate-log $RAMCLOUD_SERVER_LOG
567 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700568
569 local coord_addr=`rc-coord-addr`
570 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700571
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700572 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.masterServiceThreads 5)
573 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.logCleanerThreads 1)
574 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.detectFailures 0)
575
Naoki Shiota4e463182014-03-21 15:13:24 -0700576 # Run ramcloud
577 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700578 ${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 -0700579 echo "STARTED"
580}
581
582function stop-server {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700583 kill-processes "RAMCloud server" `pgrep -f obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700584}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700585############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700586
587
Naoki Shiota590c18d2014-03-31 17:52:59 -0700588## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700589function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700590 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700591 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700592 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700593 ${MVN} -f ${ONOS_HOME}/pom.xml compile
594 fi
595 JAVA_CP=`cat ${CPFILE}`
596 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
597
598 case "$1" in
599 start)
600 stop-onos
601 start-onos
602 ;;
603 startnokill)
604 start-onos
605 ;;
606 startifdown)
607 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
608 if [ $n == 0 ]; then
609 start-onos
610 else
611 echo "$n instance of onos running"
612 fi
613 ;;
614 stop)
615 stop-onos
616 ;;
617 stat*) # <- status
618 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
619 echo "$n instance of onos running"
620 ;;
621 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700622 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700623 exit 1
624 esac
625}
626
627function start-onos {
628 if [ ! -d ${LOGDIR} ]; then
629 mkdir -p ${LOGDIR}
630 fi
631 # Backup log files
632 for log in ${LOGS}; do
633 if [ -f ${log} ]; then
634 rotate-log ${log}
635 fi
636 done
Naoki Shiota9df15d32014-03-27 14:26:20 -0700637
Naoki Shiota4e463182014-03-21 15:13:24 -0700638 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700639 echo "[WARNING] ${ONOS_LOGBACK} not found."
640 echo " Run \"\$ $0 setup\" to create."
641 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700642 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700643
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700644 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700645 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
646 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
647 fi
648
Naoki Shiota9df15d32014-03-27 14:26:20 -0700649 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -0700650
651 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
652 cd ${ONOS_HOME}
653
Naoki Shiota9df15d32014-03-27 14:26:20 -0700654 echo -n "Starting ONOS controller ..."
Naoki Shiota4e463182014-03-21 15:13:24 -0700655 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
656
657 # We need to wait a bit to find out whether starting the ONOS process succeeded
658 sleep 1
659
660 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700661 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700662 echo " STARTED"
663 else
664 echo " FAILED"
665 fi
666
667# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
668# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
669}
670
671function stop-onos {
672 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
673# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
674}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700675############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700676
677
Naoki Shiota590c18d2014-03-31 17:52:59 -0700678################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -0700679case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -0700680 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -0700681 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700682 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700683 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -0700684 mode_parameter=${ONOS_HOST_ROLE}
685 if [ ! -z "$2" ]; then
686 mode_parameter=$2
687 fi
688
689 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -0700690 single-node)
691 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700692 start-backend coord
693 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700694 onos startifdown
695 ;;
696 coord-node)
697 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700698 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -0700699 onos startifdown
700 ;;
701 server-node)
702 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700703 start-backend server
704 onos startifdown
705 ;;
706 coord-and-server-node)
707 zk start
708 start-backend coord
709 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700710 onos startifdown
711 ;;
712 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700713 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700714 ;;
715 esac
716 echo
717 ;;
718 stop)
719 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
720 if [ $on != 0 ]; then
721 onos stop
722 fi
723
Naoki Shiota9df15d32014-03-27 14:26:20 -0700724 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -0700725
726 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
727 if [ $zkn != 0 ]; then
728 zk stop
729 fi
730 echo
731 ;;
732 restart)
733 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
734 if [ $on != 0 ]; then
735 onos stop
736 fi
737
738 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
739 if [ $rcsn != 0 ]; then
740 rc-server stop
741 fi
742
Naoki Shiota590c18d2014-03-31 17:52:59 -0700743 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700744 if [ $rccn != 0 ]; then
745 rc-coord stop
746 fi
747
748 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
749 if [ $zkn != 0 ]; then
750 zk restart
751 fi
752
753 if [ $rccn != 0 ]; then
754 rc-coord startifdown
755 fi
756
757 if [ $rcsn != 0 ]; then
758 rc-server startifdown
759 fi
760
761 if [ $on != 0 ]; then
762 onos startifdown
763 fi
764 echo
765 ;;
766 stat*) # <- status
767 echo '[ZooKeeper]'
768 zk status
769 echo
770 echo '[RAMCloud coordinator]'
771 rc-coord status
772 echo
773 echo '[RAMCloud server]'
774 rc-server status
775 echo
776 echo '[ONOS core]'
777 onos status
778 echo
779 ;;
780 zk)
781 zk $2
782 ;;
783 rc-c*) # <- rc-coordinator
784 rc-coord $2
785 ;;
786 rc-s*) # <- rc-server
787 rc-server $2
788 ;;
789 core)
790 onos $2
791 ;;
792 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700793 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700794 exit 1
795esac
Naoki Shiota590c18d2014-03-31 17:52:59 -0700796