Skip to content

Instantly share code, notes, and snippets.

@vadimii
Last active December 3, 2015 00:21
Show Gist options
  • Save vadimii/fae63c83d1d04f9b74cc to your computer and use it in GitHub Desktop.
Save vadimii/fae63c83d1d04f9b74cc to your computer and use it in GitHub Desktop.
Fedora SysV Initscript for Consul
#!/bin/sh
#
# consul Consul Service Discovery Platform
#
# chkconfig: 2345 20 80
# description: Consul is a tool for discovering and configuring services
# in your infrastructure. It provides several key features:
# * Service Discovery
# * Health Checking
# * Key/Valuye Store
# * Multi Datacenter
### BEGIN INIT INFO
# Provides: consul
# Required-Start: $network
# Required-Stop: $network
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Consul Service Discovery Platform
# Description: Consul is a tool for discovering and configuring services
# in your infrastructure. It provides several key features:
# * Service Discovery
# * Health Checking
# * Key/Valuye Store
# * Multi Datacenter
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
export GOMAXPROCS=${GOMAXPROCS}
exec="/opt/consul/consul"
prog="consul"
config="/etc/consul.d"
pidfile="/var/run/$prog.pid"
logfile="/var/log/$prog.log"
cmd="$exec agent -config-file $config -ui-dir /opt/consul/ui"
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
lockfile=/var/lock/subsys/$prog
get_pid() {
cat "$pidfile"
}
is_running() {
[ -f "$pidfile" ] && ps `get_pid` > /dev/null 2>&1
}
start() {
[ -x $exec ] || exit 5
# [ -f $config ] || exit 6
echo -n $"Starting $prog: "
if is_running; then
echo "$prog already running"
else
echo "Starting $prog"
$cmd >> "$logfile" &
echo $! > "$pidfile"
if ! is_running; then
echo "Unable to start $prog, see $logfile"
exit 1
fi
fi
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
if is_running; then
echo -n "Stopping $prog..."
kill -INT `get_pid`
for i in 1 2 3 4 5 6 7 8 9 10
do
if ! is_running; then
break
fi
echo -n "."
sleep 1
done
echo
if is_running; then
echo "$prog not stopped; may still be shutting down or shutdown may have failed"
exit 1
else
echo "$prog stopped"
if [ -f "$pidfile" ]; then
rm "$pidfile"
fi
fi
else
echo "$prog not running"
fi
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
if is_running; then
echo -n "Reloading $prog..."
kill -HUP `get_pid`
sleep 1
echo
if ! is_running; then
echo "$prog has died, see $logfile"
exit 1
fi
else
echo "$prog not running"
fi
}
force_reload() {
restart
}
rh_status() {
# run checks to determine if the service is running or use generic status
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
exit $?
{
"datacenter": "dc1",
"node_name": "{{ name }}",
"data_dir": "/var/lib/consul",
"log_level": "INFO",
"server": {{ 'true' if server == inventory_hostname else 'false'}},
"bootstrap": {{ 'true' if server == inventory_hostname else 'false'}},
"addresses": {
"dns": "0.0.0.0",
"http": "0.0.0.0"
},
"ports": {
"dns": 53
}
}
---
# Ansible Playbook
- hosts: consul
vars:
consul_ver: 0.4.1
consul_arch: "{{ consul_ver }}_linux_amd64.zip"
consul_web_arch: "{{ consul_ver }}_web_ui.zip"
consul_dist: https://dl.bintray.com/mitchellh/consul/{{ consul_arch }}
consul_web_dist: >
https://dl.bintray.com/mitchellh/consul/{{ consul_web_arch }}
tasks:
- name: download consul
shell: |
curl -sSLfO {{ consul_dist }}
curl -sSLfO {{ consul_web_dist }}
unzip {{ consul_arch }}
unzip {{ consul_web_arch }}
mkdir -p /opt/consul_{{ consul_ver }}/ui
mv consul /opt/consul_{{ consul_ver }}
mv consul dist/* /opt/consul_{{ consul_ver }}/ui
ln -sf /opt/consul_{{ consul_ver }} /opt/consul
rm -rf {{ consul_arch }} {{ consul_web_arch }} dist
args:
creates: /opt/consul_{{ consul_ver }}/consul
- name: copy init script
copy: >
src=conf/etc/init.d/consul
dest=/etc/init.d/consul
mode=0755
notify: restart consul
- name: copy sysconfig
copy: >
src=conf/etc/sysconfig/consul
dest=/etc/sysconfig/consul
notify: restart consul
- name: ensure consul.d dir exists
file: path=/etc/consul.d state=directory
- name: ensure data dir exists
file: path=/var/lib/consul state=directory
- name: copy configuration
template: >
src=conf/etc/consul.d/consul.json
dest=/etc/consul.d/consul.json
notify: restart consul
- name: enable consul service
service: name=consul enabled=yes
handlers:
- name: restart consul
service: name=consul state=restarted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment