From 06e5944133d7616efd6223fa72f37dabf8b2e204 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Fri, 8 Jun 2018 23:40:12 +0100 Subject: [PATCH 1/5] Allow `port` to be configurable Regex: - `^` Line start - `#*\s*` Find line even if commented out - `(port)` 'port' -- capture as backreference `\1` - `\s*=\s*` Equals sign, whether or not surrounded by whitespace - `\d{4,5}` Existing port value, expected at 4/5 digits - `(.*)` Remainder (i.e. comment) -- capture as backreference `\2` - `$` Line end --- pillar.example | 4 ++++ postgres/defaults.yaml | 1 + postgres/macros.jinja | 1 + postgres/server/init.sls | 21 +++++++++++++++++++++ 4 files changed, 27 insertions(+) diff --git a/pillar.example b/pillar.example index 48d0e07..ec11cdc 100644 --- a/pillar.example +++ b/pillar.example @@ -32,6 +32,10 @@ postgres: hard: 64000 # POSTGRES + # Non-standard port to use for the cluster + # Only set if port `5432` is not appropriate + port: 5433 + # Append the lines under this item to your postgresql.conf file. # Pay attention to indent exactly with 4 spaces for all lines. postgresconf: | diff --git a/postgres/defaults.yaml b/postgres/defaults.yaml index f84ba3d..4d50d32 100644 --- a/postgres/defaults.yaml +++ b/postgres/defaults.yaml @@ -3,6 +3,7 @@ postgres: use_upstream_repo: True version: '9.5' + default_port: 5432 pkg: postgresql pkgs_extra: [] pkg_client: postgresql-client diff --git a/postgres/macros.jinja b/postgres/macros.jinja index 4eedddb..e530b11 100644 --- a/postgres/macros.jinja +++ b/postgres/macros.jinja @@ -23,6 +23,7 @@ {{ state }}-{{ name }}: {{ state }}.{{ ensure|default('present') }}: {{- format_kwargs(kwarg) }} + - db_port: {{ postgres.port|default(postgres.default_port) }} - onchanges: - test: postgres-reload-modules diff --git a/postgres/server/init.sls b/postgres/server/init.sls index ace913d..d41775b 100644 --- a/postgres/server/init.sls +++ b/postgres/server/init.sls @@ -192,3 +192,24 @@ postgresql-running: - file: postgresql-pg_ident {%- endif %} + +{%- if postgres.port|default(false) %} + +postgresql-port: + file.replace: + - name: {{ postgres.conf_dir }}/postgresql.conf + - pattern: ^#*\s*(port)\s*=\s*\d{4,5}(.*)$ + - repl: \1 = {{ postgres.port }}\2 + - flags: 8 # ['MULTILINE'] + - show_changes: True + - append_if_not_found: True + - backup: {{ postgres.config_backup|default(false, true) }} + - require: + - file: postgresql-config-dir + - watch_in: + - service: postgresql-port + + service.running: + - name: {{ postgres.service }} + +{%- endif %} From 0d9a5ba239c781d300b384356afade429f48e892 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Sun, 10 Jun 2018 23:07:44 +0100 Subject: [PATCH 2/5] Append `Managed by SaltStack` comment to `port` setting * Regex modified: - Ensure whitespace before comment is maintained - Consistent with surrounding lines in the file * Used YAML pipe `>-` due to the colon-space (`: `) in the comment --- postgres/server/init.sls | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/postgres/server/init.sls b/postgres/server/init.sls index d41775b..35f385f 100644 --- a/postgres/server/init.sls +++ b/postgres/server/init.sls @@ -198,8 +198,9 @@ postgresql-running: postgresql-port: file.replace: - name: {{ postgres.conf_dir }}/postgresql.conf - - pattern: ^#*\s*(port)\s*=\s*\d{4,5}(.*)$ - - repl: \1 = {{ postgres.port }}\2 + - pattern: ^#*\s*(port)\s*=\s*\d{4,5}(\s*).*$ + - repl: >- + \1 = {{ postgres.port }}\2# Managed by SaltStack: please do not edit - flags: 8 # ['MULTILINE'] - show_changes: True - append_if_not_found: True From e1a19ed78f2485b640ec2e3cf54da6381f0d4848 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 12 Jun 2018 08:38:51 +0100 Subject: [PATCH 3/5] Use top-level `postgres.port` and existing states to set `port` --- pillar.example | 10 ++++----- postgres/defaults.yaml | 1 - postgres/macros.jinja | 1 - postgres/server/init.sls | 48 +++++++++++++++++++--------------------- 4 files changed, 28 insertions(+), 32 deletions(-) diff --git a/pillar.example b/pillar.example index ec11cdc..766b004 100644 --- a/pillar.example +++ b/pillar.example @@ -1,3 +1,7 @@ +# Port to use for the cluster -- can be used to provide a non-standard port +# NOTE: If already set in the minion config, that value takes priority +postgres.port: '5432' + postgres: # UPSTREAM REPO # Set True to configure upstream postgresql.org repository for YUM/APT/ZYPP @@ -32,13 +36,9 @@ postgres: hard: 64000 # POSTGRES - # Non-standard port to use for the cluster - # Only set if port `5432` is not appropriate - port: 5433 - # Append the lines under this item to your postgresql.conf file. # Pay attention to indent exactly with 4 spaces for all lines. - postgresconf: | + postgresconf: |- listen_addresses = '*' # listen on all interfaces # Path to the `pg_hba.conf` file Jinja template on Salt Fileserver diff --git a/postgres/defaults.yaml b/postgres/defaults.yaml index 4d50d32..f84ba3d 100644 --- a/postgres/defaults.yaml +++ b/postgres/defaults.yaml @@ -3,7 +3,6 @@ postgres: use_upstream_repo: True version: '9.5' - default_port: 5432 pkg: postgresql pkgs_extra: [] pkg_client: postgresql-client diff --git a/postgres/macros.jinja b/postgres/macros.jinja index e530b11..4eedddb 100644 --- a/postgres/macros.jinja +++ b/postgres/macros.jinja @@ -23,7 +23,6 @@ {{ state }}-{{ name }}: {{ state }}.{{ ensure|default('present') }}: {{- format_kwargs(kwarg) }} - - db_port: {{ postgres.port|default(postgres.default_port) }} - onchanges: - test: postgres-reload-modules diff --git a/postgres/server/init.sls b/postgres/server/init.sls index 35f385f..99ffe78 100644 --- a/postgres/server/init.sls +++ b/postgres/server/init.sls @@ -86,7 +86,19 @@ postgresql-config-dir: - require: - cmd: postgresql-cluster-prepared -{%- if postgres.postgresconf %} +{%- set db_port = salt['config.option']('postgres.port') %} +{%- if db_port %} + +postgresql-conf-comment-port: + file.comment: + - name: {{ postgres.conf_dir }}/postgresql.conf + - regex: ^port\s*=.+ + - require: + - file: postgresql-config-dir + +{%- endif %} + +{%- if postgres.postgresconf or db_port %} postgresql-conf: file.blockreplace: @@ -94,15 +106,23 @@ postgresql-conf: - marker_start: "# Managed by SaltStack: listen_addresses: please do not edit" - marker_end: "# Managed by SaltStack: end of salt managed zone --" - content: | + {%- if postgres.postgresconf %} {{ postgres.postgresconf|indent(8) }} + {%- endif %} + {%- if db_port %} + port = {{ db_port }} + {%- endif %} - show_changes: True - append_if_not_found: True {#- Detect empty values (none, '') in the config_backup #} - backup: {{ postgres.config_backup|default(false, true) }} - require: - file: postgresql-config-dir + {%- if db_port %} + - file: postgresql-conf-comment-port + {%- endif %} - watch_in: - - service: postgresql-running + - service: postgresql-running {%- endif %} @@ -184,7 +204,7 @@ postgresql-running: service.running: - name: {{ postgres.service }} - enable: True - {% if grains.os not in ('MacOS',) %} + {% if grains.os not in ('MacOS',) and not db_port %} - reload: True {% endif %} - watch: @@ -192,25 +212,3 @@ postgresql-running: - file: postgresql-pg_ident {%- endif %} - -{%- if postgres.port|default(false) %} - -postgresql-port: - file.replace: - - name: {{ postgres.conf_dir }}/postgresql.conf - - pattern: ^#*\s*(port)\s*=\s*\d{4,5}(\s*).*$ - - repl: >- - \1 = {{ postgres.port }}\2# Managed by SaltStack: please do not edit - - flags: 8 # ['MULTILINE'] - - show_changes: True - - append_if_not_found: True - - backup: {{ postgres.config_backup|default(false, true) }} - - require: - - file: postgresql-config-dir - - watch_in: - - service: postgresql-port - - service.running: - - name: {{ postgres.service }} - -{%- endif %} From 65683252c4bbd02876dbfa6a31282af038abc86f Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Tue, 12 Jun 2018 19:52:58 +0100 Subject: [PATCH 4/5] Use separate state for restarting service re: `postgresql.conf` --- postgres/server/init.sls | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/postgres/server/init.sls b/postgres/server/init.sls index 99ffe78..9a89184 100644 --- a/postgres/server/init.sls +++ b/postgres/server/init.sls @@ -122,7 +122,7 @@ postgresql-conf: - file: postgresql-conf-comment-port {%- endif %} - watch_in: - - service: postgresql-running + - service: postgresql-service-restart {%- endif %} @@ -204,11 +204,19 @@ postgresql-running: service.running: - name: {{ postgres.service }} - enable: True - {% if grains.os not in ('MacOS',) and not db_port %} + {% if grains.os not in ('MacOS',) %} - reload: True {% endif %} - watch: - file: postgresql-pg_hba - file: postgresql-pg_ident +# Restart the service where reloading is not sufficient +# Currently when changes are made to `postgresql.conf` +{%- if postgres.postgresconf or db_port %} +postgresql-service-restart: + service.running: + - name: {{ postgres.service }} +{%- endif %} + {%- endif %} From 79ab1a29309daba6bb19b59831e354b8d07ae1c7 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Sat, 16 Jun 2018 12:38:48 +0100 Subject: [PATCH 5/5] Avoid duplicate Jinja conditional block by moving service restart --- postgres/server/init.sls | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/postgres/server/init.sls b/postgres/server/init.sls index 9a89184..ddb736c 100644 --- a/postgres/server/init.sls +++ b/postgres/server/init.sls @@ -122,7 +122,14 @@ postgresql-conf: - file: postgresql-conf-comment-port {%- endif %} - watch_in: - - service: postgresql-service-restart + - module: postgresql-service-restart + +# Restart the service where reloading is not sufficient +# Currently only when changes are made to `postgresql.conf` +postgresql-service-restart: + module.wait: + - name: service.restart + - m_name: {{ postgres.service }} {%- endif %} @@ -211,12 +218,4 @@ postgresql-running: - file: postgresql-pg_hba - file: postgresql-pg_ident -# Restart the service where reloading is not sufficient -# Currently when changes are made to `postgresql.conf` -{%- if postgres.postgresconf or db_port %} -postgresql-service-restart: - service.running: - - name: {{ postgres.service }} -{%- endif %} - {%- endif %}