blob: a94cf3ccba433ec7418698f9507476eee8dc3b3c [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#
Bob Lantzd22c9912016-12-15 14:26:18 -08005# This provides the core for an ONOS service in a variety of System V/init.d
6# compatible environments. It can also be invoked directly.
7# It reads configuration options from $ONOS_HOME/options
Jonathan Hartf51950d2016-01-15 09:57:10 -08008# -----------------------------------------------------------------------------
Jonathan Harte01652f2016-03-29 10:25:11 -07009### BEGIN INIT INFO
10# Provides: onos
11# Required-Start: $network $remote_fs $syslog
12# Required-Stop: $network $remote_fs $syslog
13# Default-Start: 2 3 4 5
14# Default-Stop: 0 1 6
15# Short-Description: ONOS network operating system
16# Description: ONOS is a network operating system for controlling SDN networks, designed for high availablility, performance, and scale out.
17### END INIT INFO
18
Jonathan Hartf51950d2016-01-15 09:57:10 -080019
Bob Lantz3ec585d2016-02-25 02:54:33 -080020ONOS_HOME=${ONOS_HOME:-/opt/onos}
21[ -f $ONOS_HOME/options ] && . $ONOS_HOME/options
22ONOS_USER=${ONOS_USER:-root}
23ONOS_GROUP=${ONOS_GROUP:-$ONOS_USER}
Bob Lantzd22c9912016-12-15 14:26:18 -080024ONOS_OPTS=${ONOS_OPTS:-server}
Jonathan Hartf51950d2016-01-15 09:57:10 -080025
Bob Lantz3ec585d2016-02-25 02:54:33 -080026start () {
Bob Lantzd22c9912016-12-15 14:26:18 -080027 # Fix permissions and symlinks
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 Lantzd22c9912016-12-15 14:26:18 -080030 mkdir -p $ONOS_HOME/apps 2>/dev/null && chown -R $ONOS_USER:$ONOS_GROUP $ONOS_HOME/apps
Yuta HIGUCHI4aa54e32017-08-15 10:22:05 -070031 [ ! -h $ONOS_HOME/karaf ] && ln -s /opt/onos/apache-karaf* $ONOS_HOME/karaf || :
Bob Lantzd22c9912016-12-15 14:26:18 -080032 [ ! -h $ONOS_HOME/log ] && ln -s $ONOS_HOME/karaf/data/log $ONOS_HOME/log || :
Bob Lantz641d5452016-03-04 19:39:17 -080033 # Start ONOS if it's not already running
Bob Lantz7c751b52016-04-15 15:42:28 -070034 if ! status >/dev/null; then
35 echo "Starting ONOS"
Bob Lantzd22c9912016-12-15 14:26:18 -080036 startonos $ONOS_HOME/bin/onos-service $ONOS_OPTS
Bob Lantz641d5452016-03-04 19:39:17 -080037 else
38 echo "ONOS/karaf is already running"
39 fi
Jonathan Hartf51950d2016-01-15 09:57:10 -080040}
41
Bob Lantz7c751b52016-04-15 15:42:28 -070042startonos () {
43 cmd=$1
44 shift
45 # Start ONOS as a daemon
Phil Huang5c7279e2016-06-24 18:19:56 +080046 if test -f /lib/lsb/init-functions; then
47 . /lib/lsb/init-functions
48 else
49 . /etc/init.d/functions && true
50 fi
51
Bob Lantzd22c9912016-12-15 14:26:18 -080052 if type daemon 2>/dev/null| grep -i function >/dev/null 2>&1; then
Bob Lantz7c751b52016-04-15 15:42:28 -070053 # Use 'daemon' function if available
54 # Shell metacharacters are passed as arguments to daemon
55 daemon --user $ONOS_USER $cmd $* \
56 \>$ONOS_HOME/var/stdout.log 2\>$ONOS_HOME/var/stderr.log \&
57 elif type start-stop-daemon >/dev/null 2>&1; then
58 # Use start-stop-daemon if available
Bob Lantzd22c9912016-12-15 14:26:18 -080059 # Warning! Running as root can overwrite any linked log file.
Bob Lantz7c751b52016-04-15 15:42:28 -070060 start-stop-daemon --signal INT --start --chuid $ONOS_USER \
61 --background --exec $cmd -- $* \
62 >$ONOS_HOME/var/stdout.log 2>$ONOS_HOME/var/stderr.log
63 else
64 # Fall back to using sudo
Bob Lantzd22c9912016-12-15 14:26:18 -080065 # Warning! Running as root can overwrite any linked log file.
66 # preserve Env, background, non-interactive, user $ONOS_USER
67 sudo -E -b -n -u $ONOS_USER $cmd $* \
Bob Lantz7c751b52016-04-15 15:42:28 -070068 >$ONOS_HOME/var/stdout.log 2>$ONOS_HOME/var/stderr.log
69 fi
70}
71
Jonathan Hartf51950d2016-01-15 09:57:10 -080072stop () {
Bob Lantz7c751b52016-04-15 15:42:28 -070073 if status >/dev/null; then
74 echo "Stopping ONOS"
Bob Lantz641d5452016-03-04 19:39:17 -080075 $ONOS_HOME/karaf/bin/stop
Bob Lantz7c751b52016-04-15 15:42:28 -070076 # Wait until karaf claims not to be running
77 while status >/dev/null; do echo -n .; sleep 1; done
Bob Lantz641d5452016-03-04 19:39:17 -080078 else
79 echo "ONOS/karaf is not running"
80 fi
Jonathan Hartf51950d2016-01-15 09:57:10 -080081}
82
83restart () {
84 stop
Bob Lantz7c751b52016-04-15 15:42:28 -070085 sleep 2 # Bogus hack since karaf stop doesn't work ;-(
Jonathan Hartf51950d2016-01-15 09:57:10 -080086 start
87}
88
89status () {
Bob Lantz641d5452016-03-04 19:39:17 -080090 # karaf status returns 0 if running, 1 if not
Bob Lantz7c751b52016-04-15 15:42:28 -070091 if [ `id -u` == 0 ]; then
92 # Avoid creating data dir as root
93 sudo -n -u $ONOS_USER $ONOS_HOME/karaf/bin/status
94 else
95 $ONOS_HOME/karaf/bin/status
96 fi
Jonathan Hartf51950d2016-01-15 09:57:10 -080097}
98
99case $1 in
100 start)
101 start
Bob Lantzd22c9912016-12-15 14:26:18 -0800102 ;;
Jonathan Hartf51950d2016-01-15 09:57:10 -0800103 stop | force-stop)
104 stop
105 ;;
106 restart)
107 shift
108 restart "$@"
109 ;;
110 status)
111 status
112 exit $?
113 ;;
114 *)
115 echo "Usage: $0 {start|stop|restart|status}" >&2
116 exit 1
117 ;;
118esac
119
120exit 0