From f29aa2da1688ab445d78d3c6596467bae9281f48 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Thu, 22 Aug 2019 17:05:20 +1000 Subject: [PATCH] Make haproxy role more generic This makes the haproxy role more generic so we can run another (or potentially even more) haproxy instance(s) to manage other services. The config file is moved to a variable for the haproxy role. The gitea specific config is then installed for the gitea-lb service by a new gitea-lb role. statsd reporting is made optional with an argument. This enables/disables the service in the docker compose. Role documenation is updated. Needed-By: https://review.opendev.org/678159 Change-Id: I3506ebbed9dda17d910001e71b17a865eba4225d --- inventory/service/group_vars/gitea-lb.yaml | 2 +- playbooks/roles/gitea-lb/README.rst | 9 +++++ playbooks/roles/gitea-lb/tasks/main.yaml | 5 +++ .../templates/gitea-haproxy.cfg.j2} | 2 +- playbooks/roles/haproxy/README.rst | 15 ++++++++ playbooks/roles/haproxy/defaults/main.yaml | 1 + playbooks/roles/haproxy/tasks/main.yaml | 35 ++++++++++++++++--- .../docker-compose.yaml.j2} | 3 ++ playbooks/service-gitea-lb.yaml | 2 +- .../templates/group_vars/gitea-lb.yaml.j2 | 2 +- 10 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 playbooks/roles/gitea-lb/README.rst create mode 100644 playbooks/roles/gitea-lb/tasks/main.yaml rename playbooks/roles/{haproxy/templates/haproxy.cfg.j2 => gitea-lb/templates/gitea-haproxy.cfg.j2} (94%) create mode 100644 playbooks/roles/haproxy/defaults/main.yaml rename playbooks/roles/haproxy/{files/docker/docker-compose.yaml => templates/docker-compose.yaml.j2} (98%) diff --git a/inventory/service/group_vars/gitea-lb.yaml b/inventory/service/group_vars/gitea-lb.yaml index b201a12888..41780cc8e8 100644 --- a/inventory/service/group_vars/gitea-lb.yaml +++ b/inventory/service/group_vars/gitea-lb.yaml @@ -1,4 +1,4 @@ -haproxy_listeners: +gitea_lb_listeners: - name: balance_git_http bind: - ':::80' diff --git a/playbooks/roles/gitea-lb/README.rst b/playbooks/roles/gitea-lb/README.rst new file mode 100644 index 0000000000..de1031d3c1 --- /dev/null +++ b/playbooks/roles/gitea-lb/README.rst @@ -0,0 +1,9 @@ +Install the gitea-lb services + +This configures haproxy + +**Role Variables** + +.. zuul:rolevar:: gitea_lb_listeners + + The backends to configure diff --git a/playbooks/roles/gitea-lb/tasks/main.yaml b/playbooks/roles/gitea-lb/tasks/main.yaml new file mode 100644 index 0000000000..9d7f7150ee --- /dev/null +++ b/playbooks/roles/gitea-lb/tasks/main.yaml @@ -0,0 +1,5 @@ +- name: Install haproxy with gitea config + include_role: + name: haproxy + vars: + haproxy_config_template: gitea-haproxy.cfg.j2 diff --git a/playbooks/roles/haproxy/templates/haproxy.cfg.j2 b/playbooks/roles/gitea-lb/templates/gitea-haproxy.cfg.j2 similarity index 94% rename from playbooks/roles/haproxy/templates/haproxy.cfg.j2 rename to playbooks/roles/gitea-lb/templates/gitea-haproxy.cfg.j2 index c655a692a5..200de378ba 100644 --- a/playbooks/roles/haproxy/templates/haproxy.cfg.j2 +++ b/playbooks/roles/gitea-lb/templates/gitea-haproxy.cfg.j2 @@ -20,7 +20,7 @@ defaults timeout server 2m timeout check 10s -{% for listener in haproxy_listeners %} +{% for listener in gitea_lb_listeners %} listen {{ listener.name }} {% for bind in listener.bind %} bind {{ bind }} diff --git a/playbooks/roles/haproxy/README.rst b/playbooks/roles/haproxy/README.rst index 3e5aa70398..f28d4e73db 100644 --- a/playbooks/roles/haproxy/README.rst +++ b/playbooks/roles/haproxy/README.rst @@ -1 +1,16 @@ Install, configure, and run a haproxy server. + +**Role Variables** + +.. zuul:rolevar:: haproxy_config_template + :type: string + :default: Undefined + + The config template to install for haproxy. Must be defined. + +.. zuul:rolevar:: haproxy_run_statsd + :type: string + :default: True + + Run the ``haproxy-statsd`` docker container to report back-end + stats to graphite.opendev.org diff --git a/playbooks/roles/haproxy/defaults/main.yaml b/playbooks/roles/haproxy/defaults/main.yaml new file mode 100644 index 0000000000..4a6c122618 --- /dev/null +++ b/playbooks/roles/haproxy/defaults/main.yaml @@ -0,0 +1 @@ +haproxy_run_statsd: True \ No newline at end of file diff --git a/playbooks/roles/haproxy/tasks/main.yaml b/playbooks/roles/haproxy/tasks/main.yaml index 6854b94852..cf9f6b5c27 100644 --- a/playbooks/roles/haproxy/tasks/main.yaml +++ b/playbooks/roles/haproxy/tasks/main.yaml @@ -2,10 +2,7 @@ package: name: socat state: present -- name: Synchronize docker-compose directory - synchronize: - src: docker/ - dest: /etc/haproxy-docker/ + - name: Ensure registry volume directories exists file: state: directory @@ -15,19 +12,47 @@ loop: - etc - run + +- name: Ensure haproxy config template available + assert: + that: + - haproxy_config_template is defined + - name: Write haproxy config file template: - src: haproxy.cfg.j2 + src: '{{ haproxy_config_template }}' dest: /var/haproxy/etc/haproxy.cfg + owner: 1000 + group: 1000 + mode: 0644 + +- name: Ensure docker compose configuration directory + file: + path: /etc/haproxy-docker + state: directory + owner: root + group: root + mode: 0755 + +- name: Install docker-compose configuration + template: + src: docker-compose.yaml.j2 + dest: /etc/haproxy-docker/docker-compose.yaml + owner: root + group: root + mode: 0644 + notify: Reload haproxy - name: Run docker-compose pull shell: cmd: docker-compose pull chdir: /etc/haproxy-docker/ + - name: Run docker-compose up shell: cmd: docker-compose up -d chdir: /etc/haproxy-docker/ + - name: Run docker prune to cleanup unneeded images shell: cmd: docker image prune -f diff --git a/playbooks/roles/haproxy/files/docker/docker-compose.yaml b/playbooks/roles/haproxy/templates/docker-compose.yaml.j2 similarity index 98% rename from playbooks/roles/haproxy/files/docker/docker-compose.yaml rename to playbooks/roles/haproxy/templates/docker-compose.yaml.j2 index 4eb63e3ec7..2b8361b266 100644 --- a/playbooks/roles/haproxy/files/docker/docker-compose.yaml +++ b/playbooks/roles/haproxy/templates/docker-compose.yaml.j2 @@ -42,6 +42,8 @@ services: driver: syslog options: tag: "docker-haproxy" + +{% if haproxy_run_statsd %} haproxy-statsd: restart: always image: docker.io/opendevorg/haproxy-statsd:latest @@ -56,3 +58,4 @@ services: driver: syslog options: tag: "docker-haproxy-statsd" +{% endif %} diff --git a/playbooks/service-gitea-lb.yaml b/playbooks/service-gitea-lb.yaml index 466c41392a..f0999c7d99 100644 --- a/playbooks/service-gitea-lb.yaml +++ b/playbooks/service-gitea-lb.yaml @@ -3,4 +3,4 @@ roles: - iptables - install-docker - - haproxy + - gitea-lb diff --git a/playbooks/zuul/templates/group_vars/gitea-lb.yaml.j2 b/playbooks/zuul/templates/group_vars/gitea-lb.yaml.j2 index c6fe4e26fc..9eb0f3f456 100644 --- a/playbooks/zuul/templates/group_vars/gitea-lb.yaml.j2 +++ b/playbooks/zuul/templates/group_vars/gitea-lb.yaml.j2 @@ -1,4 +1,4 @@ -haproxy_listeners: +gitea_lb_listeners: - name: balance_git_http bind: - ":::80"