- re-arrange cluster initialization to a set of variables in a dictionary

which control three things:
    1. should we initialize?
    2. if so, how?
    3. what environment variables and user to use
  The approach taken is very similar to what the Apache formula uses, namely:
  a default dictionary which is over-ridden by:
     os-specific defaults,
     then os codename defaults,
     then os finger defaults,
     and finally user-specified pillar values
- this also adds support for grains['osfinger']
This commit is contained in:
Jon Nelson 2016-07-08 15:34:45 -05:00
parent 783d78bde7
commit 0220b67a85
7 changed files with 78 additions and 60 deletions

View file

@ -1,17 +1,19 @@
postgres: postgres:
pg_hba.conf: salt://postgres/pg_hba.conf pg_hba.conf: salt://postgres/pg_hba.conf
commands: prepare_cluster:
initdb: service postgresql initdb user: root
command: service postgresql initdb
test: test -f /path/to/some/file
env:
LC_ALL: C.UTF-8
use_upstream_repo: False use_upstream_repo: False
lookup: pkg: 'postgresql-9.3'
pkg: 'postgresql-9.3' pkg_client: 'postgresql-client-9.3'
pkg_client: 'postgresql-client-9.3' pkgs_extra:
pkgs_extra: - postgresql-contrib
- postgresql-contrib - postgresql-plpython
- postgresql-plpython
pg_hba: '/etc/postgresql/9.3/main/pg_hba.conf'
users: users:
localUser: localUser:

View file

@ -1,9 +1,13 @@
{%- macro ubuntu_block(name, version) %} {%- macro ubuntu_block(name, version) %}
{{ name }}: {{ name }}:
version: {{ version }}
pkg_repo: deb http://apt.postgresql.org/pub/repos/apt/ {{ name }}-pgdg main pkg_repo: deb http://apt.postgresql.org/pub/repos/apt/ {{ name }}-pgdg main
conf_dir: /etc/postgresql/{{ version }}/main conf_dir: /etc/postgresql/{{ version }}/main
data_dir: /var/lib/postgresql/{{ version }}/main prepare_cluster:
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:
LC_ALL: C.UTF-8
{%- endmacro %} {%- endmacro %}
{{ ubuntu_block('wheezy', 9.1) }} {{ ubuntu_block('wheezy', 9.1) }}

View file

@ -7,8 +7,6 @@ postgres:
python: python-psycopg2 python: python-psycopg2
service: postgresql service: postgresql
conf_dir: /var/lib/pgsql/data conf_dir: /var/lib/pgsql/data
data_dir: /var/lib/pgsql/data
version: 9.1
use_upstream_repo: False use_upstream_repo: False
users: {} users: {}
acls: [] acls: []
@ -17,10 +15,17 @@ postgres:
postgresconf_backup: True postgresconf_backup: True
postgresconf: "" postgresconf: ""
pg_hba.conf: salt://postgres/pg_hba.conf pg_hba.conf: salt://postgres/pg_hba.conf
initdb: True
initdb_user: root
initdb_args: --data-checksum
commands:
initdb: service postgresql initdb
user: postgres user: postgres
group: 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
test: test -f /var/lib/pgsql/data/PG_VERSION
env:
LC_ALL: C.UTF-8

View file

