Skip to content

Allow port to be configurable #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pillar.example
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -34,7 +38,7 @@ postgres:
# POSTGRES
# Append the lines under this item to your postgresql.conf file.
# Pay attention to indent exactly with 4 spaces for all lines.
postgresconf: |
postgresconf: |-
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise an unnecessary blank line rendered, more noticeable if both listen_addresses and port are given.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

listen_addresses = '*' # listen on all interfaces

# Path to the `pg_hba.conf` file Jinja template on Salt Fileserver
Expand Down
31 changes: 29 additions & 2 deletions postgres/server/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,50 @@ postgresql-config-dir:
- require:
- cmd: postgresql-cluster-prepared

{%- if postgres.postgresconf %}
{%- set db_port = salt['config.option']('postgres.port') %}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the suggested:

{%- if salt['pillar.get']('postgres.port') %}
  • config.option gets the value from the minion configuration first, otherwise the pillar (which then works correctly with the postgres.manage states)
  • Same as the method used in the code for salt.modules.postgres

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

{%- if db_port %}

postgresql-conf-comment-port:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the port setting is commented in the PG distribution, this is rather safe.

Unfortunately, the port is set as part of the installation process, at least on Ubuntu.

So commenting out all and then letting the file.blockreplace below do its job. Works without changes for subsequent runs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it appears that file.comment state is more intelligent than I thought, and it does not comment out other strings matching same pattern when encounter the first one already commented. Interesting, I was not aware of that...

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:
- name: {{ postgres.conf_dir }}/postgresql.conf
- marker_start: "# Managed by SaltStack: listen_addresses: please do not edit"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously can't change this marker to mention that the managed block is also for the port.

- 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
- 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 %}

Expand Down