diff --git a/README.rst b/README.rst index e0c69ea..00cf2c3 100644 --- a/README.rst +++ b/README.rst @@ -18,3 +18,7 @@ Available states Installs and configures the Consul service. +``consul-template`` +------------------- + +Installs and configures Consul template. diff --git a/consul-template/config.sls b/consul-template/config.sls new file mode 100644 index 0000000..6fad67b --- /dev/null +++ b/consul-template/config.sls @@ -0,0 +1,22 @@ +{% from "consul-template/map.jinja" import consul_template with context %} + +consul-template-config: + file.managed: + - source: salt://consul-template/files/config.json + - template: jinja + - name: /etc/consul-template.d/config.json + +{% if consul_template.tmpl %} +{% for tmpl in consul_template.tmpl %} +consul-template-tmpl-file-{{ loop.index }}: + file.managed: + - source: {{ tmpl.source }} + - name: /etc/consul-template/tmpl-source/{{ tmpl.name }}.ctmpl + +consul-template.d-tmpl-{{ loop.index }}: + file.serialize: + - name: /etc/consul-template.d/{{ tmpl.name }}.json + - dataset: {{ tmpl.config }} + - formatter: json +{% endfor %} +{% endif %} diff --git a/consul-template/defaults.yaml b/consul-template/defaults.yaml new file mode 100644 index 0000000..c541ced --- /dev/null +++ b/consul-template/defaults.yaml @@ -0,0 +1,15 @@ +consul_template: + version: 0.14.0 + hash: 7c70ea5f230a70c809333e75fdcff2f6f1e838f29cfb872e1420a63cdf7f3a78 + + service: false + config: + consul: 127.0.0.1:8500 + log_level: info + tmpl: + - name: example + source: salt://consul-template/files/example.ctmpl + config: + template: + source: /etc/consul-template/tmpl-source/example.ctmpl + destination: /etc/consul-template/example diff --git a/consul-template/files/config.json b/consul-template/files/config.json new file mode 100644 index 0000000..9d4975b --- /dev/null +++ b/consul-template/files/config.json @@ -0,0 +1,2 @@ +{% from "consul-template/map.jinja" import consul_template with context %} +{{ consul_template.config | json }} diff --git a/consul-template/files/consul-template.service b/consul-template/files/consul-template.service new file mode 100644 index 0000000..3abe82f --- /dev/null +++ b/consul-template/files/consul-template.service @@ -0,0 +1,13 @@ +[Unit] +Description=consul-template +Wants=network.target +After=network.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 + +[Install] +WantedBy=multi-user.target diff --git a/consul-template/files/example.ctmpl b/consul-template/files/example.ctmpl new file mode 100644 index 0000000..ac67c51 --- /dev/null +++ b/consul-template/files/example.ctmpl @@ -0,0 +1 @@ +{{with node}}{{.Node.Node}}{{end}} diff --git a/consul-template/init.sls b/consul-template/init.sls new file mode 100644 index 0000000..fa3b68c --- /dev/null +++ b/consul-template/init.sls @@ -0,0 +1,7 @@ +{% from "consul-template/map.jinja" import consul with context %} + +include: + - consul + - consul-template.install + - consul-template.config + - consul-template.service diff --git a/consul-template/install.sls b/consul-template/install.sls new file mode 100644 index 0000000..2185847 --- /dev/null +++ b/consul-template/install.sls @@ -0,0 +1,46 @@ +{% from "consul-template/map.jinja" import consul_template with context %} + +consul-template-config-dir: + file.directory: + - name: /etc/consul-template.d + +consul-template-template-dir: + file.directory: + - name: /etc/consul-template/tmpl-source + - makedirs: True + +# Install template renderer +consul-template-download: + file.managed: + - name: /tmp/consul_template_{{ consul_template.version }}_linux_amd64.zip + - source: https://releases.hashicorp.com/consul-template/{{ consul_template.version }}/consul-template_{{ consul_template.version }}_linux_amd64.zip + - source_hash: sha256={{ consul_template.hash }} + - unless: test -f /usr/local/bin/consul-template-{{ consul_template.version }} + +consul-template-extract: + cmd.wait: + - name: unzip /tmp/consul_template_{{ consul_template.version }}_linux_amd64.zip -d /tmp + - watch: + - file: consul-template-download + +consul-template-install: + file.rename: + - name: /usr/local/bin/consul-template-{{ consul_template.version }} + - source: /tmp/consul-template + - require: + - file: /usr/local/bin + - watch: + - cmd: consul-template-extract + +consul-template-clean: + file.absent: + - name: /tmp/consul_template_{{ consul_template.version }}_linux_amd64.zip + - watch: + - file: consul-template-install + +consul-template-link: + file.symlink: + - target: consul-template-{{ consul_template.version }} + - name: /usr/local/bin/consul-template + - watch: + - file: consul-template-install diff --git a/consul-template/map.jinja b/consul-template/map.jinja new file mode 100644 index 0000000..fc2e0cb --- /dev/null +++ b/consul-template/map.jinja @@ -0,0 +1,3 @@ +{% import_yaml "consul-template/defaults.yaml" as defaults %} + +{% set consul_template = salt['pillar.get']('consul_template', default=defaults.consul_template, merge=True) %} diff --git a/consul-template/service.sls b/consul-template/service.sls new file mode 100644 index 0000000..022c5c7 --- /dev/null +++ b/consul-template/service.sls @@ -0,0 +1,21 @@ +{% from "consul-template/map.jinja" import consul_template with context %} + +consul-template-init-script: + file.managed: + {% if salt['test.provider']('service') == 'systemd' %} + - source: salt://consul-template/files/consul-template.service + - name: /etc/systemd/system/consul-template.service + - mode: 0644 + {% endif %} + {% if consul_template.service != False %} + - watch_in: + - service: consul + {% endif %} + +{% if consul_template.service != False %} +consul-template-service: + service.running: + - name: consul-template + - watch: + - file: /etc/consul-template.d/* +{% endif %} diff --git a/consul/defaults.yaml b/consul/defaults.yaml index 2188417..429f4f7 100644 --- a/consul/defaults.yaml +++ b/consul/defaults.yaml @@ -5,9 +5,6 @@ consul: ui_version: 0.6.1 ui_hash: afccdd540b166b778c7c0483becc5e282bbbb1ee52335bfe94bf757df8c55efc - template_version: 0.12.1 - template_hash: 693c52a2b1aed9b63584f47c377efd0fc349df3616985a0709246faaf9a80c8e - service: false config: server: false diff --git a/consul/files/consul.service b/consul/files/consul.service new file mode 100644 index 0000000..ecc2775 --- /dev/null +++ b/consul/files/consul.service @@ -0,0 +1,14 @@ +[Unit] +Description=consul +Wants=network.target +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 +KillSignal=TERM +User=consul + +[Install] +WantedBy=multi-user.target diff --git a/consul/install.sls b/consul/install.sls index a7fb559..0515edf 100644 --- a/consul/install.sls +++ b/consul/install.sls @@ -40,7 +40,7 @@ consul-data-dir: - name: /usr/local/share/consul - user: consul - group: consul - - makedirs: + - makedirs: # Install agent consul-download: @@ -113,39 +113,3 @@ consul-ui-link: - name: /usr/local/share/consul/ui - watch: - file: consul-ui-install - -# Install template renderer -consul-template-download: - file.managed: - - name: /tmp/consul_template_{{ consul.template_version }}_linux_amd64.zip - - source: https://releases.hashicorp.com/consul-template/{{ consul.template_version }}/consul-template_{{ consul.template_version }}_linux_amd64.zip - - source_hash: sha256={{ consul.template_hash }} - - unless: test -f /usr/local/bin/consul-template-{{ consul.template_version }} - -consul-template-extract: - cmd.wait: - - name: unzip /tmp/consul_template_{{ consul.template_version }}_linux_amd64.zip -d /tmp - - watch: - - file: consul-template-download - -consul-template-install: - file.rename: - - name: /usr/local/bin/consul-template-{{ consul.template_version }} - - source: /tmp/consul-template - - require: - - file: /usr/local/bin - - watch: - - cmd: consul-template-extract - -consul-template-clean: - file.absent: - - name: /tmp/consul_template_{{ consul.template_version }}_linux_amd64.zip - - watch: - - file: consul-template-install - -consul-template-link: - file.symlink: - - target: consul-template-{{ consul.template_version }} - - name: /usr/local/bin/consul-template - - watch: - - file: consul-template-install diff --git a/consul/service.sls b/consul/service.sls index afbfc38..d18ce87 100644 --- a/consul/service.sls +++ b/consul/service.sls @@ -2,7 +2,11 @@ consul-init-script: file.managed: - {% if salt['test.provider']('service') == 'upstart' %} + {% if salt['test.provider']('service') == 'systemd' %} + - source: salt://consul/files/consul.service + - name: /etc/systemd/system/consul.service + - mode: 0644 + {% elif salt['test.provider']('service') == 'upstart' %} - source: salt://consul/files/consul.upstart - name: /etc/init/consul.conf - mode: 0644 diff --git a/pillar.example b/pillar.example index 4fb85ef..46cf4eb 100644 --- a/pillar.example +++ b/pillar.example @@ -25,3 +25,17 @@ consul: scripts: - source: salt://files/consul/check_redis.py name: /usr/local/share/consul/check_redis.py + +consul_template: + service: true + config: + consul: 127.0.0.1:8500 + log_level: info + tmpl: + - name: example.com + source: salt://files/example.com.ctmpl + config: + template: + source: /etc/consul-template/tmpl-source/example.com.ctmpl + destination: /etc/nginx/sites-enabled/example.com + command: systemctl restart nginx