From ce0fcb8482f7ea055f1c9c12c741d4b64dd085fb Mon Sep 17 00:00:00 2001 From: noelmcloughlin Date: Sat, 5 Jun 2021 00:14:02 +0100 Subject: [PATCH] feat(cluster): join state with erlang_cookie --- docs/README.rst | 10 ++++++++-- pillar.example | 18 +++++++++-------- rabbitmq/config/cluster/install.sls | 31 ++++++++++++++++++++++------- rabbitmq/config/init.sls | 2 +- rabbitmq/package/repo/clean.sls | 1 - rabbitmq/service/running.sls | 11 ++-------- 6 files changed, 45 insertions(+), 28 deletions(-) diff --git a/docs/README.rst b/docs/README.rst index 5aaeb517..0a7054ec 100644 --- a/docs/README.rst +++ b/docs/README.rst @@ -67,7 +67,7 @@ now ``pre-commit`` will run automatically on each ``git commit``. :: Special notes ------------- -None +The main state ``rabbitmq`` excludes ``rabbitmq.config.cluster`` (initial cluster setup) state. Available states ---------------- @@ -99,7 +99,13 @@ This state will install the rabbitmqadmin package only. ^^^^^^^^^^^^^^^^^^^ This state will configure the rabbitmq service and has a dependency on ``rabbitmq.install`` -via include list. See ``pillar.example``. +via include list. It excludes ``rabbitmq.config.cluster`` state + +``rabbitmq.config.cluster`` +^^^^^^^^^^^^^^^^^^^^^^^^^ + +For initial setup this state writes the erlang cookie, joins cluster, and restarts service. +The erlang cookie comes from pillar data and must the identical for all cluster members ``rabbitmq.service`` ^^^^^^^^^^^^^^^^^^^^ diff --git a/pillar.example b/pillar.example index f4d56ad9..f9ab7fa0 100644 --- a/pillar.example +++ b/pillar.example @@ -2,13 +2,22 @@ # vim: ft=yaml --- rabbitmq: + cluster: + rabbitmq@locahost: + user: rabbitmq + host: rabbitmq # short hostname of node to join to, not fqdn + ram_node: None + runas: rabbitmq + erlang_cookie: + name: /var/lib/rabbitmq/.erlang_cookie + value: shared-value-for-all-cluster-members + pkg: # https://github.com/rabbitmq/rabbitmq-server/releases/tag/v3.8.14 use_upstream: repo # if available (i.e. packagecloud) service: enabled: true running: true - config: # see rabbitmq.conf.example from rabbitmq-server github docs context: {} @@ -59,13 +68,6 @@ rabbitmq: - 'alternate-**exchange': 'amq.fanout' - 'test-header': 'testing' - cluster: - rabbit@locahost: - - user: rabbit - - host: localhost - - ram_node: None - - runas: root - plugin: rabbitmq_management: runas: root diff --git a/rabbitmq/config/cluster/install.sls b/rabbitmq/config/cluster/install.sls index a084c3cf..701c36d8 100644 --- a/rabbitmq/config/cluster/install.sls +++ b/rabbitmq/config/cluster/install.sls @@ -10,15 +10,32 @@ include: - {{ sls_service_running }} - {{ sls_config_user }} - {% for name, cluster in salt["pillar.get"]("rabbitmq:cluster", {}).items() %} + {%- for name, cluster in salt["pillar.get"]("rabbitmq:cluster", {}).items() %} + {%- if 'erlang_cookie' in cluster and cluster.erlang_cookie is mapping %} rabbitmq-config-cluster-join-{{ name }}: - rabbitmq_cluster.join: - {% for value in cluster %} - - {{ value | json }} - {% endfor %} + file.managed: + - name: {{ cluster.erlang_cookie.name }} + - contents: {{ cluster.erlang_cookie.value }} + - mode: 400 + - user: {{ cluster.user }} + - group: root + - makedirs: True + - watch_in: + - service: rabbitmq-service-running-service-running + + {%- if 'host' in grains and grains.host not in cluster.host %} + + rabbitmq_cluster.joined: + - user: {{ cluster.user }} + - host: {{ cluster.host }} + - ram_node: {{ cluster.ram_node }} + - runas: {{ cluster.runas }} - require: - - service: {{ rabbitmq.service.name }} + - file: rabbitmq-config-cluster-join-{{ name }} - sls: {{ sls_config_user }} + - service: rabbitmq-service-running-service-running - {% endfor %} + {%- endif %} + {%- endif %} + {%- endfor %} diff --git a/rabbitmq/config/init.sls b/rabbitmq/config/init.sls index 84e1cd59..ab1e7d9c 100644 --- a/rabbitmq/config/init.sls +++ b/rabbitmq/config/init.sls @@ -8,4 +8,4 @@ include: - .policy - .plugin - .upstream - # .cluster + # .cluster should be run once, not always diff --git a/rabbitmq/package/repo/clean.sls b/rabbitmq/package/repo/clean.sls index d5ce2970..deb16235 100644 --- a/rabbitmq/package/repo/clean.sls +++ b/rabbitmq/package/repo/clean.sls @@ -15,7 +15,6 @@ rabbitmq-package-repo-erlang-clean: pkgrepo.absent: - names: - rabbitmq-rabbitmq-erlang - - rabbitmq_rabbitmq-erlang - rabbitmq-rabbitmq - rabbitmq_rabbitmq-server - deb https://dl.bintray.com/rabbitmq-erlang/debian {{ salt['grains.get']('oscodename') }} erlang diff --git a/rabbitmq/service/running.sls b/rabbitmq/service/running.sls index 3afcc15b..4d5e597f 100644 --- a/rabbitmq/service/running.sls +++ b/rabbitmq/service/running.sls @@ -20,15 +20,8 @@ rabbitmq-service-running-service-running: - enable: True - watch: - sls: {{ sls_config_file }} + - onfail_in: + - cmd: rabbitmq-service-running-service-running cmd.run: - names: - - localectl || true - journalctl -xe -u {{ rabbitmq.service.name }} || true - - systemctl status {{ rabbitmq.service.name }} || true - - systemd-analyze blame || true - - hostname - - hostname -s - - hostname -A - - ip addr - - onfail: - - service: rabbitmq-service-running-service-running