From 8b250c9f62fbf68beebc5581bdb3ab8d57c109a3 Mon Sep 17 00:00:00 2001
From: Tino Vazquez <tinova@opennebula.org>
Date: Mon, 11 Nov 2013 17:33:49 +0100
Subject: [PATCH] feature #2183: Add init scripts for CentOS and Debian

(cherry picked from commit 33f535e4f1307f9ff6fd107fef4991c091fc5494)
---
 share/pkgs/CentOS/opennebula-econe    | 98 +++++++++++++++++++++++++++
 share/pkgs/Debian/opennebula-econe    | 94 +++++++++++++++++++++++++
 share/pkgs/Debian/opennebula-flow     | 94 +++++++++++++++++++++++++
 share/pkgs/Debian/opennebula-gate     | 94 +++++++++++++++++++++++++
 share/pkgs/Debian/opennebula-occi     | 94 +++++++++++++++++++++++++
 share/pkgs/Debian/opennebula-sunstone | 94 +++++++++++++++++++++++++
 6 files changed, 568 insertions(+)
 create mode 100644 share/pkgs/CentOS/opennebula-econe
 create mode 100644 share/pkgs/Debian/opennebula-econe
 create mode 100644 share/pkgs/Debian/opennebula-flow
 create mode 100644 share/pkgs/Debian/opennebula-gate
 create mode 100644 share/pkgs/Debian/opennebula-occi
 create mode 100644 share/pkgs/Debian/opennebula-sunstone

