mirror of
https://github.com/saltstack-formulas/postgres-formula.git
synced 2025-04-16 09:40:26 +00:00
Merge pull request #127 from vutny/manage-postgres-with-client
Manage PostgreSQL entities with `client.sls` states
This commit is contained in:
commit
69154a74f6
5 changed files with 186 additions and 183 deletions
10
README.rst
10
README.rst
|
@ -16,18 +16,26 @@ Available states
|
||||||
``postgres``
|
``postgres``
|
||||||
------------
|
------------
|
||||||
|
|
||||||
Installs the PostgreSQL server package and prepares the DB cluster.
|
Installs and configures both PostgreSQL server and client with creation of
|
||||||
|
various DB objects in the cluster.
|
||||||
|
|
||||||
``postgres.client``
|
``postgres.client``
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
Installs the PostgreSQL client binaries and libraries.
|
Installs the PostgreSQL client binaries and libraries.
|
||||||
|
Allows to create such DB objects as: users, tablespaces, databases, schemas and
|
||||||
|
extensions. See ``pillar.example`` file for details.
|
||||||
|
|
||||||
``postgres.python``
|
``postgres.python``
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
Installs the PostgreSQL adapter for Python.
|
Installs the PostgreSQL adapter for Python.
|
||||||
|
|
||||||
|
``postgres.server``
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
Installs the PostgreSQL server package and prepares the DB cluster.
|
||||||
|
|
||||||
``postgres.upstream``
|
``postgres.upstream``
|
||||||
---------------------
|
---------------------
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,8 @@ postgres:
|
||||||
pkg: 'postgresql-9.3'
|
pkg: 'postgresql-9.3'
|
||||||
pkg_client: 'postgresql-client-9.3'
|
pkg_client: 'postgresql-client-9.3'
|
||||||
|
|
||||||
# Addtional packages to install, this should be in a list format
|
# Additional packages to install with PostgreSQL server,
|
||||||
|
# this should be in a list format
|
||||||
pkgs_extra:
|
pkgs_extra:
|
||||||
- postgresql-contrib
|
- postgresql-contrib
|
||||||
- postgresql-plpython
|
- postgresql-plpython
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
{%- from "postgres/map.jinja" import postgres with context -%}
|
{%- from "postgres/map.jinja" import postgres with context -%}
|
||||||
|
{%- from "postgres/macros.jinja" import format_state with context -%}
|
||||||
|
|
||||||
{%- set pkgs = [] %}
|
{%- set pkgs = [] %}
|
||||||
{%- for pkg in (postgres.pkg_client, postgres.pkg_libpq_dev) %}
|
{%- for pkg in (postgres.pkg_client, postgres.pkg_libpq_dev) %}
|
||||||
|
@ -7,13 +8,15 @@
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
{%- endfor -%}
|
{%- endfor -%}
|
||||||
|
|
||||||
{%- if postgres.use_upstream_repo %}
|
{%- if postgres.use_upstream_repo -%}
|
||||||
|
|
||||||
include:
|
include:
|
||||||
- postgres.upstream
|
- postgres.upstream
|
||||||
|
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
|
|
||||||
|
# Install PostgreSQL client and libraries
|
||||||
|
|
||||||
postgresql-client-libs:
|
postgresql-client-libs:
|
||||||
pkg.installed:
|
pkg.installed:
|
||||||
- pkgs: {{ pkgs }}
|
- pkgs: {{ pkgs }}
|
||||||
|
@ -43,3 +46,78 @@ postgresql-client-libs:
|
||||||
{%- endfor %}
|
{%- endfor %}
|
||||||
|
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
|
|
||||||
|
# Ensure that Salt is able to use postgres modules
|
||||||
|
# after installing client binaries
|
||||||
|
|
||||||
|
postgres-reload-modules:
|
||||||
|
test.nop:
|
||||||
|
- reload_modules: True
|
||||||
|
|
||||||
|
# User states
|
||||||
|
|
||||||
|
{%- for name, user in postgres.users|dictsort() %}
|
||||||
|
|
||||||
|
{{ format_state(name, 'postgres_user', user) }}
|
||||||
|
- require:
|
||||||
|
- pkg: postgresql-client-libs
|
||||||
|
|
||||||
|
{%- endfor %}
|
||||||
|
|
||||||
|
# Tablespace states
|
||||||
|
|
||||||
|
{%- for name, tblspace in postgres.tablespaces|dictsort() %}
|
||||||
|
|
||||||
|
{{ format_state(name, 'postgres_tablespace', tblspace) }}
|
||||||
|
- require:
|
||||||
|
- pkg: postgresql-client-libs
|
||||||
|
{%- if 'owner' in tblspace %}
|
||||||
|
- postgres_user: postgres_user-{{ tblspace.owner }}
|
||||||
|
{%- endif %}
|
||||||
|
|
||||||
|
{%- endfor %}
|
||||||
|
|
||||||
|
# Database states
|
||||||
|
|
||||||
|
{%- for name, db in postgres.databases|dictsort() %}
|
||||||
|
|
||||||
|
{{ format_state(name, 'postgres_database', db) }}
|
||||||
|
- require:
|
||||||
|
- pkg: postgresql-client-libs
|
||||||
|
{%- 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:
|
||||||
|
- pkg: postgresql-client-libs
|
||||||
|
{%- 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:
|
||||||
|
- pkg: postgresql-client-libs
|
||||||
|
{%- 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 %}
|
||||||
|
|
|
@ -1,181 +1,3 @@
|
||||||
# -*- 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:
|
include:
|
||||||
- postgres.upstream
|
- postgres.server
|
||||||
|
- postgres.client
|
||||||
{%- endif %}
|
|
||||||
|
|
||||||
### Installation states
|
|
||||||
|
|
||||||
postgresql-server:
|
|
||||||
pkg.installed:
|
|
||||||
- name: {{ postgres.pkg }}
|
|
||||||
{%- if postgres.use_upstream_repo %}
|
|
||||||
- refresh: True
|
|
||||||
- require:
|
|
||||||
- pkgrepo: 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-server
|
|
||||||
|
|
||||||
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 %}
|
|
||||||
|
|
94
postgres/server.sls
Normal file
94
postgres/server.sls
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
{%- from "postgres/map.jinja" import postgres with context -%}
|
||||||
|
|
||||||
|
{%- set pkgs = [postgres.pkg] + postgres.pkgs_extra -%}
|
||||||
|
|
||||||
|
{%- if postgres.use_upstream_repo -%}
|
||||||
|
|
||||||
|
include:
|
||||||
|
- postgres.upstream
|
||||||
|
|
||||||
|
{%- endif %}
|
||||||
|
|
||||||
|
# Install, configure and start PostgreSQL server
|
||||||
|
|
||||||
|
postgresql-server:
|
||||||
|
pkg.installed:
|
||||||
|
- pkgs: {{ pkgs }}
|
||||||
|
{%- if postgres.use_upstream_repo %}
|
||||||
|
- refresh: True
|
||||||
|
- require:
|
||||||
|
- pkgrepo: postgresql-repo
|
||||||
|
{%- endif %}
|
||||||
|
|
||||||
|
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-server
|
||||||
|
|
||||||
|
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 }}
|
||||||
|
- require:
|
||||||
|
- file: postgresql-config-dir
|
||||||
|
- watch_in:
|
||||||
|
- service: postgresql-running
|
||||||
|
|
||||||
|
{%- 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
|
||||||
|
|
||||||
|
{%- 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
|
||||||
|
|
||||||
|
{%- endfor %}
|
Loading…
Add table
Reference in a new issue