blob: 0a2ea2e516769009de3366746b407d94e5e9b9ef [file] [log] [blame]
Naoki Shiota4e463182014-03-21 15:13:24 -07001#!/bin/bash
Naoki Shiota4e463182014-03-21 15:13:24 -07002
Naoki Shiota590c18d2014-03-31 17:52:59 -07003### Env vars used by this script. (default value) ###
4# $ONOS_HOME : path of root directory of ONOS repository (this script's dir)
5# $ONOS_CONF_DIR : path of ONOS config directory (~/ONOS/conf)
Naoki Shiota9a1e6d12014-04-03 14:47:12 -07006# $ONOS_CONF : path of ONOS node config file (~/ONOS/conf/onos_node.`hostname`.conf or onos_node.conf)
Naoki Shiota590c18d2014-03-31 17:52:59 -07007# $ONOS_PROPS : path of ONOS properties file (~/ONOS/conf/onos.properties)
8# $ONOS_LOGBACK : path of logback config file (~/ONOS/conf/logback.`hostname`.xml)
Naoki Shiotad8ea71a2014-04-23 17:04:51 -07009# $ONOS_LOGDIR : path of log output directory (~/ONOS/onos-logs)
10# $ONOS_LOGBASE : base name of log output file (onos.`hostname`)
Naoki Shiota590c18d2014-03-31 17:52:59 -070011# $RAMCLOUD_HOME : path of root directory of RAMCloud repository (~/ramcloud)
12# $RAMCLOUD_BRANCH : branch name of RAMCloud to use (master)
Naoki Shiota72209722014-04-08 14:32:17 -070013# $ZK_HOME : path of root directory of ZooKeeper (~/zookeeper-3.4.5)
14# $ZK_LIB_DIR : path of ZooKeeper library (/var/lib/zookeeper)
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070015# $ZK_LOG_DIR : path of ZooKeeper log output directory (~/ONOS/onos-logs/zk-`hostname`)
Naoki Shiota590c18d2014-03-31 17:52:59 -070016# $JVM_OPTS : JVM options ONOS starts with
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070017# $ZK_CONF : path of ZooKeeper config file (~/ONOS/conf/zoo.cfg)
18# $HC_CONF : path of Hazelcast config file (~/ONOS/conf/hazelcast.xml)
19# $RAMCLOUD_CONF : path of RAMCloud config file (~/ONOS/conf/ramcloud.conf)
Naoki Shiota590c18d2014-03-31 17:52:59 -070020#####################################################
21
Naoki Shiota590c18d2014-03-31 17:52:59 -070022ONOS_HOME=${ONOS_HOME:-$(cd `dirname $0`; pwd)}
Naoki Shiota9df15d32014-03-27 14:26:20 -070023ONOS_CONF_DIR=${ONOS_CONF_DIR:-${ONOS_HOME}/conf}
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070024ONOS_CONF=${ONOS_CONF:-${ONOS_CONF_DIR}/onos_node.`hostname`.conf}
Naoki Shiota590c18d2014-03-31 17:52:59 -070025
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070026source ${ONOS_HOME}/scripts/common/utils.sh
27
Yuta HIGUCHIe748ecc2014-05-28 13:55:03 -070028confirm-if-root
29
Naoki Shiota590c18d2014-03-31 17:52:59 -070030if [ ! -f ${ONOS_CONF} ]; then
Naoki Shiota9a1e6d12014-04-03 14:47:12 -070031 # falling back to default config file
32 ONOS_CONF=${ONOS_CONF_DIR}/onos_node.conf
33 if [ ! -f ${ONOS_CONF} ]; then
34 echo "${ONOS_CONF} not found."
35 exit 1
36 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -070037fi
Naoki Shiota4e928512014-04-03 15:49:35 -070038
Naoki Shiota4e928512014-04-03 15:49:35 -070039### Variables read from ONOS config file ###
40ONOS_HOST_NAME=$(read-conf ${ONOS_CONF} host.name `hostname`)
41ONOS_HOST_IP=$(read-conf ${ONOS_CONF} host.ip)
42ONOS_HOST_ROLE=$(read-conf ${ONOS_CONF} host.role)
43ONOS_HOST_BACKEND=$(read-conf ${ONOS_CONF} host.backend)
44ZK_HOSTS=$(read-conf ${ONOS_CONF} zookeeper.hosts ${ONOS_HOST_NAME})
45RC_COORD_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.coordinator.protocol "fast+udp")
46RC_COORD_IP=$(read-conf ${ONOS_CONF} ramcloud.coordinator.ip ${ONOS_HOST_IP})
47RC_COORD_PORT=$(read-conf ${ONOS_CONF} ramcloud.coordinator.port 12246)
48RC_SERVER_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.server.protocol "fast+udp")
49RC_SERVER_IP=$(read-conf ${ONOS_CONF} ramcloud.server.ip ${ONOS_HOST_IP})
50RC_SERVER_PORT=$(read-conf ${ONOS_CONF} ramcloud.server.port 12242)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070051HC_HOST_PORT=$(read-conf ${ONOS_CONF} hazelcast.host.port 5701)
Naoki Shiota4e928512014-04-03 15:49:35 -070052HC_TCPIP_MEMBERS=$(read-conf ${ONOS_CONF} hazelcast.tcp-ip.members)
53HC_MULTICAST_GROUP=$(read-conf ${ONOS_CONF} hazelcast.multicast.group "224.2.2.3")
54HC_MULTICAST_PORT=$(read-conf ${ONOS_CONF} hazelcast.multicast.port 54327)
Naoki Shiota590c18d2014-03-31 17:52:59 -070055############################################
56
57
58############## Other variables #############
Naoki Shiota4e928512014-04-03 15:49:35 -070059ONOS_TEMPLATE_DIR=${ONOS_CONF_DIR}/template
60
Naoki Shiota4e463182014-03-21 15:13:24 -070061LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
62
Naoki Shiota72209722014-04-08 14:32:17 -070063ZK_HOME=${ZK_HOME:-~/zookeeper-3.4.5}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070064ZK_CONF=${ZK_CONF:-${ONOS_CONF_DIR}/zoo.cfg}
Naoki Shiota4e928512014-04-03 15:49:35 -070065ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070066# Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070067ZK_LOG_DIR=${ZK_LOG_DIR:-${ONOS_HOME}/onos-logs/zk-${ONOS_HOST_NAME}}
Naoki Shiota72209722014-04-08 14:32:17 -070068ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070069ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070070
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070071HC_CONF=${HC_CONF:-${ONOS_CONF_DIR}/hazelcast.xml}
Naoki Shiota4e928512014-04-03 15:49:35 -070072HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
73
Naoki Shiota4e463182014-03-21 15:13:24 -070074RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070075RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
76RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070077RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070078RAMCLOUD_CONF=${RAMCLOUD_CONF:-${ONOS_CONF_DIR}/ramcloud.conf}
Naoki Shiota4e463182014-03-21 15:13:24 -070079
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070080export LD_LIBRARY_PATH=${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}:$LD_LIBRARY_PATH
Naoki Shiota4e463182014-03-21 15:13:24 -070081
82## 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 -070083ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070084ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070085ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070086LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070087LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070088ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070089ONOS_LOG_ROLLING_PATTERN="${LOGDIR}/${LOGBASE}.%i.log.gz"
90ONOS_STDOUT_LOG="${LOGDIR}/${LOGBASE}.stdout"
91ONOS_STDERR_LOG="${LOGDIR}/${LOGBASE}.stderr"
Naoki Shiota4e463182014-03-21 15:13:24 -070092PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070093LOGS="$ONOS_LOG $ONOS_STDOUT_LOG $ONOS_STDERR_LOG $PCAP_LOG"
Naoki Shiota4e463182014-03-21 15:13:24 -070094
Naoki Shiota9df15d32014-03-27 14:26:20 -070095ONOS_PROPS=${ONOS_PROPS:-${ONOS_CONF_DIR}/onos.properties}
Naoki Shiota4e463182014-03-21 15:13:24 -070096JMX_PORT=${JMX_PORT:-7189}
97
98# Set JVM options
99JVM_OPTS="${JVM_OPTS:-}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700100JVM_OPTS="$JVM_OPTS -server -d64"
101#JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700102
103# Uncomment or specify appropriate value as JVM_OPTS environment variables.
104#JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m"
Naoki Shiota4e463182014-03-21 15:13:24 -0700105#JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m"
106#JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m"
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700107
108#JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC"
109JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
110JVM_OPTS="$JVM_OPTS -XX:+AggressiveOpts"
111
112# We may want to remove UseFastAccessorMethods option: http://bugs.java.com/view_bug.do?bug_id=6385687
113JVM_OPTS="$JVM_OPTS -XX:+UseFastAccessorMethods"
114
115JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192"
116JVM_OPTS="$JVM_OPTS -XX:FreqInlineSize=8192"
117JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500"
118
Naoki Shiota4e463182014-03-21 15:13:24 -0700119JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700120
121# Workaround for Thread Priority http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
122JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42"
123
124JVM_OPTS="$JVM_OPTS -XX:+UseCompressedOops"
125
126JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
127JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
128JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
129
Naoki Shiota4e463182014-03-21 15:13:24 -0700130JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j"
131
Yuta HIGUCHI18354592014-04-01 13:53:42 -0700132# Uncomment to dump final JVM flags to stdout
133#JVM_OPTS="$JVM_OPTS -XX:+PrintFlagsFinal"
134
Naoki Shiota4e463182014-03-21 15:13:24 -0700135# Set ONOS core main class
Jonathan Hart51f6f5b2014-04-03 10:32:10 -0700136MAIN_CLASS="net.onrc.onos.core.main.Main"
Naoki Shiota4e463182014-03-21 15:13:24 -0700137
138MVN=${MVN:-mvn -o}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700139############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700140
Naoki Shiota9df15d32014-03-27 14:26:20 -0700141
Naoki Shiota590c18d2014-03-31 17:52:59 -0700142############# Common functions #############
143function print_usage {
Naoki Shiota05721b32014-04-29 14:41:12 -0700144 local scriptname=`basename $0`
Naoki Shiota590c18d2014-03-31 17:52:59 -0700145 local filename=`basename ${ONOS_CONF}`
146 local usage="Usage: setup/start/stop ONOS on this server.
Naoki Shiota05721b32014-04-29 14:41:12 -0700147 \$ ${scriptname} 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.
Naoki Shiota05721b32014-04-29 14:41:12 -0700155 \$ ${scriptname} start [single-node|coord-node|server-node|coord-and-server-node]
Naoki Shiota590c18d2014-03-31 17:52:59 -0700156 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}
Naoki Shiota05721b32014-04-29 14:41:12 -0700162 \$ ${scriptname} stop
Naoki Shiota590c18d2014-03-31 17:52:59 -0700163 Stop all ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700164 \$ ${scriptname} restart
Naoki Shiota590c18d2014-03-31 17:52:59 -0700165 Stop and start currently running ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700166 \$ ${scriptname} status
Naoki Shiota590c18d2014-03-31 17:52:59 -0700167 Show status of ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700168 \$ ${scriptname} {zk|rc-coord|rc-server|core} {start|stop|restart|status}
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700169 Control specific ONOS-related process
170 \$ ${scriptname} rc deldb
171 Delete data in RAMCloud"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700172
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700173 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700174}
175
176function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700177 local logfile=$1
178 local nr_max=${2:-10}
179 if [ -f $logfile ]; then
180 for i in `seq $(expr $nr_max - 1) -1 1`; do
181 if [ -f ${logfile}.${i} ]; then
182 mv -f ${logfile}.${i} ${logfile}.`expr $i + 1`
183 fi
184 done
185 mv $logfile $logfile.1
186 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700187}
188
189# kill-processes {module-name} {array of pids}
190function kill-processes {
191 # Kill the existing processes
192 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700193 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700194 echo -n "Stopping $1 ... "
195 fi
196 for p in ${pids}; do
197 if [ x$p != "x" ]; then
Naoki Shiota4b355762014-05-27 11:46:43 -0700198 # Check if target process is accesible from current user
199 kill -0 $p
200 if [ "$?" -ne 0 ]; then
201 # Error exit code here means "failed to send signal".
202 # Supposedly because of permission error.
203 echo "Failed to kill process (pid: $p)."
204 echo "Check if current user is the same as process owner."
205 exit 1
206 fi
207
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700208 (
209 # Ask process with SIGTERM first, if that did not kill the process
210 # wait 1s and if process still exist, force process to be killed.
211 kill -TERM $p && kill -0 $p && sleep 1 && kill -0 $p && kill -KILL $p
212 ) 2> /dev/null
Naoki Shiota4b355762014-05-27 11:46:43 -0700213
214 # Ensure process is killed.
215 kill -0 $p 2> /dev/null
216 if [ "$?" -ne 0 ]; then
217 # Error exit code here means "process not found", i.e. "kill succeeded".
218 echo "Killed existing process (pid: $p)"
219 else
220 # Process still exists. Some unexpected error occurs.
221 echo "Failed to kill process (pid: $p)."
222 echo "Unexpected error occurs during process termination."
223 exit 1
224 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700225 fi
226 done
227}
228
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700229function handle-error {
230 set -e
231
232 revert-confs
233
234 set +e
235
236 exit 1
237}
238
Naoki Shiota4e928512014-04-03 15:49:35 -0700239# revert-confs [error message]
240function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700241 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700242
Naoki Shiota4e928512014-04-03 15:49:35 -0700243 revert-file `basename ${ZK_CONF}`
244 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700245
246 echo "EXIT"
247
248 if [ ! -z "$1" ]; then
249 echo $1
250 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700251}
252
Naoki Shiota590c18d2014-03-31 17:52:59 -0700253function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700254 echo -n "Creating ${ZK_CONF} ... "
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700255
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700256 # Create the ZooKeeper lib directory
257 if [ ! -d ${ZK_LIB_DIR} ]; then
Naoki Shiota7f495cf2014-05-21 17:23:25 -0700258 local SUDO=${SUDO:-sudo}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700259 local whoami=`whoami`
260 {
261 ${SUDO} mkdir ${ZK_LIB_DIR}
262 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
263 } || {
264 echo "FAILED"
265 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
266 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
267 exit 1
268 }
269 fi
270
271 # creation of ZooKeeper config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700272 local temp_zk=`begin-conf-creation ${ZK_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700273
Naoki Shiota4e928512014-04-03 15:49:35 -0700274 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700275
276 local i=1
277 local myid=
278 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700279 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700280 myid=$i
281 break
282 fi
283 i=`expr $i + 1`
284 done
285
286 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700287 local filename=`basename ${ONOS_CONF}`
Naoki Shiota35f8d5c2014-04-08 11:13:18 -0700288 revert-confs "[ERROR] In ${filename}, zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700289 fi
290
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700291 if [ -f "${ZK_MY_ID}" ]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700292 # sudo will be needed if ZK_MY_ID is already created by other (old) script
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700293 local SUDO=${SUDO:-}
294 {
295 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
296 } || {
297 echo "FAILED"
298 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700299 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700300 exit 1
301 }
302 fi
303
Naoki Shiota9df15d32014-03-27 14:26:20 -0700304 echo ${myid} > ${ZK_MY_ID}
305
306 echo -n "myid is assigned to ${myid} ... "
307
308 while read line; do
309 if [[ $line =~ ^__HOSTS__$ ]]; then
310 i=1
311 for host in ${hostarr}; do
312 # TODO: ports might be configurable
313 local hostline="server.${i}=${host}:2888:3888"
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700314 echo $hostline
Naoki Shiota9df15d32014-03-27 14:26:20 -0700315 i=`expr $i + 1`
316 done
317 elif [[ $line =~ __DATADIR__ ]]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700318 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700319 else
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700320 echo $line
Naoki Shiota9df15d32014-03-27 14:26:20 -0700321 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700322 done < ${ZK_CONF_TEMPLATE} > ${temp_zk}
323
324 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700325
326 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700327}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700328
Naoki Shiota4e928512014-04-03 15:49:35 -0700329function create-hazelcast-conf {
330 echo -n "Creating ${HC_CONF} ... "
331
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700332 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700333
334 # To keep indent of XML file, change IFS
335 local IFS=''
336 while read line; do
337 if [[ $line =~ __HC_NETWORK__ ]]; then
338 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
339 # temporary change
340 IFS=' '
341 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
342 echo '<multicast enabled="false" />'
343 echo '<tcp-ip enabled="true">'
344 for member in ${memberarr}; do
345 echo " <member>${member}</member>"
346 done
347 echo '</tcp-ip>'
348 IFS=''
349 else
350 echo '<multicast enabled="true">'
351 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
352 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
353 echo '</multicast>'
354 echo '<tcp-ip enabled="false" />'
355 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700356 elif [[ $line =~ __HC_PORT__ ]]; then
357 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700358 else
359 echo "${line}"
360 fi
361 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
362
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700363 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700364
365 echo "DONE"
366}
367
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700368function create-ramcloud-conf {
369 echo -n "Creating ${RAMCLOUD_CONF} ... "
370
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700371 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700372
373 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
374
375 # TODO make ZooKeeper address configurable.
376 echo "ramcloud.locator=zk:localhost:2181" > ${temp_rc}
377 echo "ramcloud.clusterName=${rc_cluster_name}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700378
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700379 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700380
381 echo "DONE"
382}
383
Naoki Shiota590c18d2014-03-31 17:52:59 -0700384function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700385 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700386
Naoki Shiota9df15d32014-03-27 14:26:20 -0700387 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700388 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700389
390 sed -e "s|__FILENAME__|${ONOS_LOG}|" \
391 -e "s|__ROLLING_PATTERN__|${ONOS_LOG_ROLLING_PATTERN}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700392
393 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700394
Naoki Shiota9df15d32014-03-27 14:26:20 -0700395 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700396}
397
Naoki Shiota4e928512014-04-03 15:49:35 -0700398function create-confs {
399 local key
400 local filename
401
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700402 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700403
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700404 echo "Config file : ${ONOS_CONF}"
405
Naoki Shiota4e928512014-04-03 15:49:35 -0700406 if [ "$1" == "-f" ]; then
407 create-zk-conf
408 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700409 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700410 create-logback-conf
411 else
412 create-conf-interactive ${ZK_CONF} create-zk-conf
413 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700414 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700415 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
416 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700417
418 trap - ERR
419}
420############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700421
Naoki Shiota590c18d2014-03-31 17:52:59 -0700422
423###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700424function zk {
425 case "$1" in
426 start)
427 start-zk
428 ;;
429 stop)
430 stop-zk
431 ;;
432 stat*) # <- status
433 status-zk
434 ;;
435 re*) # <- restart
436 stop-zk
437 start-zk
438 ;;
439 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700440 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700441 exit 1
442 esac
443}
444
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700445function load-zk-cfg {
446 if [ -f "${ZK_CONF}" ]; then
447 local filename=`basename ${ZK_CONF}`
448 local dirname=`dirname ${ZK_CONF}`
449
450 # Run ZooKeeper with our configuration
451 export ZOOCFG=${filename}
452 export ZOOCFGDIR=${dirname}
453 fi
454}
455
Naoki Shiota4e463182014-03-21 15:13:24 -0700456function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700457 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700458
Naoki Shiota4e928512014-04-03 15:49:35 -0700459 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700460 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700461
462 load-zk-cfg
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700463
464 # log4j.properties is read from classpath if not found in CWD.
465 # Using the ZooKeeper supplied default in ZooKeeper conf dir.
466 # TODO: To explicitly specify our customized log4j config file:
467 # export SERVER_JVMFLAGS="-Dlog4j.configuration=${ZK_LOG4J}"
468 env CLASSPATH="${ZK_HOME}/conf:${CLASSPATH}" ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700469}
470
471function stop-zk {
472 kill-processes "ZooKeeper" `jps -l | grep org.apache.zookeeper.server | awk '{print $1}'`
473}
474
475function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700476 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700477
Naoki Shiota72209722014-04-08 14:32:17 -0700478 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700479}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700480
481function check-zk {
482 # assumption here is that ZK status script is the last command in status-zk.
483 status-zk &> /dev/null
484 local zk_status=$?
485 if [ "$zk_status" -ne 0 ]; then
486 return 1;
487 fi
488 return 0
489}
490
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700491function check-and-start-zk {
492 check-zk
493 local zk_status=$?
494 if [ "$zk_status" -ne 0 ]; then
495 start-zk
496 fi
497}
498
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700499# wait-zk-or-die {timeout-sec}
500function wait-zk-or-die {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700501 local retries=${1:-5}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700502 # do-while retries >= 0
503 while true; do
504 check-zk
505 local zk_status=$?
506 if [ "$zk_status" -eq 0 ]; then
507 return 0
508 fi
509 sleep 1;
510 ((retries -= 1))
511 (( retries >= 0 )) || break
512 done
513 echo "ZooKeeper is not running."
514 exit 1
515}
516
Naoki Shiota590c18d2014-03-31 17:52:59 -0700517############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700518
519
Naoki Shiota590c18d2014-03-31 17:52:59 -0700520####### Functions related to RAMCloud ######
Naoki Shiota9df15d32014-03-27 14:26:20 -0700521function start-backend {
522 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700523 if [ $1 == "coord" ]; then
524 rc-coord startifdown
525 elif [ $1 == "server" ]; then
526 rc-server startifdown
527 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700528 fi
529}
530
531function stop-backend {
532 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
533 if [ $rcsn != 0 ]; then
534 rc-server stop
535 fi
536
Naoki Shiota590c18d2014-03-31 17:52:59 -0700537 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700538 if [ $rccn != 0 ]; then
539 rc-coord stop
540 fi
541}
542
Naoki Shiota590c18d2014-03-31 17:52:59 -0700543
Naoki Shiota4e463182014-03-21 15:13:24 -0700544### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700545function rc-coord-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700546 local host=${RC_COORD_IP}
547 if [ -z "${host}" ]; then
548 # falling back to 0.0.0.0
549 host="0.0.0.0"
550 fi
551 echo "${RC_COORD_PROTOCOL}:host=${host},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700552}
553
554function rc-server-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700555 local host=${RC_SERVER_IP}
556 if [ -z "${host}" ]; then
557 # falling back to 0.0.0.0
558 host="0.0.0.0"
559 fi
560 echo "${RC_SERVER_PROTOCOL}:host=${host},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700561}
562
Naoki Shiota4e463182014-03-21 15:13:24 -0700563function rc-coord {
564 case "$1" in
565 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700566 stop-coord
567 start-coord
568 ;;
569 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700570 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700571 if [ $n == 0 ]; then
572 start-coord
573 else
574 echo "$n instance of RAMCloud coordinator running"
575 fi
576 ;;
577 stop)
578 stop-coord
579 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700580 deldb)
581 stop-backend
582 del-coord-info
583 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700584 stat*) # <- status
585 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
586 echo "$n RAMCloud coordinator running"
587 ;;
588 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700589 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700590 exit 1
591 esac
592}
593
594function start-coord {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700595 check-and-start-zk
596 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700597
Naoki Shiota4e463182014-03-21 15:13:24 -0700598 if [ ! -d ${LOGDIR} ]; then
599 mkdir -p ${LOGDIR}
600 fi
601 if [ -f $RAMCLOUD_COORD_LOG ]; then
602 rotate-log $RAMCLOUD_COORD_LOG
603 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700604
605 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700606
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700607 # TODO Configuration for ZK address, port
608 local zk_addr="localhost:2181"
609 # RAMCloud cluster name
610 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700611 # RAMCloud transport timeout
612 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700613 # RAMCloud option deadServerTimeout
614 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
615 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
616
617 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
618 # but actual RAMCloud code only uses -C argument now.
619 # (FYI: -C is documented to be deprecated in the document)
620
621 local coord_args="-C ${coord_addr}"
622 coord_args="${coord_args} --externalStorage zk:${zk_addr}"
623 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700624 coord_args="${coord_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700625 coord_args="${coord_args} --deadServerTimeout ${rc_coord_deadServerTimeout}"
626
627 # Read environment variables if set
628 coord_args="${coord_args} ${RC_COORDINATOR_OPTS}"
629
630 if [ "${ONOS_HOST_ROLE}" == "single-node" ]; then
631 # Note: Following reset is required, since RC restart is considered node failure,
632 # and tries recovery of server, which will never succeed after restart.
633 echo "Role configured to single-node mode. RAMCloud cluster will be reset on each start-up."
634 coord_args="${coord_args} --reset"
635 fi
636
Naoki Shiota4e463182014-03-21 15:13:24 -0700637 # Run ramcloud
638 echo -n "Starting RAMCloud coordinator ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700639 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700640 echo "STARTED"
641}
642
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700643function del-coord-info {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700644 check-and-start-zk
645 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700646
647 if [ ! -d ${LOGDIR} ]; then
648 mkdir -p ${LOGDIR}
649 fi
650 if [ -f $RAMCLOUD_COORD_LOG ]; then
651 rotate-log $RAMCLOUD_COORD_LOG
652 fi
653
654 local coord_addr=`rc-coord-addr`
655
656 # TODO Configuration for ZK address, port
657 local zk_addr="localhost:2181"
658 # RAMCloud cluster name
659 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
660 # RAMCloud option deadServerTimeout
661 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
662 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
663
664 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
665 # but actual RAMCloud code only uses -C argument now.
666 # (FYI: -C is documented to be deprecated in the document)
667
668 local coord_args="-C ${coord_addr}"
669 coord_args="${coord_args} --externalStorage zk:${zk_addr}"
670 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
671
672 # Note: --reset will reset ZK stored info and start running as acoordinator.
673 echo -n "Deleting RAMCloud cluster coordination info ... "
674 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} --reset &> $RAMCLOUD_COORD_LOG &
675
Yuta HIGUCHIcd44eb32014-05-24 16:51:31 -0700676 # TODO Assuming 5 sec is enough. To be sure monitor log?
677 sleep 5
678 # kill coordinator
Yuta HIGUCHIce9f3ee2014-05-14 09:48:40 -0700679 (pkill -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator &> /dev/null)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700680
681 echo "DONE"
682}
Naoki Shiota4e463182014-03-21 15:13:24 -0700683
684function stop-coord {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700685 kill-processes "RAMCloud coordinator" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700686}
687
Naoki Shiota4e463182014-03-21 15:13:24 -0700688### Functions related to RAMCloud server
689function rc-server {
690 case "$1" in
691 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700692 stop-server
693 start-server
694 ;;
695 startifdown)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700696 local n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700697 if [ $n == 0 ]; then
698 start-server
699 else
700 echo "$n instance of RAMCloud server running"
701 fi
702 ;;
703 stop)
704 stop-server
705 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700706 deldb)
707 stop-server
708 del-server-backup
709 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700710 stat*) # <- status
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700711 n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700712 echo "$n RAMCloud server running"
713 ;;
714 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700715 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700716 exit 1
717 esac
718}
719
720function start-server {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700721 check-and-start-zk
722 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700723
Naoki Shiota4e463182014-03-21 15:13:24 -0700724 if [ ! -d ${LOGDIR} ]; then
725 mkdir -p ${LOGDIR}
726 fi
727 if [ -f $RAMCLOUD_SERVER_LOG ]; then
728 rotate-log $RAMCLOUD_SERVER_LOG
729 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700730
731 local coord_addr=`rc-coord-addr`
732 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700733
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700734 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.server.masterServiceThreads 5)
735 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.server.logCleanerThreads 1)
Yuta HIGUCHI52efcbb2014-05-16 09:30:33 -0700736 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.server.detectFailures 0)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700737
738 # TODO Configuration for ZK address, port
739 local zk_addr="localhost:2181"
740 # RAMCloud cluster name
741 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName "ONOS-RC")
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700742 # RAMCloud transport timeout
743 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700744 # replication factor (-r) config
745 local rc_replicas=$(read-conf ${ONOS_CONF} ramcloud.server.replicas 0)
746 # backup file path (-f) config
747 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
748 mkdir -p `dirname ${rc_datafile}`
749
750 local server_args="-L ${server_addr}"
751 server_args="${server_args} --externalStorage zk:${zk_addr}"
752 server_args="${server_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700753 server_args="${server_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700754 server_args="${server_args} --masterServiceThreads ${masterServiceThreads}"
755 server_args="${server_args} --logCleanerThreads ${logCleanerThreads}"
756 server_args="${server_args} --detectFailures ${detectFailures}"
757 server_args="${server_args} --replicas ${rc_replicas}"
758 server_args="${server_args} --file ${rc_datafile}"
759
760 # Read environment variables if set
761 server_args="${server_args} ${RC_SERVER_OPTS}"
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700762
Naoki Shiota4e463182014-03-21 15:13:24 -0700763 # Run ramcloud
764 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700765 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server ${server_args} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700766 echo "STARTED"
767}
768
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700769function del-server-backup {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700770 echo -n "Delete RAMCloud backup server data [y/N]? "
771 while [ 1 ]; do
772 read key
773 if [ "${key}" == "Y" -o "${key}" == "y" ]; then
774 break
775 elif [ -z "${key}" -o "${key}" == "N" -o "${key}" == "n" ]; then
776 echo "Cancelled."
777 return
778 fi
779 echo "[y/N]? "
780 done
781
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700782 echo -n "Removing RAMCloud backup server data ... "
783 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
784 rm -f ${rc_datafile}
785 echo "DONE"
786}
787
Naoki Shiota4e463182014-03-21 15:13:24 -0700788function stop-server {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700789 kill-processes "RAMCloud server" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700790}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700791############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700792
793
Naoki Shiota590c18d2014-03-31 17:52:59 -0700794## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700795function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700796 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700797 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700798 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700799 ${MVN} -f ${ONOS_HOME}/pom.xml compile
800 fi
801 JAVA_CP=`cat ${CPFILE}`
802 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
803
804 case "$1" in
805 start)
806 stop-onos
807 start-onos
808 ;;
809 startnokill)
810 start-onos
811 ;;
812 startifdown)
813 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
814 if [ $n == 0 ]; then
815 start-onos
816 else
817 echo "$n instance of onos running"
818 fi
819 ;;
820 stop)
821 stop-onos
822 ;;
823 stat*) # <- status
824 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
825 echo "$n instance of onos running"
826 ;;
827 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700828 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700829 exit 1
830 esac
831}
832
833function start-onos {
834 if [ ! -d ${LOGDIR} ]; then
835 mkdir -p ${LOGDIR}
836 fi
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700837 # Rotate log files
Naoki Shiota4e463182014-03-21 15:13:24 -0700838 for log in ${LOGS}; do
839 if [ -f ${log} ]; then
840 rotate-log ${log}
841 fi
842 done
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700843
844 # Rotate logs rolled at runtime.
845 local rolled_log_shellpat=`echo ${ONOS_LOG_ROLLING_PATTERN} | sed -e "s/%i/\\*/"`
846 for rolled_log in ${rolled_log_shellpat}; do
847 if [ -f ${rolled_log} ]; then
848 rotate-log ${rolled_log}
849 # NOTE: renamed file will end up with an extension like .log.gz.1
850 fi
851 done
852
Naoki Shiota4e463182014-03-21 15:13:24 -0700853 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700854 echo "[WARNING] ${ONOS_LOGBACK} not found."
855 echo " Run \"\$ $0 setup\" to create."
856 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700857 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700858
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700859 if [ ! -f ${HC_CONF} ]; then
860 echo "[WARNING] ${HC_CONF} not found."
861 echo " Run \"\$ $0 setup\" to create."
862 exit 1
863 fi
864
865 # specify hazelcast.xml to datagrid
866 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
867
868 # specify backend config
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700869 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700870 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
871 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700872 elif [ "${ONOS_HOST_BACKEND}" = "hazelcast" ]; then
873 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.hazelcast.baseConfig=${HC_CONF}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700874 fi
875
Naoki Shiota9df15d32014-03-27 14:26:20 -0700876 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -0700877
878 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
879 cd ${ONOS_HOME}
880
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700881 echo -n "Starting ONOS controller ... "
Naoki Shiota4e463182014-03-21 15:13:24 -0700882 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
883
884 # We need to wait a bit to find out whether starting the ONOS process succeeded
885 sleep 1
886
887 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700888 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700889 echo " STARTED"
890 else
891 echo " FAILED"
892 fi
893
894# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
895# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
896}
897
898function stop-onos {
899 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
900# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
901}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700902############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700903
904
Naoki Shiota590c18d2014-03-31 17:52:59 -0700905################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -0700906case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -0700907 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -0700908 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700909 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700910 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -0700911 mode_parameter=${ONOS_HOST_ROLE}
912 if [ ! -z "$2" ]; then
913 mode_parameter=$2
914 fi
915
916 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -0700917 single-node)
918 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700919 start-backend coord
920 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700921 onos startifdown
922 ;;
923 coord-node)
924 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700925 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -0700926 onos startifdown
927 ;;
928 server-node)
929 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700930 start-backend server
931 onos startifdown
932 ;;
933 coord-and-server-node)
934 zk start
935 start-backend coord
936 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700937 onos startifdown
938 ;;
939 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700940 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700941 ;;
942 esac
943 echo
944 ;;
945 stop)
946 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
947 if [ $on != 0 ]; then
948 onos stop
949 fi
950
Naoki Shiota9df15d32014-03-27 14:26:20 -0700951 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -0700952
953 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
954 if [ $zkn != 0 ]; then
955 zk stop
956 fi
957 echo
958 ;;
959 restart)
960 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
961 if [ $on != 0 ]; then
962 onos stop
963 fi
964
965 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
966 if [ $rcsn != 0 ]; then
967 rc-server stop
968 fi
969
Naoki Shiota590c18d2014-03-31 17:52:59 -0700970 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700971 if [ $rccn != 0 ]; then
972 rc-coord stop
973 fi
974
975 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
976 if [ $zkn != 0 ]; then
977 zk restart
978 fi
979
980 if [ $rccn != 0 ]; then
981 rc-coord startifdown
982 fi
983
984 if [ $rcsn != 0 ]; then
985 rc-server startifdown
986 fi
987
988 if [ $on != 0 ]; then
989 onos startifdown
990 fi
991 echo
992 ;;
993 stat*) # <- status
994 echo '[ZooKeeper]'
995 zk status
996 echo
997 echo '[RAMCloud coordinator]'
998 rc-coord status
999 echo
1000 echo '[RAMCloud server]'
1001 rc-server status
1002 echo
1003 echo '[ONOS core]'
1004 onos status
1005 echo
1006 ;;
1007 zk)
1008 zk $2
1009 ;;
1010 rc-c*) # <- rc-coordinator
1011 rc-coord $2
1012 ;;
1013 rc-s*) # <- rc-server
1014 rc-server $2
1015 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001016 rc)
Naoki Shiota9109a1e2014-05-13 11:11:01 -07001017 # TODO make deldb command more organized (clarify when it can work)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001018 rc-coord $2
1019 rc-server $2
1020 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001021 core)
1022 onos $2
1023 ;;
1024 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001025 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001026 exit 1
1027esac
Naoki Shiota590c18d2014-03-31 17:52:59 -07001028