-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathmanage.sls
112 lines (82 loc) · 3.1 KB
/
manage.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
{%- from tpldir + "/map.jinja" import postgres with context -%}
{%- from tpldir + "/macros.jinja" import format_state with context -%}
{%- if salt['postgres.user_create']|default(none) is not callable %}
# Salt states for managing PostgreSQL is not available,
# need to provision client binaries first
include:
- postgres.client
{%- if 'server_bins' in postgres and grains['saltversion'] == '2016.11.0' %}
# FIXME: Salt v2016.11.0 bug https://github.com/saltstack/salt/issues/37935
- postgres.server
{%- endif %}
{%- endif %}
# Ensure that Salt is able to use postgres modules
postgres-reload-modules:
test.succeed_with_changes:
- reload_modules: True
# User states
{%- for name, user in postgres.users|dictsort() %}
{{ format_state(name, 'postgres_user', user) }}
{%- endfor %}
# Tablespace states
{%- for name, tblspace in postgres.tablespaces|dictsort() %}
{{ format_state(name, 'postgres_tablespace', tblspace) }}
{%- if 'owner' in tblspace %}
- require:
- postgres_user: postgres_user-{{ tblspace.owner }}
{%- endif %}
{%- endfor %}
# Database states
{%- for name, db in postgres.databases|dictsort() %}
{%- if 'extensions' in db %}
{%- for ext_name, extension in db.pop('extensions')|dictsort() %}
{%- do extension.update({'name': ext_name, 'maintenance_db': name}) %}
{{ format_state( name + '-' + ext_name, 'postgres_extension', extension) }}
- require:
- postgres_database: postgres_database-{{ name }}
{%- if 'schema' in extension and 'schemas' in postgres %}
- postgres_schema: postgres_schema-{{ name }}-{{ extension.schema }}
{%- endif %}
{%- endfor %}
{%- endif %}
{%- if 'schemas' in db %}
{%- for schema_name, schema in db.pop('schemas')|dictsort() %}
{%- do schema.update({'name': schema_name, 'dbname': name }) %}
{{ format_state( name + '-' + schema_name, 'postgres_schema', schema) }}
- require:
- postgres_database: postgres_database-{{ name }}
{%- endfor %}
{%- endif %}
{{ format_state(name, 'postgres_database', db) }}
{%- if 'owner' in db or 'tablespace' in db %}
- require:
{%- endif %}
{%- if 'owner' in db %}
- postgres_user: postgres_user-{{ db.owner }}
{%- endif %}
{%- if 'tablespace' in db %}
- postgres_tablespace: postgres_tablespace-{{ db.tablespace }}
{%- endif %}
{%- endfor %}
# Schema states
{%- for name, schema in postgres.schemas|dictsort() %}
{{ format_state(name, 'postgres_schema', schema) }}
- require:
- postgres_database-{{ schema.dbname }}
{%- if 'owner' in schema %}
- postgres_user: postgres_user-{{ schema.owner }}
{%- endif %}
{%- endfor %}
# Extension states
{%- for name, extension in postgres.extensions|dictsort() %}
{{ format_state(name, 'postgres_extension', extension) }}
{%- if 'maintenance_db' in extension or 'schema' in extension %}
- require:
{%- endif %}
{%- if 'maintenance_db' in extension %}
- postgres_database: postgres_database-{{ extension.maintenance_db }}
{%- endif %}
{%- if 'schema' in extension %}
- postgres_schema: postgres_schema-{{ extension.schema }}
{%- endif %}
{%- endfor %}