blob: 039f806c838db4103b8520ceda185335507c8225 [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
16#####################################################
17
18
Naoki Shiota4e928512014-04-03 15:49:35 -070019# read-conf {filename} {parameter name} [default value]
20function read-conf {
21 local value=`grep ^${2} ${1} | cut -d "=" -f 2 | sed -e 's/^[ \t]*//'`
22 if [ -z "${value}" ]; then
23 echo $3
24 else
25 echo ${value}
26 fi
27}
28
Naoki Shiota590c18d2014-03-31 17:52:59 -070029ONOS_HOME=${ONOS_HOME:-$(cd `dirname $0`; pwd)}
Naoki Shiota9df15d32014-03-27 14:26:20 -070030ONOS_CONF_DIR=${ONOS_CONF_DIR:-${ONOS_HOME}/conf}
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070031ONOS_CONF=${ONOS_CONF:-${ONOS_CONF_DIR}/onos_node.`hostname`.conf}
Naoki Shiota590c18d2014-03-31 17:52:59 -070032
33if [ ! -f ${ONOS_CONF} ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070034 # falling back to default config file
35 ONOS_CONF=${ONOS_CONF_DIR}/onos_node.conf
36 if [ ! -f ${ONOS_CONF} ]; then
37 echo "${ONOS_CONF} not found."
38 exit 1
39 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -070040fi
Naoki Shiota4e928512014-04-03 15:49:35 -070041
42
43### Variables read from ONOS config file ###
44ONOS_HOST_NAME=$(read-conf ${ONOS_CONF} host.name `hostname`)
45ONOS_HOST_IP=$(read-conf ${ONOS_CONF} host.ip)
46ONOS_HOST_ROLE=$(read-conf ${ONOS_CONF} host.role)
47ONOS_HOST_BACKEND=$(read-conf ${ONOS_CONF} host.backend)
48ZK_HOSTS=$(read-conf ${ONOS_CONF} zookeeper.hosts ${ONOS_HOST_NAME})
49RC_COORD_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.coordinator.protocol "fast+udp")
50RC_COORD_IP=$(read-conf ${ONOS_CONF} ramcloud.coordinator.ip ${ONOS_HOST_IP})
51RC_COORD_PORT=$(read-conf ${ONOS_CONF} ramcloud.coordinator.port 12246)
52RC_SERVER_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.server.protocol "fast+udp")
53RC_SERVER_IP=$(read-conf ${ONOS_CONF} ramcloud.server.ip ${ONOS_HOST_IP})
54RC_SERVER_PORT=$(read-conf ${ONOS_CONF} ramcloud.server.port 12242)
55HC_TCPIP_MEMBERS=$(read-conf ${ONOS_CONF} hazelcast.tcp-ip.members)
56HC_MULTICAST_GROUP=$(read-conf ${ONOS_CONF} hazelcast.multicast.group "224.2.2.3")
57HC_MULTICAST_PORT=$(read-conf ${ONOS_CONF} hazelcast.multicast.port 54327)
Naoki Shiota590c18d2014-03-31 17:52:59 -070058############################################
59
60
61############## Other variables #############
Naoki Shiota4e928512014-04-03 15:49:35 -070062ONOS_TEMPLATE_DIR=${ONOS_CONF_DIR}/template
63
Naoki Shiota4e463182014-03-21 15:13:24 -070064LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
65
Naoki Shiota72209722014-04-08 14:32:17 -070066ZK_HOME=${ZK_HOME:-~/zookeeper-3.4.5}
Naoki Shiota9df15d32014-03-27 14:26:20 -070067ZK_CONF_FILE=zoo.cfg
68ZK_CONF=${ONOS_CONF_DIR}/${ZK_CONF_FILE}
Naoki Shiota4e928512014-04-03 15:49:35 -070069ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
70ZK_LOG_DIR=${ONOS_HOME}/onos-logs
Naoki Shiota72209722014-04-08 14:32:17 -070071ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070072ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070073
Naoki Shiota4e928512014-04-03 15:49:35 -070074HC_CONF=${ONOS_CONF_DIR}/hazelcast.xml
75HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
76
Naoki Shiota4e463182014-03-21 15:13:24 -070077RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070078RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
79RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070080RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
81
82export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}
83
84## 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 -070085ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070086ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070087ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070088LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070089LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070090ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
91PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
92LOGS="$ONOS_LOG $PCAP_LOG"
93
Naoki Shiota9df15d32014-03-27 14:26:20 -070094ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -070095JMX_PORT=${JMX_PORT:-7189}
96
97# Set JVM options
98JVM_OPTS="${JVM_OPTS:-}"
Naoki Shiota4e463182014-03-21 15:13:24 -070099JVM_OPTS="$JVM_OPTS -server -d64"
100#JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700101
102# Uncomment or specify appropriate value as JVM_OPTS environment variables.
103#JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
Naoki Shiota4e463182014-03-21 15:13:24 -0700104#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
105#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700106
107#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC"
108JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
109JVM_OPTS="$JVM_OPTS -XX:+AggressiveOpts"
110
111# We may want to remove UseFastAccessorMethods option: http://bugs.java.com/view_bug.do?bug_id=6385687
112JVM_OPTS="$JVM_OPTS -XX:+UseFastAccessorMethods"
113
114JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192"
115JVM_OPTS="$JVM_OPTS -XX:FreqInlineSize=8192"
116JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500"
117
Naoki Shiota4e463182014-03-21 15:13:24 -0700118JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700119
120# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
121JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
122
123JVM_OPTS="$JVM_OPTS -XX:+UseCompressedOops"
124
125JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
126JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
127JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
128
Naoki Shiota4e463182014-03-21 15:13:24 -0700129JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
130
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700131# Uncomment to dump final JVM flags to stdout
132#JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
133
Naoki Shiota4e463182014-03-21 15:13:24 -0700134# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700135MAIN_CLASS="net.onrc.onos.core.main.Main"
Naoki Shiota4e463182014-03-21 15:13:24 -0700136
137MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700138############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700139
Naoki Shiota9df15d32014-03-27 14:26:20 -0700140
Naoki Shiota590c18d2014-03-31 17:52:59 -0700141############# Common functions #############
142function print_usage {
143 local filename=`basename ${ONOS_CONF}`
144 local usage="Usage: setup/start/stop ONOS on this server.
145 \$ $0 setup [-f]
146 Set up ONOS node using ${filename}.
147 - generate and replace config file of ZooKeeper.
148 - create myid in ZooKeeper datadir.
149 - generate and replace logback.${ONOS_HOST_NAME}.xml
150 If -f option is used, all existing files will be overwritten without confirmation.
151 \$ $0 start [single-node|coord-node|server-node|coord-and-server-node]
152 Start ONOS node with specific RAMCloud entities
153 - single-node: start ONOS with stand-alone RAMCloud
154 - coord-node : start ONOS with RAMCloud coordinator
155 - server-node: start ONOS with RAMCloud server
156 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
157 * Default behavior can be defined by ${filename}
158 \$ $0 stop
159 Stop all ONOS-related processes
160 \$ $0 restart
161 Stop and start currently running ONOS-related processes
162 \$$0 status
163 Show status of ONOS-related processes
164 \$ $0 {zk|rc-coord|rc-server|core} {start|stop|restart|status}
165 Control specific ONOS-related process"
166
167 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700168}
169
170function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700171 local logfile=$1
172 local nr_max=${2:-10}
173 if [ -f $logfile ]; then
174 for i in `seq $(expr $nr_max - 1) -1 1`; do
175 if [ -f ${logfile}.${i} ]; then
176 mv -f ${logfile}.${i} ${logfile}.`expr $i + 1`
177 fi
178 done
179 mv $logfile $logfile.1
180 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700181}
182
183# kill-processes {module-name} {array of pids}
184function kill-processes {
185 # Kill the existing processes
186 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700187 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700188 echo -n "Stopping $1 ... "
189 fi
190 for p in ${pids}; do
191 if [ x$p != "x" ]; then
192 kill -KILL $p
193 echo "Killed existing process (pid: $p)"
194 fi
195 done
196}
197
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700198function handle-error {
199 set -e
200
201 revert-confs
202
203 set +e
204
205 exit 1
206}
207
Naoki Shiota4e928512014-04-03 15:49:35 -0700208# revert-file {filename}
209# revert "filename" from "filename.bak" if "filename.tmp" exists.
210function revert-file {
211 local filename=$1
212 local temp="${filename}.tmp"
213 local backup="${filename}.bak"
214
215 if [ -f "${temp}" ]; then
216 echo -n "reverting ${filename} ... "
217 mv ${backup} ${filename}
218 rm ${temp}
219 echo "DONE"
220 fi
221}
222
223# revert-confs [error message]
224function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700225 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700226
Naoki Shiota4e928512014-04-03 15:49:35 -0700227 revert-file `basename ${ZK_CONF}`
228 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700229
230 echo "EXIT"
231
232 if [ ! -z "$1" ]; then
233 echo $1
234 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700235}
236
Naoki Shiota590c18d2014-03-31 17:52:59 -0700237function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700238 echo -n "Creating ${ZK_CONF} ... "
239
Naoki Shiota590c18d2014-03-31 17:52:59 -0700240 # creation of zookeeper config
Naoki Shiota9df15d32014-03-27 14:26:20 -0700241
Naoki Shiota590c18d2014-03-31 17:52:59 -0700242 local temp_zk="${ZK_CONF}.tmp"
Naoki Shiota4e928512014-04-03 15:49:35 -0700243 if [ -f ${temp_zk} ]; then
244 rm ${temp_zk}
245 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700246 touch ${temp_zk}
Naoki Shiota4e928512014-04-03 15:49:35 -0700247
Naoki Shiota9df15d32014-03-27 14:26:20 -0700248 if [ -f ${ZK_CONF} ]; then
Naoki Shiota4e928512014-04-03 15:49:35 -0700249 mv ${ZK_CONF} ${ZK_CONF}.bak
250 local filename=`basename ${ZK_CONF}`
251 echo -n "backup old file to ${filename}.bak ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700252 fi
253
Naoki Shiota4e928512014-04-03 15:49:35 -0700254 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700255
256 local i=1
257 local myid=
258 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700259 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700260 myid=$i
261 break
262 fi
263 i=`expr $i + 1`
264 done
265
266 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700267 local filename=`basename ${ONOS_CONF}`
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700268 revert-confs "[ERROR in ${filename}] zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700269 fi
270
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700271 if [ -f "${ZK_MY_ID}" ]; then
272 local SUDO=${SUDO:-}
273 {
274 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
275 } || {
276 echo "FAILED"
277 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
278 echo "[ERROR] Please retry after setting \"export SUDO=sudo\""
279 exit 1
280 }
281 fi
282
Naoki Shiota9df15d32014-03-27 14:26:20 -0700283 echo ${myid} > ${ZK_MY_ID}
284
285 echo -n "myid is assigned to ${myid} ... "
286
287 while read line; do
288 if [[ $line =~ ^__HOSTS__$ ]]; then
289 i=1
290 for host in ${hostarr}; do
291 # TODO: ports might be configurable
292 local hostline="server.${i}=${host}:2888:3888"
293 echo $hostline >> "${temp_zk}"
294 i=`expr $i + 1`
295 done
296 elif [[ $line =~ __DATADIR__ ]]; then
297 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|" >> ${temp_zk}
298 else
299 echo $line >> ${temp_zk}
300 fi
301 done < ${ZK_CONF_TEMPLATE}
302 mv ${temp_zk} ${ZK_CONF}
303
304 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700305}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700306
Naoki Shiota4e928512014-04-03 15:49:35 -0700307function create-hazelcast-conf {
308 echo -n "Creating ${HC_CONF} ... "
309
310 local temp_hc="${HC_CONF}.tmp"
311 if [ -f ${temp_hc} ]; then
312 rm ${temp_hc}
313 fi
314 touch ${temp_hc}
315
316 if [ -f ${HC_CONF} ]; then
317 mv ${HC_CONF} ${HC_CONF}.bak
318 local filename=`basename ${HC_CONF}`
319 echo -n "backup old file to ${filename}.bak ... "
320 fi
321
322 # To keep indent of XML file, change IFS
323 local IFS=''
324 while read line; do
325 if [[ $line =~ __HC_NETWORK__ ]]; then
326 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
327 # temporary change
328 IFS=' '
329 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
330 echo '<multicast enabled="false" />'
331 echo '<tcp-ip enabled="true">'
332 for member in ${memberarr}; do
333 echo " <member>${member}</member>"
334 done
335 echo '</tcp-ip>'
336 IFS=''
337 else
338 echo '<multicast enabled="true">'
339 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
340 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
341 echo '</multicast>'
342 echo '<tcp-ip enabled="false" />'
343 fi
344 else
345 echo "${line}"
346 fi
347 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
348
349 mv ${temp_hc} ${HC_CONF}
350
351 echo "DONE"
352}
353
Naoki Shiota590c18d2014-03-31 17:52:59 -0700354function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700355 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700356
Naoki Shiota9df15d32014-03-27 14:26:20 -0700357 # creation of logback config
358 if [ -f $ONOS_LOGBACK ]; then
359 local logback_file=`basename ${ONOS_LOGBACK}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700360 mv ${ONOS_LOGBACK} ${ONOS_LOGBACK}.bak
361 local filename=`basename ${ONOS_LOGBACK}`
362 echo -n "backup old file to ${filename}.bak ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700363 fi
364 sed -e "s|__FILENAME__|${ONOS_LOG}|" ${ONOS_LOGBACK_TEMPLATE} > ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700365
Naoki Shiota9df15d32014-03-27 14:26:20 -0700366 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700367}
368
Naoki Shiota4e928512014-04-03 15:49:35 -0700369# create-conf-interactive {filename} {function to create conf}
370function create-conf-interactive {
371 local filepath=$1
372 local filename=`basename ${filepath}`
373 local func=$2
Naoki Shiota590c18d2014-03-31 17:52:59 -0700374
Naoki Shiota4e928512014-04-03 15:49:35 -0700375 if [ -f ${filepath} ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700376 # confirmation to overwrite existing config file
Naoki Shiota590c18d2014-03-31 17:52:59 -0700377 echo -n "Overwriting ${filename} [Y/n]? "
378 while [ 1 ]; do
379 read key
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700380 if [ -z "${key}" -o "${key}" == "Y" -o "${key}" == "y" ]; then
Naoki Shiota4e928512014-04-03 15:49:35 -0700381 ${func}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700382 break
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700383 elif [ "${key}" == "N" -o "${key}" == "n" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700384 break
385 fi
386 echo "[Y/n]?"
387 done
388 else
Naoki Shiota4e928512014-04-03 15:49:35 -0700389 ${func}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700390 fi
Naoki Shiota4e928512014-04-03 15:49:35 -0700391}
392
393function create-confs {
394 local key
395 local filename
396
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700397 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700398
399 if [ "$1" == "-f" ]; then
400 create-zk-conf
401 create-hazelcast-conf
402 create-logback-conf
403 else
404 create-conf-interactive ${ZK_CONF} create-zk-conf
405 create-conf-interactive ${HC_CONF} create-hazelcast-conf
406 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
407 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700408
409 trap - ERR
410}
411############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700412
Naoki Shiota590c18d2014-03-31 17:52:59 -0700413
414###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700415function zk {
416 case "$1" in
417 start)
418 start-zk
419 ;;
420 stop)
421 stop-zk
422 ;;
423 stat*) # <- status
424 status-zk
425 ;;
426 re*) # <- restart
427 stop-zk
428 start-zk
429 ;;
430 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700431 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700432 exit 1
433 esac
434}
435
436function start-zk {
Naoki Shiota4e463182014-03-21 15:13:24 -0700437 echo -n "Starting Zookeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700438
Naoki Shiota4e928512014-04-03 15:49:35 -0700439 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700440 if [ -f "${ZK_CONF}" ]; then
441 # Run Zookeeper with our configuration
442 export ZOOCFG=${ZK_CONF_FILE}
443 export ZOOCFGDIR=${ONOS_CONF_DIR}
444 fi
445
Naoki Shiota72209722014-04-08 14:32:17 -0700446 ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700447}
448
449function stop-zk {
450 kill-processes "ZooKeeper" `jps -l | grep org.apache.zookeeper.server | awk '{print $1}'`
451}
452
453function status-zk {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700454 if [ -f ${ZK_CONF} ]; then
455 export ZOOCFG=${ZK_CONF_FILE}
456 export ZOOCFGDIR=${ONOS_CONF_DIR}
457 fi
458
Naoki Shiota72209722014-04-08 14:32:17 -0700459 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700460}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700461############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700462
463
Naoki Shiota590c18d2014-03-31 17:52:59 -0700464####### Functions related to RAMCloud ######
Naoki Shiota9df15d32014-03-27 14:26:20 -0700465function start-backend {
466 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700467 if [ $1 == "coord" ]; then
468 rc-coord startifdown
469 elif [ $1 == "server" ]; then
470 rc-server startifdown
471 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700472 fi
473}
474
475function stop-backend {
476 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
477 if [ $rcsn != 0 ]; then
478 rc-server stop
479 fi
480
Naoki Shiota590c18d2014-03-31 17:52:59 -0700481 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700482 if [ $rccn != 0 ]; then
483 rc-coord stop
484 fi
485}
486
Naoki Shiota590c18d2014-03-31 17:52:59 -0700487function deldb {
488# TODO: implement
489 return
490}
491
492
Naoki Shiota4e463182014-03-21 15:13:24 -0700493### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700494function rc-coord-addr {
Naoki Shiota4e928512014-04-03 15:49:35 -0700495 echo "${RC_COORD_PROTOCOL}:host=${RC_COORD_IP},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700496}
497
498function rc-server-addr {
Naoki Shiota4e928512014-04-03 15:49:35 -0700499 echo "${RC_SERVER_PROTOCOL}:host=${RC_SERVER_IP},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700500}
501
Naoki Shiota4e463182014-03-21 15:13:24 -0700502function rc-coord {
503 case "$1" in
504 start)
505 deldb
Naoki Shiota4e463182014-03-21 15:13:24 -0700506 stop-coord
507 start-coord
508 ;;
509 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700510 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700511 if [ $n == 0 ]; then
512 start-coord
513 else
514 echo "$n instance of RAMCloud coordinator running"
515 fi
516 ;;
517 stop)
518 stop-coord
519 ;;
520 stat*) # <- status
521 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
522 echo "$n RAMCloud coordinator running"
523 ;;
524 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700525 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700526 exit 1
527 esac
528}
529
530function start-coord {
531 if [ ! -d ${LOGDIR} ]; then
532 mkdir -p ${LOGDIR}
533 fi
534 if [ -f $RAMCLOUD_COORD_LOG ]; then
535 rotate-log $RAMCLOUD_COORD_LOG
536 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700537
538 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700539
540 # Run ramcloud
541 echo -n "Starting RAMCloud coordinator ... "
Naoki Shiota72209722014-04-08 14:32:17 -0700542 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator -L ${coord_addr} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700543 echo "STARTED"
544}
545
546
547function stop-coord {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700548 kill-processes "RAMCloud coordinator" `pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700549}
550
Naoki Shiota4e463182014-03-21 15:13:24 -0700551### Functions related to RAMCloud server
552function rc-server {
553 case "$1" in
554 start)
555 deldb
Naoki Shiota4e463182014-03-21 15:13:24 -0700556 stop-server
557 start-server
558 ;;
559 startifdown)
560 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
561 if [ $n == 0 ]; then
562 start-server
563 else
564 echo "$n instance of RAMCloud server running"
565 fi
566 ;;
567 stop)
568 stop-server
569 ;;
570# deldb)
571# deldb
572# ;;
573 stat*) # <- status
574 n=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
575 echo "$n RAMCloud server 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-server {
584 if [ ! -d ${LOGDIR} ]; then
585 mkdir -p ${LOGDIR}
586 fi
587 if [ -f $RAMCLOUD_SERVER_LOG ]; then
588 rotate-log $RAMCLOUD_SERVER_LOG
589 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700590
591 local coord_addr=`rc-coord-addr`
592 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700593
594 # Run ramcloud
595 echo -n "Starting RAMCloud server ... "
Naoki Shiota72209722014-04-08 14:32:17 -0700596 ${RAMCLOUD_HOME}/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 -0700597 echo "STARTED"
598}
599
600function stop-server {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700601 kill-processes "RAMCloud server" `pgrep -f obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700602}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700603############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700604
605
Naoki Shiota590c18d2014-03-31 17:52:59 -0700606## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700607function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700608 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700609 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700610 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700611 ${MVN} -f ${ONOS_HOME}/pom.xml compile
612 fi
613 JAVA_CP=`cat ${CPFILE}`
614 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
615
616 case "$1" in
617 start)
618 stop-onos
619 start-onos
620 ;;
621 startnokill)
622 start-onos
623 ;;
624 startifdown)
625 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
626 if [ $n == 0 ]; then
627 start-onos
628 else
629 echo "$n instance of onos running"
630 fi
631 ;;
632 stop)
633 stop-onos
634 ;;
635 stat*) # <- status
636 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
637 echo "$n instance of onos running"
638 ;;
639 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700640 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700641 exit 1
642 esac
643}
644
645function start-onos {
646 if [ ! -d ${LOGDIR} ]; then
647 mkdir -p ${LOGDIR}
648 fi
649 # Backup log files
650 for log in ${LOGS}; do
651 if [ -f ${log} ]; then
652 rotate-log ${log}
653 fi
654 done
Naoki Shiota9df15d32014-03-27 14:26:20 -0700655
Naoki Shiota4e463182014-03-21 15:13:24 -0700656 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700657 echo "[WARNING] ${ONOS_LOGBACK} not found."
658 echo " Run \"\$ $0 setup\" to create."
659 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700660 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700661
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700662 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700663
664 # Run ONOS
665
666 echo -n "Starting ONOS controller ..."
Naoki Shiota4e463182014-03-21 15:13:24 -0700667 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
668
669 # We need to wait a bit to find out whether starting the ONOS process succeeded
670 sleep 1
671
672 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700673 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700674 echo " STARTED"
675 else
676 echo " FAILED"
677 fi
678
679# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
680# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
681}
682
683function stop-onos {
684 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
685# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
686}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700687############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700688
689
Naoki Shiota590c18d2014-03-31 17:52:59 -0700690################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -0700691case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -0700692 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -0700693 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700694 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700695 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -0700696 mode_parameter=${ONOS_HOST_ROLE}
697 if [ ! -z "$2" ]; then
698 mode_parameter=$2
699 fi
700
701 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -0700702 single-node)
703 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700704 start-backend coord
705 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700706 onos startifdown
707 ;;
708 coord-node)
709 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700710 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -0700711 onos startifdown
712 ;;
713 server-node)
714 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700715 start-backend server
716 onos startifdown
717 ;;
718 coord-and-server-node)
719 zk start
720 start-backend coord
721 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700722 onos startifdown
723 ;;
724 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700725 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700726 ;;
727 esac
728 echo
729 ;;
730 stop)
731 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
732 if [ $on != 0 ]; then
733 onos stop
734 fi
735
Naoki Shiota9df15d32014-03-27 14:26:20 -0700736 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -0700737
738 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
739 if [ $zkn != 0 ]; then
740 zk stop
741 fi
742 echo
743 ;;
744 restart)
745 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
746 if [ $on != 0 ]; then
747 onos stop
748 fi
749
750 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
751 if [ $rcsn != 0 ]; then
752 rc-server stop
753 fi
754
Naoki Shiota590c18d2014-03-31 17:52:59 -0700755 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700756 if [ $rccn != 0 ]; then
757 rc-coord stop
758 fi
759
760 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
761 if [ $zkn != 0 ]; then
762 zk restart
763 fi
764
765 if [ $rccn != 0 ]; then
766 rc-coord startifdown
767 fi
768
769 if [ $rcsn != 0 ]; then
770 rc-server startifdown
771 fi
772
773 if [ $on != 0 ]; then
774 onos startifdown
775 fi
776 echo
777 ;;
778 stat*) # <- status
779 echo '[ZooKeeper]'
780 zk status
781 echo
782 echo '[RAMCloud coordinator]'
783 rc-coord status
784 echo
785 echo '[RAMCloud server]'
786 rc-server status
787 echo
788 echo '[ONOS core]'
789 onos status
790 echo
791 ;;
792 zk)
793 zk $2
794 ;;
795 rc-c*) # <- rc-coordinator
796 rc-coord $2
797 ;;
798 rc-s*) # <- rc-server
799 rc-server $2
800 ;;
801 core)
802 onos $2
803 ;;
804 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700805 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700806 exit 1
807esac
Naoki Shiota590c18d2014-03-31 17:52:59 -0700808