Skip to content

Commit 7aacf98

Browse files
Merge pull request #136 from vutny/start-postgresql-even-if-there-is-no-init
Attempt to start and manage PostgreSQL service even if init system fails
2 parents 8007716 + 52d57d3 commit 7aacf98

File tree

10 files changed

+156
-126
lines changed

10 files changed

+156
-126
lines changed

README.rst

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Available states
1616
``postgres``
1717
------------
1818

19-
Installs and configures both PostgreSQL server and client with creation of
20-
various DB objects in the cluster.
19+
Installs and configures both PostgreSQL server and client with creation of various DB objects in
20+
the cluster.
2121

2222
``postgres.client``
2323
-------------------
@@ -27,8 +27,8 @@ Installs the PostgreSQL client binaries and libraries.
2727
``postgres.manage``
2828
-------------------
2929

30-
Creates such DB objects as: users, tablespaces, databases, schemas and
31-
extensions. See ``pillar.example`` file for details.
30+
Creates such DB objects as: users, tablespaces, databases, schemas and extensions.
31+
See ``pillar.example`` file for details.
3232

3333
``postgres.python``
3434
-------------------
@@ -38,22 +38,48 @@ Installs the PostgreSQL adapter for Python.
3838
``postgres.server``
3939
-------------------
4040

41-
Installs the PostgreSQL server package and prepares the DB cluster.
41+
Installs the PostgreSQL server package, prepares the DB cluster and starts the server using
42+
packaged init script, job or unit.
43+
44+
``postgres.server.image``
45+
-------------------------
46+
47+
Installs the PostgreSQL server package, prepares the DB cluster and starts the server by issuing
48+
raw ``pg_ctl`` command. The ``postgres:bake_image`` Pillar toggles this behaviour. For example:
49+
50+
.. code:: yaml
51+
52+
postgres:
53+
bake_image: True
54+
55+
If set ``True``, then it becomes possible to fully provision PostgreSQL with all supported entities
56+
from ``postgres.manage`` state during the build ("baking") of AMI / VM / Container images (using
57+
Packer, Docker or similar tools), i.e. when OS ``init`` process is not available to start the
58+
service and enable it on "boot" of resulting appliance.
59+
60+
Also it allows to make Docker images with PostgreSQL using functionality being available since Salt
61+
2016.11.0 release:
62+
63+
.. code:: console
64+
65+
salt 'minion.with.docker' dockerng.sls_build my-postgres base=centos/systemd mods=postgres
66+
67+
If a lookup dictionary or Pillar has ``postgres:bake_image`` set ``False`` (this is default), it is
68+
equivalent of applying ``postgres.server`` state.
4269

4370
``postgres.upstream``
4471
---------------------
4572

4673
Configures the PostgreSQL Official (upstream) repository on target system if
4774
applicable.
4875

49-
The state relies on the ``postgres:use_upstream_repo`` Pillar value which could
50-
be set as following:
76+
The state relies on the ``postgres:use_upstream_repo`` Pillar value which could be set as following:
5177

5278
* ``True`` (default): adds the upstream repository to install packages from
5379
* ``False``: makes sure that the repository configuration is absent
5480

55-
The ``postgres:version`` Pillar controls which version of the PostgreSQL
56-
packages should be installed from the upstream repository. Defaults to ``9.5``.
81+
The ``postgres:version`` Pillar controls which version of the PostgreSQL packages should be
82+
installed from the upstream repository. Defaults to ``9.5``.
5783

5884
Testing
5985
=======
@@ -79,3 +105,5 @@ Builds and runs tests from scratch.
79105
-----------------
80106

81107
Gives you ssh to the vagrant machine for manual testing.
108+
109+
.. vim: fenc=utf-8 spell spl=en cc=100 tw=99 fo=want sts=2 sw=2 et

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 ``bake_image`` setting to contol how PostgreSQL will be started: if set
56+
# to ``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/codenamemap.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
command: pg_createcluster {{ version }} main
3232
test: test -f /var/lib/postgresql/{{ version }}/main/PG_VERSION && test -f /etc/postgresql/{{ version }}/main/postgresql.conf
3333
user: root
34-
env: {}
3534

3635
{% endmacro %}
3736

postgres/defaults.yaml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,10 @@ postgres:
1212
user: postgres
1313
group: postgres
1414

