Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Available states
``postgres``
------------

Installs and configures both PostgreSQL server and client with creation of
various DB objects in the cluster.
Installs and configures both PostgreSQL server and client with creation of various DB objects in
the cluster.

``postgres.client``
-------------------
Expand All @@ -27,8 +27,8 @@ Installs the PostgreSQL client binaries and libraries.
``postgres.manage``
-------------------

Creates such DB objects as: users, tablespaces, databases, schemas and
extensions. See ``pillar.example`` file for details.
Creates such DB objects as: users, tablespaces, databases, schemas and extensions.
See ``pillar.example`` file for details.

``postgres.python``
-------------------
Expand All @@ -38,22 +38,48 @@ Installs the PostgreSQL adapter for Python.
``postgres.server``
-------------------

Installs the PostgreSQL server package and prepares the DB cluster.
Installs the PostgreSQL server package, prepares the DB cluster and starts the server using
packaged init script, job or unit.

``postgres.server.image``
-------------------------

Installs the PostgreSQL server package, prepares the DB cluster and starts the server by issuing
raw ``pg_ctl`` command. The ``postgres:bake_image`` Pillar toggles this behaviour. For example:

.. code:: yaml
postgres:
bake_image: True
If set ``True``, then it becomes possible to fully provision PostgreSQL with all supported entities
from ``postgres.manage`` state during the build ("baking") of AMI / VM / Container images (using
Packer, Docker or similar tools), i.e. when OS ``init`` process is not available to start the
service and enable it on "boot" of resulting appliance.

Also it allows to make Docker images with PostgreSQL using functionality being available since Salt
2016.11.0 release:

.. code:: console
salt 'minion.with.docker' dockerng.sls_build my-postgres base=centos/systemd mods=postgres
If a lookup dictionary or Pillar has ``postgres:bake_image`` set ``False`` (this is default), it is
equivalent of applying ``postgres.server`` state.

``postgres.upstream``
---------------------

Configures the PostgreSQL Official (upstream) repository on target system if
applicable.

The state relies on the ``postgres:use_upstream_repo`` Pillar value which could
be set as following:
The state relies on the ``postgres:use_upstream_repo`` Pillar value which could be set as following:

* ``True`` (default): adds the upstream repository to install packages from
* ``False``: makes sure that the repository configuration is absent

The ``postgres:version`` Pillar controls which version of the PostgreSQL
packages should be installed from the upstream repository. Defaults to ``9.5``.
The ``postgres:version`` Pillar controls which version of the PostgreSQL packages should be
installed from the upstream repository. Defaults to ``9.5``.

Testing
=======
Expand All @@ -79,3 +105,5 @@ Builds and runs tests from scratch.
-----------------

Gives you ssh to the vagrant machine for manual testing.

.. vim: fenc=utf-8 spell spl=en cc=100 tw=99 fo=want sts=2 sw=2 et
17 changes: 14 additions & 3 deletions pillar.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ postgres:
postgresconf: |
listen_addresses = '*' # listen on all interfaces
# Backup extension for postgresql.conf file, defaults to `.bak`.
# Backup extension for postgresql.conf file, defaults to ``.bak``.
# Set to False to stop creation of backup on postgresql.conf changes.
{%- if 'status.time' in salt.keys() %}
postgresconf_backup: ".backup@{{ salt['status.time']('%y-%m-%d_%H:%M:%S') }}"
Expand All @@ -28,7 +28,7 @@ postgres:
# Path to the `pg_hba.conf` file Jinja template on Salt Fileserver
pg_hba.conf: salt://postgres/templates/pg_hba.conf.j2

# This section covers ACL management in the `pg_hba.conf` file.
# This section covers ACL management in the ``pg_hba.conf`` file.
# acls list controls: which hosts are allowed to connect, how clients
# are authenticated, which PostgreSQL user names they can use, which
# databases they can access. Records take one of these forms:
Expand All @@ -48,9 +48,20 @@ postgres:
# PostgreSQL service name
service: postgresql

{%- if grains['init'] == 'unknown' %}

# If Salt is unable to detect init system running in the scope of state run,
# probably we are trying to bake a container/VM image with PostgreSQL.
# Use ``bake_image`` setting to contol how PostgreSQL will be started: if set
# to ``True`` the raw ``pg_ctl`` will be utilized instead of packaged init
# script, job or unit run with Salt ``service`` state.
bake_image: True

{%- endif %}

# Create/remove users, tablespaces, databases, schema and extensions.
# Each of these dictionaries contains PostgreSQL entities which
# mapped to the `postgres_*` Salt states with arguments. See the Salt
# mapped to the ``postgres_*`` Salt states with arguments. See the Salt
# documentaion to get all supported argument for a particular state.
#
# Format is the following:
Expand Down
1 change: 0 additions & 1 deletion postgres/codenamemap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
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 %}

Expand Down
11 changes: 4 additions & 7 deletions postgres/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@ postgres:
user: postgres
group: postgres

# 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
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
Expand All @@ -38,6 +33,8 @@ postgres:

service: postgresql

bake_image: False

users: {}
tablespaces: {}
databases: {}
Expand Down
7 changes: 1 addition & 6 deletions postgres/map.jinja
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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',
Expand Down
67 changes: 0 additions & 67 deletions postgres/osmajorreleasemap.yaml

This file was deleted.

8 changes: 6 additions & 2 deletions postgres/osmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
Arch:
conf_dir: /var/lib/postgres/data
prepare_cluster:
user: postgres
command: initdb -D /var/lib/postgresql/data
test: test -f /var/lib/postgres/data/PG_VERSION
env: {}
pkg_client: postgresql
pkg_dev: postgresql

Expand Down Expand Up @@ -37,11 +35,17 @@ RedHat:

{% if repo.use_upstream_repo %}

{% set data_dir = '/var/lib/pgsql/' ~ repo.version ~ '/data' %}

pkg: postgresql{{ release }}-server
pkg_client: postgresql{{ release }}
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'

# Directory containing PostgreSQL client executables
bin_dir: /usr/pgsql-{{ repo.version }}/bin
client_bins:
Expand Down
45 changes: 45 additions & 0 deletions postgres/server/image.sls
Original file line number Diff line number Diff line change
@@ -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 %}
Loading