blob: bb090e8fd35d7a1e19f7d8888776ac212f62e91a [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
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070070# Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
71ZK_LOG_DIR=${ONOS_HOME}/onos-logs/${ONOS_HOST_NAME}
Naoki Shiota72209722014-04-08 14:32:17 -070072ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070073ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070074
Naoki Shiota4e928512014-04-03 15:49:35 -070075HC_CONF=${ONOS_CONF_DIR}/hazelcast.xml
76HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
77
Naoki Shiota4e463182014-03-21 15:13:24 -070078RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070079RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
80RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070081RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070082RAMCLOUD_CONF=${ONOS_CONF_DIR}/ramcloud.conf
Naoki Shiota4e463182014-03-21 15:13:24 -070083
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070084export LD_LIBRARY_PATH=${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}:$LD_LIBRARY_PATH
Naoki Shiota4e463182014-03-21 15:13:24 -070085
86## 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 -070087ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070088ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070089ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070090LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070091LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070092ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
93PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
94LOGS="$ONOS_LOG $PCAP_LOG"
95
Naoki Shiota9df15d32014-03-27 14:26:20 -070096ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -070097JMX_PORT=${JMX_PORT:-7189}
98
99# Set JVM options
100JVM_OPTS="${JVM_OPTS:-}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700101JVM_OPTS="$JVM_OPTS -server -d64"
102#JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700103
104# Uncomment or specify appropriate value as JVM_OPTS environment variables.
105#JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
Naoki Shiota4e463182014-03-21 15:13:24 -0700106#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
107#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700108
109#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC"
110JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
111JVM_OPTS="$JVM_OPTS -XX:+AggressiveOpts"
112
113# We may want to remove UseFastAccessorMethods option: http://bugs.java.com/view_bug.do?bug_id=6385687
114JVM_OPTS="$JVM_OPTS -XX:+UseFastAccessorMethods"
115
116JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192"
117JVM_OPTS="$JVM_OPTS -XX:FreqInlineSize=8192"
118JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500"
119
Naoki Shiota4e463182014-03-21 15:13:24 -0700120JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700121
122# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
123JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
124
125JVM_OPTS="$JVM_OPTS -XX:+UseCompressedOops"
126
127JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
128JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
129JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
130
Naoki Shiota4e463182014-03-21 15:13:24 -0700131JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
132
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700133# Uncomment to dump final JVM flags to stdout
134#JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
135
Naoki Shiota4e463182014-03-21 15:13:24 -0700136# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700137MAIN_CLASS="net.onrc.onos.core.main.Main"
Naoki Shiota4e463182014-03-21 15:13:24 -0700138
139MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700140############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700141
Naoki Shiota9df15d32014-03-27 14:26:20 -0700142
Naoki Shiota590c18d2014-03-31 17:52:59 -0700143############# Common functions #############
144function print_usage {
145 local filename=`basename ${ONOS_CONF}`
146 local usage="Usage: setup/start/stop ONOS on this server.
147 \$ $0 setup [-f]
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700148 Set up ONOS node using ${ONOS_CONF} .
Naoki Shiota590c18d2014-03-31 17:52:59 -0700149 - generate and replace config file of ZooKeeper.
150 - create myid in ZooKeeper datadir.
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700151 - generate and replace config file for Hazelcast.
152 - generate and replace config file for RAMCloud.
Naoki Shiota590c18d2014-03-31 17:52:59 -0700153 - generate and replace logback.${ONOS_HOST_NAME}.xml
154 If -f option is used, all existing files will be overwritten without confirmation.
155 \$ $0 start [single-node|coord-node|server-node|coord-and-server-node]
156 Start ONOS node with specific RAMCloud entities
157 - single-node: start ONOS with stand-alone RAMCloud
158 - coord-node : start ONOS with RAMCloud coordinator
159 - server-node: start ONOS with RAMCloud server
160 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
161 * Default behavior can be defined by ${filename}
162 \$ $0 stop
163 Stop all ONOS-related processes
164 \$ $0 restart
165 Stop and start currently running ONOS-related processes
166 \$$0 status
167 Show status of ONOS-related processes
168 \$ $0 {zk|rc-coord|rc-server|core} {start|stop|restart|status}
169 Control specific ONOS-related process"
170
171 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700172}
173
174function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700175 local logfile=$1
176 local nr_max=${2:-10}
177 if [ -f $logfile ]; then
178 for i in `seq $(expr $nr_max - 1) -1 1`; do
179 if [ -f ${logfile}.${i} ]; then
180 mv -f ${logfile}.${i} ${logfile}.`expr $i + 1`
181 fi
182 done
183 mv $logfile $logfile.1
184 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700185}
186
187# kill-processes {module-name} {array of pids}
188function kill-processes {
189 # Kill the existing processes
190 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700191 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700192 echo -n "Stopping $1 ... "
193 fi
194 for p in ${pids}; do
195 if [ x$p != "x" ]; then
196 kill -KILL $p
197 echo "Killed existing process (pid: $p)"
198 fi
199 done
200}
201
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700202function handle-error {
203 set -e
204
205 revert-confs
206
207 set +e
208
209 exit 1
210}
211
Naoki Shiota4e928512014-04-03 15:49:35 -0700212# revert-file {filename}
213# revert "filename" from "filename.bak" if "filename.tmp" exists.
214function revert-file {
215 local filename=$1
216 local temp="${filename}.tmp"
217 local backup="${filename}.bak"
218
219 if [ -f "${temp}" ]; then
220 echo -n "reverting ${filename} ... "
221 mv ${backup} ${filename}
222 rm ${temp}
223 echo "DONE"
224 fi
225}
226
227# revert-confs [error message]
228function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700229 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700230
Naoki Shiota4e928512014-04-03 15:49:35 -0700231 revert-file `basename ${ZK_CONF}`
232 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700233
234 echo "EXIT"
235
236 if [ ! -z "$1" ]; then
237 echo $1
238 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700239}
240
Naoki Shiota590c18d2014-03-31 17:52:59 -0700241function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700242 echo -n "Creating ${ZK_CONF} ... "
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700243
244 # Create the ZooKeeper lib directory
245 if [ ! -d ${ZK_LIB_DIR} ]; then
246 local SUDO=${SUDO:-}
247 local whoami=`whoami`
248 {
249 ${SUDO} mkdir ${ZK_LIB_DIR}
250 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
251 } || {
252 echo "FAILED"
253 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
254 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
255 exit 1
256 }
257 fi
258
259 # creation of ZooKeeper config
Naoki Shiota9df15d32014-03-27 14:26:20 -0700260
Naoki Shiota590c18d2014-03-31 17:52:59 -0700261 local temp_zk="${ZK_CONF}.tmp"
Naoki Shiota4e928512014-04-03 15:49:35 -0700262 if [ -f ${temp_zk} ]; then
263 rm ${temp_zk}
264 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700265 touch ${temp_zk}
Naoki Shiota4e928512014-04-03 15:49:35 -0700266
Naoki Shiota9df15d32014-03-27 14:26:20 -0700267 if [ -f ${ZK_CONF} ]; then
Naoki Shiota4e928512014-04-03 15:49:35 -0700268 mv ${ZK_CONF} ${ZK_CONF}.bak
269 local filename=`basename ${ZK_CONF}`
270 echo -n "backup old file to ${filename}.bak ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700271 fi
272
Naoki Shiota4e928512014-04-03 15:49:35 -0700273 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700274
275 local i=1
276 local myid=
277 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700278 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700279 myid=$i
280 break
281 fi
282 i=`expr $i + 1`
283 done
284
285 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700286 local filename=`basename ${ONOS_CONF}`
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700287 revert-confs "[ERROR in ${filename}] zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700288 fi
289
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700290 if [ -f "${ZK_MY_ID}" ]; then
291 local SUDO=${SUDO:-}
292 {
293 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
294 } || {
295 echo "FAILED"
296 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700297 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700298 exit 1
299 }
300 fi
301
Naoki Shiota9df15d32014-03-27 14:26:20 -0700302 echo ${myid} > ${ZK_MY_ID}
303
304 echo -n "myid is assigned to ${myid} ... "
305
306 while read line; do
307 if [[ $line =~ ^__HOSTS__$ ]]; then
308 i=1
309 for host in ${hostarr}; do
310 # TODO: ports might be configurable
311 local hostline="server.${i}=${host}:2888:3888"
312 echo $hostline >> "${temp_zk}"
313 i=`expr $i + 1`
314 done
315 elif [[ $line =~ __DATADIR__ ]]; then
316 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|" >> ${temp_zk}
317 else
318 echo $line >> ${temp_zk}
319 fi
320 done < ${ZK_CONF_TEMPLATE}
321 mv ${temp_zk} ${ZK_CONF}
322
323 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700324}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700325
Naoki Shiota4e928512014-04-03 15:49:35 -0700326function create-hazelcast-conf {
327 echo -n "Creating ${HC_CONF} ... "
328
329 local temp_hc="${HC_CONF}.tmp"
330 if [ -f ${temp_hc} ]; then
331 rm ${temp_hc}
332 fi
333 touch ${temp_hc}
334
335 if [ -f ${HC_CONF} ]; then
336 mv ${HC_CONF} ${HC_CONF}.bak
337 local filename=`basename ${HC_CONF}`
338 echo -n "backup old file to ${filename}.bak ... "
339 fi
340
341 # To keep indent of XML file, change IFS
342 local IFS=''
343 while read line; do
344 if [[ $line =~ __HC_NETWORK__ ]]; then
345 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
346 # temporary change
347 IFS=' '
348 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
349 echo '<multicast enabled="false" />'
350 echo '<tcp-ip enabled="true">'
351 for member in ${memberarr}; do
352 echo " <member>${member}</member>"
353 done
354 echo '</tcp-ip>'
355 IFS=''
356 else
357 echo '<multicast enabled="true">'
358 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
359 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
360 echo '</multicast>'
361 echo '<tcp-ip enabled="false" />'
362 fi
363 else
364 echo "${line}"
365 fi
366 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
367
368 mv ${temp_hc} ${HC_CONF}
369
370 echo "DONE"
371}
372
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700373function create-ramcloud-conf {
374 echo -n "Creating ${RAMCLOUD_CONF} ... "
375
376 local temp_hc="${RAMCLOUD_CONF}.tmp"
377 if [ -f ${temp_hc} ]; then
378 rm ${temp_hc}
379 fi
380 touch ${temp_hc}
381
382 if [ -f ${RAMCLOUD_CONF} ]; then
383 mv ${RAMCLOUD_CONF} ${RAMCLOUD_CONF}.bak
384 local filename=`basename ${RAMCLOUD_CONF}`
385 echo -n "backup old file to ${filename}.bak ... "
386 fi
387 echo "ramcloud.coordinatorIp=${RC_COORD_PROTOCOL}:host=${RC_COORD_IP}" > ${temp_hc}
388 echo "ramcloud.coordinatorPort=port=${RC_COORD_PORT}" >> ${temp_hc}
389
390 # FIXME remove these when old start-up script is removed.
391 echo "# Following lines is not used by ONOS core or onos.sh" >> ${temp_hc}
392 echo "ramcloud.serverIp=${RC_SERVER_PROTOCOL}:host=${RC_SERVER_IP}" >> ${temp_hc}
393 echo "ramcloud.serverPort=port=${RC_SERVER_PORT}" >> ${temp_hc}
394
395 mv ${temp_hc} ${RAMCLOUD_CONF}
396
397 echo "DONE"
398}
399
Naoki Shiota590c18d2014-03-31 17:52:59 -0700400function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700401 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700402
Naoki Shiota9df15d32014-03-27 14:26:20 -0700403 # creation of logback config
404 if [ -f $ONOS_LOGBACK ]; then
405 local logback_file=`basename ${ONOS_LOGBACK}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700406 mv ${ONOS_LOGBACK} ${ONOS_LOGBACK}.bak
407 local filename=`basename ${ONOS_LOGBACK}`
408 echo -n "backup old file to ${filename}.bak ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700409 fi
410 sed -e "s|__FILENAME__|${ONOS_LOG}|" ${ONOS_LOGBACK_TEMPLATE} > ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700411
Naoki Shiota9df15d32014-03-27 14:26:20 -0700412 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700413}
414
Naoki Shiota4e928512014-04-03 15:49:35 -0700415# create-conf-interactive {filename} {function to create conf}
416function create-conf-interactive {
417 local filepath=$1
418 local filename=`basename ${filepath}`
419 local func=$2
Naoki Shiota590c18d2014-03-31 17:52:59 -0700420
Naoki Shiota4e928512014-04-03 15:49:35 -0700421 if [ -f ${filepath} ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700422 # confirmation to overwrite existing config file
Naoki Shiota590c18d2014-03-31 17:52:59 -0700423 echo -n "Overwriting ${filename} [Y/n]? "
424 while [ 1 ]; do
425 read key
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700426 if [ -z "${key}" -o "${key}" == "Y" -o "${key}" == "y" ]; then
Naoki Shiota4e928512014-04-03 15:49:35 -0700427 ${func}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700428 break
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700429 elif [ "${key}" == "N" -o "${key}" == "n" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700430 break
431 fi
432 echo "[Y/n]?"
433 done
434 else
Naoki Shiota4e928512014-04-03 15:49:35 -0700435 ${func}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700436 fi
Naoki Shiota4e928512014-04-03 15:49:35 -0700437}
438
439function create-confs {
440 local key
441 local filename
442
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700443 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700444
445 if [ "$1" == "-f" ]; then
446 create-zk-conf
447 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700448 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700449 create-logback-conf
450 else
451 create-conf-interactive ${ZK_CONF} create-zk-conf
452 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700453 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700454 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
455 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700456
457 trap - ERR
458}
459############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700460
Naoki Shiota590c18d2014-03-31 17:52:59 -0700461
462###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700463function zk {
464 case "$1" in
465 start)
466 start-zk
467 ;;
468 stop)
469 stop-zk
470 ;;
471 stat*) # <- status
472 status-zk
473 ;;
474 re*) # <- restart
475 stop-zk
476 start-zk
477 ;;
478 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700479 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700480 exit 1
481 esac
482}
483
484function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700485 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700486
Naoki Shiota4e928512014-04-03 15:49:35 -0700487 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700488 mkdir -p ${ZK_LOG_DIR}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700489 if [ -f "${ZK_CONF}" ]; then
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700490 # Run ZooKeeper with our configuration
Naoki Shiota9df15d32014-03-27 14:26:20 -0700491 export ZOOCFG=${ZK_CONF_FILE}
492 export ZOOCFGDIR=${ONOS_CONF_DIR}
493 fi
494
Naoki Shiota72209722014-04-08 14:32:17 -0700495 ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700496}
497
498function stop-zk {
499 kill-processes "ZooKeeper" `jps -l | grep org.apache.zookeeper.server | awk '{print $1}'`
500}
501
502function status-zk {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700503 if [ -f ${ZK_CONF} ]; then
504 export ZOOCFG=${ZK_CONF_FILE}
505 export ZOOCFGDIR=${ONOS_CONF_DIR}
506 fi
507
Naoki Shiota72209722014-04-08 14:32:17 -0700508 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700509}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700510############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700511
512
Naoki Shiota590c18d2014-03-31 17:52:59 -0700513####### Functions related to RAMCloud ######
Naoki Shiota9df15d32014-03-27 14:26:20 -0700514function start-backend {
515 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700516 if [ $1 == "coord" ]; then
517 rc-coord startifdown
518 elif [ $1 == "server" ]; then
519 rc-server startifdown
520 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700521 fi
522}
523
524function stop-backend {
525 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
526 if [ $rcsn != 0 ]; then
527 rc-server stop
528 fi
529
Naoki Shiota590c18d2014-03-31 17:52:59 -0700530 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700531 if [ $rccn != 0 ]; then
532 rc-coord stop
533 fi
534}
535
Naoki Shiota590c18d2014-03-31 17:52:59 -0700536function deldb {
537# TODO: implement
538 return
539}
540
541
Naoki Shiota4e463182014-03-21 15:13:24 -0700542### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700543function rc-coord-addr {
Naoki Shiota4e928512014-04-03 15:49:35 -0700544 echo "${RC_COORD_PROTOCOL}:host=${RC_COORD_IP},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700545}
546
547function rc-server-addr {
Naoki Shiota4e928512014-04-03 15:49:35 -0700548 echo "${RC_SERVER_PROTOCOL}:host=${RC_SERVER_IP},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700549}
550
Naoki Shiota4e463182014-03-21 15:13:24 -0700551function rc-coord {
552 case "$1" in
553 start)
554 deldb
Naoki Shiota4e463182014-03-21 15:13:24 -0700555 stop-coord
556 start-coord
557 ;;
558 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700559 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700560 if [ $n == 0 ]; then
561 start-coord
562 else
563 echo "$n instance of RAMCloud coordinator running"
564 fi
565 ;;
566 stop)
567 stop-coord
568 ;;
569 stat*) # <- status
570 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
571 echo "$n RAMCloud coordinator running"
572 ;;
573 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700574 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700575 exit 1
576 esac
577}
578
579function start-coord {
580 if [ ! -d ${LOGDIR} ]; then
581 mkdir -p ${LOGDIR}
582 fi
583 if [ -f $RAMCLOUD_COORD_LOG ]; then
584 rotate-log $RAMCLOUD_COORD_LOG
585 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700586
587 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700588
589 # Run ramcloud
590 echo -n "Starting RAMCloud coordinator ... "
Naoki Shiota72209722014-04-08 14:32:17 -0700591 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator -L ${coord_addr} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700592 echo "STARTED"
593}
594
595
596function stop-coord {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700597 kill-processes "RAMCloud coordinator" `pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700598}
599
Naoki Shiota4e463182014-03-21 15:13:24 -0700600### Functions related to RAMCloud server
601function rc-server {
602 case "$1" in
603 start)
604 deldb
Naoki Shiota4e463182014-03-21 15:13:24 -0700605 stop-server
606 start-server
607 ;;
608 startifdown)
609 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
610 if [ $n == 0 ]; then
611 start-server
612 else
613 echo "$n instance of RAMCloud server running"
614 fi
615 ;;
616 stop)
617 stop-server
618 ;;
619# deldb)
620# deldb
621# ;;
622 stat*) # <- status
623 n=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
624 echo "$n RAMCloud server running"
625 ;;
626 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700627 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700628 exit 1
629 esac
630}
631
632function start-server {
633 if [ ! -d ${LOGDIR} ]; then
634 mkdir -p ${LOGDIR}
635 fi
636 if [ -f $RAMCLOUD_SERVER_LOG ]; then
637 rotate-log $RAMCLOUD_SERVER_LOG
638 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700639
640 local coord_addr=`rc-coord-addr`
641 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700642
643 # Run ramcloud
644 echo -n "Starting RAMCloud server ... "
Naoki Shiota72209722014-04-08 14:32:17 -0700645 ${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 -0700646 echo "STARTED"
647}
648
649function stop-server {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700650 kill-processes "RAMCloud server" `pgrep -f obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700651}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700652############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700653
654
Naoki Shiota590c18d2014-03-31 17:52:59 -0700655## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700656function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700657 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700658 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700659 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700660 ${MVN} -f ${ONOS_HOME}/pom.xml compile
661 fi
662 JAVA_CP=`cat ${CPFILE}`
663 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
664
665 case "$1" in
666 start)
667 stop-onos
668 start-onos
669 ;;
670 startnokill)
671 start-onos
672 ;;
673 startifdown)
674 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
675 if [ $n == 0 ]; then
676 start-onos
677 else
678 echo "$n instance of onos running"
679 fi
680 ;;
681 stop)
682 stop-onos
683 ;;
684 stat*) # <- status
685 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
686 echo "$n instance of onos running"
687 ;;
688 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700689 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700690 exit 1
691 esac
692}
693
694function start-onos {
695 if [ ! -d ${LOGDIR} ]; then
696 mkdir -p ${LOGDIR}
697 fi
698 # Backup log files
699 for log in ${LOGS}; do
700 if [ -f ${log} ]; then
701 rotate-log ${log}
702 fi
703 done
Naoki Shiota9df15d32014-03-27 14:26:20 -0700704
Naoki Shiota4e463182014-03-21 15:13:24 -0700705 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700706 echo "[WARNING] ${ONOS_LOGBACK} not found."
707 echo " Run \"\$ $0 setup\" to create."
708 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700709 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700710
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700711 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700712 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
713 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
714 fi
715
Naoki Shiota9df15d32014-03-27 14:26:20 -0700716 # Run ONOS
717
718 echo -n "Starting ONOS controller ..."
Naoki Shiota4e463182014-03-21 15:13:24 -0700719 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
720
721 # We need to wait a bit to find out whether starting the ONOS process succeeded
722 sleep 1
723
724 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700725 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700726 echo " STARTED"
727 else
728 echo " FAILED"
729 fi
730
731# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
732# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
733}
734
735function stop-onos {
736 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
737# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
738}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700739############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700740
741
Naoki Shiota590c18d2014-03-31 17:52:59 -0700742################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -0700743case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -0700744 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -0700745 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700746 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700747 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -0700748 mode_parameter=${ONOS_HOST_ROLE}
749 if [ ! -z "$2" ]; then
750 mode_parameter=$2
751 fi
752
753 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -0700754 single-node)
755 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700756 start-backend coord
757 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700758 onos startifdown
759 ;;
760 coord-node)
761 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700762 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -0700763 onos startifdown
764 ;;
765 server-node)
766 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700767 start-backend server
768 onos startifdown
769 ;;
770 coord-and-server-node)
771 zk start
772 start-backend coord
773 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700774 onos startifdown
775 ;;
776 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700777 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700778 ;;
779 esac
780 echo
781 ;;
782 stop)
783 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
784 if [ $on != 0 ]; then
785 onos stop
786 fi
787
Naoki Shiota9df15d32014-03-27 14:26:20 -0700788 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -0700789
790 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
791 if [ $zkn != 0 ]; then
792 zk stop
793 fi
794 echo
795 ;;
796 restart)
797 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
798 if [ $on != 0 ]; then
799 onos stop
800 fi
801
802 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
803 if [ $rcsn != 0 ]; then
804 rc-server stop
805 fi
806
Naoki Shiota590c18d2014-03-31 17:52:59 -0700807 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700808 if [ $rccn != 0 ]; then
809 rc-coord stop
810 fi
811
812 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
813 if [ $zkn != 0 ]; then
814 zk restart
815 fi
816
817 if [ $rccn != 0 ]; then
818 rc-coord startifdown
819 fi
820
821 if [ $rcsn != 0 ]; then
822 rc-server startifdown
823 fi
824
825 if [ $on != 0 ]; then
826 onos startifdown
827 fi
828 echo
829 ;;
830 stat*) # <- status
831 echo '[ZooKeeper]'
832 zk status
833 echo
834 echo '[RAMCloud coordinator]'
835 rc-coord status
836 echo
837 echo '[RAMCloud server]'
838 rc-server status
839 echo
840 echo '[ONOS core]'
841 onos status
842 echo
843 ;;
844 zk)
845 zk $2
846 ;;
847 rc-c*) # <- rc-coordinator
848 rc-coord $2
849 ;;
850 rc-s*) # <- rc-server
851 rc-server $2
852 ;;
853 core)
854 onos $2
855 ;;
856 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700857 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700858 exit 1
859esac
Naoki Shiota590c18d2014-03-31 17:52:59 -0700860