server.sls 5.05 KB
Newer Older
Julien Cigar's avatar
import  
Julien Cigar committed
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
{%- if salt.pillar.get('zfs:fs') %}
include:
  - zfs.fs
{%- endif %}

{% set pgsql = salt.pillar.get('postgresql') %}

pkg_postgresql_server:
  pkg.installed:
    - name: {{ pgsql.lookup.pkg_server }}

postgresql_pg_data:
  file.directory:
    - name: {{ pgsql.lookup.pg_data }}
    - user: {{ pgsql.lookup.user }}
    - group: {{ pgsql.lookup.user }}
    - mode: 700
    - makdedirs: True

{% if pgsql.conf.log_directory is defined %}
postgresql_log_directory:
  file.directory:
    - name: {{ pgsql.conf.log_directory }}
    - user: {{ pgsql.lookup.user }}
    - group: {{ pgsql.lookup.user }}
    - mode: 755
{% endif %}

postgresql_sysrc_data_dir:
  sysrc.managed:
    - name: postgresql_data
    - value: {{ pgsql.lookup.pg_data }}
    - require:
      - pkg: pkg_postgresql_server

postgresql_init_db:
  cmd.run:
    - name: service postgresql oneinitdb
    - cwd: /
    - require:
      - sysrc: postgresql_sysrc_data_dir
      - pkg: pkg_postgresql_server
      - file: postgresql_pg_data
    - unless:
      - test -d {{ pgsql.lookup.pg_data | path_join('base') }}

postgresql_conf:
  file.append:
    - name: {{ pgsql.lookup.pg_conf_file }}
    - text:
      - include_dir = '{{ pgsql.lookup.pg_confd_dir }}'

postgresql_override_conf:
  file.managed:
    - name: {{ pgsql.lookup.pg_confd_dir | path_join('saltstack.conf') }}
    - user: {{ pgsql.lookup.user }}
    - group: {{ pgsql.lookup.user }}
    - makedirs: True
    - mode: 400
    - contents: |
      {% for k,v in pgsql.conf.items() %}
        {{ k }} = '{{ v }}'
      {%- endfor %}
        data_directory = '{{ pgsql.lookup.pg_data }}'
        hba_file = '{{ pgsql.lookup.pg_hba_file }}'
        ident_file = '{{ pgsql.lookup.pg_ident_file }}'
    - require:
      - file: postgresql_conf

postgresql_pghba_conf:
  file.managed:
    - name: {{ pgsql.lookup.pg_hba_file }}
    - user: {{ pgsql.lookup.user }}
    - group: {{ pgsql.lookup.user }}
    - mode: 600
    {%- if pgsql.acls is defined %}
    - source: salt://postgresql/files/pg_hba.conf.jinja
    - template: jinja
    - defaults:
        acls: {{ pgsql.acls|yaml() }}
    {%- endif %}
    - require:
      - file: postgresql_override_conf

postgresql_service:
  service.running:
    - name: postgresql
    - enable: True
    - watch:
      - file: postgresql_override_conf
      - file: postgresql_pghba_conf
    - require:
      - sysrc: postgresql_sysrc_data_dir

#############
### ROLES ###
#############

{% if pgsql.roles is defined %}
{% for k, v in pgsql.roles.items() %}

{% if k != pgsql.lookup.user %}

{% if v.absent|default(False) %}
postgresql_role_{{ k }}:
  postgres_user.absent:
    - name: {{ k }}
    - user: {{ pgsql.lookup.user }}

{% else %}

postgresql_role_{{ k }}:
  postgres_user.present:
    - name: {{ k }}
    - login: {{ v.get('login', True) }}
    - createdb: {{ v.get('createdb', False) }}
    - user: {{ pgsql.lookup.user }}
    - password: {{ v.get('password') }}
  #- require:
    #- service: postgresql_service
{% endif %}

{% endif %}

{% endfor %}
{% endif %}

#################
### DATABASES ###
#################

{% if pgsql.databases is defined %}
{% for k,v in pgsql.databases.items() %}

{% if k != 'postgres' %}

{% if v.absent|default(False) %}
postgresql_database_{{ k }}:
  postgres_database.absent:
    - name: {{ k }}
    - user: {{ pgsql.lookup.user }}

{% else %}

postgresql_database_{{ k }}:
  postgres_database.present:
    - name: {{ k }}
    - owner: {{ v.owner }}
    - user: {{ pgsql.lookup.user }}
150
151
152
153
154
155
    {% if v.lc_ctype is defined %}
    - lc_ctype: {{ v.lc_ctype }}
    {% endif %}
    {% if v.template is defined %}
    - template: {{ v.template }}
    {% endif %}
Julien Cigar's avatar
import  
Julien Cigar committed
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
    - require:
      - postgres_user: {{ v.owner }}

{% if v.search_path is defined %}
# XXX: check if this is correct..
postgresql_database_{{ k }}_set_search_path:
  cmd.run:
    - name: psql --no-psqlrc --no-align --no-readline -d postgres -c 'ALTER DATABASE "{{ k }}" SET search_path TO {{ v.search_path|join(',') }}'
    - runas: {{ pgsql.lookup.user }}
    - require:
      - postgres_database: {{ k }}
{% endif %} 

{% endif %}

{% endif %}

# Create a dedicated pgbouncer schema to put the dedicated auth_query function
postgresql_database_{{ k }}_pgbouncer_schema:
  postgres_schema.present:
    - dbname: {{ k }}
    - name: pgbouncer
    - owner: pgbouncer
    - user: {{ pgsql.lookup.user }}
    - require:
      - postgres_user: pgbouncer

# Note: the query is run inside target database, so the dedicated function
# needs to be installed into each database.
# Use a non-admin user (pgbouncer) that calls SECURITY DEFINER function.
postgresql_database_{{ k }}_pgbouncer_lookup:
  cmd.script:
Julien Cigar's avatar
Julien Cigar committed
188
    - source: salt://postgresql/files/auth_query.sh
Julien Cigar's avatar
import  
Julien Cigar committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    - runas: {{ pgsql.lookup.user }}
    - env:
      - PSQL_ARGS: --no-psqlrc --no-align --no-readline -d {{ k }}
    - require:
      - postgres_schema: postgresql_database_{{ k }}_pgbouncer_schema

postgresql_database_{{ k }}_pgbouncer_connect:
  postgres_privileges.present:
    - name: pgbouncer
    - object_type: database
    - object_name: {{ k }}
    - privileges:
      - CONNECT
    - user: {{ pgsql.lookup.user }}

{% endfor %}
{% endif %}