Skip to content

Commit

Permalink
fix(postgres.manage): only reload modules if needed; fixes saltstack-…
Browse files Browse the repository at this point in the history
…formulas#214

Based on the great work of @RobRuana in saltstack-formulas#216.
  • Loading branch information
alxwr committed Apr 30, 2019
1 parent 29cd2d8 commit ad6caee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 2 additions & 0 deletions postgres/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ postgres:

bake_image: False

manage_force_reload_modules: False

fromrepo: ''

users: {}
Expand Down
2 changes: 1 addition & 1 deletion postgres/macros.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{{ state }}-{{ name }}:
{{ state }}.{{ ensure|default('present') }}:
{{- format_kwargs(kwarg) }}
- onchanges:
- require:
- test: postgres-reload-modules

{%- endmacro %}
Expand Down
25 changes: 15 additions & 10 deletions postgres/manage.sls
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{%- 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 %}
{%- set needs_client_binaries = salt['postgres.user_create']|default(none) is not callable -%}
{%- if needs_client_binaries %}
# Salt states for managing PostgreSQL is not available,
# need to provision client binaries first
Expand All @@ -18,7 +20,12 @@ include:
# Ensure that Salt is able to use postgres modules
postgres-reload-modules:
test.succeed_with_changes:
test.configurable_test_state:
- changes:
{%- if needs_client_binaries or postgres.manage_force_reload_modules %} True
{%- else %} False
{%- endif %}
- result: True
- reload_modules: True
# User states
Expand All @@ -35,7 +42,7 @@ postgres-reload-modules:
{{ format_state(name, 'postgres_tablespace', tblspace) }}
{%- if 'owner' in tblspace %}
- require:
{#- - require: #}
- postgres_user: postgres_user-{{ tblspace.owner }}
{%- endif %}
Expand All @@ -49,7 +56,7 @@ postgres-reload-modules:
{%- do extension.update({'name': ext_name, 'maintenance_db': name}) %}
{{ format_state( name + '-' + ext_name, 'postgres_extension', extension) }}
- require:
{#- - require: #}
- postgres_database: postgres_database-{{ name }}
{%- if 'schema' in extension and 'schemas' in postgres %}
- postgres_schema: postgres_schema-{{ name }}-{{ extension.schema }}
Expand All @@ -62,15 +69,15 @@ postgres-reload-modules:
{%- do schema.update({'name': schema_name, 'dbname': name }) %}
{{ format_state( name + '-' + schema_name, 'postgres_schema', schema) }}
- require:
{#- - require: #}
- postgres_database: postgres_database-{{ name }}
{%- endfor %}
{%- endif %}
{{ format_state(name, 'postgres_database', db) }}
{%- if 'owner' in db or 'tablespace' in db %}
- require:
{#- - require: #}
{%- endif %}
{%- if 'owner' in db %}
- postgres_user: postgres_user-{{ db.owner }}
Expand All @@ -86,7 +93,7 @@ postgres-reload-modules:
{%- for name, schema in postgres.schemas|dictsort() %}
{{ format_state(name, 'postgres_schema', schema) }}
- require:
{#- - require: #}
- postgres_database-{{ schema.dbname }}
{%- if 'owner' in schema %}
- postgres_user: postgres_user-{{ schema.owner }}
Expand All @@ -99,9 +106,7 @@ postgres-reload-modules:
{%- for name, extension in postgres.extensions|dictsort() %}
{{ format_state(name, 'postgres_extension', extension) }}
{%- if 'maintenance_db' in extension or 'schema' in extension %}
- require:
{%- endif %}
{#- - require: #}
{%- if 'maintenance_db' in extension %}
- postgres_database: postgres_database-{{ extension.maintenance_db }}
{%- endif %}
Expand Down

0 comments on commit ad6caee

Please sign in to comment.