Merge pull request #6 from grobinson-blockchain/master

Separate consul and consul-template states
This commit is contained in:
Wolodja Wentland 2016-05-16 20:26:42 +02:00
commit 54fd5045a7
15 changed files with 168 additions and 41 deletions

View file

@ -18,3 +18,7 @@ Available states
Installs and configures the Consul service. Installs and configures the Consul service.
``consul-template``
-------------------
Installs and configures Consul template.

View file

@ -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 %}

View file

@ -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

View file

@ -0,0 +1,2 @@
{% from "consul-template/map.jinja" import consul_template with context %}
{{ consul_template.config | json }}

View file

@ -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

View file

@ -0,0 +1 @@
{{with node}}{{.Node.Node}}{{end}}

7
consul-template/init.sls Normal file
View file

@ -0,0 +1,7 @@
{% from "consul-template/map.jinja" import consul with context %}
include:
- consul
- consul-template.install
- consul-template.config
- consul-template.service

View file

@ -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

View file

@ -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) %}

View file

@ -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 %}

View file

@ -5,9 +5,6 @@ consul:
ui_version: 0.6.1 ui_version: 0.6.1
ui_hash: afccdd540b166b778c7c0483becc5e282bbbb1ee52335bfe94bf757df8c55efc ui_hash: afccdd540b166b778c7c0483becc5e282bbbb1ee52335bfe94bf757df8c55efc
template_version: 0.12.1
template_hash: 693c52a2b1aed9b63584f47c377efd0fc349df3616985a0709246faaf9a80c8e
service: false service: false
config: config:
server: false server: false

View file

@ -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

View file

@ -40,7 +40,7 @@ consul-data-dir:
- name: /usr/local/share/consul - name: /usr/local/share/consul
- user: consul - user: consul
- group: consul - group: consul
- makedirs: - makedirs:
# Install agent # Install agent
consul-download: consul-download:
@ -113,39 +113,3 @@ consul-ui-link:
- name: /usr/local/share/consul/ui - name: /usr/local/share/consul/ui
- watch: - watch:
- file: consul-ui-install - 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

View file

@ -2,7 +2,11 @@
consul-init-script: consul-init-script:
file.managed: 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 - source: salt://consul/files/consul.upstart
- name: /etc/init/consul.conf - name: /etc/init/consul.conf
- mode: 0644 - mode: 0644

View file

@ -25,3 +25,17 @@ consul:
scripts: scripts:
- source: salt://files/consul/check_redis.py - source: salt://files/consul/check_redis.py
name: /usr/local/share/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