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}
[ -f $ONOS_HOME/options ] && . $ONOS_HOME/options
ONOS_USER=${ONOS_USER:-root}
ONOS_GROUP=${ONOS_GROUP:-$ONOS_USER}
ONOS_OPTS=${ONOS_OPTS:-server}
ONOS_PID=${ONOS_PID:-/var/run/onos.pid}
ONOS_OPTS=${ONOS_OPTS:-""}
start () {
mkdir -p $ONOS_HOME/var 2>/dev/null && chown $ONOS_USER.$ONOS_GROUP $ONOS_HOME/var
mkdir -p $ONOS_HOME/config 2>/dev/null && chown $ONOS_USER.$ONOS_GROUP $ONOS_HOME/config
mkdir -p $ONOS_HOME/var 2>/dev/null && chown $ONOS_USER:$ONOS_GROUP $ONOS_HOME/var
mkdir -p $ONOS_HOME/config 2>/dev/null && chown $ONOS_USER:$ONOS_GROUP $ONOS_HOME/config
[ ! -h $ONOS_HOME/log ] && ln -s $ONOS_HOME/karaf/data/log $ONOS_HOME/log || :
# Start ONOS if it's not already running
if ! status > /dev/null; then
start-stop-daemon --signal INT --start --chuid $ONOS_USER \
--background --exec $ONOS_HOME/bin/onos-service \
-- $ONOS_OPTS >$ONOS_HOME/var/stdout.log 2>$ONOS_HOME/var/stderr.log
if ! status >/dev/null; then
echo "Starting ONOS"
startonos $ONOS_HOME/bin/onos-service server $ONOS_OPTS
else
echo "ONOS/karaf is already running"
fi
}
startonos () {
cmd=$1
shift
# Start ONOS as a daemon
. /etc/init.d/functions && true
if type daemon | grep -i function >/dev/null 2>&1; then
# Use 'daemon' function if available
# Shell metacharacters are passed as arguments to daemon
daemon --user $ONOS_USER $cmd $* \
\>$ONOS_HOME/var/stdout.log 2\>$ONOS_HOME/var/stderr.log \&
elif type start-stop-daemon >/dev/null 2>&1; then
# Use start-stop-daemon if available
# Warning! running as root can overwrite any linked log file
start-stop-daemon --signal INT --start --chuid $ONOS_USER \
--background --exec $cmd -- $* \
>$ONOS_HOME/var/stdout.log 2>$ONOS_HOME/var/stderr.log
else
# Fall back to using sudo
# Warning! running as root can overwrite any linked log file
sudo -E -n -u -b $ONOS_USER $cmd $* \
>$ONOS_HOME/var/stdout.log 2>$ONOS_HOME/var/stderr.log
fi
}
stop () {
if status> /dev/null; then
if status >/dev/null; then
echo "Stopping ONOS"
$ONOS_HOME/karaf/bin/stop
# Wait until karaf claims not to be running
while status >/dev/null; do echo -n .; sleep 1; done
else
echo "ONOS/karaf is not running"
fi
......@@ -49,12 +74,18 @@ stop () {
restart () {
stop
sleep 2 # Bogus hack since karaf stop doesn't work ;-(
start
}
status () {
# karaf status returns 0 if running, 1 if not
if [ `id -u` == 0 ]; then
# Avoid creating data dir as root
sudo -n -u $ONOS_USER $ONOS_HOME/karaf/bin/status
else
$ONOS_HOME/karaf/bin/status
fi
}
case $1 in
......
......@@ -37,7 +37,7 @@ export ONOS_BOOT_FEATURES="${ONOS_BOOT_FEATURES:-webconsole,onos-api,onos-core,o
# ONOS builtin apps and providers ignited by default
export ONOS_APPS="${ONOS_APPS:-drivers,openflow}"
ssh $remote "
ssh -tt $remote "
echo \"onos.ip = \$(sudo ifconfig | grep $ONOS_NIC | cut -d: -f2 | cut -d\\ -f1)\" \
>> $ONOS_INSTALL_DIR/$KARAF_DIST/etc/system.properties
......
......@@ -55,7 +55,7 @@ $(dirname $0)/onos-push-bits $node
[ ! -z "$mvn_settings" ] && scp -q $mvn_settings $remote:/tmp/settings.xml
ssh $remote "
ssh -tt $remote "
[ -d $ONOS_INSTALL_DIR/bin ] && echo \"ONOS is already installed\" && exit 1
# Prepare a landing zone and unroll the bits
......
......@@ -43,7 +43,7 @@ case $2 in
# Execute the remote commands
for node in $nodes; do
ssh $ONOS_USER@${node} "sudo service onos ${2:-status}"
ssh -tt $ONOS_USER@${node} "sudo service onos ${2:-status}"
done
;;
*)
......
......@@ -26,9 +26,8 @@ _EOF_
remote=$ONOS_USER@${1:-$OCI}
ssh $remote "
sudo stop onos 1>/dev/null 2>/dev/null
ssh -tt $remote "
sudo service onos stop 1>/dev/null 2>/dev/null
# Wait for onos to stop up to 5 seconds
for i in \$(seq 1 5); do
[ -z \"\$(ps -ef | grep karaf.jar | grep -v grep)\" ] && break
......