From b4ee8e891dedf1288af78e6ad026393353c8a42a Mon Sep 17 00:00:00 2001 From: Adam Bolte Date: Mon, 13 Jul 2015 19:11:43 +1000 Subject: [PATCH 1/5] Cleanup haproxy.jinja comments and whitespace. --- haproxy/templates/haproxy.jinja | 3 --- 1 file changed, 3 deletions(-) diff --git a/haproxy/templates/haproxy.jinja b/haproxy/templates/haproxy.jinja index 0d777f5..b0dc46c 100644 --- a/haproxy/templates/haproxy.jinja +++ b/haproxy/templates/haproxy.jinja @@ -5,7 +5,6 @@ # This file is managed by Salt. # Any changes will be overwritten. - #------------------ # Global settings #------------------ @@ -50,7 +49,6 @@ defaults {%- endfor %} {% endif %} - #------------------ # listen instances #------------------ @@ -169,7 +167,6 @@ frontend {{ frontend[1].get('name', frontend[0]) }} {%- endfor %} {% endif %} - #------------------ # backend instances #------------------ From eb6b073ad227641dc9ab865e0ce9dd64a759ceae Mon Sep 17 00:00:00 2001 From: Adam Bolte Date: Mon, 13 Jul 2015 19:14:51 +1000 Subject: [PATCH 2/5] Adjust ENABLED value based on pillar value. --- haproxy/service.sls | 16 +++++++++------- pillar.example | 1 + 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/haproxy/service.sls b/haproxy/service.sls index 74ff3ff..4b36610 100644 --- a/haproxy/service.sls +++ b/haproxy/service.sls @@ -7,11 +7,13 @@ haproxy.service: - pkg: haproxy - watch: - file: haproxy.config - file.managed: + file.replace: - name: /etc/default/haproxy -#TODO: Add switch to turn the service on and off based on pillar configuration. - - source: salt://haproxy/files/haproxy-init-enable - - create: True - - user: "root" - - group: "root" - - mode: "0644" +{% if salt['pillar.get']('haproxy:enabled', True) %} + - pattern: ENABLED=0$ + - repl: ENABLED=1 +{% else %} + - pattern: ENABLED=1$ + - repl: ENABLED=0 +{% endif %} + - show_changes: True diff --git a/pillar.example b/pillar.example index 53a3911..f90debc 100644 --- a/pillar.example +++ b/pillar.example @@ -3,6 +3,7 @@ # haproxy: + enabled: True config_file_path: /etc/haproxy/haproxy.cfg global: stats: From 672fe36551de625cd0339bab71371946b586b800 Mon Sep 17 00:00:00 2001 From: Adam Bolte Date: Mon, 13 Jul 2015 19:17:26 +1000 Subject: [PATCH 3/5] If not enabled in pillar, kill the service. --- haproxy/service.sls | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/haproxy/service.sls b/haproxy/service.sls index 4b36610..62d4193 100644 --- a/haproxy/service.sls +++ b/haproxy/service.sls @@ -1,12 +1,19 @@ haproxy.service: +{% if salt['pillar.get']('haproxy:enable', True) %} service.running: - name: haproxy - enable: True - reload: True - require: - pkg: haproxy + file: haproxy.service - watch: - file: haproxy.config +{% else %} + service.dead: + - name: haproxy + - enable: False +{% endif %} file.replace: - name: /etc/default/haproxy {% if salt['pillar.get']('haproxy:enabled', True) %} From 1bbf7fd182f654bd00c9d4d7bb1489bafb01e568 Mon Sep 17 00:00:00 2001 From: Adam Bolte Date: Fri, 17 Jul 2015 12:43:19 +1000 Subject: [PATCH 4/5] haproxy to optionally depend on a custom state. --- haproxy/init.sls | 7 ++++++- haproxy/install.sls | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/haproxy/init.sls b/haproxy/init.sls index afdc9f8..f614841 100644 --- a/haproxy/init.sls +++ b/haproxy/init.sls @@ -3,6 +3,11 @@ # Meta-state to fully setup haproxy on debian. (or any other distro that has haproxy in their repo) include: +{% if salt['pillar.get']('haproxy:include') %} +{% for item in salt['pillar.get']('haproxy:include') %} + - {{ item }} +{% endfor %} +{% endif %} - haproxy.install - haproxy.service - - haproxy.config \ No newline at end of file + - haproxy.config diff --git a/haproxy/install.sls b/haproxy/install.sls index 5981aca..bb56d80 100644 --- a/haproxy/install.sls +++ b/haproxy/install.sls @@ -12,3 +12,9 @@ haproxy_ppa_repo: haproxy.install: pkg.installed: - name: haproxy +{% if salt['pillar.get']('haproxy:require') %} + - require: +{% for item in salt['pillar.get']('haproxy:require') %} + - {{ item }} +{% endfor %} +{% endif %} \ No newline at end of file From 1ffa423bfc32bde48478206f67cfadb0d1640fe3 Mon Sep 17 00:00:00 2001 From: Adam Bolte Date: Thu, 23 Jul 2015 18:46:44 +1000 Subject: [PATCH 5/5] Additional cleanup for haproxy.jinja whitespace. --- haproxy/templates/haproxy.jinja | 38 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/haproxy/templates/haproxy.jinja b/haproxy/templates/haproxy.jinja index b0dc46c..3f6c548 100644 --- a/haproxy/templates/haproxy.jinja +++ b/haproxy/templates/haproxy.jinja @@ -5,6 +5,7 @@ # This file is managed by Salt. # Any changes will be overwritten. + #------------------ # Global settings #------------------ @@ -15,14 +16,15 @@ global group {{ salt['pillar.get']('haproxy:global:group', 'haproxy') }} {%- if salt['pillar.get']('haproxy:global:chroot:enable', 'no') == True %} chroot {{ salt['pillar.get']('haproxy:global:chroot:path', '/tmp') }} -{% endif %} +{%- endif %} {%- if salt['pillar.get']('haproxy:global:daemon', 'no') == True %} daemon -{% endif %} -{%- if salt['pillar.get']('haproxy:global:stats:enable', 'no') == True %} +{%- endif %} +{% if salt['pillar.get']('haproxy:global:stats:enable', 'no') == True %} # Stats support is currently limited to socket mode stats socket {{ salt['pillar.get']('haproxy:global:stats:socketpath', '/tmp/ha_stats.sock') }} -{% endif %} +{%- endif %} + #------------------ # common defaults that all the 'listen' and 'backend' sections will @@ -47,13 +49,14 @@ defaults {%- for errorfile in salt['pillar.get']('haproxy:defaults:errorfiles').iteritems() %} errorfile {{ errorfile[0] }} {{ errorfile[1] }} {%- endfor %} -{% endif %} +{%- endif %} +{%- if 'listens' in salt['pillar.get']('haproxy', {}) %} + #------------------ # listen instances #------------------ -{%- if 'listens' in salt['pillar.get']('haproxy', {}) %} - {%- for listener in salt['pillar.get']('haproxy:listens', {}).iteritems() %} +{%- for listener in salt['pillar.get']('haproxy:listens', {}).iteritems() %} listen {{ listener[1].get('name', listener[0]) }} {%- if 'bind' in listener[1] %} {%- if listener[1].bind[1] is defined and listener[1].bind[1]|length > 1 %} @@ -121,14 +124,14 @@ listen {{ listener[1].get('name', listener[0]) }} {%- endfor %} {% endif %} {%- endfor %} -{% endif %} +{%- endif %} +{%- if 'frontends' in salt['pillar.get']('haproxy', {}) %} #------------------ # frontend instances #------------------ -{%- if 'frontends' in salt['pillar.get']('haproxy', {}) %} - {%- for frontend in salt['pillar.get']('haproxy:frontends', {}).iteritems() %} +{%- for frontend in salt['pillar.get']('haproxy:frontends', {}).iteritems() %} frontend {{ frontend[1].get('name', frontend[0]) }} {%- if 'bind' in frontend[1] %} {%- if frontend[1].bind[1] is defined and frontend[1].bind[1]|length > 1 %} @@ -163,15 +166,16 @@ frontend {{ frontend[1].get('name', frontend[0]) }} {%- for use_backend in frontend[1].use_backends %} use_backend {{ use_backend }} {%- endfor %} - {% endif %} + {%- endif %} {%- endfor %} -{% endif %} +{%- endif %} +{%- if 'backends' in salt['pillar.get']('haproxy', {}) %} + #------------------ # backend instances #------------------ -{%- if 'backends' in salt['pillar.get']('haproxy', {}) %} - {%- for backend in salt['pillar.get']('haproxy:backends', {}).iteritems() %} +{%- for backend in salt['pillar.get']('haproxy:backends', {}).iteritems() %} backend {{ backend[1].get('name',backend[0]) }} {%- if 'redirects' in backend[1] %} {%- for redirect in backend[1].redirects %} @@ -180,7 +184,7 @@ backend {{ backend[1].get('name',backend[0]) }} {% endif %} {%- if 'balance' in backend[1] %} balance {{ backend[1].balance }} - {% endif %} + {%- endif %} {%- if 'options' in backend[1] %} {%- for option in backend[1].options %} option {{ option }} @@ -200,11 +204,11 @@ backend {{ backend[1].get('name',backend[0]) }} {% endif %} {%- if 'defaultserver' in backend[1] %} default-server {%- for option, value in backend[1].defaultserver.iteritems() %} {{ ' '.join((option, value|string, '')) }} {%- endfor %} - {% endif %} + {%- endif %} {%- if 'servers' in backend[1] %} {%- for server in backend[1].servers.iteritems() %} server {{ server[1].get('name',server[0]) }} {{ server[1].host }}:{{ server[1].port }} {{ server[1].check }} {%- endfor %} {% endif %} {%- endfor %} -{% endif %} +{%- endif %}