2017-01-08 17:41:12 +02:00
|
|
|
{%- from "postgres/map.jinja" import postgres with context %}
|
2016-10-04 14:38:21 +03:00
|
|
|
|
2017-01-08 17:41:12 +02:00
|
|
|
{%- set includes = [] %}
|
|
|
|
{%- if postgres.bake_image %}
|
|
|
|
{%- do includes.append('postgres.server.image') %}
|
|
|
|
{%- endif %}
|
2018-03-08 12:30:25 +00:00
|
|
|
{%- if postgres.use_upstream_repo == true -%}
|
2017-01-08 17:41:12 +02:00
|
|
|
{%- do includes.append('postgres.upstream') %}
|
|
|
|
{%- endif %}
|
|
|
|
|
|
|
|
{%- if includes -%}
|
2016-10-04 14:38:21 +03:00
|
|
|
include:
|
2017-01-08 17:41:12 +02:00
|
|
|
{{ includes|yaml(false)|indent(2) }}
|
2016-10-04 14:38:21 +03:00
|
|
|
{%- endif %}
|
|
|
|
|
2017-01-13 12:09:45 +02:00
|
|
|
{%- set pkgs = [postgres.pkg] + postgres.pkgs_extra %}
|
2016-10-04 14:38:21 +03:00
|
|
|
# Install, configure and start PostgreSQL server
|
|
|
|
postgresql-server:
|
|
|
|
pkg.installed:
|
|
|
|
- pkgs: {{ pkgs }}
|
2018-03-08 12:30:25 +00:00
|
|
|
{%- if postgres.use_upstream_repo == true %}
|
2016-10-04 14:38:21 +03:00
|
|
|
- refresh: True
|
|
|
|
- require:
|
|
|
|
- pkgrepo: postgresql-repo
|
|
|
|
{%- endif %}
|
2018-03-08 12:30:25 +00:00
|
|
|
{%- 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
|
|
|
|
{%- else %}
|
2016-10-04 14:38:21 +03:00
|
|
|
|
2018-01-14 16:12:17 +00:00
|
|
|
# Alternatives system. Make server binaries available in $PATH
|
2018-03-08 12:30:25 +00:00
|
|
|
{%- if 'bin_dir' in postgres and postgres.linux.altpriority %}
|
|
|
|
{%- for bin in postgres.server_bins %}
|
|
|
|
{%- set path = salt['file.join'](postgres.bin_dir, bin) %}
|
2017-01-03 11:54:00 +02:00
|
|
|
|
|
|
|
{{ bin }}:
|
|
|
|
alternatives.install:
|
|
|
|
- link: {{ salt['file.join']('/usr/bin', bin) }}
|
|
|
|
- path: {{ path }}
|
2018-01-14 16:12:17 +00:00
|
|
|
- priority: {{ postgres.linux.altpriority }}
|
2017-01-03 11:54:00 +02:00
|
|
|
- onlyif: test -f {{ path }}
|
|
|
|
- require:
|
|
|
|
- pkg: postgresql-server
|
2017-01-08 17:41:12 +02:00
|
|
|
- require_in:
|
|
|
|
- cmd: postgresql-cluster-prepared
|
2017-01-03 11:54:00 +02:00
|
|
|
|
2018-03-08 12:30:25 +00:00
|
|
|
{%- endfor %}
|
|
|
|
{%- endif %}
|
|
|
|
|
2017-01-03 11:54:00 +02:00
|
|
|
{%- endif %}
|
|
|
|
|
2016-10-04 14:38:21 +03:00
|
|
|
postgresql-cluster-prepared:
|
|
|
|
cmd.run:
|
|
|
|
- name: {{ postgres.prepare_cluster.command }}
|
|
|
|
- cwd: /
|
|
|
|
- runas: {{ postgres.prepare_cluster.user }}
|
2018-01-22 14:06:03 +00:00
|
|
|
- env: {{ postgres.prepare_cluster.env }}
|
2016-10-04 14:38:21 +03:00
|
|
|
- unless:
|
|
|
|
- {{ postgres.prepare_cluster.test }}
|
|
|
|
- require:
|
|
|
|
- pkg: postgresql-server
|
|
|
|
|
|
|
|
postgresql-config-dir:
|
|
|
|
file.directory:
|
|
|
|
- name: {{ postgres.conf_dir }}
|
|
|
|
- user: {{ postgres.user }}
|
|
|
|
- group: {{ postgres.group }}
|
2018-03-08 12:30:25 +00:00
|
|
|
- dir_mode: 775
|
|
|
|
- force: True
|
|
|
|
- file_mode: 644
|
|
|
|
- recurse:
|
|
|
|
- user
|
|
|
|
- group
|
2016-10-04 14:38:21 +03:00
|
|
|
- makedirs: True
|
|
|
|
- require:
|
|
|
|
- cmd: postgresql-cluster-prepared
|
|
|
|
|
|
|
|
{%- if postgres.postgresconf %}
|
|
|
|
|
|
|
|
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: |
|
|
|
|
{{ postgres.postgresconf|indent(8) }}
|
|
|
|
- show_changes: True
|
|
|
|
- append_if_not_found: True
|
2017-02-15 12:05:56 +02:00
|
|
|
{#- Detect empty values (none, '') in the config_backup #}
|
|
|
|
- backup: {{ postgres.config_backup|default(false, true) }}
|
2016-10-04 14:38:21 +03:00
|
|
|
- require:
|
|
|
|
- file: postgresql-config-dir
|
|
|
|
- watch_in:
|
|
|
|
- service: postgresql-running
|
|
|
|
|
|
|
|
{%- endif %}
|
|
|
|
|
2017-01-13 12:09:45 +02:00
|
|
|
{%- set pg_hba_path = salt['file.join'](postgres.conf_dir, 'pg_hba.conf') %}
|
|
|
|
|
2016-10-04 14:38:21 +03:00
|
|
|
postgresql-pg_hba:
|
|
|
|
file.managed:
|
2017-01-13 12:09:45 +02:00
|
|
|
- name: {{ pg_hba_path }}
|
2016-10-04 14:38:21 +03:00
|
|
|
- user: {{ postgres.user }}
|
|
|
|
- group: {{ postgres.group }}
|
|
|
|
- mode: 600
|
2017-01-12 11:34:41 +02:00
|
|
|
{%- if postgres.acls %}
|
|
|
|
- source: {{ postgres['pg_hba.conf'] }}
|
|
|
|
- template: jinja
|
2017-01-06 16:37:07 +02:00
|
|
|
- defaults:
|
|
|
|
acls: {{ postgres.acls }}
|
2017-02-15 12:05:56 +02:00
|
|
|
{%- 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 %}
|
2017-01-12 11:34:41 +02:00
|
|
|
{%- else %}
|
|
|
|
- replace: False
|
|
|
|
{%- endif %}
|
2016-10-04 14:38:21 +03:00
|
|
|
- require:
|
|
|
|
- file: postgresql-config-dir
|
|
|
|
|
|
|
|
{%- 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
|
2017-01-09 16:01:50 +02:00
|
|
|
- require:
|
|
|
|
- pkg: postgresql-server
|
2016-10-04 14:38:21 +03:00
|
|
|
|
|
|
|
{%- endfor %}
|
2016-12-01 16:11:37 +02:00
|
|
|
|
2017-01-07 15:11:26 +02:00
|
|
|
{%- if not postgres.bake_image %}
|
|
|
|
|
|
|
|
# Start PostgreSQL server using OS init
|
2017-01-06 16:37:07 +02:00
|
|
|
postgresql-running:
|
|
|
|
service.running:
|
|
|
|
- name: {{ postgres.service }}
|
|
|
|
- enable: True
|
2018-03-08 12:30:25 +00:00
|
|
|
{% if grains.os not in ('MacOS',) %}
|
2017-01-06 16:37:07 +02:00
|
|
|
- reload: True
|
2018-03-08 12:30:25 +00:00
|
|
|
{% endif %}
|
2017-01-06 16:37:07 +02:00
|
|
|
- watch:
|
|
|
|
- file: postgresql-pg_hba
|
|
|
|
|
|
|
|
{%- endif %}
|