Skip to content

Commit d4ed4be

Browse files
committed
Attempt to start and manage PostgreSQL service even if init system fails
1 parent 8007716 commit d4ed4be

File tree

6 files changed

+53
-101
lines changed

6 files changed

+53
-101
lines changed

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: 2 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

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.sls

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@ postgresql-server:
2020
- pkgrepo: postgresql-repo
2121
{%- endif %}
2222
23+
{%- if 'bin_dir' in postgres %}
24+
25+
# Make server binaries available in $PATH
26+
27+
{%- for bin in postgres.server_bins %}
28+
29+
{%- set path = salt['file.join'](postgres.bin_dir, bin) %}
30+
31+
{{ bin }}:
32+
alternatives.install:
33+
- link: {{ salt['file.join']('/usr/bin', bin) }}
34+
- path: {{ path }}
35+
- priority: 30
36+
- onlyif: test -f {{ path }}
37+
- require:
38+
- pkg: postgresql-server
39+
40+
{%- endfor %}
41+
42+
{%- endif %}
43+
2344
postgresql-cluster-prepared:
2445
cmd.run:
2546
- name: {{ postgres.prepare_cluster.command }}
@@ -93,23 +114,28 @@ postgresql-tablespace-dir-{{ name }}:
93114
94115
{%- endfor %}
95116
96-
{%- if 'bin_dir' in postgres %}
97-
98-
# Make server binaries available in $PATH
99-
100-
{%- for bin in postgres.server_bins %}
101-
102-
{%- set path = salt['file.join'](postgres.bin_dir, bin) %}
103-
104-
{{ bin }}:
105-
alternatives.install:
106-
- link: {{ salt['file.join']('/usr/bin', bin) }}
107-
- path: {{ path }}
108-
- priority: 30
109-
- onlyif: test -f {{ path }}
110-
- require:
111-
- pkg: postgresql-server
112-
113-
{%- endfor %}
117+
# An attempt to launch PostgreSQL with `pg_ctl` if service failed to start
118+
# with init system or Salt unable to load the `service` state module
119+
postgresql-start:
120+
cmd.run:
121+
- name: pg_ctl -D {{ postgres.conf_dir }} -l logfile start
122+
- runas: {{ postgres.user }}
123+
- unless:
124+
- ps -p $(head -n 1 {{ postgres.conf_dir }}/postmaster.pid) 2>/dev/null
125+
- onfail:
126+
- service: postgresql-running
114127
128+
# Try to enable PostgreSQL in "manual" way for systemd and RedHat-based distros.
129+
# The packages for other OS (i.e. `*.deb`) should do it automatically by default
130+
postgresql-enable:
131+
cmd.run:
132+
{%- if salt['file.file_exists']('/bin/systemctl') %}
133+
- name: systemctl enable {{ postgres.service }}
134+
{%- elif salt['cmd.which']('chkconfig') %}
135+
- name: chkconfig {{ postgres.service }} on
136+
{%- else %}
137+
# Nothing to do
138+
- name: 'true'
115139
{%- endif %}
140+
- onchanges:
141+
- cmd: postgresql-start

0 commit comments

Comments
 (0)