Bob Lantz
Committed by Gerrit Code Review

Cross-platform changes to onos.initd

We now can use start-stop-daemon (debian), daemon()
shell function (centos), or sudo (others) to start
onos-service.

It should be backward compatible on systemd systems.

Tested on Ubuntu 14 and CentOS 6. Should also work on
Ubuntu 15/16, CentOS 7, Debian, and Fedora.

(Note that we should test this against various OSes,
preferably automatically rather than manually!)

Addresses at least part of ONOS-2907

Change-Id: I4ded98baf02321a5a9db37fdff19e1ce4a3d23d2
...@@ -22,26 +22,51 @@ ONOS_HOME=${ONOS_HOME:-/opt/onos} ...@@ -22,26 +22,51 @@ ONOS_HOME=${ONOS_HOME:-/opt/onos}
22 [ -f $ONOS_HOME/options ] && . $ONOS_HOME/options 22 [ -f $ONOS_HOME/options ] && . $ONOS_HOME/options
23 ONOS_USER=${ONOS_USER:-root} 23 ONOS_USER=${ONOS_USER:-root}
24 ONOS_GROUP=${ONOS_GROUP:-$ONOS_USER} 24 ONOS_GROUP=${ONOS_GROUP:-$ONOS_USER}
25 -ONOS_OPTS=${ONOS_OPTS:-server} 25 +ONOS_OPTS=${ONOS_OPTS:-""}
26 -ONOS_PID=${ONOS_PID:-/var/run/onos.pid}
27 26
28 start () { 27 start () {
29 - mkdir -p $ONOS_HOME/var 2>/dev/null && chown $ONOS_USER.$ONOS_GROUP $ONOS_HOME/var 28 + mkdir -p $ONOS_HOME/var 2>/dev/null && chown $ONOS_USER:$ONOS_GROUP $ONOS_HOME/var
30 - mkdir -p $ONOS_HOME/config 2>/dev/null && chown $ONOS_USER.$ONOS_GROUP $ONOS_HOME/config 29 + mkdir -p $ONOS_HOME/config 2>/dev/null && chown $ONOS_USER:$ONOS_GROUP $ONOS_HOME/config
31 [ ! -h $ONOS_HOME/log ] && ln -s $ONOS_HOME/karaf/data/log $ONOS_HOME/log || : 30 [ ! -h $ONOS_HOME/log ] && ln -s $ONOS_HOME/karaf/data/log $ONOS_HOME/log || :
32 # Start ONOS if it's not already running 31 # Start ONOS if it's not already running
33 - if ! status > /dev/null; then 32 + if ! status >/dev/null; then
34 - start-stop-daemon --signal INT --start --chuid $ONOS_USER \ 33 + echo "Starting ONOS"
35 - --background --exec $ONOS_HOME/bin/onos-service \ 34 + startonos $ONOS_HOME/bin/onos-service server $ONOS_OPTS
36 - -- $ONOS_OPTS >$ONOS_HOME/var/stdout.log 2>$ONOS_HOME/var/stderr.log
37 else 35 else
38 echo "ONOS/karaf is already running" 36 echo "ONOS/karaf is already running"
39 fi 37 fi
40 } 38 }
41 39
40 +startonos () {
41 + cmd=$1
42 + shift
43 + # Start ONOS as a daemon
44 + . /etc/init.d/functions && true
45 + if type daemon | grep -i function >/dev/null 2>&1; then
46 + # Use 'daemon' function if available
47 + # Shell metacharacters are passed as arguments to daemon
48 + daemon --user $ONOS_USER $cmd $* \
49 + \>$ONOS_HOME/var/stdout.log 2\>$ONOS_HOME/var/stderr.log \&
50 + elif type start-stop-daemon >/dev/null 2>&1; then
51 + # Use start-stop-daemon if available
52 + # Warning! running as root can overwrite any linked log file
53 + start-stop-daemon --signal INT --start --chuid $ONOS_USER \
54 + --background --exec $cmd -- $* \
55 + >$ONOS_HOME/var/stdout.log 2>$ONOS_HOME/var/stderr.log
56 + else
57 + # Fall back to using sudo
58 + # Warning! running as root can overwrite any linked log file
59 + sudo -E -n -u -b $ONOS_USER $cmd $* \
60 + >$ONOS_HOME/var/stdout.log 2>$ONOS_HOME/var/stderr.log
61 + fi
62 +}
63 +
42 stop () { 64 stop () {
43 - if status> /dev/null; then 65 + if status >/dev/null; then
66 + echo "Stopping ONOS"
44 $ONOS_HOME/karaf/bin/stop 67 $ONOS_HOME/karaf/bin/stop
68 + # Wait until karaf claims not to be running
69 + while status >/dev/null; do echo -n .; sleep 1; done
45 else 70 else
46 echo "ONOS/karaf is not running" 71 echo "ONOS/karaf is not running"
47 fi 72 fi
...@@ -49,12 +74,18 @@ stop () { ...@@ -49,12 +74,18 @@ stop () {
49 74
50 restart () { 75 restart () {
51 stop 76 stop
77 + sleep 2 # Bogus hack since karaf stop doesn't work ;-(
52 start 78 start
53 } 79 }
54 80
55 status () { 81 status () {
56 # karaf status returns 0 if running, 1 if not 82 # karaf status returns 0 if running, 1 if not
83 + if [ `id -u` == 0 ]; then
84 + # Avoid creating data dir as root
85 + sudo -n -u $ONOS_USER $ONOS_HOME/karaf/bin/status
86 + else
57 $ONOS_HOME/karaf/bin/status 87 $ONOS_HOME/karaf/bin/status
88 + fi
58 } 89 }
59 90
60 case $1 in 91 case $1 in
......
...@@ -37,7 +37,7 @@ export ONOS_BOOT_FEATURES="${ONOS_BOOT_FEATURES:-webconsole,onos-api,onos-core,o ...@@ -37,7 +37,7 @@ export ONOS_BOOT_FEATURES="${ONOS_BOOT_FEATURES:-webconsole,onos-api,onos-core,o
37 # ONOS builtin apps and providers ignited by default 37 # ONOS builtin apps and providers ignited by default
38 export ONOS_APPS="${ONOS_APPS:-drivers,openflow}" 38 export ONOS_APPS="${ONOS_APPS:-drivers,openflow}"
39 39
40 -ssh $remote " 40 +ssh -tt $remote "
41 echo \"onos.ip = \$(sudo ifconfig | grep $ONOS_NIC | cut -d: -f2 | cut -d\\ -f1)\" \ 41 echo \"onos.ip = \$(sudo ifconfig | grep $ONOS_NIC | cut -d: -f2 | cut -d\\ -f1)\" \
42 >> $ONOS_INSTALL_DIR/$KARAF_DIST/etc/system.properties 42 >> $ONOS_INSTALL_DIR/$KARAF_DIST/etc/system.properties
43 43
......
...@@ -55,7 +55,7 @@ $(dirname $0)/onos-push-bits $node ...@@ -55,7 +55,7 @@ $(dirname $0)/onos-push-bits $node
55 55
56 [ ! -z "$mvn_settings" ] && scp -q $mvn_settings $remote:/tmp/settings.xml 56 [ ! -z "$mvn_settings" ] && scp -q $mvn_settings $remote:/tmp/settings.xml
57 57
58 -ssh $remote " 58 +ssh -tt $remote "
59 [ -d $ONOS_INSTALL_DIR/bin ] && echo \"ONOS is already installed\" && exit 1 59 [ -d $ONOS_INSTALL_DIR/bin ] && echo \"ONOS is already installed\" && exit 1
60 60
61 # Prepare a landing zone and unroll the bits 61 # Prepare a landing zone and unroll the bits
......
...@@ -43,7 +43,7 @@ case $2 in ...@@ -43,7 +43,7 @@ case $2 in
43 43
44 # Execute the remote commands 44 # Execute the remote commands
45 for node in $nodes; do 45 for node in $nodes; do
46 - ssh $ONOS_USER@${node} "sudo service onos ${2:-status}" 46 + ssh -tt $ONOS_USER@${node} "sudo service onos ${2:-status}"
47 done 47 done
48 ;; 48 ;;
49 *) 49 *)
......
...@@ -26,9 +26,8 @@ _EOF_ ...@@ -26,9 +26,8 @@ _EOF_
26 26
27 remote=$ONOS_USER@${1:-$OCI} 27 remote=$ONOS_USER@${1:-$OCI}
28 28
29 -ssh $remote " 29 +ssh -tt $remote "
30 - sudo stop onos 1>/dev/null 2>/dev/null 30 + sudo service onos stop 1>/dev/null 2>/dev/null
31 -
32 # Wait for onos to stop up to 5 seconds 31 # Wait for onos to stop up to 5 seconds
33 for i in \$(seq 1 5); do 32 for i in \$(seq 1 5); do
34 [ -z \"\$(ps -ef | grep karaf.jar | grep -v grep)\" ] && break 33 [ -z \"\$(ps -ef | grep karaf.jar | grep -v grep)\" ] && break
......