From 0220b67a856bcb04ee3e4a3d5fc39cd66c5ce47b Mon Sep 17 00:00:00 2001 From: Jon Nelson Date: Fri, 8 Jul 2016 15:34:45 -0500 Subject: [PATCH] - 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'] --- pillar.example | 20 +++++++++++--------- postgres/codenamemap.yaml | 8 ++++++-- postgres/defaults.yaml | 19 ++++++++++++------- postgres/init.sls | 24 +++++++++++------------- postgres/map.jinja | 34 +++++++++++++++++----------------- postgres/osfingermap.yaml | 1 + postgres/osmap.yaml | 32 ++++++++++++++++++++------------ 7 files changed, 78 insertions(+), 60 deletions(-) create mode 100644 postgres/osfingermap.yaml diff --git a/pillar.example b/pillar.example index 2b64166..fa459b7 100644 --- a/pillar.example +++ b/pillar.example @@ -1,17 +1,19 @@ postgres: pg_hba.conf: salt://postgres/pg_hba.conf - commands: - initdb: service postgresql initdb + prepare_cluster: + user: root + command: service postgresql initdb + test: test -f /path/to/some/file + env: + LC_ALL: C.UTF-8 use_upstream_repo: False - lookup: - pkg: 'postgresql-9.3' - pkg_client: 'postgresql-client-9.3' - pkgs_extra: - - postgresql-contrib - - postgresql-plpython - pg_hba: '/etc/postgresql/9.3/main/pg_hba.conf' + pkg: 'postgresql-9.3' + pkg_client: 'postgresql-client-9.3' + pkgs_extra: + - postgresql-contrib + - postgresql-plpython users: localUser: diff --git a/postgres/codenamemap.yaml b/postgres/codenamemap.yaml index 10052ba..9fa89be 100644 --- a/postgres/codenamemap.yaml +++ b/postgres/codenamemap.yaml @@ -1,9 +1,13 @@ {%- macro ubuntu_block(name, version) %} {{ name }}: - version: {{ version }} pkg_repo: deb http://apt.postgresql.org/pub/repos/apt/ {{ name }}-pgdg 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 %} {{ ubuntu_block('wheezy', 9.1) }} diff --git a/postgres/defaults.yaml b/postgres/defaults.yaml index 7dd6013..9c85efa 100644 --- a/postgres/defaults.yaml +++ b/postgres/defaults.yaml @@ -7,8 +7,6 @@ postgres: python: python-psycopg2 service: postgresql conf_dir: /var/lib/pgsql/data - data_dir: /var/lib/pgsql/data - version: 9.1 use_upstream_repo: False users: {} acls: [] @@ -17,10 +15,17 @@ postgres: postgresconf_backup: True postgresconf: "" pg_hba.conf: salt://postgres/pg_hba.conf - initdb: True - initdb_user: root - initdb_args: --data-checksum - commands: - initdb: service postgresql initdb 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 + test: test -f /var/lib/pgsql/data/PG_VERSION + env: + LC_ALL: C.UTF-8 + diff --git a/postgres/init.sls b/postgres/init.sls index e6f8de5..4f97783 100644 --- a/postgres/init.sls +++ b/postgres/init.sls @@ -16,9 +16,7 @@ postgresql-config-dir: - makedirs: True - require: - pkg: postgresql-installed -{% if postgres.conf_dir == postgres.data_dir %} - cmd: postgresql-cluster-prepared -{% endif %} postgresql-installed: pkg.installed: @@ -28,20 +26,17 @@ postgresql-installed: # make sure the data directory and contents have been initialized postgresql-cluster-prepared: 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: / - - user: {{ postgres.initdb_user }} + - name: {{ postgres.prepare_cluster.command }} + - user: {{ postgres.prepare_cluster.user }} - unless: - - test -f {{ postgres.data_dir }}/PG_VERSION + - {{ postgres.prepare_cluster.test }} - require: - pkg: postgresql-installed - env: - LC_ALL: C.UTF-8 +{% for name, value in postgres.prepare_cluster.env.items() %} + {{ name }}: {{ value }} +{% endfor %} postgresql-running: service.running: @@ -154,8 +149,11 @@ postgresql-db-{{ name }}: - user: {{ db.get('runas', postgres.user) }} - require: - service: postgresql-running - {% if db.get('user') %} - - postgres_user: postgresql-user-{{ db.get('user') }} + {% if db.get('tablespace') %} + - postgres_tablespace: postgresql-tablespace-{{ name }} + {% endif %} + {% if db.get('owner') %} + - postgres_user: postgresql-user-{{ db.get('owner') }} {% endif %} {% if db.schemas is defined %} diff --git a/postgres/map.jinja b/postgres/map.jinja index c6032f0..c2889f4 100644 --- a/postgres/map.jinja +++ b/postgres/map.jinja @@ -1,22 +1,22 @@ {% import_yaml "postgres/defaults.yaml" as defaults %} {% 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 osfam = salt['grains.filter_by'](osmap, grain='os_family') or {} %} -{# get the settings for the oscodename grain, os_family data will override - oscodename data #} -{% set oscode = salt['grains.filter_by'](codemap, - grain='oscodename', - merge=osfam) or {} %} +{% set _postgres = salt['grains.filter_by']( + osmap, + grain='os_family', + merge=salt['grains.filter_by']( + oscodenamemap, + grain='oscodename', + 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(oscode) %} +{% do defaults.postgres.update(_postgres) %} -{# merge the pillar:lookup dict into the defaults/os specific dict #} -{% 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) %} +{% set postgres = defaults.postgres %} diff --git a/postgres/osfingermap.yaml b/postgres/osfingermap.yaml new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/postgres/osfingermap.yaml @@ -0,0 +1 @@ +{} diff --git a/postgres/osmap.yaml b/postgres/osmap.yaml index 72d4981..f5325c7 100644 --- a/postgres/osmap.yaml +++ b/postgres/osmap.yaml @@ -1,29 +1,37 @@ RedHat: - initdb_user: postgres - commands: - initdb: initdb pkg: postgresql-server pkg_client: postgresql pkg_repo: pgdg94 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: conf_dir: /var/lib/postgres/data - data_dir: /var/lib/postgres/data - initdb_user: postgres - initdb_args: --locale en_US.UTF8 -E UTF8 - commands: - initdb: initdb + prepare_cluster: + user: postgres + command: initdb -D /var/lib/postgresql/data + test: test -f /var/lib/postgres/data/PG_VERSION + env: + LC_ALL: C.UTF-8 pkg_client: postgresql pkg_dev: postgresql Debian: pkg_repo_file: /etc/apt/sources.list.d/pgdg.list pkg_dev: postgresql-server-dev-all pkg_libpq_dev: libpq-dev - initdb: False Suse: - initdb_user: postgres - commands: - initdb: initdb pkg: postgresql-server pkg_client: postgresql FreeBSD: