blob: 524e17252e233ed700d88e48f37eb6c83bdd140b [file] [log] [blame]
Jonathan Hartf51950d2016-01-15 09:57:10 -08001#! /bin/bash
2# -----------------------------------------------------------------------------
Bob Lantz3ec585d2016-02-25 02:54:33 -08003# init.d script to run ONOS
4#
5# This provides the core for an ONOS service in a variety of environments,
6# including init.d, upstart, and systemd. It can also be invoked directly.
7# If it is invoked by a boot system, environment variables will usually be
8# empty and the default values will be used.
Jonathan Hartf51950d2016-01-15 09:57:10 -08009# -----------------------------------------------------------------------------
Jonathan Harte01652f2016-03-29 10:25:11 -070010### BEGIN INIT INFO
11# Provides: onos
12# Required-Start: $network $remote_fs $syslog
13# Required-Stop: $network $remote_fs $syslog
14# Default-Start: 2 3 4 5
15# Default-Stop: 0 1 6
16# Short-Description: ONOS network operating system
17# Description: ONOS is a network operating system for controlling SDN networks, designed for high availablility, performance, and scale out.
18### END INIT INFO
19
Jonathan Hartf51950d2016-01-15 09:57:10 -080020
Bob Lantz3ec585d2016-02-25 02:54:33 -080021ONOS_HOME=${ONOS_HOME:-/opt/onos}
22[ -f $ONOS_HOME/options ] && . $ONOS_HOME/options
23ONOS_USER=${ONOS_USER:-root}
24ONOS_GROUP=${ONOS_GROUP:-$ONOS_USER}
Bob Lantz7c751b52016-04-15 15:42:28 -070025ONOS_OPTS=${ONOS_OPTS:-""}
Jonathan Hartf51950d2016-01-15 09:57:10 -080026
Bob Lantz3ec585d2016-02-25 02:54:33 -080027start () {
Bob Lantz7c751b52016-04-15 15:42:28 -070028 mkdir -p $ONOS_HOME/var 2>/dev/null && chown $ONOS_USER:$ONOS_GROUP $ONOS_HOME/var
29 mkdir -p $ONOS_HOME/config 2>/dev/null && chown $ONOS_USER:$ONOS_GROUP $ONOS_HOME/config
Bob Lantz3ec585d2016-02-25 02:54:33 -080030 [ ! -h $ONOS_HOME/log ] && ln -s $ONOS_HOME/karaf/data/log $ONOS_HOME/log || :
Bob Lantz641d5452016-03-04 19:39:17 -080031 # Start ONOS if it's not already running
Bob Lantz7c751b52016-04-15 15:42:28 -070032 if ! status >/dev/null; then
33 echo "Starting ONOS"
34 startonos $ONOS_HOME/bin/onos-service server $ONOS_OPTS
Bob Lantz641d5452016-03-04 19:39:17 -080035 else
36 echo "ONOS/karaf is already running"
37 fi
Jonathan Hartf51950d2016-01-15 09:57:10 -080038}
39
Bob Lantz7c751b52016-04-15 15:42:28 -070040startonos () {
41 cmd=$1
42 shift
43 # Start ONOS as a daemon
Phil Huang5c7279e2016-06-24 18:19:56 +080044 if test -f /lib/lsb/init-functions; then
45 . /lib/lsb/init-functions
46 else
47 . /etc/init.d/functions && true
48 fi
49
Bob Lantz7c751b52016-04-15 15:42:28 -070050 if type daemon | grep -i function >/dev/null 2>&1; then
51 # Use 'daemon' function if available
52 # Shell metacharacters are passed as arguments to daemon
53 daemon --user $ONOS_USER $cmd $* \
54 \>$ONOS_HOME/var/stdout.log 2\>$ONOS_HOME/var/stderr.log \&
55 elif type start-stop-daemon >/dev/null 2>&1; then
56 # Use start-stop-daemon if available
57 # Warning! running as root can overwrite any linked log file
58 start-stop-daemon --signal INT --start --chuid $ONOS_USER \
59 --background --exec $cmd -- $* \
60 >$ONOS_HOME/var/stdout.log 2>$ONOS_HOME/var/stderr.log
61 else
62 # Fall back to using sudo
63 # Warning! running as root can overwrite any linked log file
64 sudo -E -n -u -b $ONOS_USER $cmd $* \
65 >$ONOS_HOME/var/stdout.log 2>$ONOS_HOME/var/stderr.log
66 fi
67}
68
Jonathan Hartf51950d2016-01-15 09:57:10 -080069stop () {
Bob Lantz7c751b52016-04-15 15:42:28 -070070 if status >/dev/null; then
71 echo "Stopping ONOS"
Bob Lantz641d5452016-03-04 19:39:17 -080072 $ONOS_HOME/karaf/bin/stop
Bob Lantz7c751b52016-04-15 15:42:28 -070073 # Wait until karaf claims not to be running
74 while status >/dev/null; do echo -n .; sleep 1; done
Bob Lantz641d5452016-03-04 19:39:17 -080075 else
76 echo "ONOS/karaf is not running"
77 fi
Jonathan Hartf51950d2016-01-15 09:57:10 -080078}
79
80restart () {
81 stop
Bob Lantz7c751b52016-04-15 15:42:28 -070082 sleep 2 # Bogus hack since karaf stop doesn't work ;-(
Jonathan Hartf51950d2016-01-15 09:57:10 -080083 start
84}
85
86status () {
Bob Lantz641d5452016-03-04 19:39:17 -080087 # karaf status returns 0 if running, 1 if not
Bob Lantz7c751b52016-04-15 15:42:28 -070088 if [ `id -u` == 0 ]; then
89 # Avoid creating data dir as root
90 sudo -n -u $ONOS_USER $ONOS_HOME/karaf/bin/status
91 else
92 $ONOS_HOME/karaf/bin/status
93 fi
Jonathan Hartf51950d2016-01-15 09:57:10 -080094}
95
96case $1 in
97 start)
98 start
Bob Lantz3ec585d2016-02-25 02:54:33 -080099 ;;
Jonathan Hartf51950d2016-01-15 09:57:10 -0800100 stop | force-stop)
101 stop
102 ;;
103 restart)
104 shift
105 restart "$@"
106 ;;
107 status)
108 status
109 exit $?
110 ;;
111 *)
112 echo "Usage: $0 {start|stop|restart|status}" >&2
113 exit 1
114 ;;
115esac
116
117exit 0