This commit is contained in:
noelmcloughlin 2018-03-27 18:42:53 +01:00
commit 44f5b7d158
19 changed files with 450 additions and 74 deletions

View file

@ -17,12 +17,12 @@ Available states
------------ ------------
Installs and configures both PostgreSQL server and client with creation of various DB objects in Installs and configures both PostgreSQL server and client with creation of various DB objects in
the cluster. the cluster. This state applies to both Linux and MacOS.
``postgres.client`` ``postgres.client``
------------------- -------------------
Installs the PostgreSQL client binaries and libraries. Installs the PostgreSQL client binaries and libraries on Linux.
``postgres.manage`` ``postgres.manage``
------------------- -------------------
@ -33,18 +33,18 @@ See ``pillar.example`` file for details.
``postgres.python`` ``postgres.python``
------------------- -------------------
Installs the PostgreSQL adapter for Python. Installs the PostgreSQL adapter for Python on Linux.
``postgres.server`` ``postgres.server``
------------------- -------------------
Installs the PostgreSQL server package, prepares the DB cluster and starts the server using Installs the PostgreSQL server package on Linux, prepares the DB cluster and starts the server using
packaged init script, job or unit. packaged init script, job or unit.
``postgres.server.image`` ``postgres.server.image``
------------------------- -------------------------
Installs the PostgreSQL server package, prepares the DB cluster and starts the server by issuing Installs the PostgreSQL server package on Linux, prepares the DB cluster and starts the server by issuing
raw ``pg_ctl`` command. The ``postgres:bake_image`` Pillar toggles this behaviour. For example: raw ``pg_ctl`` command. The ``postgres:bake_image`` Pillar toggles this behaviour. For example:
.. code:: yaml .. code:: yaml
@ -77,12 +77,15 @@ The state relies on the ``postgres:use_upstream_repo`` Pillar value which could
* ``True`` (default): adds the upstream repository to install packages from * ``True`` (default): adds the upstream repository to install packages from
* ``False``: makes sure that the repository configuration is absent * ``False``: makes sure that the repository configuration is absent
* ``'postgresapp'`` (MacOS) uses upstream PostgresApp package repository.
* ``'homebrew'`` (MacOS) uses Homebrew postgres
The ``postgres:version`` Pillar controls which version of the PostgreSQL packages should be The ``postgres:version`` Pillar controls which version of the PostgreSQL packages should be
installed from the upstream repository. Defaults to ``9.5``. installed from the upstream Linux repository. Defaults to ``9.5``.
Testing Testing
======= =======
The postgres state was tested on MacOS (El Capitan 10.11.6)
Testing is done with the ``kitchen-salt``. Testing is done with the ``kitchen-salt``.

View file

@ -1,19 +1,34 @@
postgres: postgres:
# Set True to configure upstream postgresql.org repository for YUM or APT # UPSTREAM REPO
# Set True to configure upstream postgresql.org repository for YUM/APT/ZYPP
use_upstream_repo: False use_upstream_repo: False
# Version to install from upstream repository # Version to install from upstream repository (if upstream_repo: True)
version: '9.3' version: '9.6'
# These are Debian/Ubuntu specific package names ### MACOS
pkg: 'postgresql-9.3' # Set to 'postgresapp' OR 'homebrew' for MacOS
pkg_client: 'postgresql-client-9.3' #use_upstream_repo: 'postgresapp'
#use_upstream_repo: 'homebrew'
# Additional packages to install with PostgreSQL server, # PACKAGE
# this should be in a list format # These pillars are typically never required.
# pkg: 'postgresql'
# pkg_client: 'postgresql-client'
# service: postgresql
pkgs_extra: pkgs_extra:
- postgresql-contrib - postgresql-contrib
- postgresql-plpython - postgresql-plpython
#'Alternatives system' priority incremental. 0 disables feature.
linux:
altpriority: 30
# macos limits
limits:
soft: 64000
hard: 64000
# POSTGRES
# Append the lines under this item to your postgresql.conf file. # Append the lines under this item to your postgresql.conf file.
# Pay attention to indent exactly with 4 spaces for all lines. # Pay attention to indent exactly with 4 spaces for all lines.
postgresconf: | postgresconf: |
@ -39,18 +54,20 @@ postgres:
# If ``acls`` item value is empty ('', [], null), then the contents of # If ``acls`` item value is empty ('', [], null), then the contents of
# ``pg_hba.conf`` file will not be touched at all. # ``pg_hba.conf`` file will not be touched at all.
acls: acls:
- ['local', 'db0', 'connuser', 'peer map=users_as_appuser']
- ['local', 'db1', 'localUser'] - ['local', 'db1', 'localUser']
- ['host', 'db2', 'remoteUser', '192.168.33.0/24'] - ['host', 'db2', 'remoteUser', '192.168.33.0/24']
identity_map:
- ['users_as_appuser', 'jdoe', 'connuser']
- ['users_as_appuser', 'jsmith', 'connuser']
# Backup extension for configuration files, defaults to ``.bak``. # Backup extension for configuration files, defaults to ``.bak``.
# Set ``False`` to stop creation of backups when config files change. # Set ``False`` to stop creation of backups when config files change.
{%- if salt['status.time']|default(none) is callable %} {%- if salt['status.time']|default(none) is callable %}
config_backup: ".backup@{{ salt['status.time']('%y-%m-%d_%H:%M:%S') }}" config_backup: ".backup@{{ salt['status.time']('%y-%m-%d_%H:%M:%S') }}"
{%- endif %} {%- endif %}
# PostgreSQL service name
service: postgresql
{%- if grains['init'] == 'unknown' %} {%- if grains['init'] == 'unknown' %}
# If Salt is unable to detect init system running in the scope of state run, # If Salt is unable to detect init system running in the scope of state run,

