From 0e36358ea8c2dd1b00b856e06e1a515a636710b7 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Mon, 10 Oct 2022 10:16:32 +0200 Subject: [PATCH] updated haproxy state --- states/haproxy/defaults.yaml | 2 ++ states/haproxy/install.sls | 21 ++++++++++++++++++++- states/haproxy/scripts/hello_world.lua | 11 +++++++++++ states/haproxy/service.sls | 2 +- states/haproxy/templates/haproxy.cfg.j2 | 17 ++++++++++++++--- 5 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 states/haproxy/scripts/hello_world.lua diff --git a/states/haproxy/defaults.yaml b/states/haproxy/defaults.yaml index bf0eeed..6d0096b 100644 --- a/states/haproxy/defaults.yaml +++ b/states/haproxy/defaults.yaml @@ -3,6 +3,8 @@ haproxy: enabled: true packages: - haproxy + scripts: + - hello_world.lua config: defaults: #log global: diff --git a/states/haproxy/install.sls b/states/haproxy/install.sls index 565a382..97b8ac0 100644 --- a/states/haproxy/install.sls +++ b/states/haproxy/install.sls @@ -2,4 +2,23 @@ {%- from "haproxy/map.jinja" import haproxy with context %} haproxy-pkg: pkg.installed: - - pkgs: {{ haproxy.packages }} \ No newline at end of file + - pkgs: {{ haproxy.packages }} + +haproxy-script-dir: + file.directory: + - name: {{ haproxy.config.dir }}/scripts + - user: {{ haproxy.config.user }} + - group: {{ haproxy.config.group }} + - mode: "0700" + +{% for filename in haproxy.scripts %} +haprox-script-{{ filename }}: + file.managed: + - name: {{ haproxy.config.dir }}/scripts/{{ filename }} + - source: salt://haproxy/scripts/{{ filename }} + - user: {{ haproxy.config.user }} + - group: {{ haproxy.config.group }} + - mode: "0700" + - watch_in: + - service: haproxy-service +{% endfor %} \ No newline at end of file diff --git a/states/haproxy/scripts/hello_world.lua b/states/haproxy/scripts/hello_world.lua new file mode 100644 index 0000000..6cbca0f --- /dev/null +++ b/states/haproxy/scripts/hello_world.lua @@ -0,0 +1,11 @@ +local args = table.pack(...) + +core.register_service("hello-world", "http", function(applet) + local response = "Hello World !" + applet:set_status(200) + applet:add_header("content-length", string.len(response)) + applet:add_header("content-type", "text/plain") + applet:start_response() + applet:send(response) + core.Info("test".."blabla") +end) diff --git a/states/haproxy/service.sls b/states/haproxy/service.sls index 54e7f70..f64cd47 100644 --- a/states/haproxy/service.sls +++ b/states/haproxy/service.sls @@ -4,4 +4,4 @@ haproxy-service: service.running: - name: haproxy - enable: true - - restart: true \ No newline at end of file + - reload: true \ No newline at end of file diff --git a/states/haproxy/templates/haproxy.cfg.j2 b/states/haproxy/templates/haproxy.cfg.j2 index 58d1b8c..9f5e1a5 100644 --- a/states/haproxy/templates/haproxy.cfg.j2 +++ b/states/haproxy/templates/haproxy.cfg.j2 @@ -35,7 +35,9 @@ listen stats {%- endmacro %} global - #lua-load /etc/haproxy/hello_world.lua +{%- for filename in haproxy.scripts %} + lua-load {{ haproxy.config.dir }}/scripts/{{ filename }} +{%- endfor %} maxconn 1000 stats socket ipv4@127.0.0.1:9990 level admin stats socket /var/run/hap-lb.sock mode 666 level admin @@ -53,9 +55,14 @@ defaults {%- endfor %} {%- if haproxy.config.admin %} - {{ admin() }} +{{ admin() }} {%- endif %} +cache static + total-max-size 4095 + max-object-size 50000 + max-age 120 + frontend http bind *:80,:::80 v4v6 mode http @@ -75,6 +82,10 @@ backend {{ name }} {%- if values.handle_head|default(false) %} {{ handle_head() }} {%- endif %} +{%- if values.usecache|default(true) %} + http-request cache-use static if { path_end .css .js .png .jpg } + http-response cache-store static +{%- endif %} {%- if values.internal_access|default(false) %} {{ internal_access() }} {%- endif %} @@ -90,6 +101,6 @@ listen {{ name }} {%- endif %} default-server inter 3s fall 3 {%- for endpoint in values.endpoints %} - server {{ endpoint.name }} {{ endpoint.name }}:{{ endpoint.port }} check port {{ endpoint.port }} + server {{ endpoint.name }} {{ endpoint.name }}:{{ endpoint.port }} check {{ "backup" if endpoint.backup|default(false) }} port {{ endpoint.port }} {%- endfor %} {% endfor %}