diff --git a/share/pkgs/CentOS/opennebula-econe b/share/pkgs/CentOS/opennebula-econe
new file mode 100644
index 0000000000..9e907eba3c
--- /dev/null
+++ b/share/pkgs/CentOS/opennebula-econe
@@ -0,0 +1,98 @@
+#!/bin/bash
+#
+#    /etc/rc.d/init.d/opennebula-econe
+#
+# Starts the OpenNebula ECONE Server
+#
+# chkconfig: 345 66 34
+# description: Starts the ECONE Server daemon
+# processname: opennebula-econe
+
+### BEGIN INIT INFO
+# Provides: opennebula-econe
+# Required-Start: $local_fs $remote_fs oned
+# Required-Stop: $local_fs $remote_fs oned
+# Default-Start:  2 3 4 5
+# Default-Stop: 0 1 6
+# Short-Description: start and stop opennebula-econe
+# Description: start and stop opennebula-econe
+### END INIT INFO
+
+prog="opennebula-econe"
+
+SERVER_BIN=/usr/bin/econe-server
+SERVER_PROGRAM=econe-server.rb
+LOCKFILE=/var/lock/subsys/${prog}
+PID_FILE=/var/run/${prog}.pid
+
+# Source function library.
+. /etc/rc.d/init.d/functions
+
+
+RETVAL=0
+
+check() {
+    # Check that we're a privileged user
+    [ `id -u` = 0 ] || exit 4
+
+    # Check if $SERVER_BIN is executable
+    test -x $SERVER_BIN || exit 5
+}
+
+start() {
+    check
+
+    echo -n $"Starting $prog Server: "
+    daemon --user oneadmin $SERVER_BIN start
+    RETVAL=$?
+
+    echo
+    [ $RETVAL -eq 0 ] && {
+        touch $LOCKFILE
+        echo $(ps -ef|grep $SERVER_PROGRAM | awk '{print $2}') > $PID_FILE
+    }
+
+    return $RETVAL
+}
+
+stop() {
+
+    check
+
+    echo -n $"Stopping $prog Server daemon: "
+    daemon --user oneadmin $SERVER_BIN stop
+    RETVAL=$?
+
+    [ $RETVAL -eq 0 ] && success || failure
+    echo
+    [ $RETVAL -eq 0 ] && rm -f $LOCKFILE $PID_FILE
+
+    return $RETVAL
+}
+
+restart() {
+    stop
+    start
+}
+
+
+case "$1" in
+start)
+    start
+    ;;
+stop)
+    stop
+    ;;
+restart)
+    restart
+    ;;
+status)
+    status $prog
+    RETVAL=$?
+    ;;
+*)
+    echo $"Usage: $0 {start|stop|status|restart}"
+    RETVAL=2
+esac
+
+exit $RETVAL
diff --git a/share/pkgs/Debian/opennebula-econe b/share/pkgs/Debian/opennebula-econe
new file mode 100644
index 0000000000..db2bbd625a
--- /dev/null
+++ b/share/pkgs/Debian/opennebula-econe
@@ -0,0 +1,94 @@
+#! /bin/sh
+### BEGIN INIT INFO
+# Provides:          opennebula-econe
+# Required-Start:    $remote_fs
+# Required-Stop:     $remote_fs
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: ECONE Server init script
+# Description:       OpenNebula ECONE service initialisation script
+### END INIT INFO
+
+# Author: Tino Vázquez <tinova@opennebula.org>
+
+PATH=/sbin:/usr/sbin:/bin:/usr/bin
+DESC="ECONE Service"
+NAME=econe-server
+DAEMON=/usr/bin/$NAME
+DAEMON_ARGS=""
+SCRIPTNAME=/etc/init.d/$NAME
+
+# Exit if the package is not installed
+[ -x "$DAEMON" ] || exit 0
+
+# Load the VERBOSE setting and other rcS variables
+. /lib/init/vars.sh
+
+# Define LSB log_* functions.
+# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
+. /lib/lsb/init-functions
+
+#
+# Function that starts the daemon/service
+#
+do_start()
+{
+    mkdir -p /var/run/one /var/lock/one /var/log/one
+    chown oneadmin /var/run/one /var/lock/one /var/log/one
+    su oneadmin -s /bin/sh -c "$DAEMON start"
+}
+
+#
+# Function that stops the daemon/service
+#
+do_stop()
+{
+    su oneadmin -s /bin/sh -c "$DAEMON stop"
+}
+
+case "$1" in
+  start)
+    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
+    do_start
+    case "$?" in
+        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+    esac
+    ;;
+  stop)
+    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
+    do_stop
+    case "$?" in
+        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+    esac
+    ;;
+  restart|force-reload)
+    #
+    # If the "reload" option is implemented then remove the
+    # 'force-reload' alias
+    #
+    log_daemon_msg "Restarting $DESC" "$NAME"
+    do_stop
+    case "$?" in
+      0|1)
+        do_start
+        case "$?" in
+            0) log_end_msg 0 ;;
+            1) log_end_msg 1 ;; # Old process is still running
+            *) log_end_msg 1 ;; # Failed to start
+        esac
+        ;;
+      *)
+          # Failed to stop
+        log_end_msg 1
+        ;;
+    esac
+    ;;
+  *)
+    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
+    exit 3
+    ;;
+esac
+
+:
diff --git a/share/pkgs/Debian/opennebula-flow b/share/pkgs/Debian/opennebula-flow
new file mode 100644
index 0000000000..6467dd9476
--- /dev/null
+++ b/share/pkgs/Debian/opennebula-flow
@@ -0,0 +1,94 @@
+#! /bin/sh
+### BEGIN INIT INFO
+# Provides:          opennebula-flow
+# Required-Start:    $remote_fs
+# Required-Stop:     $remote_fs
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: OneFlow init script
+# Description:       OpenNebula OneFlow service initialisation script
+### END INIT INFO
+
+# Author: Tino Vázquez <tinova@opennebula.org>
+
+PATH=/sbin:/usr/sbin:/bin:/usr/bin
+DESC="OneFlow Service"
+NAME=oneflow-server
+DAEMON=/usr/bin/$NAME
+DAEMON_ARGS=""
+SCRIPTNAME=/etc/init.d/$NAME
+
+# Exit if the package is not installed
+[ -x "$DAEMON" ] || exit 0
+
+# Load the VERBOSE setting and other rcS variables
+. /lib/init/vars.sh
+
+# Define LSB log_* functions.
+# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
+. /lib/lsb/init-functions
+
+#
+# Function that starts the daemon/service
+#
+do_start()
+{
+    mkdir -p /var/run/one /var/lock/one /var/log/one
+    chown oneadmin /var/run/one /var/lock/one /var/log/one
+    su oneadmin -s /bin/sh -c "$DAEMON start"
+}
+
+#
+# Function that stops the daemon/service
+#
+do_stop()
+{
+    su oneadmin -s /bin/sh -c "$DAEMON stop"
+}
+
+case "$1" in
+  start)
+    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
+    do_start
+    case "$?" in
+        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+    esac
+    ;;
+  stop)
+    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
+    do_stop
+    case "$?" in
+        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+    esac
+    ;;
+  restart|force-reload)
+    #
+    # If the "reload" option is implemented then remove the
+    # 'force-reload' alias
+    #
+    log_daemon_msg "Restarting $DESC" "$NAME"
+    do_stop
+    case "$?" in
+      0|1)
+        do_start
+        case "$?" in
+            0) log_end_msg 0 ;;
+            1) log_end_msg 1 ;; # Old process is still running
+            *) log_end_msg 1 ;; # Failed to start
+        esac
+        ;;
+      *)
+          # Failed to stop
+        log_end_msg 1
+        ;;
+    esac
+    ;;
+  *)
+    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
+    exit 3
+    ;;
+esac
+
+:
diff --git a/share/pkgs/Debian/opennebula-gate b/share/pkgs/Debian/opennebula-gate
new file mode 100644
index 0000000000..613f2614ef
--- /dev/null
+++ b/share/pkgs/Debian/opennebula-gate
@@ -0,0 +1,94 @@
+#! /bin/sh
+### BEGIN INIT INFO
+# Provides:          opennebula-gate
+# Required-Start:    $remote_fs
+# Required-Stop:     $remote_fs
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: OneGate init script
+# Description:       OpenNebula OneGate service initialisation script
+### END INIT INFO
+
+# Author: Tino Vázquez <tinova@opennebula.org>
+
+PATH=/sbin:/usr/sbin:/bin:/usr/bin
+DESC="OneGate Service"
+NAME=onegate-server
+DAEMON=/usr/bin/$NAME
+DAEMON_ARGS=""
+SCRIPTNAME=/etc/init.d/$NAME
+
+# Exit if the package is not installed
+[ -x "$DAEMON" ] || exit 0
+
+# Load the VERBOSE setting and other rcS variables
+. /lib/init/vars.sh
+
+# Define LSB log_* functions.
+# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
+. /lib/lsb/init-functions
+
+#
+# Function that starts the daemon/service
+#
+do_start()
+{
+    mkdir -p /var/run/one /var/lock/one /var/log/one
+    chown oneadmin /var/run/one /var/lock/one /var/log/one
+    su oneadmin -s /bin/sh -c "$DAEMON start"
+}
+
+#
+# Function that stops the daemon/service
+#
+do_stop()
+{
+    su oneadmin -s /bin/sh -c "$DAEMON stop"
+}
+
+case "$1" in
+  start)
+    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
+    do_start
+    case "$?" in
+        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+    esac
+    ;;
+  stop)
+    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
+    do_stop
+    case "$?" in
+        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+    esac
+    ;;
+  restart|force-reload)
+    #
+    # If the "reload" option is implemented then remove the
+    # 'force-reload' alias
+    #
+    log_daemon_msg "Restarting $DESC" "$NAME"
+    do_stop
+    case "$?" in
+      0|1)
+        do_start
+        case "$?" in
+            0) log_end_msg 0 ;;
+            1) log_end_msg 1 ;; # Old process is still running
+            *) log_end_msg 1 ;; # Failed to start
+        esac
+        ;;
+      *)
+          # Failed to stop
+        log_end_msg 1
+        ;;
+    esac
+    ;;
+  *)
+    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
+    exit 3
+    ;;
+esac
+
+:
diff --git a/share/pkgs/Debian/opennebula-occi b/share/pkgs/Debian/opennebula-occi
new file mode 100644
index 0000000000..c0816c2811
--- /dev/null
+++ b/share/pkgs/Debian/opennebula-occi
@@ -0,0 +1,94 @@
+#! /bin/sh
+### BEGIN INIT INFO
+# Provides:          opennebula-occi
+# Required-Start:    $remote_fs
+# Required-Stop:     $remote_fs
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: OCCI Server init script
+# Description:       OpenNebula OCCI service initialisation script
+### END INIT INFO
+
+# Author: Tino Vázquez <tinova@opennebula.org>
+
+PATH=/sbin:/usr/sbin:/bin:/usr/bin
+DESC="OCCI Service"
+NAME=occi-server
+DAEMON=/usr/bin/$NAME
+DAEMON_ARGS=""
+SCRIPTNAME=/etc/init.d/$NAME
+
+# Exit if the package is not installed
+[ -x "$DAEMON" ] || exit 0
+
+# Load the VERBOSE setting and other rcS variables
+. /lib/init/vars.sh
+
+# Define LSB log_* functions.
+# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
+. /lib/lsb/init-functions
+
+#
+# Function that starts the daemon/service
+#
+do_start()
+{
+    mkdir -p /var/run/one /var/lock/one /var/log/one
+    chown oneadmin /var/run/one /var/lock/one /var/log/one
+    su oneadmin -s /bin/sh -c "$DAEMON start"
+}
+
+#
+# Function that stops the daemon/service
+#
+do_stop()
+{
+    su oneadmin -s /bin/sh -c "$DAEMON stop"
+}
+
+case "$1" in
+  start)
+    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
+    do_start
+    case "$?" in
+        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+    esac
+    ;;
+  stop)
+    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
+    do_stop
+    case "$?" in
+        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+    esac
+    ;;
+  restart|force-reload)
+    #
+    # If the "reload" option is implemented then remove the
+    # 'force-reload' alias
+    #
+    log_daemon_msg "Restarting $DESC" "$NAME"
+    do_stop
+    case "$?" in
+      0|1)
+        do_start
+        case "$?" in
+            0) log_end_msg 0 ;;
+            1) log_end_msg 1 ;; # Old process is still running
+            *) log_end_msg 1 ;; # Failed to start
+        esac
+        ;;
+      *)
+          # Failed to stop
+        log_end_msg 1
+        ;;
+    esac
+    ;;
+  *)
+    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
+    exit 3
+    ;;
+esac
+
+:
diff --git a/share/pkgs/Debian/opennebula-sunstone b/share/pkgs/Debian/opennebula-sunstone
new file mode 100644
index 0000000000..f7917044d3
--- /dev/null
+++ b/share/pkgs/Debian/opennebula-sunstone
@@ -0,0 +1,94 @@
+#! /bin/sh
+### BEGIN INIT INFO
+# Provides:          opennebula-sunstone
+# Required-Start:    $remote_fs
+# Required-Stop:     $remote_fs
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Sunstone init script
+# Description:       OpenNebula Sunstone web interface cloud initialisation script
+### END INIT INFO
+
+# Author: Jaime Melis <jmelis@opennebula.org>
+
+PATH=/sbin:/usr/sbin:/bin:/usr/bin
+DESC="Sunstone Web interface"
+NAME=sunstone-server
+DAEMON=/usr/bin/$NAME
+DAEMON_ARGS=""
+SCRIPTNAME=/etc/init.d/$NAME
+
+# Exit if the package is not installed
+[ -x "$DAEMON" ] || exit 0
+
+# Load the VERBOSE setting and other rcS variables
+. /lib/init/vars.sh
+
+# Define LSB log_* functions.
+# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
+. /lib/lsb/init-functions
+
+#
+# Function that starts the daemon/service
+#
+do_start()
+{
+    mkdir -p /var/run/one /var/lock/one /var/log/one
+    chown oneadmin /var/run/one /var/lock/one /var/log/one
+    su oneadmin -s /bin/sh -c "$DAEMON start"
+}
+
+#
+# Function that stops the daemon/service
+#
+do_stop()
+{
+    su oneadmin -s /bin/sh -c "$DAEMON stop"
+}
+
+case "$1" in
+  start)
+    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
+    do_start
+    case "$?" in
+        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+    esac
+    ;;
+  stop)
+    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
+    do_stop
+    case "$?" in
+        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+    esac
+    ;;
+  restart|force-reload)
+    #
+    # If the "reload" option is implemented then remove the
+    # 'force-reload' alias
+    #
+    log_daemon_msg "Restarting $DESC" "$NAME"
+    do_stop
+    case "$?" in
+      0|1)
+        do_start
+        case "$?" in
+            0) log_end_msg 0 ;;
+            1) log_end_msg 1 ;; # Old process is still running
+            *) log_end_msg 1 ;; # Failed to start
+        esac
+        ;;
+      *)
+          # Failed to stop
+        log_end_msg 1
+        ;;
+    esac
+    ;;
+  *)
+    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
+    exit 3
+    ;;
+esac
+
+: