This commit is contained in:
noelmcloughlin
2018-03-26 09:44:31 +01:00
25 changed files with 905 additions and 728 deletions

View File

@@ -1,21 +1,40 @@
{% from "postgres/map.jinja" import postgres with context %}
{%- from "postgres/map.jinja" import postgres with context -%}
{% if postgres.use_upstream_repo %}
{%- set pkgs = [] %}
{%- for pkg in (postgres.pkg_client, postgres.pkg_libpq_dev) %}
{%- if pkg %}
{%- do pkgs.append(pkg) %}
{%- endif %}
{%- endfor %}
{%- if postgres.use_upstream_repo == true %}
include:
- postgres.upstream
{% endif %}
install-postgresql-client:
pkg.installed:
- name: {{ postgres.pkg_client }}
- refresh: {{ postgres.use_upstream_repo }}
{% if postgres.use_upstream_repo %}
- require:
- pkgrepo: install-postgresql-repo
{%- endif %}
{% if postgres.pkg_libpq_dev %}
install-postgres-libpq-dev:
# Install PostgreSQL client and libraries
postgresql-client-libs:
pkg.installed:
- name: {{ postgres.pkg_libpq_dev }}
{% endif %}
- pkgs: {{ pkgs }}
{%- if postgres.use_upstream_repo == true %}
- refresh: True
- require:
- pkgrepo: postgresql-repo
{%- endif %}
# Alternatives system. Make client binaries available in $PATH
{%- if 'bin_dir' in postgres and postgres.linux.altpriority %}
{%- for bin in postgres.client_bins %}
{%- set path = salt['file.join'](postgres.bin_dir, bin) %}
{{ bin }}:
alternatives.install:
- link: {{ salt['file.join']('/usr/bin', bin) }}
- path: {{ path }}
- priority: {{ postgres.linux.altpriority }}
- onlyif: test -f {{ path }}
- require:
- pkg: postgresql-client-libs
{%- endfor %}
{%- endif %}

View File

