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 | # |
Bob Lantz | d22c991 | 2016-12-15 14:26:18 -0800 | [diff] [blame] | 5 | # 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 Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 8 | # ----------------------------------------------------------------------------- |
Jonathan Hart | e01652f | 2016-03-29 10:25:11 -0700 | [diff] [blame] | 9 | ### 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 Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 19 | |
Bob Lantz | 3ec585d | 2016-02-25 02:54:33 -0800 | [diff] [blame] | 20 | ONOS_HOME=${ONOS_HOME:-/opt/onos} |
| 21 | [ -f $ONOS_HOME/options ] && . $ONOS_HOME/options |
| 22 | ONOS_USER=${ONOS_USER:-root} |
| 23 | ONOS_GROUP=${ONOS_GROUP:-$ONOS_USER} |
Bob Lantz | d22c991 | 2016-12-15 14:26:18 -0800 | [diff] [blame] | 24 | ONOS_OPTS=${ONOS_OPTS:-server} |
Jonathan Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 25 | |
Bob Lantz | 3ec585d | 2016-02-25 02:54:33 -0800 | [diff] [blame] | 26 | start () { |
Bob Lantz | d22c991 | 2016-12-15 14:26:18 -0800 | [diff] [blame] | 27 | # Fix permissions and symlinks |
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 | d22c991 | 2016-12-15 14:26:18 -0800 | [diff] [blame] | 30 | mkdir -p $ONOS_HOME/apps 2>/dev/null && chown -R $ONOS_USER:$ONOS_GROUP $ONOS_HOME/apps |
| 31 | [ ! -h $ONOS_OME/karaf ] && ln -s /opt/onos/apache-karaf* $ONOS_HOME/karaf || : |
| 32 | [ ! -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] | 33 | # Start ONOS if it's not already running |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 34 | if ! status >/dev/null; then |
| 35 | echo "Starting ONOS" |
Bob Lantz | d22c991 | 2016-12-15 14:26:18 -0800 | [diff] [blame] | 36 | startonos $ONOS_HOME/bin/onos-service $ONOS_OPTS |
Bob Lantz | 641d545 | 2016-03-04 19:39:17 -0800 | [diff] [blame] | 37 | else |
| 38 | echo "ONOS/karaf is already running" |
| 39 | fi |
Jonathan Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 42 | startonos () { |
| 43 | cmd=$1 |
| 44 | shift |
| 45 | # Start ONOS as a daemon |
Phil Huang | 5c7279e | 2016-06-24 18:19:56 +0800 | [diff] [blame] | 46 | 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 Lantz | d22c991 | 2016-12-15 14:26:18 -0800 | [diff] [blame] | 52 | if type daemon 2>/dev/null| grep -i function >/dev/null 2>&1; then |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 53 | # 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 Lantz | d22c991 | 2016-12-15 14:26:18 -0800 | [diff] [blame] | 59 | # Warning! Running as root can overwrite any linked log file. |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 60 | 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 Lantz | d22c991 | 2016-12-15 14:26:18 -0800 | [diff] [blame] | 65 | # 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 Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 68 | >$ONOS_HOME/var/stdout.log 2>$ONOS_HOME/var/stderr.log |
| 69 | fi |
| 70 | } |
| 71 | |
Jonathan Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 72 | stop () { |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 73 | if status >/dev/null; then |
| 74 | echo "Stopping ONOS" |
Bob Lantz | 641d545 | 2016-03-04 19:39:17 -0800 | [diff] [blame] | 75 | $ONOS_HOME/karaf/bin/stop |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 76 | # Wait until karaf claims not to be running |
| 77 | while status >/dev/null; do echo -n .; sleep 1; done |
Bob Lantz | 641d545 | 2016-03-04 19:39:17 -0800 | [diff] [blame] | 78 | else |
| 79 | echo "ONOS/karaf is not running" |
| 80 | fi |
Jonathan Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | restart () { |
| 84 | stop |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 85 | sleep 2 # Bogus hack since karaf stop doesn't work ;-( |
Jonathan Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 86 | start |
| 87 | } |
| 88 | |
| 89 | status () { |
Bob Lantz | 641d545 | 2016-03-04 19:39:17 -0800 | [diff] [blame] | 90 | # karaf status returns 0 if running, 1 if not |
Bob Lantz | 7c751b5 | 2016-04-15 15:42:28 -0700 | [diff] [blame] | 91 | 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 Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | case $1 in |
| 100 | start) |
| 101 | start |
Bob Lantz | d22c991 | 2016-12-15 14:26:18 -0800 | [diff] [blame] | 102 | ;; |
Jonathan Hart | f51950d | 2016-01-15 09:57:10 -0800 | [diff] [blame] | 103 | 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 | ;; |
| 118 | esac |
| 119 | |
| 120 | exit 0 |