From 186e972075619bb4b57e3f392dbfdc0192d65472 Mon Sep 17 00:00:00 2001 From: Denys Havrysh Date: Sat, 7 Jan 2017 15:11:26 +0200 Subject: [PATCH] Use ``bake_image`` Pillar setting to control PG startup behavior --- pillar.example | 17 ++++++++++++++--- postgres/defaults.yaml | 2 ++ postgres/server.sls | 15 ++++++++------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/pillar.example b/pillar.example index ddc2be7..e877314 100644 --- a/pillar.example +++ b/pillar.example @@ -19,7 +19,7 @@ postgres: postgresconf: | 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. {%- if 'status.time' in salt.keys() %} 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 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 # are authenticated, which PostgreSQL user names they can use, which # databases they can access. Records take one of these forms: @@ -48,9 +48,20 @@ postgres: # PostgreSQL service name 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. # 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. # # Format is the following: diff --git a/postgres/defaults.yaml b/postgres/defaults.yaml index 67f643f..d421aff 100644 --- a/postgres/defaults.yaml +++ b/postgres/defaults.yaml @@ -33,6 +33,8 @@ postgres: service: postgresql + bake_image: False + users: {} tablespaces: {} databases: {} diff --git a/postgres/server.sls b/postgres/server.sls index b9930db..8c12ba4 100644 --- a/postgres/server.sls +++ b/postgres/server.sls @@ -108,7 +108,9 @@ postgresql-tablespace-dir-{{ name }}: {%- endfor %} -{%- if grains['init'] != 'unknown' %} +{%- if not postgres.bake_image %} + +# Start PostgreSQL server using OS init postgresql-running: service.running: @@ -120,8 +122,7 @@ postgresql-running: {%- else %} -# An attempt to launch PostgreSQL with `pg_ctl` if Salt was unable to -# detect local init system (`service` module would fail in this case) +# An attempt to launch PostgreSQL with `pg_ctl` during an image preparation postgresql-start: cmd.run: @@ -129,10 +130,10 @@ postgresql-start: - runas: {{ postgres.user }} - unless: - 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 -# is currently not available (e.g. during Docker or Packer build when is no init -# system running) +# Try to enable PostgreSQL in "manual" way when baking an image postgresql-enable: cmd.run: @@ -146,7 +147,7 @@ postgresql-enable: # Nothing to do - name: 'true' {%- endif %} - - onchanges: + - require: - cmd: postgresql-start {%- endif %}