Skip to content

Commit

Permalink
Add custom template for "sysvinit" service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
legal90 committed Jan 28, 2016
1 parent c8f5187 commit 93cb534
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
1 change: 1 addition & 0 deletions libraries/consul_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def service_options(service)
service.environment(new_resource.environment)
service.restart_on_update(true)
service.options(:systemd, template: 'consul:systemd.service.erb')
service.options(:sysvinit, template: 'consul:sysvinit.service.erb')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def command(config_file, config_dir)
if windows?
%{agent -config-file="""#{config_file}""" -config-dir="""#{config_dir}"""}
else
"/usr/bin/env consul agent -config-file=#{config_file} -config-dir=#{config_dir}"
"consul agent -config-file=#{config_file} -config-dir=#{config_dir}"
end
end

Expand Down
131 changes: 131 additions & 0 deletions templates/default/sysvinit.service.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#!/bin/sh
#
# <%= @name %> - this script manages the consul agent
#
### BEGIN INIT INFO
# Provides: <%= @name %>
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: Manage the consul agent
### END INIT INFO

prog="<%= File.basename(@daemon) %>"
user="<%= @user %>"
exec="<%= @daemon %>"
pidfile="<%= @pid_file %>"
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() {
start-stop-daemon --start --quiet --background \
--pidfile $pidfile<% unless @pid_file_external %> --make-pidfile<% end %> \
--chuid $user --chdir "<%= @directory %>" \
--exec $exec -- <%= @daemon_options %>
}

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

_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
chown $user $pidfile

echo -n $"Starting <%= @name %>: "
daemon \
--pidfile=$pidfile \
--user=$user \
" { $exec <%= @daemon_options %> &> /dev/null & } ; 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 $?

0 comments on commit 93cb534

Please sign in to comment.