Naoki Shiota | 4e46318 | 2014-03-21 15:13:24 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Set paths |
| 3 | |
| 4 | ONOS_HOME=`dirname $0` |
| 5 | LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs} |
| 6 | |
| 7 | ZK_DIR=${HOME}/zookeeper-3.4.5 |
| 8 | #ZK_CONF=${ONOS_HOME}/conf/zoo.cfg |
| 9 | ZOO_LOG_DIR=${ONOS_HOME}/onos-logs |
| 10 | |
| 11 | RAMCLOUD_DIR=${HOME}/ramcloud |
| 12 | RAMCLOUD_HOME=${RAMCLOUD_HOME:-~/ramcloud} |
| 13 | RAMCLOUD_COORD_LOG=${LOGDIR}/ramcloud.coordinator.`hostname`.log |
| 14 | RAMCLOUD_SERVER_LOG=${LOGDIR}/ramcloud.server.`hostname`.log |
| 15 | coordinatorip=`grep coordinatorIp ${ONOS_HOME}/conf/ramcloud.conf | cut -d "=" -f 2,3` |
| 16 | coordinatorport=`grep coordinatorPort ${ONOS_HOME}/conf/ramcloud.conf | cut -d "=" -f 2,3` |
| 17 | serverip=`grep serverIp ${ONOS_HOME}/conf/ramcloud.conf | cut -d "=" -f 2,3` |
| 18 | serverport=`grep serverPort ${ONOS_HOME}/conf/ramcloud.conf | cut -d "=" -f 2,3` |
| 19 | RAMCLOUD_COORDINATOR=`echo $coordinatorip","$coordinatorport` |
| 20 | RAMCLOUD_SERVER=`echo $serverip","$serverport` |
| 21 | RAMCLOUD_BRANCH=${RAMCLOUD_BRANCH:-master} |
| 22 | |
| 23 | export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${ONOS_HOME}/lib:${RAMCLOUD_HOME}/obj.${RAMCLOUD_BRANCH} |
| 24 | |
| 25 | ## Because the script change dir to $ONOS_HOME, we can set ONOS_LOGBACK and LOGDIR relative to $ONOS_HOME |
| 26 | ONOS_LOGBACK=${ONOS_LOGBACK:-${ONOS_HOME}/conf/logback.`hostname`.xml} |
| 27 | ONOS_LOGBACK_TEMPLATE=${ONOS_HOME}/conf/logback.xml.template |
| 28 | LOGDIR=${ONOS_LOGDIR:-${ONOS_HOME}/onos-logs} |
| 29 | LOGBASE=${ONOS_LOGBASE:-onos.`hostname`} |
| 30 | ONOS_LOG="${LOGDIR}/${LOGBASE}.log" |
| 31 | PCAP_LOG="${LOGDIR}/${LOGBASE}.pcap" |
| 32 | LOGS="$ONOS_LOG $PCAP_LOG" |
| 33 | |
| 34 | ONOS_PROPS=${ONOS_PROPS:-${ONOS_HOME}/conf/onos.properties} |
| 35 | JMX_PORT=${JMX_PORT:-7189} |
| 36 | |
| 37 | # Set JVM options |
| 38 | JVM_OPTS="${JVM_OPTS:-}" |
| 39 | ## If you want JaCoCo Code Coverage reports... uncomment line below |
| 40 | #JVM_OPTS="$JVM_OPTS -javaagent:${ONOS_HOME}/lib/jacocoagent.jar=dumponexit=true,output=file,destfile=${LOGDIR}/jacoco.exec" |
| 41 | JVM_OPTS="$JVM_OPTS -server -d64" |
| 42 | #JVM_OPTS="$JVM_OPTS -XX:+TieredCompilation -XX:InitialCodeCacheSize=512m -XX:ReservedCodeCacheSize=512m" |
| 43 | JVM_OPTS="$JVM_OPTS -Xmx4g -Xms4g -Xmn800m" |
| 44 | #JVM_OPTS="$JVM_OPTS -Xmx2g -Xms2g -Xmn800m" |
| 45 | #JVM_OPTS="$JVM_OPTS -Xmx1g -Xms1g -Xmn800m" |
| 46 | #JVM_OPTS="$JVM_OPTS -XX:+UseParallelGC -XX:+AggressiveOpts -XX:+UseFastAccessorMethods" |
| 47 | JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC -XX:+UseAdaptiveSizePolicy -XX:+AggressiveOpts -XX:+UseFastAccessorMethods" |
| 48 | JVM_OPTS="$JVM_OPTS -XX:MaxInlineSize=8192 -XX:FreqInlineSize=8192" |
| 49 | JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500 -XX:PreBlockSpin=8" |
| 50 | JVM_OPTS="$JVM_OPTS -XX:OnError=crash-logger" ;# For dumping core |
| 51 | #JVM_OPTS="$JVM_OPTS -Dpython.security.respectJavaAccessibility=false" |
| 52 | JVM_OPTS="$JVM_OPTS -XX:CompileThreshold=1500 -XX:PreBlockSpin=8 \ |
| 53 | -XX:+UseThreadPriorities \ |
| 54 | -XX:ThreadPriorityPolicy=42 \ |
| 55 | -XX:+UseCompressedOops \ |
| 56 | -Dcom.sun.management.jmxremote.port=$JMX_PORT \ |
| 57 | -Dcom.sun.management.jmxremote.ssl=false \ |
| 58 | -Dbenchmark.measureBP=0 \ |
| 59 | -Dbenchmark.measureRc=0 \ |
| 60 | -Dbenchmark.measureONOS=0 \ |
| 61 | -Dcom.sun.management.jmxremote.authenticate=false" |
| 62 | JVM_OPTS="$JVM_OPTS -Dhazelcast.logging.type=slf4j" |
| 63 | |
| 64 | # Set ONOS core main class |
| 65 | MAIN_CLASS="net.onrc.onos.ofcontroller.core.Main" |
| 66 | |
| 67 | MVN=${MVN:-mvn -o} |
| 68 | |
| 69 | function usage { |
| 70 | echo "Usage" |
| 71 | echo " $0 start {single-node|coord-node|server-node}" |
| 72 | echo " single-node: start ONOS with RAMCloud coordinator/server" |
| 73 | echo " coord-node : start ONOS with RAMCloud coordinator" |
| 74 | echo " server-node: start ONOS with RAMCloud server" |
| 75 | echo " $0 stop" |
| 76 | echo " $0 restart" |
| 77 | echo " $0 status" |
| 78 | echo " $0 {zk|rc-coord|rc-server|core} {start|stop|restart|status}" |
| 79 | } |
| 80 | |
| 81 | function rotate-log { |
| 82 | local logfile=$1 |
| 83 | local nr_max=${2:-10} |
| 84 | if [ -f $logfile ]; then |
| 85 | for i in `seq $(expr $nr_max - 1) -1 1`; do |
| 86 | if [ -f ${logfile}.${i} ]; then |
| 87 | mv -f ${logfile}.${i} ${logfile}.`expr $i + 1` |
| 88 | fi |
| 89 | done |
| 90 | mv $logfile $logfile.1 |
| 91 | fi |
| 92 | } |
| 93 | |
| 94 | # kill-processes {module-name} {array of pids} |
| 95 | function kill-processes { |
| 96 | # Kill the existing processes |
| 97 | local pids=$2 |
| 98 | local n=${#pids[*]} |
| 99 | if [ $n != 0 ]; then |
| 100 | echo -n "Stopping $1 ... " |
| 101 | fi |
| 102 | for p in ${pids}; do |
| 103 | if [ x$p != "x" ]; then |
| 104 | kill -KILL $p |
| 105 | echo "Killed existing process (pid: $p)" |
| 106 | fi |
| 107 | done |
| 108 | } |
| 109 | |
| 110 | |
| 111 | ### Functions related to ZooKeeper |
| 112 | function zk { |
| 113 | case "$1" in |
| 114 | start) |
| 115 | start-zk |
| 116 | ;; |
| 117 | stop) |
| 118 | stop-zk |
| 119 | ;; |
| 120 | stat*) # <- status |
| 121 | status-zk |
| 122 | ;; |
| 123 | re*) # <- restart |
| 124 | stop-zk |
| 125 | start-zk |
| 126 | ;; |
| 127 | *) |
| 128 | usage |
| 129 | exit 1 |
| 130 | esac |
| 131 | } |
| 132 | |
| 133 | function start-zk { |
| 134 | # Run Zookeeper with our configuration |
| 135 | echo -n "Starting Zookeeper ... " |
| 136 | $ZK_DIR/bin/zkServer.sh start |
| 137 | } |
| 138 | |
| 139 | function stop-zk { |
| 140 | kill-processes "ZooKeeper" `jps -l | grep org.apache.zookeeper.server | awk '{print $1}'` |
| 141 | } |
| 142 | |
| 143 | function status-zk { |
| 144 | $ZK_DIR/bin/zkServer.sh status |
| 145 | } |
| 146 | |
| 147 | |
| 148 | ### Functions related to RAMCloud coordinator |
| 149 | function rc-coord { |
| 150 | case "$1" in |
| 151 | start) |
| 152 | deldb |
| 153 | cp $ONOS_HOME/conf/ramcloud.conf /tmp |
| 154 | stop-coord |
| 155 | start-coord |
| 156 | ;; |
| 157 | startifdown) |
| 158 | local n=`pgrep coordinator | wc -l` |
| 159 | if [ $n == 0 ]; then |
| 160 | start-coord |
| 161 | else |
| 162 | echo "$n instance of RAMCloud coordinator running" |
| 163 | fi |
| 164 | ;; |
| 165 | stop) |
| 166 | stop-coord |
| 167 | ;; |
| 168 | stat*) # <- status |
| 169 | local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/coordinator | wc -l` |
| 170 | echo "$n RAMCloud coordinator running" |
| 171 | ;; |
| 172 | *) |
| 173 | usage |
| 174 | exit 1 |
| 175 | esac |
| 176 | } |
| 177 | |
| 178 | function start-coord { |
| 179 | if [ ! -d ${LOGDIR} ]; then |
| 180 | mkdir -p ${LOGDIR} |
| 181 | fi |
| 182 | if [ -f $RAMCLOUD_COORD_LOG ]; then |
| 183 | rotate-log $RAMCLOUD_COORD_LOG |
| 184 | fi |
| 185 | |
| 186 | # Run ramcloud |
| 187 | echo -n "Starting RAMCloud coordinator ... " |
| 188 | $RAMCLOUD_DIR/obj.${RAMCLOUD_BRANCH}/coordinator -L $RAMCLOUD_COORDINATOR > $RAMCLOUD_COORD_LOG 2>&1 & |
| 189 | echo "STARTED" |
| 190 | } |
| 191 | |
| 192 | |
| 193 | function stop-coord { |
| 194 | kill-processes "RAMCloud coordinator" `pgrep coordinator | awk '{print $1}'` |
| 195 | } |
| 196 | |
| 197 | function deldb { |
| 198 | # TODO: implement |
| 199 | return |
| 200 | } |
| 201 | |
| 202 | ### Functions related to RAMCloud server |
| 203 | function rc-server { |
| 204 | case "$1" in |
| 205 | start) |
| 206 | deldb |
| 207 | cp $ONOS_HOME/conf/ramcloud.conf /tmp |
| 208 | stop-server |
| 209 | start-server |
| 210 | ;; |
| 211 | startifdown) |
| 212 | local n=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l` |
| 213 | if [ $n == 0 ]; then |
| 214 | start-server |
| 215 | else |
| 216 | echo "$n instance of RAMCloud server running" |
| 217 | fi |
| 218 | ;; |
| 219 | stop) |
| 220 | stop-server |
| 221 | ;; |
| 222 | # deldb) |
| 223 | # deldb |
| 224 | # ;; |
| 225 | stat*) # <- status |
| 226 | n=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l` |
| 227 | echo "$n RAMCloud server running" |
| 228 | ;; |
| 229 | *) |
| 230 | usage |
| 231 | exit 1 |
| 232 | esac |
| 233 | } |
| 234 | |
| 235 | function start-server { |
| 236 | if [ ! -d ${LOGDIR} ]; then |
| 237 | mkdir -p ${LOGDIR} |
| 238 | fi |
| 239 | if [ -f $RAMCLOUD_SERVER_LOG ]; then |
| 240 | rotate-log $RAMCLOUD_SERVER_LOG |
| 241 | fi |
| 242 | |
| 243 | # Run ramcloud |
| 244 | echo -n "Starting RAMCloud server ... " |
| 245 | $RAMCLOUD_DIR/obj.${RAMCLOUD_BRANCH}/server -M -L $RAMCLOUD_SERVER -C $RAMCLOUD_COORDINATOR --masterServiceThreads 1 --logCleanerThreads 1 --detectFailures 0 > $RAMCLOUD_SERVER_LOG 2>&1 & |
| 246 | echo "STARTED" |
| 247 | } |
| 248 | |
| 249 | function stop-server { |
| 250 | kill-processes "RAMCloud server" `pgrep -f obj.${RAMCLOUD_BRANCH}/server | awk '{print $1}'` |
| 251 | } |
| 252 | |
| 253 | |
| 254 | ### Functions related to ONOS core process |
| 255 | function onos { |
| 256 | CPFILE=${ONOS_HOME}/.javacp.`hostname` |
| 257 | if [ ! -f ${CPFILE} ]; then |
| 258 | ${MVN} -f ${ONOS_HOME}/pom.xml compile |
| 259 | fi |
| 260 | JAVA_CP=`cat ${CPFILE}` |
| 261 | JAVA_CP="${JAVA_CP}:${ONOS_HOME}/target/classes" |
| 262 | |
| 263 | case "$1" in |
| 264 | start) |
| 265 | stop-onos |
| 266 | start-onos |
| 267 | ;; |
| 268 | startnokill) |
| 269 | start-onos |
| 270 | ;; |
| 271 | startifdown) |
| 272 | n=`jps -l | grep "${MAIN_CLASS}" | wc -l` |
| 273 | if [ $n == 0 ]; then |
| 274 | start-onos |
| 275 | else |
| 276 | echo "$n instance of onos running" |
| 277 | fi |
| 278 | ;; |
| 279 | stop) |
| 280 | stop-onos |
| 281 | ;; |
| 282 | stat*) # <- status |
| 283 | n=`jps -l | grep "${MAIN_CLASS}" | wc -l` |
| 284 | echo "$n instance of onos running" |
| 285 | ;; |
| 286 | *) |
| 287 | usage |
| 288 | exit 1 |
| 289 | esac |
| 290 | } |
| 291 | |
| 292 | function start-onos { |
| 293 | if [ ! -d ${LOGDIR} ]; then |
| 294 | mkdir -p ${LOGDIR} |
| 295 | fi |
| 296 | # Backup log files |
| 297 | for log in ${LOGS}; do |
| 298 | if [ -f ${log} ]; then |
| 299 | rotate-log ${log} |
| 300 | fi |
| 301 | done |
| 302 | |
| 303 | # Run ONOS |
| 304 | echo -n "Starting ONOS controller ..." |
| 305 | # Create default logback config file using template |
| 306 | if [ ! -f ${ONOS_LOGBACK} ]; then |
| 307 | sed -e "s|__FILENAME__|${ONOS_LOG}|" ${ONOS_LOGBACK_TEMPLATE} > ${ONOS_LOGBACK} |
| 308 | fi |
| 309 | java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -cp ${JAVA_CP} ${MAIN_CLASS} -cf ${ONOS_PROPS} > ${LOGDIR}/${LOGBASE}.stdout 2>${LOGDIR}/${LOGBASE}.stderr & |
| 310 | |
| 311 | # We need to wait a bit to find out whether starting the ONOS process succeeded |
| 312 | sleep 1 |
| 313 | |
| 314 | n=`jps -l |grep "${MAIN_CLASS}" | wc -l` |
| 315 | if [ "$n" -ge "1" ]; then |
| 316 | echo " STARTED" |
| 317 | else |
| 318 | echo " FAILED" |
| 319 | fi |
| 320 | |
| 321 | # echo "java ${JVM_OPTS} -Dlogback.configurationFile=${ONOS_LOGBACK} -jar ${ONOS_JAR} -cf ./onos.properties > /dev/null 2>&1 &" |
| 322 | # sudo -b /usr/sbin/tcpdump -n -i eth0 -s0 -w ${PCAP_LOG} 'tcp port 6633' > /dev/null 2>&1 |
| 323 | } |
| 324 | |
| 325 | function stop-onos { |
| 326 | kill-processes "ONOS controller" `jps -l | grep ${MAIN_CLASS} | awk '{print $1}'` |
| 327 | # kill-processes "tcpdump" `ps -edalf |grep tcpdump |grep ${PCAP_LOG} | awk '{print $4}'` |
| 328 | } |
| 329 | |
| 330 | |
| 331 | case "$1" in |
| 332 | start) |
| 333 | case $2 in |
| 334 | single-node) |
| 335 | zk start |
| 336 | rc-coord startifdown |
| 337 | rc-server startifdown |
| 338 | onos startifdown |
| 339 | ;; |
| 340 | coord-node) |
| 341 | zk start |
| 342 | rc-coord startifdown |
| 343 | onos startifdown |
| 344 | ;; |
| 345 | server-node) |
| 346 | zk start |
| 347 | rc-server startifdown |
| 348 | onos startifdown |
| 349 | ;; |
| 350 | *) |
| 351 | usage |
| 352 | ;; |
| 353 | esac |
| 354 | echo |
| 355 | ;; |
| 356 | stop) |
| 357 | on=`jps -l | grep "${MAIN_CLASS}" | wc -l` |
| 358 | if [ $on != 0 ]; then |
| 359 | onos stop |
| 360 | fi |
| 361 | |
| 362 | rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l` |
| 363 | if [ $rcsn != 0 ]; then |
| 364 | rc-server stop |
| 365 | fi |
| 366 | |
| 367 | rccn=`pgrep coordinator | wc -l` |
| 368 | if [ $rccn != 0 ]; then |
| 369 | rc-coord stop |
| 370 | fi |
| 371 | |
| 372 | zkn=`jps -l | grep org.apache.zookeeper.server | wc -l` |
| 373 | if [ $zkn != 0 ]; then |
| 374 | zk stop |
| 375 | fi |
| 376 | echo |
| 377 | ;; |
| 378 | restart) |
| 379 | on=`jps -l | grep "${MAIN_CLASS}" | wc -l` |
| 380 | if [ $on != 0 ]; then |
| 381 | onos stop |
| 382 | fi |
| 383 | |
| 384 | rcsn=`pgrep -f obj.${RAMCLOUD_BRANCH}/server | wc -l` |
| 385 | if [ $rcsn != 0 ]; then |
| 386 | rc-server stop |
| 387 | fi |
| 388 | |
| 389 | rccn=`pgrep coordinator | wc -l` |
| 390 | if [ $rccn != 0 ]; then |
| 391 | rc-coord stop |
| 392 | fi |
| 393 | |
| 394 | zkn=`jps -l | grep org.apache.zookeeper.server | wc -l` |
| 395 | if [ $zkn != 0 ]; then |
| 396 | zk restart |
| 397 | fi |
| 398 | |
| 399 | if [ $rccn != 0 ]; then |
| 400 | rc-coord startifdown |
| 401 | fi |
| 402 | |
| 403 | if [ $rcsn != 0 ]; then |
| 404 | rc-server startifdown |
| 405 | fi |
| 406 | |
| 407 | if [ $on != 0 ]; then |
| 408 | onos startifdown |
| 409 | fi |
| 410 | echo |
| 411 | ;; |
| 412 | stat*) # <- status |
| 413 | echo '[ZooKeeper]' |
| 414 | zk status |
| 415 | echo |
| 416 | echo '[RAMCloud coordinator]' |
| 417 | rc-coord status |
| 418 | echo |
| 419 | echo '[RAMCloud server]' |
| 420 | rc-server status |
| 421 | echo |
| 422 | echo '[ONOS core]' |
| 423 | onos status |
| 424 | echo |
| 425 | ;; |
| 426 | zk) |
| 427 | zk $2 |
| 428 | ;; |
| 429 | rc-c*) # <- rc-coordinator |
| 430 | rc-coord $2 |
| 431 | ;; |
| 432 | rc-s*) # <- rc-server |
| 433 | rc-server $2 |
| 434 | ;; |
| 435 | core) |
| 436 | onos $2 |
| 437 | ;; |
| 438 | *) |
| 439 | usage |
| 440 | exit 1 |
| 441 | esac |