View file

@ -7,41 +7,34 @@
{%- endif %} {%- endif %}
{%- endfor %} {%- endfor %}
{%- if postgres.use_upstream_repo %} {%- if postgres.use_upstream_repo == true %}
include: include:
- postgres.upstream - postgres.upstream
{%- endif %} {%- endif %}
# Install PostgreSQL client and libraries # Install PostgreSQL client and libraries
postgresql-client-libs: postgresql-client-libs:
pkg.installed: pkg.installed:
- pkgs: {{ pkgs }} - pkgs: {{ pkgs }}
{%- if postgres.use_upstream_repo %} {%- if postgres.use_upstream_repo == true %}
- refresh: True - refresh: True
- require: - require:
- pkgrepo: postgresql-repo - pkgrepo: postgresql-repo
{%- endif %} {%- endif %}
{%- if 'bin_dir' in postgres %} # Alternatives system. Make client binaries available in $PATH
{%- if 'bin_dir' in postgres and postgres.linux.altpriority %}
# Make client binaries available in $PATH {%- for bin in postgres.client_bins %}
{%- set path = salt['file.join'](postgres.bin_dir, bin) %}
{%- for bin in postgres.client_bins %}
{%- set path = salt['file.join'](postgres.bin_dir, bin) %}
{{ bin }}: {{ bin }}:
alternatives.install: alternatives.install:
- link: {{ salt['file.join']('/usr/bin', bin) }} - link: {{ salt['file.join']('/usr/bin', bin) }}
- path: {{ path }} - path: {{ path }}
- priority: 30 - priority: {{ postgres.linux.altpriority }}
- onlyif: test -f {{ path }} - onlyif: test -f {{ path }}
- require: - require:
- pkg: postgresql-client-libs - pkg: postgresql-client-libs
{%- endfor %} {%- endfor %}
{%- endif %} {%- endif %}

View file

