blob: 95c26410464fee9ee496add51772c901e5e3e64f [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
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
Jonathan Hartf51950d2016-01-15 09:57:10 -080064stop () {
Bob Lantz7c751b52016-04-15 15:42:28 -070065 if status >/dev/null; then
66 echo "Stopping ONOS"
Bob Lantz641d5452016-03-04 19:39:17 -080067 $ONOS_HOME/karaf/bin/stop
Bob Lantz7c751b52016-04-15 15:42:28 -070068 # Wait until karaf claims not to be running
69 while status >/dev/null; do echo -n .; sleep 1; done
Bob Lantz641d5452016-03-04 19:39:17 -080070 else
71 echo "ONOS/karaf is not running"
72 fi
Jonathan Hartf51950d2016-01-15 09:57:10 -080073}
74
75restart () {
76 stop
Bob Lantz7c751b52016-04-15 15:42:28 -070077 sleep 2 # Bogus hack since karaf stop doesn't work ;-(
Jonathan Hartf51950d2016-01-15 09:57:10 -080078 start
79}
80
81status () {
Bob Lantz641d5452016-03-04 19:39:17 -080082 # karaf status returns 0 if running, 1 if not
Bob Lantz7c751b52016-04-15 15:42:28 -070083 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
87 $ONOS_HOME/karaf/bin/status
88 fi
Jonathan Hartf51950d2016-01-15 09:57:10 -080089}
90
91case $1 in
92 start)
93 start
Bob Lantz3ec585d2016-02-25 02:54:33 -080094 ;;
Jonathan Hartf51950d2016-01-15 09:57:10 -080095 stop | force-stop)
96 stop
97 ;;
98 restart)
99 shift
100 restart "$@"
101 ;;
102 status)
103 status
104 exit $?
105 ;;
106 *)
107 echo "Usage: $0 {start|stop|restart|status}" >&2
108 exit 1
109 ;;
110esac
111
112exit 0