@@ -16,7 +16,7 @@
#}
{# use upstream version if configured #}
{% if repo.use_upstream_repo %}
{% if repo.use_upstream_repo == true %}
{% set version = repo.version %}
{% endif %}
@@ -31,18 +31,45 @@
command: pg_createcluster {{ version }} main
test: test -f /var/lib/postgresql/{{ version }}/main/PG_VERSION && test -f /etc/postgresql/{{ version }}/main/postgresql.conf
user: root
env: {}
{% endmacro %}
{% macro fedora_codename(name, version, codename=none) %}
{#
Generate lookup dictionary map for Fedora distributions
name:
distro codename
version:
PostgreSQL release version
codename:
optional grain value if `name` does not match the one returned by
`oscodename` grain
#}
{# use upstream version if configured #}
{% if repo.use_upstream_repo == true %}
{% set version = repo.version %}
{% endif %}
{{ codename|default(name, true) }}:
# PostgreSQL packages are mostly downloaded from `main` repo component
pkg_repo:
baseurl: 'https://download.postgresql.org/pub/repos/yum/{{ version }}/fedora/fedora-$releasever-$basearch'
{% endmacro %}
## Debian GNU/Linux
{{ debian_codename('wheezy', '9.1') }}
{{ debian_codename('jessie', '9.4') }}
{{ debian_codename('stretch', '9.6') }}
# `oscodename` grain has long distro name
# if `lsb-release` package not installed
{{ debian_codename('wheezy', '9.1', 'Debian GNU/Linux 7 (wheezy)') }}
{{ debian_codename('jessie', '9.4', 'Debian GNU/Linux 8 (jessie)') }}
{{ debian_codename('stretch', '9.6', 'Debian GNU/Linux 9 (stretch)') }}
## Ubuntu
{{ debian_codename('trusty', '9.3') }}
@@ -52,4 +79,10 @@
{{ debian_codename('wily', '9.4') }}
{{ debian_codename('xenial', '9.5') }}
## Fedora
# `oscodename` grain has long distro name
{{ fedora_codename('Fedora-25', '9.5', 'Fedora 25 (Twenty Five)') }}
{{ fedora_codename('Fedora-24', '9.5', 'Fedora 24 (Twenty Four)') }}
{{ fedora_codename('Fedora-23', '9.4', 'Fedora 23 (Twenty Three)') }}
# vim: ft=sls

View File

@@ -7,11 +7,37 @@ postgres:
pkgs_extra: []
pkg_client: postgresql-client
pkg_dev: postgresql-devel
pkg_libpq_dev: postgresql-libs
python: python-psycopg2
pkg_libpq_dev: libpq-dev
pkg_libs: postgresql-libs
pkg_python: python-psycopg2
userhomes: /home
user: postgres
group: postgres
prepare_cluster:
command: initdb --pgdata=/var/lib/pgsql/data
test: test -f /var/lib/pgsql/data/PG_VERSION
user: postgres
env: []
conf_dir: /var/lib/pgsql/data
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
acls:
# "local" is for Unix domain socket connections only
@@ -20,22 +46,22 @@ postgres:
- ['host', 'all', 'all', '127.0.0.1/32', 'md5']
# IPv6 local connections:
- ['host', 'all', 'all', '::1/128', 'md5']
pg_ident.conf: salt://postgres/templates/pg_ident.conf.j2
identity_map: []
config_backup: '.bak'
service: postgresql
bake_image: False
users: {}
tablespaces: {}
databases: {}
schemas: {}
extensions: {}
postgresconf_backup: True
postgresconf: ""
# if prepare_cluster is over-ridden in any of:
# - osmap.yaml
# - oscodenamemap.yaml
# - osfingermap.yaml
# you will have to specify a complete dictionary.
prepare_cluster:
user: root
command: service postgresql initdb
test: test -f /var/lib/pgsql/data/PG_VERSION
env: {}
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 %}
{% if postgres.pkg_dev %}
{% if grains.os not in ('Windows', 'MacOS',) %}
{% if postgres.pkg_dev %}
install-postgres-dev-package:
pkg.installed:
- name: {{ postgres.pkg_dev }}
{% endif %}
{% endif %}
{% if postgres.pkg_libpq_dev %}
{% if postgres.pkg_libpq_dev %}
install-postgres-libpq-dev:
pkg.installed:
- 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 %}

View File

@@ -1,181 +1,9 @@
# -*- mode: yaml -*-
{%- from "postgres/map.jinja" import postgres with context -%}
{%- from "postgres/macros.jinja" import format_state with context -%}
{%- if postgres.use_upstream_repo %}
include:
- postgres.upstream
{%- endif %}
### Installation states
postgresql-installed:
pkg.installed:
- name: {{ postgres.pkg }}
- refresh: {{ postgres.use_upstream_repo }}
{% if postgres.use_upstream_repo %}
- require:
- pkgrepo: install-postgresql-repo
{%- endif %}
# make sure the data directory and contents have been initialized
postgresql-cluster-prepared:
cmd.run:
- name: {{ postgres.prepare_cluster.command }}
- cwd: /
- runas: {{ postgres.prepare_cluster.user }}
- env: {{ postgres.prepare_cluster.env|default({}) }}
- unless:
- {{ postgres.prepare_cluster.test }}
- require:
- pkg: postgresql-installed
postgresql-config-dir:
file.directory:
- name: {{ postgres.conf_dir }}
- user: {{ postgres.user }}
- group: {{ postgres.group }}
- 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
- backup: {{ postgres.postgresconf_backup }}
- watch_in:
- service: postgresql-running
- require:
- file: postgresql-config-dir
{%- endif %}
postgresql-pg_hba:
file.managed:
- name: {{ postgres.conf_dir }}/pg_hba.conf
- source: {{ postgres['pg_hba.conf'] }}
- template: jinja
- user: {{ postgres.user }}
- group: {{ postgres.group }}
- mode: 600
- require:
- file: postgresql-config-dir
postgresql-running:
service.running:
- name: {{ postgres.service }}
- enable: True
- reload: True
- watch:
- file: postgresql-pg_hba
postgresql-extra-pkgs-installed:
pkg.installed:
- pkgs: {{ postgres.pkgs_extra }}
### User states
{%- for name, user in postgres.users|dictsort() %}
{{ format_state(name, 'postgres_user', user) }}
- require:
- service: postgresql-running
{%- if 'db_user' in user %}
- postgres_user: postgres_user-{{ user.db_user }}
{%- endif %}
{%- endfor %}
### Tablespace states
{%- for name, tblspace in postgres.tablespaces|dictsort() %}
postgres_tablespace-dir-{{ tblspace.directory}}:
file.directory:
- name: {{ tblspace.directory }}
- user: {{ postgres.user }}
- group: {{ postgres.group }}
- mode: 700
- makedirs: True
- recurse:
- user
- group
{{ format_state(name, 'postgres_tablespace', tblspace) }}
- require:
- file: postgres_tablespace-dir-{{ tblspace.directory }}
{%- if 'owner' in tblspace %}
- postgres_user: postgres_user-{{ tblspace.owner }}
{%- endif %}
- service: postgresql-running
{%- endfor %}
### Database states
{%- for name, db in postgres.databases|dictsort() %}
{{ format_state(name, 'postgres_database', db) }}
- require:
- service: postgresql-running
{%- if 'db_user' in db %}
- postgres_user: postgres_user-{{ db.db_user }}
{%- 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:
- service: postgresql-running
{%- if 'db_user' in schema %}
- postgres_user: postgres_user-{{ schema.db_user }}
{%- endif %}
{%- if 'dbname' in schema %}
- postgres_database: postgres_database-{{ schema.dbname }}
{%- endif %}
{%- 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) }}
- require:
- service: postgresql-running
- pkg: postgresql-extra-pkgs-installed
{%- if 'db_user' in extension %}
- postgres_user: postgres_user-{{ extension.db_user }}
{%- 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 %}
{% if grains.os == 'MacOS' %}
- postgres.macos
{% else %}
- postgres.server
- postgres.client
- 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 }}.{{ ensure|default('present') }}:
{{- format_kwargs(kwarg) }}
- onchanges:
- test: postgres-reload-modules
{%- endmacro %}