15-
# if prepare_cluster is over-ridden in any of:
16-
# - osmap.yaml
17-
# - oscodenamemap.yaml
18-
# - osfingermap.yaml
19-
# you will have to specify a complete dictionary.
2015
prepare_cluster:
21-
user: root
22-
command: service postgresql initdb
16+
command: initdb --pgdata=/var/lib/pgsql/data
2317
test: test -f /var/lib/pgsql/data/PG_VERSION
18+
user: postgres
2419
env: {}
2520

2621
conf_dir: /var/lib/pgsql/data
@@ -38,6 +33,8 @@ postgres:
3833

3934
service: postgresql
4035

36+
bake_image: False
37+
4138
users: {}
4239
tablespaces: {}
4340
databases: {}

postgres/map.jinja

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{% import_yaml "postgres/defaults.yaml" as defaults %}
22
{% import_yaml "postgres/osmap.yaml" as osmap %}
33
{% import_yaml "postgres/codenamemap.yaml" as oscodenamemap %}
4-
{% import_yaml "postgres/osmajorreleasemap.yaml" as osmajorreleasemap %}
54

65
{% set postgres = salt['grains.filter_by'](
76
defaults,
@@ -11,11 +10,7 @@
1110
merge=salt['grains.filter_by'](
1211
oscodenamemap,
1312
grain='oscodename',
14-
merge=salt['grains.filter_by'](
15-
osmajorreleasemap,
16-
grain='osmajorrelease',
17-
merge=salt['pillar.get']('postgres', {}),
18-
),
13+
merge=salt['pillar.get']('postgres', {}),
1914
),
2015
),
2116
base='postgres',

postgres/osmajorreleasemap.yaml

Lines changed: 0 additions & 67 deletions
This file was deleted.

postgres/osmap.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
Arch:
66
conf_dir: /var/lib/postgres/data
77
prepare_cluster:
8-
user: postgres
98
command: initdb -D /var/lib/postgresql/data
109
test: test -f /var/lib/postgres/data/PG_VERSION
11-
env: {}
1210
pkg_client: postgresql
1311
pkg_dev: postgresql
1412

@@ -37,11 +35,17 @@ RedHat:
3735

3836
{% if repo.use_upstream_repo %}
3937

38+
{% set data_dir = '/var/lib/pgsql/' ~ repo.version ~ '/data' %}
39+
4040
pkg: postgresql{{ release }}-server
4141
pkg_client: postgresql{{ release }}
4242
conf_dir: /var/lib/pgsql/{{ repo.version }}/data
4343
service: postgresql-{{ repo.version }}
4444

45+
prepare_cluster:
46+
command: initdb --pgdata='{{ data_dir }}'
47+
test: test -f '{{ data_dir }}/PG_VERSION'
48+
4549
# Directory containing PostgreSQL client executables
4650
bin_dir: /usr/pgsql-{{ repo.version }}/bin
4751
client_bins:

postgres/server/image.sls

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{%- from "postgres/map.jinja" import postgres with context -%}
2+
3+
# This state is used to launch PostgreSQL with ``pg_ctl`` command and enable it
4+
# on "boot" during an image (Docker, Virtual Appliance, AMI) preparation
5+
6+
{%- if postgres.bake_image %}
7+
8+
include:
9+
- postgres.server
10+
11+
# An attempt to start PostgreSQL with `pg_ctl`
12+
13+
postgresql-start:
14+
cmd.run:
15+
- name: pg_ctl -D {{ postgres.conf_dir }} -l logfile start
16+
- runas: {{ postgres.user }}
17+
- unless:
18+
- ps -p $(head -n 1 {{ postgres.conf_dir }}/postmaster.pid) 2>/dev/null
19+
- require:
20+
- file: postgresql-pg_hba
21+
22+
# Try to enable PostgreSQL in "manual" way
23+
24+
postgresql-enable:
25+
cmd.run:
26+
{%- if salt['file.file_exists']('/bin/systemctl') %}
27+
- name: systemctl enable {{ postgres.service }}
28+
{%- elif salt['cmd.which']('chkconfig') %}
29+
- name: chkconfig {{ postgres.service }} on
30+
{%- elif salt['file.file_exists']('/usr/sbin/update-rc.d') %}
31+
- name: update-rc.d {{ service }} defaults
32+
{%- else %}
33+
# Nothing to do
34+
- name: 'true'
35+
{%- endif %}
36+
- require:
37+
- cmd: postgresql-start
38+
39+
{%- else %}
40+
41+
postgresql-start:
42+
test.show_notification:
43+
- text: The 'postgres:bake_image' Pillar is disabled (set to 'False').
44+
45+
{%- endif %}

0 commit comments

Comments
 (0)