diff --git a/consul-template/config.sls b/consul-template/config.sls index 6fad67b..208eeca 100644 --- a/consul-template/config.sls +++ b/consul-template/config.sls @@ -12,6 +12,7 @@ consul-template-tmpl-file-{{ loop.index }}: file.managed: - source: {{ tmpl.source }} - name: /etc/consul-template/tmpl-source/{{ tmpl.name }}.ctmpl + - template: {{ tmpl.template_engine }} consul-template.d-tmpl-{{ loop.index }}: file.serialize: diff --git a/consul-template/defaults.yaml b/consul-template/defaults.yaml index c541ced..6fd95b6 100644 --- a/consul-template/defaults.yaml +++ b/consul-template/defaults.yaml @@ -1,6 +1,6 @@ consul_template: - version: 0.14.0 - hash: 7c70ea5f230a70c809333e75fdcff2f6f1e838f29cfb872e1420a63cdf7f3a78 + version: 0.20.0 + hash: 500fe023c89517f959175eb79e21c33df0acf7733d3f3681ec8c5238863caf86 service: false config: @@ -9,6 +9,7 @@ consul_template: tmpl: - name: example source: salt://consul-template/files/example.ctmpl + template_engine: null # None in Python config: template: source: /etc/consul-template/tmpl-source/example.ctmpl diff --git a/consul-template/files/consul-template.service b/consul-template/files/consul-template.service index 3abe82f..1aa3f67 100644 --- a/consul-template/files/consul-template.service +++ b/consul-template/files/consul-template.service @@ -1,13 +1,17 @@ [Unit] Description=consul-template -Wants=network.target -After=network.target +Documentation=https://github.com/hashicorp/consul-template +Wants=network-online.target +After=network-online.target [Service] Environment="GOMAXPROCS=2" "PATH=/usr/local/bin:/usr/bin:/bin" ExecStart=/usr/local/bin/consul-template -config /etc/consul-template.d ExecReload=/bin/kill -HUP $MAINPID -KillSignal=TERM +KillMode=process +KillSignal=SIGTERM +Restart=on-failure +RestartSec=42s [Install] WantedBy=multi-user.target diff --git a/consul-template/service.sls b/consul-template/service.sls index 292e938..836f08a 100644 --- a/consul-template/service.sls +++ b/consul-template/service.sls @@ -20,6 +20,7 @@ consul-template-init-script: consul-template-service: service.running: - name: consul-template + - enable: True - watch: - file: /etc/consul-template.d/* {% endif %} diff --git a/consul/config.sls b/consul/config.sls index 44c39c9..2f4787d 100644 --- a/consul/config.sls +++ b/consul/config.sls @@ -3,8 +3,9 @@ consul-config: file.serialize: - name: /etc/consul.d/config.json + - encoding: utf-8 - formatter: json - - dataset: {{ consul.config }} + - dataset: {{ consul.config | json }} - user: {{ consul.user }} - group: {{ consul.group }} - mode: 0640 @@ -40,4 +41,4 @@ consul-script-config: - user: consul-user - formatter: json - dataset: - services: {{ consul.register }} + services: {{ consul.register | json }} diff --git a/consul/defaults.yaml b/consul/defaults.yaml index 80c1306..adc0409 100644 --- a/consul/defaults.yaml +++ b/consul/defaults.yaml @@ -1,13 +1,14 @@ consul: version: 1.4.0 download_host: releases.hashicorp.com + bin_dir: /usr/local/bin service: false user: consul + user_uid: group: consul - - ui_beta: false + group_gid: config: server: false @@ -16,6 +17,7 @@ consul: ui: true enable_debug: false log_level: info + encrypt: "" retry_join: [] retry_interval: 30s datacenter: "main" diff --git a/consul/files/consul.service b/consul/files/consul.service index 83686f8..c9f93d4 100644 --- a/consul/files/consul.service +++ b/consul/files/consul.service @@ -1,15 +1,20 @@ [Unit] -Description=consul -Wants=network.target -After=network.target +Description="HashiCorp Consul - A service mesh solution" +Documentation=https://www.consul.io/ +Requires=network-online.target +After=network-online.target +ConditionFileNotEmpty=/etc/consul.d/config.json [Service] EnvironmentFile=-/etc/{% if grains['os_family'] == 'Debian' %}default{% else %}sysconfig{% endif %}/consul -ExecStart=/opt/consul/bin/consul agent -config-dir=/etc/consul.d -ExecReload=/bin/kill -HUP $MAINPID -KillSignal=TERM User={{ user }} Group={{ group }} +ExecStart={{ bin_dir }}/consul agent -config-dir=/etc/consul.d +ExecReload={{ bin_dir }}/consul reload +KillMode=process +Restart=on-failure +RestartSec=42s +LimitNOFILE=65536 [Install] WantedBy=multi-user.target diff --git a/consul/files/consul.sysvinit b/consul/files/consul.sysvinit index 1853dd8..7d5c4fe 100644 --- a/consul/files/consul.sysvinit +++ b/consul/files/consul.sysvinit @@ -24,7 +24,7 @@ . /etc/rc.d/init.d/functions prog="consul" -exec="/opt/consul/bin/$prog" +exec="{{ bin_dir }}/$prog" pidfile="/var/run/$prog.pid" lockfile="/var/lock/subsys/$prog" logfile="/var/log/$prog" diff --git a/consul/files/consul.upstart b/consul/files/consul.upstart index db00a8d..8298821 100644 --- a/consul/files/consul.upstart +++ b/consul/files/consul.upstart @@ -18,7 +18,7 @@ script exec start-stop-daemon --start \ --chuid ${CONSUL_USER:-consul}:${CONSUL_GROUP:-consul} \ - --exec /opt/consul/bin/consul agent -- \ + --exec {{ bin_dir }}/consul agent -- \ -config-dir="/etc/consul.d" \ ${CONSUL_FLAGS} \ >> /var/log/consul.log 2>&1 diff --git a/consul/init.sls b/consul/init.sls index 4d31c6a..3750bd1 100644 --- a/consul/init.sls +++ b/consul/init.sls @@ -1,6 +1,9 @@ +{%- if pillar.get('consul', {}).get('enabled', True) %} {% from slspath+"/map.jinja" import consul with context %} include: - {{ slspath }}.install - {{ slspath }}.config - {{ slspath }}.service + +{%- endif %} \ No newline at end of file diff --git a/consul/install.sls b/consul/install.sls index a79d72d..180727e 100644 --- a/consul/install.sls +++ b/consul/install.sls @@ -6,7 +6,7 @@ consul-dep-unzip: consul-bin-dir: file.directory: - - name: /opt/consul/bin + - name: {{ consul.bin_dir }} - makedirs: True # Create consul user @@ -14,6 +14,9 @@ consul-group: group.present: - name: {{ consul.group }} - system: True + {% if consul.get('group_gid', None) != None -%} + - gid: {{ consul.group_gid }} + {%- endif %} consul-user: user.present: @@ -23,6 +26,9 @@ consul-user: - home: {{ salt['user.info'](consul.user)['home']|default(consul.config.data_dir) }} - createhome: False - system: True + {% if consul.get('user_uid', None) != None -%} + - uid: {{ consul.user_uid }} + {%- endif %} - require: - group: consul-group @@ -48,7 +54,7 @@ consul-download: - name: /tmp/consul_{{ consul.version }}_linux_{{ consul.arch }}.zip - source: https://{{ consul.download_host }}/consul/{{ consul.version }}/consul_{{ consul.version }}_linux_{{ consul.arch }}.zip - source_hash: https://releases.hashicorp.com/consul/{{ consul.version }}/consul_{{ consul.version }}_SHA256SUMS - - unless: test -f /opt/consul/bin/consul-{{ consul.version }} + - unless: test -f {{ consul.bin_dir }}-{{ consul.version }} consul-extract: cmd.wait: @@ -58,10 +64,10 @@ consul-extract: consul-install: file.rename: - - name: /opt/consul/bin/consul-{{ consul.version }} + - name: {{ consul.bin_dir }}/consul-{{ consul.version }} - source: /tmp/consul - require: - - file: /opt/consul/bin + - file: {{ consul.bin_dir }} - watch: - cmd: consul-extract @@ -74,6 +80,6 @@ consul-clean: consul-link: file.symlink: - target: consul-{{ consul.version }} - - name: /opt/consul/bin/consul + - name: {{ consul.bin_dir }}/consul - watch: - file: consul-install diff --git a/consul/service.sls b/consul/service.sls index e251b79..2c35fa7 100644 --- a/consul/service.sls +++ b/consul/service.sls @@ -14,27 +14,31 @@ consul-init-env: - contents: - CONSUL_USER={{ consul.user }} - CONSUL_GROUP={{ consul.group }} - - CONSUL_UI_BETA={{ consul.ui_beta }} - GOMAXPROCS=2 - PATH=/usr/local/bin:/usr/bin:/bin consul-init-file: file.managed: - {%- if salt['test.provider']('service') == 'systemd' %} + {%- if salt['test.provider']('service').startswith('systemd') %} - source: salt://{{ slspath }}/files/consul.service - name: /etc/systemd/system/consul.service - template: jinja - context: user: {{ consul.user }} group: {{ consul.group }} + bin_dir: {{ consul.bin_dir }} - mode: 0644 {%- elif salt['test.provider']('service') == 'upstart' %} - source: salt://{{ slspath }}/files/consul.upstart - name: /etc/init/consul.conf + - context: + bin_dir: {{ consul.bin_dir }} - mode: 0644 {%- else %} - source: salt://{{ slspath }}/files/consul.sysvinit - name: /etc/init.d/consul + - context: + bin_dir: {{ consul.bin_dir }} - mode: 0755 {%- endif %} diff --git a/pillar.example b/pillar.example index 0d8927f..a68b951 100644 --- a/pillar.example +++ b/pillar.example @@ -5,6 +5,10 @@ consul: # Set user and group for Consul config files and running service user: consul group: consul + bin_dir: /opt/consul/bin + + version: 0.7.0 + download_host: releases.hashicorp.com config: server: True @@ -22,6 +26,10 @@ consul: - 1.1.1.1 - 2.2.2.2 + ui: true + log_level: info + data_dir: /var/consul + register: - name: Redis checks: