blob: 30a7d8aaf5e81308610a144e294134ff704ee257 [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)
Yuta HIGUCHIfb1905a2014-06-09 14:07:34 -070013# $ZK_HOME : path of root directory of ZooKeeper (~/zookeeper-3.4.6)
Naoki Shiota72209722014-04-08 14:32:17 -070014# $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 ###
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -070040ONOS_CLUSTER_NAME=$(read-conf ${ONOS_CONF} onos.cluster.name "onos")
Naoki Shiota4e928512014-04-03 15:49:35 -070041ONOS_HOST_NAME=$(read-conf ${ONOS_CONF} host.name `hostname`)
42ONOS_HOST_IP=$(read-conf ${ONOS_CONF} host.ip)
43ONOS_HOST_ROLE=$(read-conf ${ONOS_CONF} host.role)
44ONOS_HOST_BACKEND=$(read-conf ${ONOS_CONF} host.backend)
45ZK_HOSTS=$(read-conf ${ONOS_CONF} zookeeper.hosts ${ONOS_HOST_NAME})
46RC_COORD_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.coordinator.protocol "fast+udp")
47RC_COORD_IP=$(read-conf ${ONOS_CONF} ramcloud.coordinator.ip ${ONOS_HOST_IP})
48RC_COORD_PORT=$(read-conf ${ONOS_CONF} ramcloud.coordinator.port 12246)
49RC_SERVER_PROTOCOL=$(read-conf ${ONOS_CONF} ramcloud.server.protocol "fast+udp")
50RC_SERVER_IP=$(read-conf ${ONOS_CONF} ramcloud.server.ip ${ONOS_HOST_IP})
51RC_SERVER_PORT=$(read-conf ${ONOS_CONF} ramcloud.server.port 12242)
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070052HC_HOST_PORT=$(read-conf ${ONOS_CONF} hazelcast.host.port 5701)
Naoki Shiota4e928512014-04-03 15:49:35 -070053HC_TCPIP_MEMBERS=$(read-conf ${ONOS_CONF} hazelcast.tcp-ip.members)
54HC_MULTICAST_GROUP=$(read-conf ${ONOS_CONF} hazelcast.multicast.group "224.2.2.3")
55HC_MULTICAST_PORT=$(read-conf ${ONOS_CONF} hazelcast.multicast.port 54327)
Naoki Shiota590c18d2014-03-31 17:52:59 -070056############################################
57
58
59############## Other variables #############
Naoki Shiota4e928512014-04-03 15:49:35 -070060ONOS_TEMPLATE_DIR=${ONOS_CONF_DIR}/template
61
Naoki Shiota4e463182014-03-21 15:13:24 -070062LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
63
Yuta HIGUCHIfb1905a2014-06-09 14:07:34 -070064ZK_HOME=${ZK_HOME:-~/zookeeper-3.4.6}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070065ZK_CONF=${ZK_CONF:-${ONOS_CONF_DIR}/zoo.cfg}
Naoki Shiota4e928512014-04-03 15:49:35 -070066ZK_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/zoo.cfg.template
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070067# Adding ONOS_HOST_NAME dir since file name (zookeeper.out) cannot be controlled.
Naoki Shiotad8ea71a2014-04-23 17:04:51 -070068ZK_LOG_DIR=${ZK_LOG_DIR:-${ONOS_HOME}/onos-logs/zk-${ONOS_HOST_NAME}}
Naoki Shiota72209722014-04-08 14:32:17 -070069ZK_LIB_DIR=${ZK_LIB_DIR:-/var/lib/zookeeper}
Naoki Shiota9df15d32014-03-27 14:26:20 -070070ZK_MY_ID=${ZK_LIB_DIR}/myid
Naoki Shiota4e463182014-03-21 15:13:24 -070071
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070072HC_CONF=${HC_CONF:-${ONOS_CONF_DIR}/hazelcast.xml}
Naoki Shiota4e928512014-04-03 15:49:35 -070073HC_CONF_TEMPLATE=${ONOS_TEMPLATE_DIR}/hazelcast.xml.template
74
Naoki Shiota4e463182014-03-21 15:13:24 -070075RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud}
Naoki Shiota590c18d2014-03-31 17:52:59 -070076RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.${ONOS_HOST_NAME}.log
77RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.${ONOS_HOST_NAME}.log
Naoki Shiota4e463182014-03-21 15:13:24 -070078RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -070079RAMCLOUD_CONF=${RAMCLOUD_CONF:-${ONOS_CONF_DIR}/ramcloud.conf}
Naoki Shiota4e463182014-03-21 15:13:24 -070080
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -070081export LD_LIBRARY_PATH=${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}:$LD_LIBRARY_PATH
Naoki Shiota4e463182014-03-21 15:13:24 -070082
83## 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 -070084ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_CONF_DIR}/logback.${ONOS_HOST_NAME}.xml}
Naoki Shiota9df15d32014-03-27 14:26:20 -070085ONOS_LOGBACK_BACKUP=${ONOS_LOGBACK}.bak
Naoki Shiota4e928512014-04-03 15:49:35 -070086ONOS_LOGBACK_TEMPLATE=${ONOS_TEMPLATE_DIR}/logback.xml.template
Naoki Shiota4e463182014-03-21 15:13:24 -070087LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs}
Naoki Shiota590c18d2014-03-31 17:52:59 -070088LOGBASE=${ONOS_LOGBASE:-onos.${ONOS_HOST_NAME}}
Naoki Shiota4e463182014-03-21 15:13:24 -070089ONOS_LOG="${LOGDIR}/${LOGBASE}.log"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070090ONOS_LOG_ROLLING_PATTERN="${LOGDIR}/${LOGBASE}.%i.log.gz"
91ONOS_STDOUT_LOG="${LOGDIR}/${LOGBASE}.stdout"
92ONOS_STDERR_LOG="${LOGDIR}/${LOGBASE}.stderr"
Naoki Shiota4e463182014-03-21 15:13:24 -070093PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap"
Yuta HIGUCHI04713972014-05-30 11:01:17 -070094LOGS="$ONOS_LOG $ONOS_STDOUT_LOG $ONOS_STDERR_LOG $PCAP_LOG"
Naoki Shiota4e463182014-03-21 15:13:24 -070095
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 {
Naoki Shiota05721b32014-04-29 14:41:12 -0700145 local scriptname=`basename $0`
Naoki Shiota590c18d2014-03-31 17:52:59 -0700146 local filename=`basename ${ONOS_CONF}`
147 local usage="Usage: setup/start/stop ONOS on this server.
Naoki Shiota05721b32014-04-29 14:41:12 -0700148 \$ ${scriptname} setup [-f]
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700149 Set up ONOS node using ${ONOS_CONF} .
Naoki Shiota590c18d2014-03-31 17:52:59 -0700150 - generate and replace config file of ZooKeeper.
151 - create myid in ZooKeeper datadir.
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700152 - generate and replace config file for Hazelcast.
153 - generate and replace config file for RAMCloud.
Naoki Shiota590c18d2014-03-31 17:52:59 -0700154 - generate and replace logback.${ONOS_HOST_NAME}.xml
155 If -f option is used, all existing files will be overwritten without confirmation.
Naoki Shiota05721b32014-04-29 14:41:12 -0700156 \$ ${scriptname} start [single-node|coord-node|server-node|coord-and-server-node]
Naoki Shiota590c18d2014-03-31 17:52:59 -0700157 Start ONOS node with specific RAMCloud entities
158 - single-node: start ONOS with stand-alone RAMCloud
159 - coord-node : start ONOS with RAMCloud coordinator
160 - server-node: start ONOS with RAMCloud server
161 - coord-and-server-node: start ONOS with RAMCloud coordinator and server
162 * Default behavior can be defined by ${filename}
Naoki Shiota05721b32014-04-29 14:41:12 -0700163 \$ ${scriptname} stop
Naoki Shiota590c18d2014-03-31 17:52:59 -0700164 Stop all ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700165 \$ ${scriptname} restart
Naoki Shiota590c18d2014-03-31 17:52:59 -0700166 Stop and start currently running ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700167 \$ ${scriptname} status
Naoki Shiota590c18d2014-03-31 17:52:59 -0700168 Show status of ONOS-related processes
Naoki Shiota05721b32014-04-29 14:41:12 -0700169 \$ ${scriptname} {zk|rc-coord|rc-server|core} {start|stop|restart|status}
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700170 Control specific ONOS-related process
171 \$ ${scriptname} rc deldb
172 Delete data in RAMCloud"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700173
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700174 echo "${usage}"
Naoki Shiota4e463182014-03-21 15:13:24 -0700175}
176
177function rotate-log {
Naoki Shiota4e928512014-04-03 15:49:35 -0700178 local logfile=$1
179 local nr_max=${2:-10}
180 if [ -f $logfile ]; then
181 for i in `seq $(expr $nr_max - 1) -1 1`; do
182 if [ -f ${logfile}.${i} ]; then
183 mv -f ${logfile}.${i} ${logfile}.`expr $i + 1`
184 fi
185 done
186 mv $logfile $logfile.1
187 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700188}
189
190# kill-processes {module-name} {array of pids}
191function kill-processes {
192 # Kill the existing processes
193 local pids=$2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700194 if [ ! -z "$pids" ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700195 echo -n "Stopping $1 ... "
196 fi
197 for p in ${pids}; do
198 if [ x$p != "x" ]; then
Naoki Shiota4b355762014-05-27 11:46:43 -0700199 # Check if target process is accesible from current user
200 kill -0 $p
201 if [ "$?" -ne 0 ]; then
202 # Error exit code here means "failed to send signal".
203 # Supposedly because of permission error.
204 echo "Failed to kill process (pid: $p)."
205 echo "Check if current user is the same as process owner."
206 exit 1
207 fi
208
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700209 (
210 # Ask process with SIGTERM first, if that did not kill the process
211 # wait 1s and if process still exist, force process to be killed.
212 kill -TERM $p && kill -0 $p && sleep 1 && kill -0 $p && kill -KILL $p
213 ) 2> /dev/null
Naoki Shiota4b355762014-05-27 11:46:43 -0700214
215 # Ensure process is killed.
216 kill -0 $p 2> /dev/null
217 if [ "$?" -ne 0 ]; then
218 # Error exit code here means "process not found", i.e. "kill succeeded".
219 echo "Killed existing process (pid: $p)"
220 else
221 # Process still exists. Some unexpected error occurs.
222 echo "Failed to kill process (pid: $p)."
223 echo "Unexpected error occurs during process termination."
224 exit 1
225 fi
Naoki Shiota4e463182014-03-21 15:13:24 -0700226 fi
227 done
228}
229
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700230function handle-error {
231 set -e
232
233 revert-confs
234
235 set +e
236
237 exit 1
238}
239
Naoki Shiota4e928512014-04-03 15:49:35 -0700240# revert-confs [error message]
241function revert-confs {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700242 echo -n "ERROR occurred ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700243
Naoki Shiota4e928512014-04-03 15:49:35 -0700244 revert-file `basename ${ZK_CONF}`
245 revert-file `basename ${HC_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700246
247 echo "EXIT"
248
249 if [ ! -z "$1" ]; then
250 echo $1
251 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700252}
253
Naoki Shiota590c18d2014-03-31 17:52:59 -0700254function create-zk-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700255 echo -n "Creating ${ZK_CONF} ... "
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700256
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700257 # Create the ZooKeeper lib directory
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700258 if [[ ! ( -w ${ZK_LIB_DIR} && -d ${ZK_LIB_DIR} ) ]]; then
Naoki Shiota7f495cf2014-05-21 17:23:25 -0700259 local SUDO=${SUDO:-sudo}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700260 local whoami=`whoami`
261 {
Yuta HIGUCHIb3983b12014-06-12 14:21:09 -0700262 ${SUDO} mkdir -p ${ZK_LIB_DIR}
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700263 ${SUDO} chown ${whoami} ${ZK_LIB_DIR}
264 } || {
265 echo "FAILED"
266 echo "[ERROR] Failed to create directory ${ZK_LIB_DIR}."
267 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
268 exit 1
269 }
270 fi
271
272 # creation of ZooKeeper config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700273 local temp_zk=`begin-conf-creation ${ZK_CONF}`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700274
Naoki Shiota4e928512014-04-03 15:49:35 -0700275 hostarr=`echo ${ZK_HOSTS} | tr "," " "`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700276
277 local i=1
278 local myid=
279 for host in ${hostarr}; do
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700280 if [ "${host}" = "${ONOS_HOST_NAME}" -o "${host}" = "${ONOS_HOST_IP}" ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700281 myid=$i
282 break
283 fi
284 i=`expr $i + 1`
285 done
286
287 if [ -z "${myid}" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700288 local filename=`basename ${ONOS_CONF}`
Naoki Shiota35f8d5c2014-04-08 11:13:18 -0700289 revert-confs "[ERROR] In ${filename}, zookeeper.hosts must have hostname \"${ONOS_HOST_NAME}\" or IP address"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700290 fi
291
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700292 if [ -f "${ZK_MY_ID}" ]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700293 # sudo will be needed if ZK_MY_ID is already created by other (old) script
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700294 local SUDO=${SUDO:-}
295 {
296 ${SUDO} mv -f ${ZK_MY_ID} ${ZK_MY_ID}.old
297 } || {
298 echo "FAILED"
299 echo "[ERROR] Failed to rename ${ZK_MY_ID}."
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700300 echo "[ERROR] Please retry after setting \"env SUDO=sudo\""
Yuta HIGUCHI69450892014-04-16 09:10:55 -0700301 exit 1
302 }
303 fi
304
Naoki Shiota9df15d32014-03-27 14:26:20 -0700305 echo ${myid} > ${ZK_MY_ID}
306
307 echo -n "myid is assigned to ${myid} ... "
308
309 while read line; do
310 if [[ $line =~ ^__HOSTS__$ ]]; then
311 i=1
312 for host in ${hostarr}; do
313 # TODO: ports might be configurable
314 local hostline="server.${i}=${host}:2888:3888"
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700315 echo $hostline
Naoki Shiota9df15d32014-03-27 14:26:20 -0700316 i=`expr $i + 1`
317 done
318 elif [[ $line =~ __DATADIR__ ]]; then
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700319 echo $line | sed -e "s|__DATADIR__|${ZK_LIB_DIR}|"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700320 else
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700321 echo $line
Naoki Shiota9df15d32014-03-27 14:26:20 -0700322 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700323 done < ${ZK_CONF_TEMPLATE} > ${temp_zk}
324
325 end-conf-creation ${ZK_CONF}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700326
327 echo "DONE"
Naoki Shiota590c18d2014-03-31 17:52:59 -0700328}
Naoki Shiota9df15d32014-03-27 14:26:20 -0700329
Naoki Shiota4e928512014-04-03 15:49:35 -0700330function create-hazelcast-conf {
331 echo -n "Creating ${HC_CONF} ... "
332
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700333 local temp_hc=`begin-conf-creation ${HC_CONF}`
Naoki Shiota4e928512014-04-03 15:49:35 -0700334
335 # To keep indent of XML file, change IFS
336 local IFS=''
337 while read line; do
338 if [[ $line =~ __HC_NETWORK__ ]]; then
339 if [ ! -z "${HC_TCPIP_MEMBERS}" ]; then
340 # temporary change
341 IFS=' '
342 local memberarr=`echo ${HC_TCPIP_MEMBERS} | tr "," " "`
343 echo '<multicast enabled="false" />'
344 echo '<tcp-ip enabled="true">'
345 for member in ${memberarr}; do
346 echo " <member>${member}</member>"
347 done
348 echo '</tcp-ip>'
349 IFS=''
350 else
351 echo '<multicast enabled="true">'
352 echo " <multicast-group>${HC_MULTICAST_GROUP}</multicast-group>"
353 echo " <multicast-port>${HC_MULTICAST_PORT}</multicast-port>"
354 echo '</multicast>'
355 echo '<tcp-ip enabled="false" />'
356 fi
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700357 elif [[ $line =~ __HC_PORT__ ]]; then
358 echo $line | sed -e "s|__HC_PORT__|${HC_HOST_PORT}|"
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700359 elif [[ $line =~ __HC_CLUSTER__ ]]; then
360 echo $line | sed -e "s|__HC_CLUSTER__|${ONOS_CLUSTER_NAME}|"
Naoki Shiota4e928512014-04-03 15:49:35 -0700361 else
362 echo "${line}"
363 fi
364 done < ${HC_CONF_TEMPLATE} > ${temp_hc}
365
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700366 end-conf-creation ${HC_CONF}
Naoki Shiota4e928512014-04-03 15:49:35 -0700367
368 echo "DONE"
369}
370
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700371function create-ramcloud-conf {
372 echo -n "Creating ${RAMCLOUD_CONF} ... "
373
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700374 local temp_rc=`begin-conf-creation ${RAMCLOUD_CONF}`
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700375
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700376 local rc_cluster_name=$(read-conf ${ONOS_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700377
378 # TODO make ZooKeeper address configurable.
379 echo "ramcloud.locator=zk:localhost:2181" > ${temp_rc}
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700380 echo "#ramcloud.locator=zk:localhost:2181,otherhost:2181" >> ${temp_rc}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700381 echo "ramcloud.clusterName=${rc_cluster_name}" >> ${temp_rc}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700382
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700383 end-conf-creation ${RAMCLOUD_CONF}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700384
385 echo "DONE"
386}
387
Naoki Shiota590c18d2014-03-31 17:52:59 -0700388function create-logback-conf {
Naoki Shiota9df15d32014-03-27 14:26:20 -0700389 echo -n "Creating ${ONOS_LOGBACK} ... "
Naoki Shiota590c18d2014-03-31 17:52:59 -0700390
Naoki Shiota9df15d32014-03-27 14:26:20 -0700391 # creation of logback config
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700392 local temp_lb=`begin-conf-creation ${ONOS_LOGBACK}`
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700393
394 sed -e "s|__FILENAME__|${ONOS_LOG}|" \
395 -e "s|__ROLLING_PATTERN__|${ONOS_LOG_ROLLING_PATTERN}|" ${ONOS_LOGBACK_TEMPLATE} > ${temp_lb}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700396
397 end-conf-creation ${ONOS_LOGBACK}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700398
Naoki Shiota9df15d32014-03-27 14:26:20 -0700399 echo "DONE"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700400}
401
Naoki Shiota4e928512014-04-03 15:49:35 -0700402function create-confs {
403 local key
404 local filename
405
Naoki Shiota9a1e6d12014-04-03 14:47:12 -0700406 trap handle-error ERR
Naoki Shiota4e928512014-04-03 15:49:35 -0700407
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700408 echo "Config file : ${ONOS_CONF}"
409
Naoki Shiota4e928512014-04-03 15:49:35 -0700410 if [ "$1" == "-f" ]; then
411 create-zk-conf
412 create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700413 create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700414 create-logback-conf
415 else
416 create-conf-interactive ${ZK_CONF} create-zk-conf
417 create-conf-interactive ${HC_CONF} create-hazelcast-conf
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700418 create-conf-interactive ${RAMCLOUD_CONF} create-ramcloud-conf
Naoki Shiota4e928512014-04-03 15:49:35 -0700419 create-conf-interactive ${ONOS_LOGBACK} create-logback-conf
420 fi
Naoki Shiota590c18d2014-03-31 17:52:59 -0700421
422 trap - ERR
423}
424############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700425
Naoki Shiota590c18d2014-03-31 17:52:59 -0700426
427###### Functions related to ZooKeeper ######
Naoki Shiota4e463182014-03-21 15:13:24 -0700428function zk {
429 case "$1" in
430 start)
431 start-zk
432 ;;
433 stop)
434 stop-zk
435 ;;
436 stat*) # <- status
437 status-zk
438 ;;
439 re*) # <- restart
440 stop-zk
441 start-zk
442 ;;
443 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700444 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700445 exit 1
446 esac
447}
448
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700449function load-zk-cfg {
450 if [ -f "${ZK_CONF}" ]; then
451 local filename=`basename ${ZK_CONF}`
452 local dirname=`dirname ${ZK_CONF}`
453
454 # Run ZooKeeper with our configuration
455 export ZOOCFG=${filename}
456 export ZOOCFGDIR=${dirname}
457 fi
458}
459
Naoki Shiota4e463182014-03-21 15:13:24 -0700460function start-zk {
Pavlin Radoslavovd8f30d92014-04-18 13:03:09 -0700461 echo -n "Starting ZooKeeper ... "
Naoki Shiota9df15d32014-03-27 14:26:20 -0700462
Naoki Shiota4e928512014-04-03 15:49:35 -0700463 export ZOO_LOG_DIR=${ZK_LOG_DIR}
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700464 mkdir -p ${ZK_LOG_DIR}
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700465
466 load-zk-cfg
Yuta HIGUCHIf66cf212014-05-29 23:16:44 -0700467
468 # log4j.properties is read from classpath if not found in CWD.
469 # Using the ZooKeeper supplied default in ZooKeeper conf dir.
470 # TODO: To explicitly specify our customized log4j config file:
471 # export SERVER_JVMFLAGS="-Dlog4j.configuration=${ZK_LOG4J}"
472 env CLASSPATH="${ZK_HOME}/conf:${CLASSPATH}" ${ZK_HOME}/bin/zkServer.sh start
Naoki Shiota4e463182014-03-21 15:13:24 -0700473}
474
475function stop-zk {
Yuta HIGUCHI7c708362014-06-02 23:15:13 -0700476 echo -n "Stopping ZooKeeper ... "
477 load-zk-cfg
478
479 ${ZK_HOME}/bin/zkServer.sh stop
Naoki Shiota4e463182014-03-21 15:13:24 -0700480}
481
482function status-zk {
Naoki Shiotab7eb55d2014-04-21 18:21:36 -0700483 load-zk-cfg
Naoki Shiota9df15d32014-03-27 14:26:20 -0700484
Naoki Shiota72209722014-04-08 14:32:17 -0700485 ${ZK_HOME}/bin/zkServer.sh status
Naoki Shiota4e463182014-03-21 15:13:24 -0700486}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700487
488function check-zk {
489 # assumption here is that ZK status script is the last command in status-zk.
490 status-zk &> /dev/null
491 local zk_status=$?
492 if [ "$zk_status" -ne 0 ]; then
493 return 1;
494 fi
495 return 0
496}
497
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700498function check-and-start-zk {
499 check-zk
500 local zk_status=$?
501 if [ "$zk_status" -ne 0 ]; then
502 start-zk
503 fi
504}
505
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700506# wait-zk-or-die {timeout-sec}
507function wait-zk-or-die {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700508 local retries=${1:-5}
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700509 # do-while retries >= 0
510 while true; do
511 check-zk
512 local zk_status=$?
513 if [ "$zk_status" -eq 0 ]; then
514 return 0
515 fi
516 sleep 1;
517 ((retries -= 1))
518 (( retries >= 0 )) || break
519 done
520 echo "ZooKeeper is not running."
521 exit 1
522}
523
Naoki Shiota590c18d2014-03-31 17:52:59 -0700524############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700525
526
Naoki Shiota590c18d2014-03-31 17:52:59 -0700527####### Functions related to RAMCloud ######
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -0700528
529# Check if this host should be running RAMCloud coordinator:
530function is-coord-role {
531 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
532 case "${ONOS_HOST_ROLE}" in
533 single-node)
534 echo "true"
535 return 0
536 ;;
537 coord-node)
538 echo "true"
539 return 0
540 ;;
541 server-node)
542 echo "false"
543 return 1
544 ;;
545 coord-and-server-node)
546 echo "true"
547 return 0
548 ;;
549 *)
550 echo "false"
551 return 1
552 ;;
553 esac
554 else
555 echo "false"
556 return 1
557 fi
558}
559
560# Check if this host should be running RAMCloud server:
561function is-server-role {
562 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
563 case "${ONOS_HOST_ROLE}" in
564 single-node)
565 echo "true"
566 return 0
567 ;;
568 coord-node)
569 echo "false"
570 return 1
571 ;;
572 server-node)
573 echo "true"
574 return 0
575 ;;
576 coord-and-server-node)
577 echo "true"
578 return 0
579 ;;
580 *)
581 echo "false"
582 return 1
583 ;;
584 esac
585 else
586 echo "false"
587 return 1
588 fi
589}
590
Naoki Shiota9df15d32014-03-27 14:26:20 -0700591function start-backend {
592 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
Naoki Shiota590c18d2014-03-31 17:52:59 -0700593 if [ $1 == "coord" ]; then
594 rc-coord startifdown
595 elif [ $1 == "server" ]; then
596 rc-server startifdown
597 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700598 fi
599}
600
601function stop-backend {
602 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
603 if [ $rcsn != 0 ]; then
604 rc-server stop
605 fi
606
Naoki Shiota590c18d2014-03-31 17:52:59 -0700607 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700608 if [ $rccn != 0 ]; then
609 rc-coord stop
610 fi
611}
612
Naoki Shiota590c18d2014-03-31 17:52:59 -0700613
Naoki Shiota4e463182014-03-21 15:13:24 -0700614### Functions related to RAMCloud coordinator
Naoki Shiota9df15d32014-03-27 14:26:20 -0700615function rc-coord-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700616 local host=${RC_COORD_IP}
617 if [ -z "${host}" ]; then
618 # falling back to 0.0.0.0
619 host="0.0.0.0"
620 fi
621 echo "${RC_COORD_PROTOCOL}:host=${host},port=${RC_COORD_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700622}
623
624function rc-server-addr {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700625 local host=${RC_SERVER_IP}
626 if [ -z "${host}" ]; then
627 # falling back to 0.0.0.0
628 host="0.0.0.0"
629 fi
630 echo "${RC_SERVER_PROTOCOL}:host=${host},port=${RC_SERVER_PORT}"
Naoki Shiota9df15d32014-03-27 14:26:20 -0700631}
632
Naoki Shiota4e463182014-03-21 15:13:24 -0700633function rc-coord {
634 case "$1" in
635 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700636 stop-coord
637 start-coord
638 ;;
639 startifdown)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700640 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700641 if [ $n == 0 ]; then
642 start-coord
643 else
644 echo "$n instance of RAMCloud coordinator running"
645 fi
646 ;;
647 stop)
648 stop-coord
649 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700650 deldb)
651 stop-backend
652 del-coord-info
653 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700654 stat*) # <- status
655 local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
656 echo "$n RAMCloud coordinator running"
657 ;;
658 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700659 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700660 exit 1
661 esac
662}
663
664function start-coord {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700665 check-and-start-zk
666 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700667
Naoki Shiota4e463182014-03-21 15:13:24 -0700668 if [ ! -d ${LOGDIR} ]; then
669 mkdir -p ${LOGDIR}
670 fi
671 if [ -f $RAMCLOUD_COORD_LOG ]; then
672 rotate-log $RAMCLOUD_COORD_LOG
673 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700674
675 local coord_addr=`rc-coord-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700676
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700677 # TODO Make ONOS_CONF readable from ONOS process then eliminate RAMCLOUD_CONF
678
679 # Configuration for ZK address, port
680 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
681
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700682 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700683 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700684
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700685 # RAMCloud transport timeout
686 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700687 # RAMCloud option deadServerTimeout
688 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
689 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
690
691 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
692 # but actual RAMCloud code only uses -C argument now.
693 # (FYI: -C is documented to be deprecated in the document)
694
695 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700696 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700697 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700698 coord_args="${coord_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700699 coord_args="${coord_args} --deadServerTimeout ${rc_coord_deadServerTimeout}"
700
701 # Read environment variables if set
702 coord_args="${coord_args} ${RC_COORDINATOR_OPTS}"
703
704 if [ "${ONOS_HOST_ROLE}" == "single-node" ]; then
705 # Note: Following reset is required, since RC restart is considered node failure,
706 # and tries recovery of server, which will never succeed after restart.
707 echo "Role configured to single-node mode. RAMCloud cluster will be reset on each start-up."
708 coord_args="${coord_args} --reset"
709 fi
710
Naoki Shiota4e463182014-03-21 15:13:24 -0700711 # Run ramcloud
712 echo -n "Starting RAMCloud coordinator ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700713 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} > $RAMCLOUD_COORD_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700714 echo "STARTED"
715}
716
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700717function del-coord-info {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700718 check-and-start-zk
719 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700720
721 if [ ! -d ${LOGDIR} ]; then
722 mkdir -p ${LOGDIR}
723 fi
724 if [ -f $RAMCLOUD_COORD_LOG ]; then
725 rotate-log $RAMCLOUD_COORD_LOG
726 fi
727
728 local coord_addr=`rc-coord-addr`
729
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700730 # Configuration for ZK address, port
731 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700732 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700733 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700734 # RAMCloud option deadServerTimeout
735 # (note RC default is 250ms, setting relaxed ONOS default to 1000ms)
736 local rc_coord_deadServerTimeout=$(read-conf ${ONOS_CONF} ramcloud.coordinator.deadServerTimeout 1000)
737
738 # NOTE RAMCloud document suggests to use -L to specify listen address:port,
739 # but actual RAMCloud code only uses -C argument now.
740 # (FYI: -C is documented to be deprecated in the document)
741
742 local coord_args="-C ${coord_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700743 coord_args="${coord_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700744 coord_args="${coord_args} --clusterName ${rc_cluster_name}"
745
746 # Note: --reset will reset ZK stored info and start running as acoordinator.
747 echo -n "Deleting RAMCloud cluster coordination info ... "
748 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator ${coord_args} --reset &> $RAMCLOUD_COORD_LOG &
749
Yuta HIGUCHIcd44eb32014-05-24 16:51:31 -0700750 # TODO Assuming 5 sec is enough. To be sure monitor log?
751 sleep 5
752 # kill coordinator
Yuta HIGUCHIce9f3ee2014-05-14 09:48:40 -0700753 (pkill -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator &> /dev/null)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700754
755 echo "DONE"
756}
Naoki Shiota4e463182014-03-21 15:13:24 -0700757
758function stop-coord {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700759 kill-processes "RAMCloud coordinator" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/coordinator`
Naoki Shiota4e463182014-03-21 15:13:24 -0700760}
761
Naoki Shiota4e463182014-03-21 15:13:24 -0700762### Functions related to RAMCloud server
763function rc-server {
764 case "$1" in
765 start)
Naoki Shiota4e463182014-03-21 15:13:24 -0700766 stop-server
767 start-server
768 ;;
769 startifdown)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700770 local n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700771 if [ $n == 0 ]; then
772 start-server
773 else
774 echo "$n instance of RAMCloud server running"
775 fi
776 ;;
777 stop)
778 stop-server
779 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700780 deldb)
781 stop-server
782 del-server-backup
783 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700784 stat*) # <- status
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700785 n=`pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -0700786 echo "$n RAMCloud server running"
787 ;;
788 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700789 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700790 exit 1
791 esac
792}
793
794function start-server {
Yuta HIGUCHI7873c932014-06-02 23:16:18 -0700795 check-and-start-zk
796 wait-zk-or-die
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700797
Naoki Shiota4e463182014-03-21 15:13:24 -0700798 if [ ! -d ${LOGDIR} ]; then
799 mkdir -p ${LOGDIR}
800 fi
801 if [ -f $RAMCLOUD_SERVER_LOG ]; then
802 rotate-log $RAMCLOUD_SERVER_LOG
803 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700804
805 local coord_addr=`rc-coord-addr`
806 local server_addr=`rc-server-addr`
Naoki Shiota4e463182014-03-21 15:13:24 -0700807
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700808 local masterServiceThreads=$(read-conf ${ONOS_CONF} ramcloud.server.masterServiceThreads 5)
809 local logCleanerThreads=$(read-conf ${ONOS_CONF} ramcloud.server.logCleanerThreads 1)
Yuta HIGUCHI52efcbb2014-05-16 09:30:33 -0700810 local detectFailures=$(read-conf ${ONOS_CONF} ramcloud.server.detectFailures 0)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700811
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700812 # Configuration for ZK address, port
813 local rc_locator=$(read-conf ${RAMCLOUD_CONF} ramcloud.locator "zk:localhost:2181")
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700814 # RAMCloud cluster name
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700815 local rc_cluster_name=$(read-conf ${RAMCLOUD_CONF} ramcloud.clusterName ${ONOS_CLUSTER_NAME})
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700816 # RAMCloud transport timeout
817 local rc_timeout=$(read-conf ${ONOS_CONF} ramcloud.timeout 1000)
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700818 # replication factor (-r) config
819 local rc_replicas=$(read-conf ${ONOS_CONF} ramcloud.server.replicas 0)
820 # backup file path (-f) config
821 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
822 mkdir -p `dirname ${rc_datafile}`
823
824 local server_args="-L ${server_addr}"
Yuta HIGUCHId082b962014-06-02 11:42:22 -0700825 server_args="${server_args} --externalStorage ${rc_locator}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700826 server_args="${server_args} --clusterName ${rc_cluster_name}"
Yuta HIGUCHIea7eba02014-05-14 11:13:52 -0700827 server_args="${server_args} --timeout ${rc_timeout}"
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700828 server_args="${server_args} --masterServiceThreads ${masterServiceThreads}"
829 server_args="${server_args} --logCleanerThreads ${logCleanerThreads}"
830 server_args="${server_args} --detectFailures ${detectFailures}"
831 server_args="${server_args} --replicas ${rc_replicas}"
832 server_args="${server_args} --file ${rc_datafile}"
833
834 # Read environment variables if set
835 server_args="${server_args} ${RC_SERVER_OPTS}"
Yuta HIGUCHI45bc3cf2014-04-19 18:12:15 -0700836
Naoki Shiota4e463182014-03-21 15:13:24 -0700837 # Run ramcloud
838 echo -n "Starting RAMCloud server ... "
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700839 ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server ${server_args} > $RAMCLOUD_SERVER_LOG 2>&1 &
Naoki Shiota4e463182014-03-21 15:13:24 -0700840 echo "STARTED"
841}
842
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700843function del-server-backup {
Naoki Shiota9109a1e2014-05-13 11:11:01 -0700844 echo -n "Delete RAMCloud backup server data [y/N]? "
845 while [ 1 ]; do
846 read key
847 if [ "${key}" == "Y" -o "${key}" == "y" ]; then
848 break
849 elif [ -z "${key}" -o "${key}" == "N" -o "${key}" == "n" ]; then
850 echo "Cancelled."
851 return
852 fi
853 echo "[y/N]? "
854 done
855
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700856 echo -n "Removing RAMCloud backup server data ... "
857 local rc_datafile=$(read-conf ${ONOS_CONF} ramcloud.server.file "/var/tmp/ramclouddata/backup.${ONOS_HOST_NAME}.log")
858 rm -f ${rc_datafile}
859 echo "DONE"
860}
861
Naoki Shiota4e463182014-03-21 15:13:24 -0700862function stop-server {
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700863 kill-processes "RAMCloud server" `pgrep -f ${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH}/server`
Naoki Shiota4e463182014-03-21 15:13:24 -0700864}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700865############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700866
867
Naoki Shiota590c18d2014-03-31 17:52:59 -0700868## Functions related to ONOS core process ##
Naoki Shiota4e463182014-03-21 15:13:24 -0700869function onos {
Naoki Shiota590c18d2014-03-31 17:52:59 -0700870 CPFILE=${ONOS_HOME}/.javacp.${ONOS_HOST_NAME}
Naoki Shiota4e463182014-03-21 15:13:24 -0700871 if [ ! -f ${CPFILE} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700872 echo "ONOS core needs to be built"
Naoki Shiota4e463182014-03-21 15:13:24 -0700873 ${MVN} -f ${ONOS_HOME}/pom.xml compile
874 fi
875 JAVA_CP=`cat ${CPFILE}`
876 JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes"
877
878 case "$1" in
879 start)
880 stop-onos
881 start-onos
882 ;;
883 startnokill)
884 start-onos
885 ;;
886 startifdown)
887 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
888 if [ $n == 0 ]; then
889 start-onos
890 else
891 echo "$n instance of onos running"
892 fi
893 ;;
894 stop)
895 stop-onos
896 ;;
897 stat*) # <- status
898 n=`jps -l | grep "${MAIN_CLASS}" | wc -l`
899 echo "$n instance of onos running"
900 ;;
901 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -0700902 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -0700903 exit 1
904 esac
905}
906
907function start-onos {
908 if [ ! -d ${LOGDIR} ]; then
909 mkdir -p ${LOGDIR}
910 fi
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700911 # Rotate log files
Naoki Shiota4e463182014-03-21 15:13:24 -0700912 for log in ${LOGS}; do
913 if [ -f ${log} ]; then
914 rotate-log ${log}
915 fi
916 done
Yuta HIGUCHI04713972014-05-30 11:01:17 -0700917
918 # Rotate logs rolled at runtime.
919 local rolled_log_shellpat=`echo ${ONOS_LOG_ROLLING_PATTERN} | sed -e "s/%i/\\*/"`
920 for rolled_log in ${rolled_log_shellpat}; do
921 if [ -f ${rolled_log} ]; then
922 rotate-log ${rolled_log}
923 # NOTE: renamed file will end up with an extension like .log.gz.1
924 fi
925 done
926
Naoki Shiota4e463182014-03-21 15:13:24 -0700927 if [ ! -f ${ONOS_LOGBACK} ]; then
Naoki Shiota9df15d32014-03-27 14:26:20 -0700928 echo "[WARNING] ${ONOS_LOGBACK} not found."
929 echo " Run \"\$ $0 setup\" to create."
930 exit 1
Naoki Shiota4e463182014-03-21 15:13:24 -0700931 fi
Naoki Shiota9df15d32014-03-27 14:26:20 -0700932
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700933 if [ ! -f ${HC_CONF} ]; then
934 echo "[WARNING] ${HC_CONF} not found."
935 echo " Run \"\$ $0 setup\" to create."
936 exit 1
937 fi
938
Yuta HIGUCHI85de40d2014-06-12 14:06:41 -0700939 # specify ZooKeeper(curator) namespace
940 JVM_OPTS="${JVM_OPTS} -Dzookeeper.namespace=${ONOS_CLUSTER_NAME}"
941
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700942 # specify hazelcast.xml to datagrid
943 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datagrid.HazelcastDatagrid.datagridConfig=${HC_CONF}"
944
945 # specify backend config
Jonathan Hartef3dc1a2014-04-03 11:39:50 -0700946 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.backend=${ONOS_HOST_BACKEND}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700947 if [ "${ONOS_HOST_BACKEND}" = "ramcloud" ]; then
948 JVM_OPTS="${JVM_OPTS} -Dramcloud.config.path=${RAMCLOUD_CONF}"
Yuta HIGUCHI3ebc9482014-05-08 16:28:28 -0700949 elif [ "${ONOS_HOST_BACKEND}" = "hazelcast" ]; then
950 JVM_OPTS="${JVM_OPTS} -Dnet.onrc.onos.core.datastore.hazelcast.baseConfig=${HC_CONF}"
Yuta HIGUCHI13d1dbf2014-04-17 20:51:38 -0700951 fi
952
Naoki Shiota9df15d32014-03-27 14:26:20 -0700953 # Run ONOS
Yuta HIGUCHIcce4f622014-04-18 16:48:53 -0700954
955 # Need to cd ONOS_HOME. onos.properties currently specify hazelcast config path relative to CWD
956 cd ${ONOS_HOME}
957
Yuta HIGUCHId150ece2014-04-29 16:25:36 -0700958 echo -n "Starting ONOS controller ... "
Naoki Shiota4e463182014-03-21 15:13:24 -0700959 java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr &
960
961 # We need to wait a bit to find out whether starting the ONOS process succeeded
962 sleep 1
963
964 n=`jps -l |grep "${MAIN_CLASS}" | wc -l`
Naoki Shiota9df15d32014-03-27 14:26:20 -0700965 if [ $n -ge 1 ]; then
Naoki Shiota4e463182014-03-21 15:13:24 -0700966 echo " STARTED"
967 else
968 echo " FAILED"
969 fi
970
971# echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &"
972# sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1
973}
974
975function stop-onos {
976 kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'`
977# kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'`
978}
Naoki Shiota590c18d2014-03-31 17:52:59 -0700979############################################
Naoki Shiota4e463182014-03-21 15:13:24 -0700980
981
Naoki Shiota590c18d2014-03-31 17:52:59 -0700982################## Main ####################
Naoki Shiota4e463182014-03-21 15:13:24 -0700983case "$1" in
Naoki Shiota9df15d32014-03-27 14:26:20 -0700984 setup)
Naoki Shiota4e928512014-04-03 15:49:35 -0700985 create-confs $2
Naoki Shiota9df15d32014-03-27 14:26:20 -0700986 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -0700987 start)
Naoki Shiota9df15d32014-03-27 14:26:20 -0700988 mode_parameter=${ONOS_HOST_ROLE}
989 if [ ! -z "$2" ]; then
990 mode_parameter=$2
991 fi
992
993 case "${mode_parameter}" in
Naoki Shiota4e463182014-03-21 15:13:24 -0700994 single-node)
995 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -0700996 start-backend coord
997 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -0700998 onos startifdown
999 ;;
1000 coord-node)
1001 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001002 start-backend coord
Naoki Shiota4e463182014-03-21 15:13:24 -07001003 onos startifdown
1004 ;;
1005 server-node)
1006 zk start
Naoki Shiota590c18d2014-03-31 17:52:59 -07001007 start-backend server
1008 onos startifdown
1009 ;;
1010 coord-and-server-node)
1011 zk start
1012 start-backend coord
1013 start-backend server
Naoki Shiota4e463182014-03-21 15:13:24 -07001014 onos startifdown
1015 ;;
1016 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001017 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001018 ;;
1019 esac
1020 echo
1021 ;;
1022 stop)
1023 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1024 if [ $on != 0 ]; then
1025 onos stop
1026 fi
1027
Naoki Shiota9df15d32014-03-27 14:26:20 -07001028 stop-backend
Naoki Shiota4e463182014-03-21 15:13:24 -07001029
1030 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1031 if [ $zkn != 0 ]; then
1032 zk stop
1033 fi
1034 echo
1035 ;;
1036 restart)
1037 on=`jps -l | grep "${MAIN_CLASS}" | wc -l`
1038 if [ $on != 0 ]; then
1039 onos stop
1040 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001041
Naoki Shiota4e463182014-03-21 15:13:24 -07001042 rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l`
1043 if [ $rcsn != 0 ]; then
1044 rc-server stop
1045 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001046
Naoki Shiota590c18d2014-03-31 17:52:59 -07001047 rccn=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l`
Naoki Shiota4e463182014-03-21 15:13:24 -07001048 if [ $rccn != 0 ]; then
1049 rc-coord stop
1050 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001051
Naoki Shiota4e463182014-03-21 15:13:24 -07001052 zkn=`jps -l | grep org.apache.zookeeper.server | wc -l`
1053 if [ $zkn != 0 ]; then
1054 zk restart
1055 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001056
Naoki Shiota4e463182014-03-21 15:13:24 -07001057 if [ $rccn != 0 ]; then
1058 rc-coord startifdown
1059 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001060
Naoki Shiota4e463182014-03-21 15:13:24 -07001061 if [ $rcsn != 0 ]; then
1062 rc-server startifdown
1063 fi
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001064
Naoki Shiota4e463182014-03-21 15:13:24 -07001065 if [ $on != 0 ]; then
1066 onos startifdown
1067 fi
1068 echo
1069 ;;
1070 stat*) # <- status
1071 echo '[ZooKeeper]'
1072 zk status
1073 echo
1074 echo '[RAMCloud coordinator]'
1075 rc-coord status
1076 echo
1077 echo '[RAMCloud server]'
1078 rc-server status
1079 echo
1080 echo '[ONOS core]'
1081 onos status
1082 echo
1083 ;;
1084 zk)
1085 zk $2
1086 ;;
1087 rc-c*) # <- rc-coordinator
1088 rc-coord $2
1089 ;;
1090 rc-s*) # <- rc-server
1091 rc-server $2
1092 ;;
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001093 rc)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001094 # XXX rc command ignores if the host is not configured to have those roles,
1095 # while rc-s or rc-c executes the task regardless of role definition.
1096 # This is a workaround since TestON does not take role in account
1097 # and always issue rc deldb right now.
1098
Naoki Shiota9109a1e2014-05-13 11:11:01 -07001099 # TODO make deldb command more organized (clarify when it can work)
Yuta HIGUCHI259b79a2014-06-04 12:21:22 -07001100 if [ "`is-coord-role`" == "true" ]; then
1101 rc-coord $2
1102 fi
1103 if [ "`is-server-role`" == "true" ]; then
1104 rc-server $2
1105 fi
Yuta HIGUCHId150ece2014-04-29 16:25:36 -07001106 ;;
Naoki Shiota4e463182014-03-21 15:13:24 -07001107 core)
1108 onos $2
1109 ;;
1110 *)
Naoki Shiota590c18d2014-03-31 17:52:59 -07001111 print_usage
Naoki Shiota4e463182014-03-21 15:13:24 -07001112 exit 1
1113esac
Naoki Shiota590c18d2014-03-31 17:52:59 -07001114