Jonathan Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 1 | #! /bin/bash |
| 2 | # ----------------------------------------------------------------------------- |
Bob Lantz | 3ec585d | 2016-02-25 02:54:33 -0800 | [diff] [blame] | 3 | # 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 Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 9 | # ----------------------------------------------------------------------------- |
Jonathan Hart | e01652f | 2016-03-29 10:25:11 -0700 | [diff] [blame] | 10 | ### 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 Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 20 | |
Bob Lantz | 3ec585d | 2016-02-25 02:54:33 -0800 | [diff] [blame] | 21 | ONOS_HOME=${ONOS_HOME:-/opt/onos} |
| 22 | [ -f $ONOS_HOME/options ] && . $ONOS_HOME/options |
| 23 | ONOS_USER=${ONOS_USER:-root} |
| 24 | ONOS_GROUP=${ONOS_GROUP:-$ONOS_USER} |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 25 | ONOS_OPTS=${ONOS_OPTS:-""} |
Jonathan Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 26 | |
Bob Lantz | 3ec585d | 2016-02-25 02:54:33 -0800 | [diff] [blame] | 27 | start () { |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 28 | 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 Lantz | 3ec585d | 2016-02-25 02:54:33 -0800 | [diff] [blame] | 30 | [ ! -h $ONOS_HOME/log ] && ln -s $ONOS_HOME/karaf/data/log $ONOS_HOME/log || : |
Bob Lantz | 641d545 | 2016-03-04 19:39:17 -0800 | [diff] [blame] | 31 | # Start ONOS if it's not already running |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 32 | if ! status >/dev/null; then |
| 33 | echo "Starting ONOS" |
| 34 | startonos $ONOS_HOME/bin/onos-service server $ONOS_OPTS |
Bob Lantz | 641d545 | 2016-03-04 19:39:17 -0800 | [diff] [blame] | 35 | else |
| 36 | echo "ONOS/karaf is already running" |
| 37 | fi |
Jonathan Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 40 | startonos () { |
| 41 | cmd=$1 |
| 42 | shift |
| 43 | # Start ONOS as a daemon |
Phil Huang | 5c7279e | 2016-06-24 18:19:56 +0800 | [diff] [blame] | 44 | 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 Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 50 | 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 Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 69 | stop () { |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 70 | if status >/dev/null; then |
| 71 | echo "Stopping ONOS" |
Bob Lantz | 641d545 | 2016-03-04 19:39:17 -0800 | [diff] [blame] | 72 | $ONOS_HOME/karaf/bin/stop |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 73 | # Wait until karaf claims not to be running |
| 74 | while status >/dev/null; do echo -n .; sleep 1; done |
Bob Lantz | 641d545 | 2016-03-04 19:39:17 -0800 | [diff] [blame] | 75 | else |
| 76 | echo "ONOS/karaf is not running" |
| 77 | fi |
Jonathan Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | restart () { |
| 81 | stop |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 82 | sleep 2 # Bogus hack since karaf stop doesn't work ;-( |
Jonathan Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 83 | start |
| 84 | } |
| 85 | |
| 86 | status () { |
Bob Lantz | 641d545 | 2016-03-04 19:39:17 -0800 | [diff] [blame] | 87 | # karaf status returns 0 if running, 1 if not |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 88 | 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 Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | case $1 in |
| 97 | start) |
| 98 | start |
Bob Lantz | 3ec585d | 2016-02-25 02:54:33 -0800 | [diff] [blame] | 99 | ;; |
Jonathan Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 100 | 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 | ;; |
| 115 | esac |
| 116 | |
| 117 | exit 0 |