Initial commit

This commit is contained in:
Bahadır Kandemir 2015-10-07 21:18:22 +03:00
commit 3676273bc4
10 changed files with 381 additions and 0 deletions

13
LICENSE Normal file
View file

@ -0,0 +1,13 @@
Copyright (c) 2013-2015 Salt Stack Formulas
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

20
README Normal file
View file

@ -0,0 +1,20 @@
======
consul
======
.. note::
See the full `Salt Formulas installation and usage instructions
<http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html>`_.
Available states
================
.. contents::
:local:
``consul``
------------
Installs and configures the Consul service.

7
consul/defaults.yaml Normal file
View file

@ -0,0 +1,7 @@
consul:
server: false
client: false
service: false
token: ""
bootstrap: 1
datacenter: "main"

7
consul/files/common.json Normal file
View file

@ -0,0 +1,7 @@
{
"data_dir": "/var/consul",
"ui_dir": "/opt/consul/dist",
"enable_debug": true,
"log_level": "info",
"encrypt": "{{ salt['pillar.get']('consul:token') }}"
}

View file

@ -0,0 +1,194 @@
#!/bin/bash
#
# consul Manage the consul agent
#
# chkconfig: 2345 95 95
# description: Consul is a tool for service discovery and configuration
# processname: consul
# config: /etc/consul.conf
# pidfile: /var/run/consul.pid
### BEGIN INIT INFO
# Provides: consul
# Required-Start: $local_fs $network
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Manage the consul agent
# Description: Consul is a tool for service discovery and configuration
### END INIT INFO
# source function library
. /etc/rc.d/init.d/functions
prog="consul"
user="consul"
exec="/usr/local/bin/$prog"
pidfile="/var/run/$prog.pid"
lockfile="/var/lock/subsys/$prog"
logfile="/var/log/$prog"
confdir="/etc/consul.d"
# pull in sysconfig settings
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
export GOMAXPROCS=${GOMAXPROCS:-2}
start() {
[ -x $exec ] || exit 5
[ -d $confdir ] || exit 6
umask 077
touch $logfile $pidfile
chown $user:$user $logfile $pidfile
echo -n $"Starting $prog: "
## holy shell shenanigans, batman!
## daemon can't be backgrounded. we need the pid of the spawned process,
## which is actually done via runuser thanks to --user.
## you can't do "cmd &; action" but you can do "{cmd &}; action".
## consul 0.2.1 added -pid-file; although the following creates $pidfile
## owned by consul:consul, using -pid-file results in a permission error.
daemon \
--pidfile=$pidfile \
--user=consul \
" { $exec agent -config-dir=$confdir &>> $logfile & } ; echo \$! >| $pidfile "
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $lockfile
echo -n $"Waiting for Consul ready: "
## wait up to 60s for the rpc port to become listened-upon
## consul 0.2.1 got much slower to start!
count=0
ready=0
pid=$( cat ${pidfile} )
while checkpid ${pid} && [ $count -lt 60 ] && [ $ready -ne 1 ]; do
count=$(( count + 1 ))
if netstat -lptn | egrep -q ":8400.*LISTEN +${pid}/" ; then
ready=1
else
sleep 1
fi
done
if [ $ready -eq 1 ]; then
RETVAL=0
success
else
RETVAL=1
failure
fi
echo
return $RETVAL
}
stop() {
echo -n $"Shutting down $prog: "
## graceful shutdown with leave
$exec leave &> /dev/null
RETVAL=$?
## wait up to 10s for the daemon to exit
if [ $RETVAL -eq 0 ]; then
count=0
stopped=0
pid=$( cat ${pidfile} )
while [ $count -lt 10 ] && [ $stopped -ne 1 ]; do
count=$(( count + 1 ))
if ! checkpid ${pid} ; then
stopped=1
else
sleep 1
fi
done
if [ $stopped -ne 1 ]; then
RETVAL=125
fi
fi
if [ $RETVAL -eq 0 ]; then
success
rm -f $lockfile $pidfile
else
failure
fi
echo
return $RETVAL
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $prog: "
killproc -p $pidfile $exec -HUP
echo
}
force_reload() {
restart
}
rh_status() {
status -p "$pidfile" -l $prog $exec
RETVAL=$?
[ $RETVAL -eq 0 ] && $exec members
return $RETVAL
}
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 $?