88
postgres/manage.sls Normal file
View File

@@ -0,0 +1,88 @@
{%- from "postgres/map.jinja" import postgres with context -%}
{%- from "postgres/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() %}
{{ 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) }}
{%- if 'owner' in schema %}
- require:
- 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 %}

View File

@@ -1,7 +1,6 @@
{% import_yaml "postgres/defaults.yaml" as defaults %}
{% import_yaml "postgres/osmap.yaml" as osmap %}
{% import_yaml "postgres/codenamemap.yaml" as oscodenamemap %}
{% import_yaml "postgres/osmajorreleasemap.yaml" as osmajorreleasemap %}
{% set postgres = salt['grains.filter_by'](
defaults,
@@ -11,11 +10,7 @@
merge=salt['grains.filter_by'](
oscodenamemap,
grain='oscodename',
merge=salt['grains.filter_by'](
osmajorreleasemap,
grain='osmajorrelease',
merge=salt['pillar.get']('postgres', {}),
),
merge=salt['pillar.get']('postgres', {}),
),
),
base='postgres',

View File

@@ -1,67 +0,0 @@
{% import_yaml "postgres/repo.yaml" as repo %}
{% if grains['os_family'] == 'RedHat' %}
### RedHat releases
{% if repo.use_upstream_repo %}
{% set data_dir = '/var/lib/pgsql/' ~ repo.version ~ '/data' %}
# PostgreSQL from upstream repository
default:
prepare_cluster:
user: postgres
command: /usr/pgsql-{{ repo.version }}/bin/initdb -D {{ data_dir }}
test: test -f {{ data_dir }}/PG_VERSION
env: {}
'6':
prepare_cluster:
user: root
command: service postgresql-{{ repo.version }} initdb
test: test -f {{ data_dir }}/PG_VERSION
env: {}
'7':
prepare_cluster:
user: root
command: /usr/pgsql-{{ repo.version }}/bin/postgresql95-setup initdb
test: test -f {{ data_dir }}/PG_VERSION
env: {}
{% else %}
{% set data_dir = '/var/lib/pgsql/data' %}
# PostgreSQL from OS repositories
default:
prepare_cluster:
user: postgres
command: initdb -D {{ data_dir }}
test: test -f {{ data_dir }}/PG_VERSION
env: {}
'6':
prepare_cluster:
user: root
command: service postgresql initdb
test: test -f {{ data_dir }}/PG_VERSION
env: {}
'7':
prepare_cluster:
user: root
command: postgresql-setup initdb
test: test -f {{ data_dir }}/PG_VERSION
env: {}
{% endif %}
{% else %}
### Empty YAML, no settings for other OS
{}
{% endif %}
# vim: ft=sls

View File

@@ -5,11 +5,9 @@
Arch:
conf_dir: /var/lib/postgres/data
prepare_cluster:
user: postgres
command: initdb -D /var/lib/postgresql/data
command: initdb -D /var/lib/postgres/data
test: test -f /var/lib/postgres/data/PG_VERSION
env: {}
pkg_client: postgresql
pkg_client: postgresql-libs
pkg_dev: postgresql
Debian:
@@ -19,7 +17,6 @@ Debian:
file: /etc/apt/sources.list.d/pgdg.list
pkg_repo_keyid: ACCC4CF8
pkg_dev: postgresql-server-dev-all
pkg_libpq_dev: libpq-dev
FreeBSD:
user: pgsql
@@ -44,18 +41,143 @@ RedHat:
pkg: postgresql{{ release }}-server
pkg_client: postgresql{{ release }}
pkg_libs: postgresql{{ release }}-libs
pkg_dev: postgresql{{ release }}-devel
conf_dir: /var/lib/pgsql/{{ repo.version }}/data
service: postgresql-{{ repo.version }}
prepare_cluster:
command: initdb --pgdata='{{ data_dir }}'
test: test -f '{{ data_dir }}/PG_VERSION'
# Alternatives system
linux:
altpriority: 30
# directory containing PostgreSQL client executables
bin_dir: /usr/pgsql-{{ repo.version }}/bin
client_bins:
- clusterdb
- createdb
- createlang
- createuser
- dropdb
- droplang
- dropuser
- pg_archivecleanup
- pg_basebackup
- pg_config
- pg_dump
- pg_dumpall
- pg_isready
- pg_receivexlog
- pg_restore
- pg_rewind
- pg_test_fsync
- pg_test_timing
- pg_upgrade
- pg_xlogdump
- pgbench
- psql
- reindexdb
- vacuumdb
server_bins:
- initdb
- pg_controldata
- pg_ctl
- pg_resetxlog
- postgres
- postgresql{{ release }}-check-db-dir
- postgresql{{ release }}-setup
- postmaster
{% else %}
pkg: postgresql-server
pkg_client: postgresql
{% endif %}
pkg_libpq_dev: libpqxx-devel
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_client: 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

View File

@@ -2,4 +2,4 @@
postgresql-python:
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',
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

45
postgres/server/image.sls Normal file
View File

@@ -0,0 +1,45 @@
{%- from "postgres/map.jinja" import postgres with context -%}
# This state is used to launch PostgreSQL with ``pg_ctl`` command and enable it
# on "boot" during an image (Docker, Virtual Appliance, AMI) preparation
{%- if postgres.bake_image %}
include:
- postgres.server
# An attempt to start PostgreSQL with `pg_ctl`
postgresql-start:
cmd.run:
- name: pg_ctl -D {{ postgres.conf_dir }} -l logfile start
- runas: {{ postgres.user }}
- unless:
- ps -p $(head -n 1 {{ postgres.conf_dir }}/postmaster.pid) 2>/dev/null
- require:
- file: postgresql-pg_hba
# Try to enable PostgreSQL in "manual" way
postgresql-enable:
cmd.run:
{%- if salt['file.file_exists']('/bin/systemctl') %}
- name: systemctl enable {{ postgres.service }}
{%- elif salt['cmd.which']('chkconfig') %}
- name: chkconfig {{ postgres.service }} on
{%- elif salt['file.file_exists']('/usr/sbin/update-rc.d') %}
- name: update-rc.d {{ service }} defaults
{%- else %}
# Nothing to do
- name: 'true'
{%- endif %}
- require:
- cmd: postgresql-start
{%- else %}
postgresql-start:
test.show_notification:
- text: The 'postgres:bake_image' Pillar is disabled (set to 'False').
{%- endif %}

190
postgres/server/init.sls Normal file
View File

@@ -0,0 +1,190 @@
{%- from "postgres/map.jinja" import postgres with context %}
{%- set includes = [] %}
{%- if postgres.bake_image %}
{%- do includes.append('postgres.server.image') %}
{%- endif %}
{%- if postgres.use_upstream_repo == true -%}
{%- do includes.append('postgres.upstream') %}
{%- endif %}
{%- if includes -%}
include:
{{ includes|yaml(false)|indent(2) }}
{%- endif %}
{%- set pkgs = [postgres.pkg] + postgres.pkgs_extra %}
# Install, configure and start PostgreSQL server
postgresql-server:
pkg.installed:
- pkgs: {{ pkgs }}
{%- if postgres.use_upstream_repo == true %}
- refresh: True
- require:
- pkgrepo: postgresql-repo
{%- 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 %}
# Alternatives system. Make server binaries available in $PATH
{%- if 'bin_dir' in postgres and postgres.linux.altpriority %}
{%- for bin in postgres.server_bins %}
{%- set path = salt['file.join'](postgres.bin_dir, bin) %}
{{ bin }}:
alternatives.install:
- link: {{ salt['file.join']('/usr/bin', bin) }}
- path: {{ path }}
- priority: {{ postgres.linux.altpriority }}
- onlyif: test -f {{ path }}
- require:
- pkg: postgresql-server
- require_in:
- cmd: postgresql-cluster-prepared
{%- endfor %}
{%- endif %}
{%- endif %}
postgresql-cluster-prepared:
cmd.run:
- name: {{ postgres.prepare_cluster.command }}
- cwd: /
- runas: {{ postgres.prepare_cluster.user }}
- env: {{ postgres.prepare_cluster.env }}
- unless:
- {{ postgres.prepare_cluster.test }}
- require:
- pkg: postgresql-server
postgresql-config-dir:
file.directory:
- name: {{ postgres.conf_dir }}
- user: {{ postgres.user }}
- group: {{ postgres.group }}
- dir_mode: 775
- force: True
- file_mode: 644
- recurse:
- user
- group
- 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
{#- Detect empty values (none, '') in the config_backup #}
- backup: {{ postgres.config_backup|default(false, true) }}
- require:
- file: postgresql-config-dir
- watch_in:
- service: postgresql-running
{%- endif %}
{%- set pg_hba_path = salt['file.join'](postgres.conf_dir, 'pg_hba.conf') %}
postgresql-pg_hba:
file.managed:
- name: {{ pg_hba_path }}
- user: {{ postgres.user }}
- group: {{ postgres.group }}
- mode: 600
{%- if postgres.acls %}
- source: {{ postgres['pg_hba.conf'] }}
- template: jinja
- defaults:
acls: {{ postgres.acls }}
{%- 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 %}
{%- else %}
- replace: False
{%- endif %}
- require:
- 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() %}
postgresql-tablespace-dir-{{ name }}:
file.directory:
- name: {{ tblspace.directory }}
- user: {{ postgres.user }}
- group: {{ postgres.group }}
- mode: 700
- makedirs: True
- recurse:
- user
- group
- require:
- pkg: postgresql-server
{%- endfor %}
{%- if not postgres.bake_image %}
# Start PostgreSQL server using OS init
postgresql-running:
service.running:
- name: {{ postgres.service }}
- enable: True
{% if grains.os not in ('MacOS',) %}
- reload: True
{% endif %}
- watch:
- file: postgresql-pg_hba
- file: postgresql-pg_ident
{%- 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

@@ -1,5 +1,3 @@
{%- from "postgres/map.jinja" import postgres with context -%}
######################################################################
# ATTENTION! Managed by SaltStack. #
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN! #
@@ -22,7 +20,7 @@ local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
{%- for acl in postgres.acls %}
{%- for acl in acls %}
{%- if acl|first() == 'local' %}
{%- if acl|length() == 3 %}
@@ -38,5 +36,5 @@ local all postgres peer
{%- endif %}
{%- endif %}
{{ '{0:<8}{1:<16}{2:<16}{3:<24}{4}'.format(*acl) -}}
{{ '{0:<7} {1:<15} {2:<15} {3:<23} {4}'.format(*acl) -}}
{% endfor %}

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,17 +3,17 @@
{%- if 'pkg_repo' in postgres -%}
{%- if postgres.use_upstream_repo -%}
{%- if postgres.use_upstream_repo == true -%}
# Add upstream repository for your distro
install-postgresql-repo:
postgresql-repo:
pkgrepo.managed:
{{- format_kwargs(postgres.pkg_repo) }}
{%- else -%}
# Remove the repo configuration (and GnuPG key) as requested
remove-postgresql-repo:
postgresql-repo:
pkgrepo.absent:
- name: {{ postgres.pkg_repo.name }}
{%- if 'pkg_repo_keyid' in postgres %}
@@ -25,9 +25,11 @@ remove-postgresql-repo:
{%- else -%}
# Notify that we don't manage this distro
install-postgresql-repo:
{% if grains.os not in ('Windows', 'MacOS',) %}
postgresql-repo:
test.show_notification:
- text: |
PostgreSQL does not provide package repository for {{ grains['osfinger'] }}
{% endif %}
{%- endif %}