Skip to content

Commit 186e972

Browse files
committed
Use bake_image Pillar setting to control PG startup behavior
1 parent d48f6f2 commit 186e972

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

pillar.example

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ postgres:
1919
postgresconf: |
2020
listen_addresses = '*' # listen on all interfaces
2121
22-
# Backup extension for postgresql.conf file, defaults to `.bak`.
22+
# Backup extension for postgresql.conf file, defaults to ``.bak``.
2323
# Set to False to stop creation of backup on postgresql.conf changes.
2424
{%- if 'status.time' in salt.keys() %}
2525
postgresconf_backup: ".backup@{{ salt['status.time']('%y-%m-%d_%H:%M:%S') }}"
@@ -28,7 +28,7 @@ postgres:
2828
# Path to the `pg_hba.conf` file Jinja template on Salt Fileserver
2929
pg_hba.conf: salt://postgres/templates/pg_hba.conf.j2
3030

31-
# This section covers ACL management in the `pg_hba.conf` file.
31+
# This section covers ACL management in the ``pg_hba.conf`` file.
3232
# acls list controls: which hosts are allowed to connect, how clients
3333
# are authenticated, which PostgreSQL user names they can use, which
3434
# databases they can access. Records take one of these forms:
@@ -48,9 +48,20 @@ postgres:
4848
# PostgreSQL service name
4949
service: postgresql
5050

51+
{%- if grains['init'] == 'unknown' %}
52+
53+
# If Salt is unable to detect init system running in the scope of state run,
54+
# probably we are trying to bake a container/VM image with PostgreSQL.
55+
# Use ``service`` setting to contol how PostgreSQL will be started: if set to
56+
# ``True`` the raw ``pg_ctl`` will be utilized instead of packaged init
57+
# script, job or unit run with Salt ``service`` state.
58+
bake_image: True
59+
60+
{%- endif %}
61+
5162
# Create/remove users, tablespaces, databases, schema and extensions.
5263
# Each of these dictionaries contains PostgreSQL entities which
53-
# mapped to the `postgres_*` Salt states with arguments. See the Salt
64+
# mapped to the ``postgres_*`` Salt states with arguments. See the Salt
5465
# documentaion to get all supported argument for a particular state.
5566
#
5667
# Format is the following:

postgres/defaults.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ postgres:
3333

3434
service: postgresql
3535

36+
bake_image: False
37+
3638
users: {}
3739
tablespaces: {}
3840
databases: {}

postgres/server.sls

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ postgresql-tablespace-dir-{{ name }}:
108108
109109
{%- endfor %}
110110
111-
{%- if grains['init'] != 'unknown' %}
111+
{%- if not postgres.bake_image %}
112+
113+
# Start PostgreSQL server using OS init
112114
113115
postgresql-running:
114116
service.running:
@@ -120,19 +122,18 @@ postgresql-running:
120122
121123
{%- else %}
122124
123-
# An attempt to launch PostgreSQL with `pg_ctl` if Salt was unable to
124-
# detect local init system (`service` module would fail in this case)
125+
# An attempt to launch PostgreSQL with `pg_ctl` during an image preparation
125126
126127
postgresql-start:
127128
cmd.run:
128129
- name: pg_ctl -D {{ postgres.conf_dir }} -l logfile start
129130
- runas: {{ postgres.user }}
130131
- unless:
131132
- ps -p $(head -n 1 {{ postgres.conf_dir }}/postmaster.pid) 2>/dev/null
133+
- require:
134+
- file: postgresql-pg_hba
132135
133-
# Try to enable PostgreSQL in "manual" way if Salt `service` state module
134-
# is currently not available (e.g. during Docker or Packer build when is no init
135-
# system running)
136+
# Try to enable PostgreSQL in "manual" way when baking an image
136137
137138
postgresql-enable:
138139
cmd.run:
@@ -146,7 +147,7 @@ postgresql-enable:
146147
# Nothing to do
147148
- name: 'true'
148149
{%- endif %}
149-
- onchanges:
150+
- require:
150151
- cmd: postgresql-start
151152
152153
{%- endif %}

0 commit comments

Comments
 (0)