johnbellone/consul-cookbook

View on GitHub
templates/default/sysvinit.service.erb

Summary

Maintainability
Test Coverage
#!/bin/sh
#
# <%= @name %> - this script manages the consul agent
#
# chkconfig: 345 99 70
# description: this script manages the consul agent
#
### BEGIN INIT INFO
# Provides:       <%= @name %>
# Required-Start: $local_fs $network
# Required-Stop:  $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Manage the consul agent
### END INIT INFO

prog="<%= File.basename(@daemon) %>"
user="<%= @user %>"
exec="<%= @daemon %>"
pidfile="<%= @pid_file %>"
logfile="/var/log/$prog.log"
lockfile="/var/lock/subsys/$prog"

<%- @environment.each do |key, val| -%>
export <%= key %>="<%= val %>"
<%- end -%>
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

<%- if @platform_family == 'debian' -%>
. /lib/lsb/init-functions

_start() {
    touch $logfile
    chown $user $logfile
    echo "Starting consul"
    start-stop-daemon --start --quiet --background \
        --pidfile $pidfile<% unless @pid_file_external %> --make-pidfile<% end %> \
        --chuid $user --chdir "<%= @directory %>" \
        --startas /bin/bash -- -c "exec $exec <%= @daemon_options %> >> $logfile 2>&1"
}

_stop() {
    start-stop-daemon --stop --quiet --pidfile $pidfile --user $user --retry="<%= @stop_signal %>"/30/KILL/5
}

_status() {
    status_of_proc -p $pidfile $exec $prog
}

_reload() {
    start-stop-daemon --stop --quiet --pidfile $pidfile --user $user --signal "<%= @reload_signal %>"
}

<%- else -%>
. /etc/rc.d/init.d/functions

_start() {
    [ -x $exec ] || exit 5

    umask 077
    touch $pidfile $logfile
    chown $user $pidfile $logfile

    echo -n $"Starting <%= @name %>: "
    daemon \
        --pidfile=$pidfile \
        --user=$user \
        " { $exec <%= @daemon_options %> >> $logfile 2>&1 & } ; echo \$! >| $pidfile "
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch $lockfile
    return $RETVAL
}

_stop() {
    echo -n $"Stopping <%= @name %>: "
    killproc -p $pidfile $exec -<%= @stop_signal %>
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f $lockfile $pidfile
    return $RETVAL
}

_status() {
    status -p $pidfile -l $prog $exec
}

_reload() {
    echo -n $"Reloading <%= @name %>: "
    killproc -p $pidfile $exec -<%= @reload_signal %>
    echo
}
<%- end -%>

_restart() {
    _stop
    while :
    do
        ss -pl | fgrep "((\"$prog\"," > /dev/null
        [ $? -ne 0 ] && break
        sleep 0.1
    done
    _start
}

_status_q() {
    _status >/dev/null 2>&1
}

case "$1" in
    start)
        _status_q && exit 0
        _start
        ;;
    stop)
        _status_q || exit 0
        _stop
        ;;
    restart|force-reload)
        _restart
        ;;
    reload)
        _status_q || exit 7
        _reload
        ;;
    status)
        _status
        ;;
    condrestart|try-restart)
        _status_q || exit 0
        _restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac

exit $?