@ -16,7 +16,7 @@
#} #}
{# use upstream version if configured #} {# use upstream version if configured #}
{% if repo.use_upstream_repo %} {% if repo.use_upstream_repo == true %}
{% set version = repo.version %} {% set version = repo.version %}
{% endif %} {% endif %}
@ -49,7 +49,7 @@
#} #}
{# use upstream version if configured #} {# use upstream version if configured #}
{% if repo.use_upstream_repo %} {% if repo.use_upstream_repo == true %}
{% set version = repo.version %} {% set version = repo.version %}
{% endif %} {% endif %}

View file

@ -7,8 +7,10 @@ postgres:
pkgs_extra: [] pkgs_extra: []
pkg_client: postgresql-client pkg_client: postgresql-client
pkg_dev: postgresql-devel pkg_dev: postgresql-devel
pkg_libpq_dev: postgresql-libs pkg_libpq_dev: libpq-dev
python: python-psycopg2 pkg_libs: postgresql-libs
pkg_python: python-psycopg2
userhomes: /home
user: postgres user: postgres
group: postgres group: postgres
@ -16,11 +18,26 @@ postgres:
command: initdb --pgdata=/var/lib/pgsql/data command: initdb --pgdata=/var/lib/pgsql/data
test: test -f /var/lib/pgsql/data/PG_VERSION test: test -f /var/lib/pgsql/data/PG_VERSION
user: postgres user: postgres
env: {} env: []
conf_dir: /var/lib/pgsql/data conf_dir: /var/lib/pgsql/data
postgresconf: "" postgresconf: ""
macos:
archive: postgres.dmg
tmpdir: /tmp/postgrestmp
postgresapp:
#See: https://github.com/PostgresApp/PostgresApp/releases/
url: https://github.com/PostgresApp/PostgresApp/releases/download/v2.1.1/Postgres-2.1.1.dmg
sum: sha256=ac0656b522a58fd337931313f09509c09610c4a6078fe0b8e469e69af1e1750b
homebrew:
url:
sum:
dl:
opts: -s -L
interval: 60
retries: 2
pg_hba.conf: salt://postgres/templates/pg_hba.conf.j2 pg_hba.conf: salt://postgres/templates/pg_hba.conf.j2
acls: acls:
# "local" is for Unix domain socket connections only # "local" is for Unix domain socket connections only
@ -30,6 +47,9 @@ postgres:
# IPv6 local connections: # IPv6 local connections:
- ['host', 'all', 'all', '::1/128', 'md5'] - ['host', 'all', 'all', '::1/128', 'md5']
pg_ident.conf: salt://postgres/templates/pg_ident.conf.j2
identity_map: []
config_backup: '.bak' config_backup: '.bak'
service: postgresql service: postgresql
@ -41,3 +61,7 @@ postgres:
databases: {} databases: {}
schemas: {} schemas: {}
extensions: {} extensions: {}
linux:
#Alternatives system are disabled by a 'altpriority=0' pillar.
altpriority:

View file

@ -1,13 +1,59 @@
{% from "postgres/map.jinja" import postgres with context %} {% from "postgres/map.jinja" import postgres with context %}
{% if postgres.pkg_dev %} {% if grains.os not in ('Windows', 'MacOS',) %}
{% if postgres.pkg_dev %}
install-postgres-dev-package: install-postgres-dev-package:
pkg.installed: pkg.installed:
- name: {{ postgres.pkg_dev }} - name: {{ postgres.pkg_dev }}
{% endif %} {% endif %}
{% if postgres.pkg_libpq_dev %} {% if postgres.pkg_libpq_dev %}
install-postgres-libpq-dev: install-postgres-libpq-dev:
pkg.installed: pkg.installed:
- name: {{ postgres.pkg_libpq_dev }} - name: {{ postgres.pkg_libpq_dev }}
{% endif %}
{% endif %}
{% if grains.os == 'MacOS' %}
# Darwin maxfiles limits
{% if postgres.limits.soft or postgres.limits.hard %}
postgres_maxfiles_limits_conf:
file.managed:
- name: /Library/LaunchDaemons/limit.maxfiles.plist
- source: salt://postgres/templates/limit.maxfiles.plist
- context:
soft_limit: {{ postgres.limits.soft or postgres.limits.hard }}
hard_limit: {{ postgres.limits.hard or postgres.limits.soft }}
- group: {{ postgres.group }}
{% endif %}
{% if postgres.use_upstream_repo == 'postgresapp' %}
# Shortcut for PostgresApp
postgres-desktop-shortcut-clean:
file.absent:
- name: '{{ postgres.userhomes }}/{{ postgres.user }}/Desktop/Postgres ({{ postgres.use_upstream_repo }})'
- require_in:
- file: postgres-desktop-shortcut-add
postgres-desktop-shortcut-add:
file.managed:
- name: /tmp/mac_shortcut.sh
- source: salt://postgres/templates/mac_shortcut.sh
- mode: 755
- template: jinja
- context:
user: {{ postgres.user }}
homes: {{ postgres.userhomes }}
cmd.run:
- name: '/tmp/mac_shortcut.sh "Postgres ({{ postgres.use_upstream_repo }})"'
- runas: {{ postgres.user }}
- require:
- file: postgres-desktop-shortcut-add
{% endif %}
{% endif %} {% endif %}

View file

@ -1,4 +1,9 @@
include: include:
{% if grains.os == 'MacOS' %}
- postgres.macos
{% else %}
- postgres.server - postgres.server
- postgres.client - postgres.client
- postgres.manage - postgres.manage
{% endif %}

10
postgres/macos/init.sls Normal file
View file

@ -0,0 +1,10 @@
{% from "postgres/map.jinja" import postgres with context %}
include:
{% if postgres.use_upstream_repo == 'postgresapp' %}
- postgres.macos.postgresapp
{% elif postgres.use_upstream_repo == 'homebrew' %}
- postgres.server
- postgres.client
{% endif %}
- postgres.dev

View file

@ -0,0 +1,64 @@
{% from "postgres/map.jinja" import postgres as pg with context %}
# Cleanup first
pg-remove-prev-archive:
file.absent:
- name: '{{ pg.macos.tmpdir }}/{{ pg.macos.archive }}'
- require_in:
- pg-extract-dirs
pg-extract-dirs:
file.directory:
- names:
- '{{ pg.macos.tmpdir }}'
- makedirs: True
- clean: True
- require_in:
- pg-download-archive
pg-download-archive:
pkg.installed:
- name: curl
cmd.run:
- name: curl {{ pg.macos.dl.opts }} -o '{{ pg.macos.tmpdir }}/{{ pg.macos.archive }}' {{ pg.macos.postgresapp.url }}
{% if grains['saltversioninfo'] >= [2017, 7, 0] %}
- retry:
attempts: {{ pg.macos.dl.retries }}
interval: {{ pg.macos.dl.interval }}
{% endif %}
{%- if pg.macos.postgresapp.sum %}
pg-check-archive-hash:
module.run:
- name: file.check_hash
- path: '{{ pg.macos.tmpdir }}/{{ pg.macos.archive }}'
- file_hash: {{ pg.macos.postgresapp.sum }}
- onchanges:
- cmd: pg-download-archive
- require_in:
- archive: pg-package-install
{%- endif %}
pg-package-install:
macpackage.installed:
- name: '{{ pg.macos.tmpdir }}/{{ pg.macos.archive }}'
- store: True
- dmg: True
- app: True
- force: True
- allow_untrusted: True
- onchanges:
- cmd: pg-download-archive
- require_in:
- file: pg-package-install
- file: pg-remove-archive
file.append:
- name: {{ pg.userhomes }}/{{ pg.user }}/.bash_profile
- text: 'export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin'
pg-remove-archive:
file.absent:
- name: '{{ pg.macos.tmpdir }}'
- onchanges:
- macpackage: pg-package-install

View file

@ -23,6 +23,8 @@
{{ state }}-{{ name }}: {{ state }}-{{ name }}:
{{ state }}.{{ ensure|default('present') }}: {{ state }}.{{ ensure|default('present') }}:
{{- format_kwargs(kwarg) }} {{- format_kwargs(kwarg) }}
- onchanges:
- test: postgres-reload-modules
{%- endmacro %} {%- endmacro %}

View file

@ -18,7 +18,7 @@ include:
# Ensure that Salt is able to use postgres modules # Ensure that Salt is able to use postgres modules
postgres-reload-modules: postgres-reload-modules:
test.nop: test.succeed_with_changes:
- reload_modules: True - reload_modules: True
# User states # User states
@ -26,8 +26,6 @@ postgres-reload-modules:
{%- for name, user in postgres.users|dictsort() %} {%- for name, user in postgres.users|dictsort() %}
{{ format_state(name, 'postgres_user', user) }} {{ format_state(name, 'postgres_user', user) }}
- require:
- test: postgres-reload-modules
{%- endfor %} {%- endfor %}
@ -36,9 +34,8 @@ postgres-reload-modules:
{%- for name, tblspace in postgres.tablespaces|dictsort() %} {%- for name, tblspace in postgres.tablespaces|dictsort() %}
{{ format_state(name, 'postgres_tablespace', tblspace) }} {{ format_state(name, 'postgres_tablespace', tblspace) }}
- require:
- test: postgres-reload-modules
{%- if 'owner' in tblspace %} {%- if 'owner' in tblspace %}
- require:
- postgres_user: postgres_user-{{ tblspace.owner }} - postgres_user: postgres_user-{{ tblspace.owner }}
{%- endif %} {%- endif %}
@ -49,8 +46,9 @@ postgres-reload-modules:
{%- for name, db in postgres.databases|dictsort() %} {%- for name, db in postgres.databases|dictsort() %}
{{ format_state(name, 'postgres_database', db) }} {{ format_state(name, 'postgres_database', db) }}
{%- if 'owner' in db or 'tablespace' in db %}
- require: - require:
- test: postgres-reload-modules {%- endif %}
{%- if 'owner' in db %} {%- if 'owner' in db %}
- postgres_user: postgres_user-{{ db.owner }} - postgres_user: postgres_user-{{ db.owner }}
{%- endif %} {%- endif %}
@ -65,9 +63,8 @@ postgres-reload-modules:
{%- for name, schema in postgres.schemas|dictsort() %} {%- for name, schema in postgres.schemas|dictsort() %}
{{ format_state(name, 'postgres_schema', schema) }} {{ format_state(name, 'postgres_schema', schema) }}
- require:
- test: postgres-reload-modules
{%- if 'owner' in schema %} {%- if 'owner' in schema %}
- require:
- postgres_user: postgres_user-{{ schema.owner }} - postgres_user: postgres_user-{{ schema.owner }}
{%- endif %} {%- endif %}
@ -78,8 +75,9 @@ postgres-reload-modules:
{%- for name, extension in postgres.extensions|dictsort() %} {%- for name, extension in postgres.extensions|dictsort() %}
{{ format_state(name, 'postgres_extension', extension) }} {{ format_state(name, 'postgres_extension', extension) }}
{%- if 'maintenance_db' in extension or 'schema' in extension %}
- require: - require:
- test: postgres-reload-modules {%- endif %}
{%- if 'maintenance_db' in extension %} {%- if 'maintenance_db' in extension %}
- postgres_database: postgres_database-{{ extension.maintenance_db }} - postgres_database: postgres_database-{{ extension.maintenance_db }}
{%- endif %} {%- endif %}

View file

@ -17,7 +17,6 @@ Debian:
file: /etc/apt/sources.list.d/pgdg.list file: /etc/apt/sources.list.d/pgdg.list
pkg_repo_keyid: ACCC4CF8 pkg_repo_keyid: ACCC4CF8
pkg_dev: postgresql-server-dev-all pkg_dev: postgresql-server-dev-all
pkg_libpq_dev: libpq-dev
FreeBSD: FreeBSD:
user: pgsql user: pgsql
@ -29,16 +28,21 @@ RedHat:
pkg_repo: pkg_repo:
name: pgdg{{ release }} name: pgdg{{ release }}
humanname: PostgreSQL {{ repo.version }} $releasever - $basearch humanname: PostgreSQL {{ repo.version }} $releasever - $basearch
baseurl: 'https://download.postgresql.org/pub/repos/yum/{{ repo.version }}/redhat/rhel-$releasever-$basearch'
gpgcheck: 1 gpgcheck: 1
gpgkey: 'https://download.postgresql.org/pub/repos/yum/RPM-GPG-KEY-PGDG-{{ release }}' gpgkey: 'https://download.postgresql.org/pub/repos/yum/RPM-GPG-KEY-PGDG-{{ release }}'
{% if grains.os == 'Fedora' %}
baseurl: 'https://download.postgresql.org/pub/repos/yum/{{ repo.version }}/fedora/fedora-$releasever-$basearch'
{% else %}
baseurl: 'https://download.postgresql.org/pub/repos/yum/{{ repo.version }}/redhat/rhel-$releasever-$basearch'
{% endif %}
{% if repo.use_upstream_repo %} {% if repo.use_upstream_repo == true %}
{% set data_dir = '/var/lib/pgsql/' ~ repo.version ~ '/data' %} {% set data_dir = '/var/lib/pgsql/' ~ repo.version ~ '/data' %}
pkg: postgresql{{ release }}-server pkg: postgresql{{ release }}-server
pkg_client: postgresql{{ release }} pkg_client: postgresql{{ release }}
pkg_libs: postgresql{{ release }}-libs
pkg_dev: postgresql{{ release }}-devel
conf_dir: /var/lib/pgsql/{{ repo.version }}/data conf_dir: /var/lib/pgsql/{{ repo.version }}/data
service: postgresql-{{ repo.version }} service: postgresql-{{ repo.version }}
@ -46,7 +50,11 @@ RedHat:
command: initdb --pgdata='{{ data_dir }}' command: initdb --pgdata='{{ data_dir }}'
test: test -f '{{ data_dir }}/PG_VERSION' test: test -f '{{ data_dir }}/PG_VERSION'
# Directory containing PostgreSQL client executables # Alternatives system
linux:
altpriority: 30
# directory containing PostgreSQL client executables
bin_dir: /usr/pgsql-{{ repo.version }}/bin bin_dir: /usr/pgsql-{{ repo.version }}/bin
client_bins: client_bins:
- clusterdb - clusterdb
@ -89,11 +97,87 @@ RedHat:
pkg_client: postgresql pkg_client: postgresql
{% endif %} {% endif %}
pkg_libpq_dev: libpqxx-devel
Suse: Suse:
pkg_repo:
name: pgdg-sles-{{ release }}
humanname: PostgreSQL {{ repo.version }} $releasever - $basearch
#Using sles-12 upstream repo for opensuse
baseurl: 'https://download.postgresql.org/pub/repos/zypp/{{ repo.version }}/suse/sles-12-$basearch'
key_url: 'https://download.postgresql.org/pub/repos/zypp/{{ repo.version }}/suse/sles-12-$basearch/repodata/repomd.xml.key'
gpgcheck: 1
gpgautoimport: True
{% if repo.use_upstream_repo == true %}
{% set lib_dir = '/var/lib/pgsql/' ~ repo.version ~ '/data' %}
pkg: postgresql{{ release }}-server
pkg_client: postgresql{{ release }}
pkg_dev: postgresql{{ release }}-devel
pkg_libs: postgresql{{ release }}-libs
conf_dir: {{ lib_dir }}
service: postgresql-{{ repo.version }}
prepare_cluster:
command: /usr/pgsql-{{ repo.version }}/bin/initdb --pgdata='{{ lib_dir }}'
test: test -f '{{ lib_dir }}/PG_VERSION'
# Alternatives system
linux:
altpriority: 30
# directory containing PostgreSQL client executables
bin_dir: /usr/pgsql-{{ repo.version }}/bin
client_bins:
- pg_archivecleanup
- pg_config
- pg_isready
- pg_receivexlog
- pg_rewind
- pg_test_fsync
- pg_test_timing
- pg_upgrade
- pg_xlogdump
- pgbench
server_bins:
- initdb
- pg_controldata
- pg_ctl
- pg_resetxlog
- postgres
- postgresql{{ release }}-check-db-dir
- postgresql{{ release }}-setup
- postmaster
{% else %}
pkg: postgresql-server pkg: postgresql-server
pkg_client: postgresql pkg_client: postgresql
pkg_libpq_dev: postgresql
{% endif %}
pkg_libpq_dev: libqpxx
{%- if grains.os == 'MacOS' %}
## jinja check avoids rendering noise/failure on Linux
MacOS:
{%- if repo.use_upstream_repo == 'homebrew' %}
service: homebrew.mxcl.postgresql
{%- elif repo.use_upstream_repo == 'postgresapp' %}
service: com.postgresapp.Postgres2
{%- endif %}
pkg: postgresql
pkg_client:
pkg_libpq_dev:
userhomes: /Users
user: {{ repo.user }}
group: {{ repo.group }}
conf_dir: /Users/{{ repo.user }}/Library/AppSupport/postgres_{{ repo.use_upstream_repo }}
prepare_cluster:
command: initdb -D /Users/{{ repo.user }}/Library/AppSupport/postgres_{{ repo.use_upstream_repo }}
test: test -f /Users/{{ repo.user }}/Library/AppSupport/postgres_{{ repo.use_upstream_repo }}/PG_VERSION
user: {{ repo.user }}
group: {{ repo.group }}
{%- endif %}
# vim: ft=sls # vim: ft=sls

View file

@ -2,4 +2,4 @@
postgresql-python: postgresql-python:
pkg.installed: pkg.installed:
- name: {{ postgres.python}} - name: {{ postgres.pkg_python}}

View file

@ -8,4 +8,12 @@ use_upstream_repo: {{ salt['pillar.get']('postgres:use_upstream_repo',
version: {{ salt['pillar.get']('postgres:version', version: {{ salt['pillar.get']('postgres:version',
defaults.postgres.version) }} defaults.postgres.version) }}
#Early lookup for system user on MacOS
{% if grains.os == 'MacOS' %}
{% set sysuser = salt['pillar.get']('postgres.user') or salt['cmd.run']("stat -f '%Su' /dev/console") %}
{% set sysgroup = salt['pillar.get']('postgres.group') or salt['cmd.run']("stat -f '%Sg' /dev/console") %}
user: {{ sysuser }}
group: {{ sysgroup }}
{% endif %}
# vim: ft=sls # vim: ft=sls

View file

@ -4,50 +4,55 @@
{%- if postgres.bake_image %} {%- if postgres.bake_image %}
{%- do includes.append('postgres.server.image') %} {%- do includes.append('postgres.server.image') %}
{%- endif %} {%- endif %}
{%- if postgres.use_upstream_repo -%} {%- if postgres.use_upstream_repo == true -%}
{%- do includes.append('postgres.upstream') %} {%- do includes.append('postgres.upstream') %}
{%- endif %} {%- endif %}
{%- if includes -%} {%- if includes -%}
include: include:
{{ includes|yaml(false)|indent(2) }} {{ includes|yaml(false)|indent(2) }}
{%- endif %} {%- endif %}
{%- set pkgs = [postgres.pkg] + postgres.pkgs_extra %} {%- set pkgs = [postgres.pkg] + postgres.pkgs_extra %}
# Install, configure and start PostgreSQL server # Install, configure and start PostgreSQL server
postgresql-server: postgresql-server:
pkg.installed: pkg.installed:
- pkgs: {{ pkgs }} - pkgs: {{ pkgs }}
{%- if postgres.use_upstream_repo %} {%- if postgres.use_upstream_repo == true %}
- refresh: True - refresh: True
- require: - require:
- pkgrepo: postgresql-repo - pkgrepo: postgresql-repo
{%- endif %} {%- 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
{%- else %}
{%- if 'bin_dir' in postgres %} # Alternatives system. Make server binaries available in $PATH
{%- if 'bin_dir' in postgres and postgres.linux.altpriority %}
# Make server binaries available in $PATH {%- for bin in postgres.server_bins %}
{%- set path = salt['file.join'](postgres.bin_dir, bin) %}
{%- for bin in postgres.server_bins %}
{%- set path = salt['file.join'](postgres.bin_dir, bin) %}
{{ bin }}: {{ bin }}:
alternatives.install: alternatives.install:
- link: {{ salt['file.join']('/usr/bin', bin) }} - link: {{ salt['file.join']('/usr/bin', bin) }}
- path: {{ path }} - path: {{ path }}
- priority: 30 - priority: {{ postgres.linux.altpriority }}
- onlyif: test -f {{ path }} - onlyif: test -f {{ path }}
- require: - require:
- pkg: postgresql-server - pkg: postgresql-server
- require_in: - require_in:
- cmd: postgresql-cluster-prepared - cmd: postgresql-cluster-prepared
{%- endfor %} {%- endfor %}
{%- endif %}
{%- endif %} {%- endif %}
@ -56,7 +61,7 @@ postgresql-cluster-prepared:
- name: {{ postgres.prepare_cluster.command }} - name: {{ postgres.prepare_cluster.command }}
- cwd: / - cwd: /
- runas: {{ postgres.prepare_cluster.user }} - runas: {{ postgres.prepare_cluster.user }}
- env: {{ postgres.prepare_cluster.env|default({}) }} - env: {{ postgres.prepare_cluster.env }}
- unless: - unless:
- {{ postgres.prepare_cluster.test }} - {{ postgres.prepare_cluster.test }}
- require: - require:
@ -67,6 +72,12 @@ postgresql-config-dir:
- name: {{ postgres.conf_dir }} - name: {{ postgres.conf_dir }}
- user: {{ postgres.user }} - user: {{ postgres.user }}
- group: {{ postgres.group }} - group: {{ postgres.group }}
- dir_mode: 775
- force: True
- file_mode: 644
- recurse:
- user
- group
- makedirs: True - makedirs: True
- require: - require:
- cmd: postgresql-cluster-prepared - cmd: postgresql-cluster-prepared
@ -118,6 +129,33 @@ postgresql-pg_hba:
- require: - require:
- file: postgresql-config-dir - file: postgresql-config-dir
{%- 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 }}
{%- 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
{%- for name, tblspace in postgres.tablespaces|dictsort() %} {%- for name, tblspace in postgres.tablespaces|dictsort() %}
postgresql-tablespace-dir-{{ name }}: postgresql-tablespace-dir-{{ name }}:
@ -138,13 +176,15 @@ postgresql-tablespace-dir-{{ name }}:
{%- if not postgres.bake_image %} {%- if not postgres.bake_image %}
# Start PostgreSQL server using OS init # Start PostgreSQL server using OS init
postgresql-running: postgresql-running:
service.running: service.running:
- name: {{ postgres.service }} - name: {{ postgres.service }}
- enable: True - enable: True
{% if grains.os not in ('MacOS',) %}
- reload: True - reload: True
{% endif %}
- watch: - watch:
- file: postgresql-pg_hba - file: postgresql-pg_hba
- file: postgresql-pg_ident
{%- endif %} {%- endif %}

View file

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxfiles</string>
<key>ProgramArguments</key>
<array>
<string>/bin/launchctl</string>
<string>limit</string>
<string>maxfiles</string>
<string>{{ soft_limit }}</string>
<string>{{ hard_limit }}</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>

View file

@ -0,0 +1,8 @@
#!/usr/bin/env bash
shortcutName='${1}'
app="postgres.app"
Source="/Applications/$app"
Destination="{{ homes }}/{{ user }}/Desktop/${shortcutName}"
/usr/bin/osascript -e "tell application \"Finder\" to make alias file to POSIX file \"$Source\" at POSIX file \"$Destination\""

View file

@ -0,0 +1,51 @@
######################################################################
# ATTENTION! Managed by SaltStack. #
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN! #
######################################################################
#
# PostgreSQL User Name Maps
# =========================
#
# Refer to the PostgreSQL documentation, chapter "Client
# Authentication" for a complete description. A short synopsis
# follows.
#
# This file controls PostgreSQL user name mapping. It maps external
# user names to their corresponding PostgreSQL user names. Records
# are of the form:
#
# MAPNAME SYSTEM-USERNAME PG-USERNAME
#
# (The uppercase quantities must be replaced by actual values.)
#
# MAPNAME is the (otherwise freely chosen) map name that was used in
# pg_hba.conf. SYSTEM-USERNAME is the detected user name of the
# client. PG-USERNAME is the requested PostgreSQL user name. The
# existence of a record specifies that SYSTEM-USERNAME may connect as
# PG-USERNAME.
#
# If SYSTEM-USERNAME starts with a slash (/), it will be treated as a
# regular expression. Optionally this can contain a capture (a
# parenthesized subexpression). The substring matching the capture
# will be substituted for \1 (backslash-one) if present in
# PG-USERNAME.
#
# Multiple maps may be specified in this file and used by pg_hba.conf.
#
# No map names are defined in the default configuration. If all
# system user names and PostgreSQL user names are the same, you don't
# need anything in this file.
#
# This file is read on server startup and when the postmaster receives
# a SIGHUP signal. If you edit the file on a running system, you have
# to SIGHUP the postmaster for the changes to take effect. You can
# use "pg_ctl reload" to do that.
# Put your actual configuration here
# ----------------------------------
# MAPNAME SYSTEM-USERNAME PG-USERNAME
{%- for mapping in mappings %}
{{ '{0:<15} {1:<22} {2}'.format(mapping) -}}
{% endfor %}

View file

@ -3,7 +3,7 @@
{%- if 'pkg_repo' in postgres -%} {%- if 'pkg_repo' in postgres -%}
{%- if postgres.use_upstream_repo -%} {%- if postgres.use_upstream_repo == true -%}
# Add upstream repository for your distro # Add upstream repository for your distro
postgresql-repo: postgresql-repo:
@ -25,9 +25,11 @@ postgresql-repo:
{%- else -%} {%- else -%}
# Notify that we don't manage this distro # Notify that we don't manage this distro
{% if grains.os not in ('Windows', 'MacOS',) %}
postgresql-repo: postgresql-repo:
test.show_notification: test.show_notification:
- text: | - text: |
PostgreSQL does not provide package repository for {{ grains['osfinger'] }} PostgreSQL does not provide package repository for {{ grains['osfinger'] }}
{% endif %}
{%- endif %} {%- endif %}