@ -16,9 +16,7 @@ postgresql-config-dir:
- makedirs: True - makedirs: True
- require: - require:
- pkg: postgresql-installed - pkg: postgresql-installed
{% if postgres.conf_dir == postgres.data_dir %}
- cmd: postgresql-cluster-prepared - cmd: postgresql-cluster-prepared
{% endif %}
postgresql-installed: postgresql-installed:
pkg.installed: pkg.installed:
@ -28,20 +26,17 @@ postgresql-installed:
# make sure the data directory and contents have been initialized # make sure the data directory and contents have been initialized
postgresql-cluster-prepared: postgresql-cluster-prepared:
cmd.run: cmd.run:
{% if postgres.initdb %}
- name: {{ postgres.commands.initdb }} {{ postgres.initdb_args }} -D {{ postgres.data_dir }}
{% elif grains.os_family == 'Debian' %}
- name: pg_createcluster {{ postgres.version }} main
{# else: TODO #}
{% endif %}
- cwd: / - cwd: /
- user: {{ postgres.initdb_user }} - name: {{ postgres.prepare_cluster.command }}
- user: {{ postgres.prepare_cluster.user }}
- unless: - unless:
- test -f {{ postgres.data_dir }}/PG_VERSION - {{ postgres.prepare_cluster.test }}
- require: - require:
- pkg: postgresql-installed - pkg: postgresql-installed
- env: - env:
LC_ALL: C.UTF-8 {% for name, value in postgres.prepare_cluster.env.items() %}
{{ name }}: {{ value }}
{% endfor %}
postgresql-running: postgresql-running:
service.running: service.running:
@ -154,8 +149,11 @@ postgresql-db-{{ name }}:
- user: {{ db.get('runas', postgres.user) }} - user: {{ db.get('runas', postgres.user) }}
- require: - require:
- service: postgresql-running - service: postgresql-running
{% if db.get('user') %} {% if db.get('tablespace') %}
- postgres_user: postgresql-user-{{ db.get('user') }} - postgres_tablespace: postgresql-tablespace-{{ name }}
{% endif %}
{% if db.get('owner') %}
- postgres_user: postgresql-user-{{ db.get('owner') }}
{% endif %} {% endif %}
{% if db.schemas is defined %} {% if db.schemas is defined %}

View file

@ -1,22 +1,22 @@
{% import_yaml "postgres/defaults.yaml" as defaults %} {% import_yaml "postgres/defaults.yaml" as defaults %}
{% import_yaml "postgres/osmap.yaml" as osmap %} {% import_yaml "postgres/osmap.yaml" as osmap %}
{% import_yaml "postgres/codenamemap.yaml" as codemap %} {% import_yaml "postgres/codenamemap.yaml" as oscodenamemap %}
{% import_yaml "postgres/osfingermap.yaml" as osfingermap %}
{# get the settings for the os_family grain #} {% set _postgres = salt['grains.filter_by'](
{% set osfam = salt['grains.filter_by'](osmap, grain='os_family') or {} %} osmap,
{# get the settings for the oscodename grain, os_family data will override grain='os_family',
oscodename data #} merge=salt['grains.filter_by'](
{% set oscode = salt['grains.filter_by'](codemap, oscodenamemap,
grain='oscodename', grain='oscodename',
merge=osfam) or {} %} merge=salt['grains.filter_by'](
osfingermap,
grain='osfinger',
merge=salt['pillar.get']('postgres', {})
)
)
) %}
{# merge the os family/codename specific data over the defaults #} {% do defaults.postgres.update(_postgres) %}
{% do defaults.postgres.update(oscode) %}
{# merge the pillar:lookup dict into the defaults/os specific dict #} {% set postgres = defaults.postgres %}
{% set lookup = salt['pillar.get']('postgres:lookup',
default=defaults.postgres,
merge=True) %}
{# merge the actual postgres pillar into the above combined dict #}
{% set postgres = salt['pillar.get']('postgres', default=lookup, merge=True) %}

View file

@ -0,0 +1 @@
{}

View file

@ -1,29 +1,37 @@
RedHat: RedHat:
initdb_user: postgres
commands:
initdb: initdb
pkg: postgresql-server pkg: postgresql-server
pkg_client: postgresql pkg_client: postgresql
pkg_repo: pgdg94 pkg_repo: pgdg94
repo_baseurl: http://yum.postgresql.org/9.4/redhat/rhel-$releasever-$basearch repo_baseurl: http://yum.postgresql.org/9.4/redhat/rhel-$releasever-$basearch
prepare_cluster:
test: test -f /var/lib/pgsql/data/PG_VERSION
env:
LC_ALL: C.UTF-8
{% if grains['os_family'] == 'RedHat' %}
{% if grains['osmajorrelease'] >= 7 %}
PGSETUP_INITDB_OPTIONS:
user: root
command: postgresql-setup initdb
{% else %}
user: postgres
command: initdb -D /var/lib/pgsql/data
{% endif %}
{% endif %}
Arch: Arch:
conf_dir: /var/lib/postgres/data conf_dir: /var/lib/postgres/data
data_dir: /var/lib/postgres/data prepare_cluster:
initdb_user: postgres user: postgres
initdb_args: --locale en_US.UTF8 -E UTF8 command: initdb -D /var/lib/postgresql/data
commands: test: test -f /var/lib/postgres/data/PG_VERSION
initdb: initdb env:
LC_ALL: C.UTF-8
pkg_client: postgresql pkg_client: postgresql
pkg_dev: postgresql pkg_dev: postgresql
Debian: Debian:
pkg_repo_file: /etc/apt/sources.list.d/pgdg.list pkg_repo_file: /etc/apt/sources.list.d/pgdg.list
pkg_dev: postgresql-server-dev-all pkg_dev: postgresql-server-dev-all
pkg_libpq_dev: libpq-dev pkg_libpq_dev: libpq-dev
initdb: False
Suse: Suse:
initdb_user: postgres
commands:
initdb: initdb
pkg: postgresql-server pkg: postgresql-server
pkg_client: postgresql pkg_client: postgresql
FreeBSD: FreeBSD: