diff --git a/consul.openrc b/consul.openrc new file mode 100644 index 00000000..21b08244 --- /dev/null +++ b/consul.openrc @@ -0,0 +1,55 @@ +#!/sbin/openrc-run + +if [ -z "${CONSUL_DIR}" ] ; then + CONSUL_DIR="/etc/consul.d" +fi +if [ -z "${CONSUL_PID}" ] ; then + CONSUL_PID="/run/consul.pid" +fi + +extra_started_commands="reload graceful" +required_files=$CONSUL_CONF + +depend() { + need net + use logger dns + after firewall + after famd + after sshd +} + +checkconfig() { + + if [ ! -d "${CONSUL_DIR}" ] ; then + ewarn "${CONSUL_DIR} does not exist." + return 1 + fi + + /usr/local/bin/consul validate "${CONSUL_DIR}" +} + +start() { + checkconfig || return 1 + + ebegin "Starting consul" + start-stop-daemon --start --quiet --background --make-pidfile --pidfile "${CONSUL_PID}" \ + --exec /usr/local/bin/consul -- agent -config-dir="${CONSUL_DIR}" + eend $? +} + +stop() { + local rv=0 + ebegin "Stopping consul" + start-stop-daemon --stop --quiet --pidfile "${CONSUL_PID}" + rm -f "${CONSUL_PID}" + eend $? +} + +reload() { + if ! service_started "${SVCNAME}" ; then + eerror "${SVCNAME} isn't running" + return 1 + fi + checkconfig || return 1 + eend $? +} diff --git a/consul/config.sls b/consul/config.sls index cde3836d..b766f5a5 100644 --- a/consul/config.sls +++ b/consul/config.sls @@ -1,12 +1,7 @@ {% from slspath + "/map.jinja" import consul with context %} -consul-config: +/etc/consul.d/config.json: file.serialize: - - name: /etc/consul.d/config.json - {% if consul.service != False %} - - watch_in: - - service: consul - {% endif %} - user: consul - group: consul - require: @@ -25,13 +20,8 @@ consul-script-install-{{ loop.index }}: - mode: 0755 {% endfor %} -consul-script-config: +/etc/consul.d/services.json: file.serialize: - - name: /etc/consul.d/services.json - {% if consul.service != False %} - - watch_in: - - service: consul - {% endif %} - user: consul - group: consul - require: diff --git a/consul/defaults.yaml b/consul/defaults.yaml index 170f3557..347496fb 100644 --- a/consul/defaults.yaml +++ b/consul/defaults.yaml @@ -16,3 +16,4 @@ consul: datacenter: "main" register: [] scripts: [] + startup_params: "" diff --git a/consul/files/consul.openrc b/consul/files/consul.openrc new file mode 100644 index 00000000..95986446 --- /dev/null +++ b/consul/files/consul.openrc @@ -0,0 +1,58 @@ +#!/sbin/openrc-run + +extra_started_commands="reload" + + +if [ -z "${CONSUL_DIR}" ] ; then + CONSUL_DIR="/etc/consul.d" +fi +if [ -z "${CONSUL_PID}" ] ; then + CONSUL_PID="/run/consul.pid" +fi + +required_files=$CONSUL_CONF + +depend() { + need net + use logger dns + after firewall + after famd + after sshd +} + +checkconfig() { + + if [ ! -d "${CONSUL_DIR}" ] ; then + ewarn "${CONSUL_DIR} does not exist." + return 1 + fi + + /usr/local/bin/consul validate "${CONSUL_DIR}" +} + +start() { + checkconfig || return 1 + + ebegin "Starting consul" + start-stop-daemon --start --quiet --background --make-pidfile --pidfile "${CONSUL_PID}" \ + --exec /usr/local/bin/consul -- agent -config-dir="${CONSUL_DIR}" + eend $? +} + +stop() { + local rv=0 + ebegin "Stopping consul" + start-stop-daemon --stop --quiet --pidfile "${CONSUL_PID}" + rm -f "${CONSUL_PID}" + eend $? +} + +reload() { + if ! service_started "${SVCNAME}" ; then + eerror "${SVCNAME} isn't running" + return 1 + fi + checkconfig || return 1 + /usr/local/bin/consul reload + eend $? +} diff --git a/consul/files/consul.service b/consul/files/consul.service index ecc27753..98faad3b 100644 --- a/consul/files/consul.service +++ b/consul/files/consul.service @@ -5,8 +5,8 @@ After=network.target [Service] Environment="GOMAXPROCS=2" "PATH=/usr/local/bin:/usr/bin:/bin" -ExecStart=/usr/local/bin/consul agent -config-dir=/etc/consul.d -ExecReload=/bin/kill -HUP $MAINPID +ExecStart=/usr/local/bin/consul agent -config-dir=/etc/consul.d {{ salt['pillar.get']('consul:startup_params', '') }} +ExecReload=/bin/sh -c "/usr/local/bin/consul validate /etc/consul.d && /bin/kill -HUP $MAINPID" KillSignal=TERM User=consul diff --git a/consul/install.sls b/consul/install.sls index 4370da97..cd993c87 100644 --- a/consul/install.sls +++ b/consul/install.sls @@ -47,7 +47,7 @@ consul-download: consul-extract: cmd.wait: - name: unzip /tmp/consul_{{ consul.version }}_linux_{{ consul.arch }}.zip -d /tmp - - watch: + - onchanges: - file: consul-download consul-install: @@ -56,18 +56,18 @@ consul-install: - source: /tmp/consul - require: - file: /usr/local/bin - - watch: + - onchanges: - cmd: consul-extract consul-clean: file.absent: - name: /tmp/consul_{{ consul.version }}_linux_{{ consul.arch }}.zip - - watch: + - onchanges: - file: consul-install consul-link: file.symlink: - target: consul-{{ consul.version }} - name: /usr/local/bin/consul - - watch: + - onchanges: - file: consul-install diff --git a/consul/service.sls b/consul/service.sls index 3870954d..7fd87e03 100644 --- a/consul/service.sls +++ b/consul/service.sls @@ -6,10 +6,15 @@ consul-init-file: - source: salt://{{ slspath }}/files/consul.service - name: /etc/systemd/system/consul.service - mode: 0644 + - template: jinja {%- elif salt['test.provider']('service') == 'upstart' %} - source: salt://{{ slspath }}/files/consul.upstart - name: /etc/init/consul.conf - mode: 0644 + {%- elif salt['test.provider']('service') == 'gentoo_service' %} + - source: salt://{{ slspath }}/files/consul.openrc + - name: /etc/init.d/consul + - mode: 0755 {%- else %} - source: salt://{{ slspath }}/files/consul.sysvinit - name: /etc/init.d/consul @@ -24,5 +29,5 @@ consul-service: - enable: True - watch: - file: consul-init-file - + - file: /etc/consul.d/* {%- endif %} diff --git a/pillar.example b/pillar.example index 84d8f613..30b63da6 100644 --- a/pillar.example +++ b/pillar.example @@ -1,7 +1,8 @@ consul: # Start Consul agent service and enable it at boot time service: True - + startup_params: --client "127.0.0.1" + config: server: True bind_addr: 0.0.0.0