Use `bake_image` Pillar setting to control PG startup behavior

This commit is contained in:
Denys Havrysh 2017-01-07 15:11:26 +02:00
parent d48f6f2a50
commit 186e972075
3 changed files with 24 additions and 10 deletions

View file

@ -19,7 +19,7 @@ postgres:
postgresconf: | postgresconf: |
listen_addresses = '*' # listen on all interfaces listen_addresses = '*' # listen on all interfaces
# Backup extension for postgresql.conf file, defaults to `.bak`. # Backup extension for postgresql.conf file, defaults to ``.bak``.
# Set to False to stop creation of backup on postgresql.conf changes. # Set to False to stop creation of backup on postgresql.conf changes.
{%- if 'status.time' in salt.keys() %} {%- if 'status.time' in salt.keys() %}
postgresconf_backup: ".backup@{{ salt['status.time']('%y-%m-%d_%H:%M:%S') }}" postgresconf_backup: ".backup@{{ salt['status.time']('%y-%m-%d_%H:%M:%S') }}"
@ -28,7 +28,7 @@ postgres:
# Path to the `pg_hba.conf` file Jinja template on Salt Fileserver # Path to the `pg_hba.conf` file Jinja template on Salt Fileserver
pg_hba.conf: salt://postgres/templates/pg_hba.conf.j2 pg_hba.conf: salt://postgres/templates/pg_hba.conf.j2
# This section covers ACL management in the `pg_hba.conf` file. # This section covers ACL management in the ``pg_hba.conf`` file.
# acls list controls: which hosts are allowed to connect, how clients # acls list controls: which hosts are allowed to connect, how clients
# are authenticated, which PostgreSQL user names they can use, which # are authenticated, which PostgreSQL user names they can use, which
# databases they can access. Records take one of these forms: # databases they can access. Records take one of these forms:
@ -48,9 +48,20 @@ postgres:
# PostgreSQL service name # PostgreSQL service name
service: postgresql service: postgresql
{%- if grains['init'] == 'unknown' %}
# If Salt is unable to detect init system running in the scope of state run,
# probably we are trying to bake a container/VM image with PostgreSQL.
# Use ``service`` setting to contol how PostgreSQL will be started: if set to
# ``True`` the raw ``pg_ctl`` will be utilized instead of packaged init
# script, job or unit run with Salt ``service`` state.
bake_image: True
{%- endif %}
# Create/remove users, tablespaces, databases, schema and extensions. # Create/remove users, tablespaces, databases, schema and extensions.
# Each of these dictionaries contains PostgreSQL entities which # Each of these dictionaries contains PostgreSQL entities which
# mapped to the `postgres_*` Salt states with arguments. See the Salt # mapped to the ``postgres_*`` Salt states with arguments. See the Salt
# documentaion to get all supported argument for a particular state. # documentaion to get all supported argument for a particular state.
# #
# Format is the following: # Format is the following:

View file

@ -33,6 +33,8 @@ postgres:
service: postgresql service: postgresql
bake_image: False
users: {} users: {}
tablespaces: {} tablespaces: {}
databases: {} databases: {}

View file

@ -108,7 +108,9 @@ postgresql-tablespace-dir-{{ name }}:
{%- endfor %} {%- endfor %}
{%- if grains['init'] != 'unknown' %} {%- if not postgres.bake_image %}
# Start PostgreSQL server using OS init
postgresql-running: postgresql-running:
service.running: service.running:
@ -120,8 +122,7 @@ postgresql-running:
{%- else %} {%- else %}
# An attempt to launch PostgreSQL with `pg_ctl` if Salt was unable to # An attempt to launch PostgreSQL with `pg_ctl` during an image preparation
# detect local init system (`service` module would fail in this case)
postgresql-start: postgresql-start:
cmd.run: cmd.run:
@ -129,10 +130,10 @@ postgresql-start:
- runas: {{ postgres.user }} - runas: {{ postgres.user }}
- unless: - unless:
- ps -p $(head -n 1 {{ postgres.conf_dir }}/postmaster.pid) 2>/dev/null - ps -p $(head -n 1 {{ postgres.conf_dir }}/postmaster.pid) 2>/dev/null
- require:
- file: postgresql-pg_hba
# Try to enable PostgreSQL in "manual" way if Salt `service` state module # Try to enable PostgreSQL in "manual" way when baking an image
# is currently not available (e.g. during Docker or Packer build when is no init
# system running)
postgresql-enable: postgresql-enable:
cmd.run: cmd.run:
@ -146,7 +147,7 @@ postgresql-enable:
# Nothing to do # Nothing to do
- name: 'true' - name: 'true'
{%- endif %} {%- endif %}
- onchanges: - require:
- cmd: postgresql-start - cmd: postgresql-start
{%- endif %} {%- endif %}