-
Notifications
You must be signed in to change notification settings - Fork 282
/
init.sls
278 lines (248 loc) · 7.96 KB
/
init.sls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
{%- from salt.file.dirname(tpldir) ~ "/map.jinja" import postgres with context -%}
{%- set includes = [] %}
{%- if postgres.bake_image %}
{%- do includes.append('postgres.server.image') %}
{%- endif %}
{%- if postgres.use_upstream_repo == true -%}
{%- do includes.append('postgres.upstream') %}
{%- endif %}
{%- if includes -%}
include:
{{ includes|yaml(false)|indent(2) }}
{%- endif %}
{%- set pkgs = [postgres.pkg] + postgres.pkgs_extra %}
# Install, configure and start PostgreSQL server
postgresql-server:
pkg.installed:
- pkgs: {{ pkgs }}
{%- if postgres.use_upstream_repo == true %}
- refresh: True
- require:
- pkgrepo: postgresql-repo
{%- endif %}
{%- if postgres.fromrepo %}
- fromrepo: {{ postgres.fromrepo }}
{%- endif %}
{%- if grains.os == 'MacOS' %}
#Register as Launchd LaunchAgent for system users
- require_in:
- file: postgresql-server
file.managed:
- name: /Library/LaunchAgents/{{ postgres.service }}.plist
- source: /usr/local/opt/postgres/{{ postgres.service }}.plist
- group: wheel
- require_in:
- service: postgresql-running
# Alternatives system. Make server binaries available in $PATH
{%- elif 'bin_dir' in postgres and postgres.linux.altpriority %}
{%- for bin in postgres.server_bins %}
{%- set path = salt['file.join'](postgres.bin_dir, bin) %}
postgresql-{{ bin }}-altinstall:
alternatives.install:
- name: {{ bin }}
- link: {{ salt['file.join']('/usr/bin', bin) }}
- path: {{ path }}
- priority: {{ postgres.linux.altpriority }}
- onlyif: test -f {{ path }}
- require:
- pkg: postgresql-server
- require_in:
- cmd: postgresql-cluster-prepared
{%- endfor %}
{%- endif %}
postgresql-cluster-prepared:
file.directory:
- name: {{ postgres.data_dir }}
- user: {{ postgres.user }}
- group: {{ postgres.group }}
- makedirs: True
- recurse:
- user
- group
{%- if postgres.prepare_cluster.run %}
cmd.run:
{%- if postgres.prepare_cluster.command is defined %}
{# support for depreciated 'prepare_cluster.command' pillar #}
- name: {{ postgres.prepare_cluster.command }}
- unless: {{ postgres.prepare_cluster.test }}
{%- else %}
- name: {{ postgres.prepare_cluster_cmd }}
- unless: test -f {{ postgres.data_dir }}/{{ postgres.prepare_cluster.pgtestfile }}
{%- endif %}
- cwd: /
- env: {{ postgres.prepare_cluster.env }}
- runas: {{ postgres.prepare_cluster.user }}
- require:
- pkg: postgresql-server
- file: postgresql-cluster-prepared
- watch_in:
- service: postgresql-running
{%- endif %}
postgresql-config-dir:
file.directory:
- names:
- {{ postgres.data_dir }}
- {{ postgres.conf_dir }}
- user: {{ postgres.user }}
- group: {{ postgres.group }}
- dir_mode: {{ postgres.conf_dir_mode }}
- force: True
- recurse:
- mode
- ignore_files
- makedirs: True
- require:
{%- if postgres.prepare_cluster.run %}
- cmd: postgresql-cluster-prepared
{%- else %}
- file: postgresql-cluster-prepared
{%- endif %}
{%- set db_port = salt['config.option']('postgres.port') %}
{%- if db_port %}
postgresql-conf-comment-port:
file.comment:
- name: {{ postgres.conf_dir }}/postgresql.conf
- regex: ^port\s*=.+
- require:
- file: postgresql-config-dir
{%- endif %}
{%- if postgres.postgresconf or db_port %}
postgresql-conf:
file.blockreplace:
- name: {{ postgres.conf_dir }}/postgresql.conf
- marker_start: "# Managed by SaltStack: listen_addresses: please do not edit"
- marker_end: "# Managed by SaltStack: end of salt managed zone --"
- content: |
{%- if postgres.postgresconf %}
{{ postgres.postgresconf|indent(8) }}
{%- endif %}
{%- if db_port %}
port = {{ db_port }}
{%- endif %}
- show_changes: True
- append_if_not_found: True
{#- Detect empty values (none, '') in the config_backup #}
- backup: {{ postgres.config_backup|default(false, true) }}
- require:
- file: postgresql-config-dir
{%- if db_port %}
- file: postgresql-conf-comment-port
{%- endif %}
- watch_in:
- service: postgresql-running
{%- endif %}
{%- set pg_hba_path = salt['file.join'](postgres.conf_dir, 'pg_hba.conf') %}
postgresql-pg_hba:
file.managed:
- name: {{ pg_hba_path }}
- user: {{ postgres.user }}
- group: {{ postgres.group }}
- mode: 600
{%- if postgres.acls %}
- source: {{ postgres['pg_hba.conf'] }}
- template: jinja
- defaults:
acls: {{ postgres.acls|yaml() }}
{%- if postgres.config_backup %}
# Create the empty file before managing to overcome the limitation of check_cmd
- onlyif: test -f {{ pg_hba_path }} || touch {{ pg_hba_path }}
# Make a local backup before the file modification
- check_cmd: >-
salt-call --local file.copy
{{ pg_hba_path }} {{ pg_hba_path ~ postgres.config_backup }} remove_existing=true
{%- endif %}
{%- else %}
- replace: False
{%- endif %}
- require:
- file: postgresql-config-dir
- watch_in:
- service: postgresql-running
{%- set pg_ident_path = salt['file.join'](postgres.conf_dir, 'pg_ident.conf') %}
postgresql-pg_ident:
file.managed:
- name: {{ pg_ident_path }}
- user: {{ postgres.user }}
- group: {{ postgres.group }}
- mode: 600
{%- if postgres.identity_map %}
- source: {{ postgres['pg_ident.conf'] }}
- template: jinja
- defaults:
mappings: {{ postgres.identity_map|yaml() }}
{%- if postgres.config_backup %}
# Create the empty file before managing to overcome the limitation of check_cmd
- onlyif: test -f {{ pg_ident_path }} || touch {{ pg_ident_path }}
# Make a local backup before the file modification
- check_cmd: >-
salt-call --local file.copy
{{ pg_ident_path }} {{ pg_ident_path ~ postgres.config_backup }} remove_existing=true
{%- endif %}
{%- else %}
- replace: False
{%- endif %}
- require:
- file: postgresql-config-dir
{%- if postgres.prepare_cluster.run %}
- cmd: postgresql-cluster-prepared
{%- else %}
- file: postgresql-cluster-prepared
{%- endif %}
- watch_in:
{%- if grains.os not in ('MacOS',) %}
- module: postgresql-service-reload
{%- else %}
- service: postgresql-running
{%- endif %}
{%- for name, tblspace in postgres.tablespaces|dictsort() %}
postgresql-tablespace-dir-{{ name }}:
file.directory:
- name: {{ tblspace.directory }}
- user: {{ postgres.user }}
- group: {{ postgres.group }}
- mode: 700
- makedirs: True
- recurse:
- user
- group
- require:
- pkg: postgresql-server
{%- if "selinux" in grains and grains.selinux.enabled %}
pkg.installed:
- names:
- policycoreutils-python
- selinux-policy-targeted
- refresh: True
selinux.fcontext_policy_present:
- name: '{{ tblspace.directory }}(/.*)?'
- sel_type: postgresql_db_t
- require:
- file: postgresql-tablespace-dir-{{ name }}
- pkg: postgresql-tablespace-dir-{{ name }}
postgresql-tablespace-dir-{{ name }}-fcontext:
selinux.fcontext_policy_applied:
- name: {{ tblspace.directory }}
- recursive: True
- require:
- selinux: postgresql-tablespace-dir-{{ name }}
{%- endif %}
{%- endfor %}
{%- if not postgres.bake_image %}
# Start PostgreSQL server using OS init
# Note: This is also the target for numerous `watch_in` requisites above, used
# for the necessary service restart after changing the relevant configuration files
postgresql-running:
service.running:
- name: {{ postgres.service }}
- enable: True
# Reload the service for changes made to `pg_ident.conf`, except for `MacOS`
# which is handled by `postgresql-running` above.
{%- if grains.os not in ('MacOS',) %}
postgresql-service-reload:
module.wait:
- name: service.reload
- m_name: {{ postgres.service }}
- require:
- service: postgresql-running
{%- endif %}
{%- endif %}