View file

@ -0,0 +1,25 @@
description "Consul agent"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
script
if [ -f "/etc/service/consul" ]; then
. /etc/service/consul
fi
# Make sure to use all our CPUs, because Consul can block a scheduler thread
export GOMAXPROCS=`nproc`
# Get the public IP
BIND=`ifconfig eth0 | grep "inet addr" | awk '{ print substr($2,6) }'`
exec start-stop-daemon --start -c consul \
--exec /usr/local/bin/consul agent -- \
-config-dir="/etc/consul.d" \
-bind=$BIND \
${CONSUL_FLAGS} \
>> /var/log/consul.log 2>&1
end script

5
consul/files/server.json Normal file
View file

@ -0,0 +1,5 @@
{
"server": true,
"bootstrap_expect": {{ salt['pillar.get']('consul:bootstrap') }},
"datacenter": "{{ salt['pillar.get']('consul:datacenter') }}"
}

101
consul/init.sls Normal file
View file

@ -0,0 +1,101 @@
{% from "consul/map.jinja" import consul with context %}
unzip:
pkg.installed
consul_download:
archive.extracted:
- name: /opt/consul
- source: http://dl.bintray.com/mitchellh/consul/0.5.2_linux_amd64.zip
- source_hash: sha1=b3ae610c670fc3b81737d44724ebde969da66ebf
- archive_format: zip
- require:
- pkg: unzip
consul_ui_download:
archive.extracted:
- name: /opt/consul
- source: http://dl.bintray.com/mitchellh/consul/0.5.2_web_ui.zip
- source_hash: sha1=67a2665e3c6aa6ca95c24d6176641010a1002cd6
- archive_format: zip
- if_missing: /opt/consul/dist
- require:
- pkg: unzip
consul_link:
file.symlink:
- target: /opt/consul/consul
- name: /usr/local/bin/consul
consul_user:
group.present:
- name: consul
user.present:
- name: consul
- createhome: false
- system: true
- groups:
- consul
- require:
- group: consul
consul_init_script:
file.managed:
{% if salt['test.provider']('service') == 'upstart' %}
- source: salt://consul/files/consul.upstart
- name: /etc/init/consul.conf
- mode: 0644
{% else %}
- source: salt://consul/files/consul.sysvinit
- name: /etc/init.d/consul
- mode: 0755
{% endif %}
consul_config_dir:
file.directory:
- name: /etc/consul.d
- user: consul
- group: consul
consul_data_dir:
file.directory:
- name: /var/consul
- user: consul
- group: consul
consul_common_config:
file.managed:
- source: salt://consul/files/common.json
- template: jinja
- name: /etc/consul.d/common.json
{% if consul.service != False %}
- watch_in:
- service: consul
{% endif %}
- user: consul
- group: consul
- require:
- user: consul
{% if consul.server != False %}
consul_server_config:
file.managed:
- source: salt://consul/files/server.json
- name: /etc/consul.d/server.json
- template: jinja
{% if consul.service != False %}
- watch_in:
- service: consul
{% endif %}
- user: consul
- group: consul
- require:
- user: consul
{% endif %}
{% if consul.service != False %}
consul_service:
service.running:
- name: consul
{% endif %}

3
consul/map.jinja Normal file
View file

@ -0,0 +1,3 @@
{% import_yaml "consul/defaults.yaml" as defaults %}
{% set consul = salt['pillar.get']('consul', default=defaults.consul, merge=True) %}

6
pillar.example Normal file
View file

@ -0,0 +1,6 @@
consul:
server: true
service: true
token: "RIxqpNlOXqtr/j4BgvIMEw=="
bootstrap: 3
datacenter: "eu"