From 3e0b210d5da0b4d7e75e6267266f7ac1aaa34862 Mon Sep 17 00:00:00 2001 From: Bernd Schlapsi Date: Sun, 4 Jan 2015 00:02:41 +0100 Subject: [PATCH 01/21] Option to disable installation of pkg_libpq_dev --- postgres/init.sls | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/postgres/init.sls b/postgres/init.sls index a2eeea4..8b84ad7 100644 --- a/postgres/init.sls +++ b/postgres/init.sls @@ -39,8 +39,11 @@ install-postgres-dev-package: - name: {{ postgres.pkg_dev }} {% endif %} -{{ postgres.pkg_libpq_dev }}: +{% if postgres.pkg_libpq_dev != False %} +install-postgres-libpq-dev pkg.installed + - name: {{ postgres.pkg_libpq_dev }}: +{% endif %} {% if 'postgresconf' in pillar.get('postgres', {}) %} postgresql-conf: From 0a429b9a97a40dde49065e4a1d651b7331b6a02b Mon Sep 17 00:00:00 2001 From: Adam Durana Date: Thu, 29 Jan 2015 18:26:47 -0500 Subject: [PATCH 02/21] Fix typo --- postgres/init.sls | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/postgres/init.sls b/postgres/init.sls index 8b84ad7..cb06cb8 100644 --- a/postgres/init.sls +++ b/postgres/init.sls @@ -40,9 +40,9 @@ install-postgres-dev-package: {% endif %} {% if postgres.pkg_libpq_dev != False %} -install-postgres-libpq-dev - pkg.installed - - name: {{ postgres.pkg_libpq_dev }}: +install-postgres-libpq-dev: + pkg.installed: + - name: {{ postgres.pkg_libpq_dev }} {% endif %} {% if 'postgresconf' in pillar.get('postgres', {}) %} From b317cebf18f5862a7aafb33cc4c67d8c1388e24d Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 1 Feb 2015 14:40:45 +0000 Subject: [PATCH 03/21] Add option to use upstream repo on Debian --- pillar.example | 2 ++ postgres/init.sls | 13 +++++++++++++ postgres/map.jinja | 4 +++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pillar.example b/pillar.example index bec474e..15750ea 100644 --- a/pillar.example +++ b/pillar.example @@ -1,6 +1,8 @@ postgres: pg_hba.conf: salt://postgres/pg_hba.conf + use_upstream_repo: True + lookup: pkg: 'postgresql-9.3' pg_hba: '/etc/postgresql/9.3/main/pg_hba.conf' diff --git a/postgres/init.sls b/postgres/init.sls index cb06cb8..156a3ea 100644 --- a/postgres/init.sls +++ b/postgres/init.sls @@ -1,8 +1,21 @@ {% from "postgres/map.jinja" import postgres with context %} +{% if 'use_upstream_repo' in pillar.get('postgres') %} +install-postgresql-repo: + pkgrepo.managed: + - humanname: PostgreSQL Official Repository + - name: {{ postgres.pkg_repo }} + - keyid: B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 + - keyserver: keyserver.ubuntu.com + - file: {{ postgres.pkg_repo_file }} + - require_in: + - install-postgresql +{% endif %} + install-postgresql: pkg.installed: - name: {{ postgres.pkg }} + - refresh: {{ 'use_upstream_repo' in pillar.get('postgres') }} {% if postgres.create_cluster != False %} create-postgresql-cluster: diff --git a/postgres/map.jinja b/postgres/map.jinja index 4ec03fe..d1b94cf 100644 --- a/postgres/map.jinja +++ b/postgres/map.jinja @@ -34,9 +34,11 @@ 'version' : pg_version.id, }, 'Debian': { - 'pkg' : 'postgresql', + 'pkg' : 'postgresql-' + pg_version.id, 'pkg_dev' : 'postgresql-server-dev-' + pg_version.id, 'pkg_libpq_dev' : 'libpq-dev', + 'pkg_repo' : 'deb http://apt.postgresql.org/pub/repos/apt/ ' + grains['lsb_distrib_codename'] + '-pgdg main', + 'pkg_repo_file' : '/etc/apt/sources.list.d/pgdg.list', 'python' : 'python-pygresql', 'service' : 'postgresql', 'pg_hba' : '/etc/postgresql/' + pg_version.id + '/main/pg_hba.conf', From 3baf9ca8e1976f6e0540b9a87909025b9b5603ed Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sat, 14 Feb 2015 17:30:12 +0000 Subject: [PATCH 04/21] Add switch to install contrib package --- postgres/init.sls | 6 ++++++ postgres/map.jinja | 1 + 2 files changed, 7 insertions(+) diff --git a/postgres/init.sls b/postgres/init.sls index 156a3ea..e94ba86 100644 --- a/postgres/init.sls +++ b/postgres/init.sls @@ -58,6 +58,12 @@ install-postgres-libpq-dev: - name: {{ postgres.pkg_libpq_dev }} {% endif %} +{% if postgres.pkg_contrib != False %} +install-postgres-contrib: + pkg.installed: + - name: {{ postgres.pkg_contrib }} +{% endif %} + {% if 'postgresconf' in pillar.get('postgres', {}) %} postgresql-conf: file.blockreplace: diff --git a/postgres/map.jinja b/postgres/map.jinja index d1b94cf..f221cf7 100644 --- a/postgres/map.jinja +++ b/postgres/map.jinja @@ -37,6 +37,7 @@ 'pkg' : 'postgresql-' + pg_version.id, 'pkg_dev' : 'postgresql-server-dev-' + pg_version.id, 'pkg_libpq_dev' : 'libpq-dev', + 'pkg_contrib' : 'postgresql-contrib-' + pg_version.id, 'pkg_repo' : 'deb http://apt.postgresql.org/pub/repos/apt/ ' + grains['lsb_distrib_codename'] + '-pgdg main', 'pkg_repo_file' : '/etc/apt/sources.list.d/pgdg.list', 'python' : 'python-pygresql', From 6dd7a6596a11addd5eda3f1bdc655e8497f4bfb2 Mon Sep 17 00:00:00 2001 From: Alfredo Palhares Date: Fri, 20 Feb 2015 17:13:54 +0000 Subject: [PATCH 05/21] Add Gemfile with kitchen-salt dependencies --- Gemfile | 5 +++++ Gemfile.lock | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..4c95fc1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +source "https://rubygems.org" + +gem "test-kitchen", "> 1.2.0" +gem "kitchen-vagrant" +gem "kitchen-salt" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..14f77c6 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,26 @@ +GEM + remote: https://rubygems.org/ + specs: + kitchen-salt (0.0.19) + kitchen-vagrant (0.15.0) + test-kitchen (~> 1.0) + mixlib-shellout (2.0.1) + net-scp (1.2.1) + net-ssh (>= 2.6.5) + net-ssh (2.9.2) + safe_yaml (1.0.4) + test-kitchen (1.3.1) + mixlib-shellout (>= 1.2, < 3.0) + net-scp (~> 1.1) + net-ssh (~> 2.7) + safe_yaml (~> 1.0) + thor (~> 0.18) + thor (0.19.1) + +PLATFORMS + ruby + +DEPENDENCIES + kitchen-salt + kitchen-vagrant + test-kitchen (> 1.2.0) From 438811d03fcf5f0aa9e7bb99c895ba67b7a034b0 Mon Sep 17 00:00:00 2001 From: Alfredo Palhares Date: Fri, 20 Feb 2015 17:29:34 +0000 Subject: [PATCH 06/21] Add test-kitchen configuration file --- .kitchen.yml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 .kitchen.yml diff --git a/.kitchen.yml b/.kitchen.yml new file mode 100644 index 0000000..d9c06bf --- /dev/null +++ b/.kitchen.yml @@ -0,0 +1,26 @@ +--- +driver: + name: vagrant + network: + - ["private_network", { ip: "192.168.33.33" }] + +provisioner: + name: salt_solo + formula: postgres + pillars-from-files: + postgres.sls: pillar.example + pillars: + top.sls: + base: + "*": + - postgres + state_top: + base: + "*": + - postgres + +platforms: + - name: ubuntu-14.04 + +suites: + - name: default From 007ed90859d9be84d3593e3d26ce2a1eeb582f69 Mon Sep 17 00:00:00 2001 From: Alfredo Palhares Date: Fri, 20 Feb 2015 17:34:20 +0000 Subject: [PATCH 07/21] Add ServerSpec test --- test/integration/default/serverspec/postgres_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 test/integration/default/serverspec/postgres_spec.rb diff --git a/test/integration/default/serverspec/postgres_spec.rb b/test/integration/default/serverspec/postgres_spec.rb new file mode 100644 index 0000000..8977a5a --- /dev/null +++ b/test/integration/default/serverspec/postgres_spec.rb @@ -0,0 +1,10 @@ +require "serverspec" + +describe service("postgres") do + it { should be_enabled } + it { should be_running } +end + +describe port("5432") do + it { should be_listening } +end From debf1a5a7f189d5bff0c5f3290adf9beba7a292b Mon Sep 17 00:00:00 2001 From: Alfredo Palhares Date: Fri, 20 Feb 2015 17:38:03 +0000 Subject: [PATCH 08/21] Add gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ab1b3b9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.kitchen/ +*.swp +*.swo From 52b92b6a01fc6a1ada4be7afcde9daa52d6701de Mon Sep 17 00:00:00 2001 From: Alfredo Palhares Date: Mon, 23 Feb 2015 10:33:21 +0000 Subject: [PATCH 09/21] Add kitchen-salt documentation to the README file --- README.rst | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.rst b/README.rst index e4f2c09..a3077ae 100644 --- a/README.rst +++ b/README.rst @@ -22,3 +22,29 @@ Installs the postgresql package. ------------------- Installs the postgresql python module + +Testing +======= + +Testing is done wit kitchen-salt + +``kitchen converge`` +-------------------- + +Runs the postgres main state + +``kitchen verify`` +------------------ + +Runs serverspec tests on the actual instance + +``kitchen test`` +---------------- + +Builds and runs test from scratch + +``kitchen login`` +----------------- + +Gives you ssh to the vagrant machine for manual testing + From 5c67797b1b869ac41c8a85f8f322615572f4a3b8 Mon Sep 17 00:00:00 2001 From: Alfredo Palhares Date: Mon, 23 Feb 2015 16:47:04 +0000 Subject: [PATCH 10/21] Update README to markdown syntax --- README.md | 37 +++++++++++++++++++++++++++++++++++++ README.rst | 50 -------------------------------------------------- 2 files changed, 37 insertions(+), 50 deletions(-) create mode 100644 README.md delete mode 100644 README.rst diff --git a/README.md b/README.md new file mode 100644 index 0000000..b9c715a --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# postgres + +It sets up [PostgreSQL](http://www.postgresql.org/). + +For the best practises see [salt formulas installation and usage instructions](http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html) + +## Available states + + +#### ``postgres`` + +Installs the postgresql package. + +#### ``postgres.python`` + +Installs the postgresql python module + +## Testing + +Testing is done wit kitchen-salt + +#### ``kitchen converge`` + +Runs the postgres main state + +#### ``kitchen verify`` + +Runs serverspec tests on the actual instance + +#### ``kitchen test`` + +Builds and runs test from scratch + +#### ``kitchen login`` + +Gives you ssh to the vagrant machine for manual testing + diff --git a/README.rst b/README.rst deleted file mode 100644 index a3077ae..0000000 --- a/README.rst +++ /dev/null @@ -1,50 +0,0 @@ -======== -postgres -======== - -.. note:: - - See the full `Salt Formulas installation and usage instructions - `_. - -Available states -================ - -.. contents:: - :local: - -``postgres`` ------------- - -Installs the postgresql package. - -``postgres.python`` -------------------- - -Installs the postgresql python module - -Testing -======= - -Testing is done wit kitchen-salt - -``kitchen converge`` --------------------- - -Runs the postgres main state - -``kitchen verify`` ------------------- - -Runs serverspec tests on the actual instance - -``kitchen test`` ----------------- - -Builds and runs test from scratch - -``kitchen login`` ------------------ - -Gives you ssh to the vagrant machine for manual testing - From edca294b29a57edc16f5be1d34587cef2b606247 Mon Sep 17 00:00:00 2001 From: Nitin Madhok Date: Mon, 23 Feb 2015 23:40:07 +0530 Subject: [PATCH 11/21] Revert "Update README to markdown syntax" --- README.md | 37 ------------------------------------- README.rst | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 37 deletions(-) delete mode 100644 README.md create mode 100644 README.rst diff --git a/README.md b/README.md deleted file mode 100644 index b9c715a..0000000 --- a/README.md +++ /dev/null @@ -1,37 +0,0 @@ -# postgres - -It sets up [PostgreSQL](http://www.postgresql.org/). - -For the best practises see [salt formulas installation and usage instructions](http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html) - -## Available states - - -#### ``postgres`` - -Installs the postgresql package. - -#### ``postgres.python`` - -Installs the postgresql python module - -## Testing - -Testing is done wit kitchen-salt - -#### ``kitchen converge`` - -Runs the postgres main state - -#### ``kitchen verify`` - -Runs serverspec tests on the actual instance - -#### ``kitchen test`` - -Builds and runs test from scratch - -#### ``kitchen login`` - -Gives you ssh to the vagrant machine for manual testing - diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..a3077ae --- /dev/null +++ b/README.rst @@ -0,0 +1,50 @@ +======== +postgres +======== + +.. note:: + + See the full `Salt Formulas installation and usage instructions + `_. + +Available states +================ + +.. contents:: + :local: + +``postgres`` +------------ + +Installs the postgresql package. + +``postgres.python`` +------------------- + +Installs the postgresql python module + +Testing +======= + +Testing is done wit kitchen-salt + +``kitchen converge`` +-------------------- + +Runs the postgres main state + +``kitchen verify`` +------------------ + +Runs serverspec tests on the actual instance + +``kitchen test`` +---------------- + +Builds and runs test from scratch + +``kitchen login`` +----------------- + +Gives you ssh to the vagrant machine for manual testing + From 9a21a59b589ee8a4a31d328119865cf0b098ab56 Mon Sep 17 00:00:00 2001 From: Rico Moorman Date: Thu, 26 Feb 2015 22:58:54 +0100 Subject: [PATCH 12/21] Adjust the init state's jinja syntax to prevent errors if there is no postgres pillar data --- postgres/init.sls | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/postgres/init.sls b/postgres/init.sls index e94ba86..8145e1f 100644 --- a/postgres/init.sls +++ b/postgres/init.sls @@ -1,6 +1,6 @@ {% from "postgres/map.jinja" import postgres with context %} -{% if 'use_upstream_repo' in pillar.get('postgres') %} +{% if salt['pillar.get']('postgres:use_upstream_repo') %} install-postgresql-repo: pkgrepo.managed: - humanname: PostgreSQL Official Repository @@ -15,7 +15,7 @@ install-postgresql-repo: install-postgresql: pkg.installed: - name: {{ postgres.pkg }} - - refresh: {{ 'use_upstream_repo' in pillar.get('postgres') }} + - refresh: {{ salt['pillar.get']('postgres:use_upstream_repo', False) }} {% if postgres.create_cluster != False %} create-postgresql-cluster: From 430c735b0d27e87f389f7e695efc128a97e71550 Mon Sep 17 00:00:00 2001 From: Dominic Ferrantelli Date: Thu, 12 Mar 2015 16:14:34 -0700 Subject: [PATCH 13/21] added automatic indentation of postgres:postgresconf to allow for multiple lines to be added to the conf --- postgres/init.sls | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/postgres/init.sls b/postgres/init.sls index 8145e1f..1c3611e 100644 --- a/postgres/init.sls +++ b/postgres/init.sls @@ -70,7 +70,8 @@ postgresql-conf: - 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: {{ salt['pillar.get']('postgres:postgresconf') }} + - content: | + {{ salt['pillar.get']('postgres:postgresconf') | indent(8) }} - show_changes: True - append_if_not_found: True - watch_in: From b38deb618fc69c58ef57e0c0d1516d2e16d82895 Mon Sep 17 00:00:00 2001 From: Nitin Madhok Date: Fri, 20 Mar 2015 20:12:44 -0400 Subject: [PATCH 14/21] Update LICENSING year --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 52ec1c1..4de972d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ - Copyright (c) 2013 Salt Stack Formulas + Copyright (c) 2013-2015 Salt Stack Formulas Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. From f6e68157a3e2e88c4d9e3734ab5c6e6c7d22abd4 Mon Sep 17 00:00:00 2001 From: Dominic Ferrantelli Date: Tue, 21 Apr 2015 13:06:41 -0700 Subject: [PATCH 15/21] added client formula and required default values --- postgres/client.sls | 24 ++++++++++++++++++++++++ postgres/map.jinja | 3 +++ 2 files changed, 27 insertions(+) create mode 100644 postgres/client.sls diff --git a/postgres/client.sls b/postgres/client.sls new file mode 100644 index 0000000..44986b1 --- /dev/null +++ b/postgres/client.sls @@ -0,0 +1,24 @@ +{% from "postgres/map.jinja" import postgres with context %} + +{% if salt['pillar.get']('postgres:use_upstream_repo') %} +install-postgresql-repo: + pkgrepo.managed: + - humanname: PostgreSQL Official Repository + - name: {{ postgres.pkg_repo }} + - keyid: B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 + - keyserver: keyserver.ubuntu.com + - file: {{ postgres.pkg_repo_file }} + - require_in: + - install-postgresql-client +{% endif %} + +install-postgresql-client: + pkg.installed: + - name: {{ postgres.pkg_client }} + - refresh: {{ salt['pillar.get']('postgres:use_upstream_repo', False) }} + +{% if postgres.pkg_libpq_dev != False %} +install-postgres-libpq-dev: + pkg.installed: + - name: {{ postgres.pkg_libpq_dev }} +{% endif %} diff --git a/postgres/map.jinja b/postgres/map.jinja index f221cf7..543b4ab 100644 --- a/postgres/map.jinja +++ b/postgres/map.jinja @@ -15,6 +15,7 @@ 'pkg' : 'postgresql-server', 'pkg_dev' : 'postgresql-devel', 'pkg_libpq_dev' : 'postgresql-libs', + 'pkg_client' : 'postgresql-client', 'python' : 'python-psycopg2', 'service' : 'postgresql', 'conf_dir' : '/var/lib/pgsql/data', @@ -26,6 +27,7 @@ 'pkg' : 'postgresql', 'pkg_dev' : 'postgresql-devel', 'pkg_libpq_dev' : 'postgresql-libs', + 'pkg_client' : 'postgresql-client', 'python' : 'python-psycopg2', 'service' : 'postgresql', 'conf_dir' : '/var/lib/pgsql/data', @@ -37,6 +39,7 @@ 'pkg' : 'postgresql-' + pg_version.id, 'pkg_dev' : 'postgresql-server-dev-' + pg_version.id, 'pkg_libpq_dev' : 'libpq-dev', + 'pkg_client' : 'postgresql-client-'+ pg_version.id, 'pkg_contrib' : 'postgresql-contrib-' + pg_version.id, 'pkg_repo' : 'deb http://apt.postgresql.org/pub/repos/apt/ ' + grains['lsb_distrib_codename'] + '-pgdg main', 'pkg_repo_file' : '/etc/apt/sources.list.d/pgdg.list', From c62b41524db24ca3f2f2aeb0a9767ad769c58a43 Mon Sep 17 00:00:00 2001 From: Dominic Ferrantelli Date: Tue, 21 Apr 2015 13:40:01 -0700 Subject: [PATCH 16/21] updated README and pillar.example to include client formula --- README.rst | 5 +++++ pillar.example | 1 + 2 files changed, 6 insertions(+) diff --git a/README.rst b/README.rst index a3077ae..71ebb2d 100644 --- a/README.rst +++ b/README.rst @@ -23,6 +23,11 @@ Installs the postgresql package. Installs the postgresql python module +``postgres.client`` +------------------- + +Installs the postgresql client + Testing ======= diff --git a/pillar.example b/pillar.example index 15750ea..4aece09 100644 --- a/pillar.example +++ b/pillar.example @@ -5,6 +5,7 @@ postgres: lookup: pkg: 'postgresql-9.3' + pkg_client: 'postgresql-client-9.3' pg_hba: '/etc/postgresql/9.3/main/pg_hba.conf' users: From 7ddfbb22e6d28c2ce4978aeaed15c1e20de6a4f9 Mon Sep 17 00:00:00 2001 From: Dominic Ferrantelli Date: Tue, 21 Apr 2015 15:13:42 -0700 Subject: [PATCH 17/21] moved upstream repo to an include, checked for Ubuntu before attempting to add it --- postgres/client.sls | 11 ++--------- postgres/init.sls | 11 ++--------- postgres/upstream.sls | 11 +++++++++++ 3 files changed, 15 insertions(+), 18 deletions(-) create mode 100644 postgres/upstream.sls diff --git a/postgres/client.sls b/postgres/client.sls index 44986b1..d171f9a 100644 --- a/postgres/client.sls +++ b/postgres/client.sls @@ -1,15 +1,8 @@ {% from "postgres/map.jinja" import postgres with context %} {% if salt['pillar.get']('postgres:use_upstream_repo') %} -install-postgresql-repo: - pkgrepo.managed: - - humanname: PostgreSQL Official Repository - - name: {{ postgres.pkg_repo }} - - keyid: B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 - - keyserver: keyserver.ubuntu.com - - file: {{ postgres.pkg_repo_file }} - - require_in: - - install-postgresql-client +include: + - postgres.upstream {% endif %} install-postgresql-client: diff --git a/postgres/init.sls b/postgres/init.sls index 1c3611e..f1a2009 100644 --- a/postgres/init.sls +++ b/postgres/init.sls @@ -1,15 +1,8 @@ {% from "postgres/map.jinja" import postgres with context %} {% if salt['pillar.get']('postgres:use_upstream_repo') %} -install-postgresql-repo: - pkgrepo.managed: - - humanname: PostgreSQL Official Repository - - name: {{ postgres.pkg_repo }} - - keyid: B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 - - keyserver: keyserver.ubuntu.com - - file: {{ postgres.pkg_repo_file }} - - require_in: - - install-postgresql +include: + - postgres.upstream {% endif %} install-postgresql: diff --git a/postgres/upstream.sls b/postgres/upstream.sls new file mode 100644 index 0000000..d5da6da --- /dev/null +++ b/postgres/upstream.sls @@ -0,0 +1,11 @@ +{% from "postgres/map.jinja" import postgres with context %} + +{% if grains['os'] == 'Ubuntu' %} # Other distro support should be added here +install-postgresql-repo: + pkgrepo.managed: + - humanname: PostgreSQL Official Repository + - name: {{ postgres.pkg_repo }} + - keyid: B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 + - keyserver: keyserver.ubuntu.com + - file: {{ postgres.pkg_repo_file }} +{% endif %} From 967fa6504b27fa2d12f9e56b11e9dde86ff90033 Mon Sep 17 00:00:00 2001 From: Brian Jackson Date: Wed, 22 Apr 2015 17:47:02 -0500 Subject: [PATCH 18/21] Switch to new style map.jinja Move this formula to the new style map.jinja. Still need to sort out RedHat handling, but it's not any worse than it was before (i.e. horribly broken) --- pillar.example | 2 +- postgres/client.sls | 4 +-- postgres/codenamemap.yaml | 9 +++++ postgres/defaults.yaml | 18 ++++++++++ postgres/init.sls | 44 ++++++++++------------- postgres/map.jinja | 74 +++++++++++---------------------------- postgres/osmap.yaml | 6 ++++ postgres/upstream.sls | 7 ++-- 8 files changed, 81 insertions(+), 83 deletions(-) create mode 100644 postgres/codenamemap.yaml create mode 100644 postgres/defaults.yaml create mode 100644 postgres/osmap.yaml diff --git a/pillar.example b/pillar.example index 4aece09..eb34251 100644 --- a/pillar.example +++ b/pillar.example @@ -1,7 +1,7 @@ postgres: pg_hba.conf: salt://postgres/pg_hba.conf - use_upstream_repo: True + use_upstream_repo: False lookup: pkg: 'postgresql-9.3' diff --git a/postgres/client.sls b/postgres/client.sls index d171f9a..84b9e07 100644 --- a/postgres/client.sls +++ b/postgres/client.sls @@ -1,6 +1,6 @@ {% from "postgres/map.jinja" import postgres with context %} -{% if salt['pillar.get']('postgres:use_upstream_repo') %} +{% if postgres.use_upstream_repo %} include: - postgres.upstream {% endif %} @@ -8,7 +8,7 @@ include: install-postgresql-client: pkg.installed: - name: {{ postgres.pkg_client }} - - refresh: {{ salt['pillar.get']('postgres:use_upstream_repo', False) }} + - refresh: {{ postgres.use_upstream_repo }} {% if postgres.pkg_libpq_dev != False %} install-postgres-libpq-dev: diff --git a/postgres/codenamemap.yaml b/postgres/codenamemap.yaml new file mode 100644 index 0000000..8759e4c --- /dev/null +++ b/postgres/codenamemap.yaml @@ -0,0 +1,9 @@ +wheezy: + pkg_repo: deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main +jessie: + version: 9.4 + pkg_repo: deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main +trusty: + version: 9.3 +utopic: + version: 9.4 diff --git a/postgres/defaults.yaml b/postgres/defaults.yaml new file mode 100644 index 0000000..fe9c3fa --- /dev/null +++ b/postgres/defaults.yaml @@ -0,0 +1,18 @@ +postgres: + pkg: postgresql + pkg_dev: postgresql-devel + pkg_libpq_dev: postgresql-libs + pkg_client: postgresql-client + pkg_contrib: postgresql-contrib + python: python-psycopg2 + service: postgresql + conf_dir: /var/lib/pgsql/data + create_cluster: False + init_db: False + version: 9.1 + use_upstream_repo: False + users: {} + acls: [] + databases: {} + postgresconf: "" + pg_hba.conf: salt://postgres/pg_hba.conf diff --git a/postgres/init.sls b/postgres/init.sls index f1a2009..2d444ed 100644 --- a/postgres/init.sls +++ b/postgres/init.sls @@ -1,6 +1,6 @@ {% from "postgres/map.jinja" import postgres with context %} -{% if salt['pillar.get']('postgres:use_upstream_repo') %} +{% if postgres.use_upstream_repo %} include: - postgres.upstream {% endif %} @@ -8,7 +8,7 @@ include: install-postgresql: pkg.installed: - name: {{ postgres.pkg }} - - refresh: {{ salt['pillar.get']('postgres:use_upstream_repo', False) }} + - refresh: {{ postgres.use_upstream_repo }} {% if postgres.create_cluster != False %} create-postgresql-cluster: @@ -57,25 +57,24 @@ install-postgres-contrib: - name: {{ postgres.pkg_contrib }} {% endif %} -{% if 'postgresconf' in pillar.get('postgres', {}) %} +{% 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: | - {{ salt['pillar.get']('postgres:postgresconf') | indent(8) }} + {{ postgres.postgresconf|indent(8) }} - show_changes: True - append_if_not_found: True - watch_in: - service: postgresql {% endif %} -{% if 'pg_hba.conf' in pillar.get('postgres', {}) %} pg_hba.conf: file.managed: - name: {{ postgres.conf_dir }}/pg_hba.conf - - source: {{ salt['pillar.get']('postgres:pg_hba.conf', 'salt://postgres/pg_hba.conf') }} + - source: {{ postgres['pg_hba.conf'] }} - template: jinja - user: postgres - group: postgres @@ -84,37 +83,32 @@ pg_hba.conf: - pkg: {{ postgres.pkg }} - watch_in: - service: postgresql -{% endif %} -{% if 'users' in pillar.get('postgres', {}) %} -{% for name, user in salt['pillar.get']('postgres:users').items() %} +{% for name, user in postgres.users.items() %} postgres-user-{{ name }}: postgres_user.present: - name: {{ name }} - - createdb: {{ salt['pillar.get']('postgres:users:' + name + ':createdb', False) }} - - password: {{ salt['pillar.get']('postgres:users:' + name + ':password', 'changethis') }} - - runas: postgres + - createdb: {{ user.get('createdb', False) }} + - password: {{ user.get('password', 'changethis') }} + - user: {{ user.get('runas', 'postgres') }} - require: - service: {{ postgres.service }} {% endfor%} -{% endif %} -{% if 'databases' in pillar.get('postgres', {}) %} -{% for name, db in salt['pillar.get']('postgres:databases').items() %} +{% for name, db in postgres.databases.items() %} postgres-db-{{ name }}: postgres_database.present: - name: {{ name }} - - encoding: {{ salt['pillar.get']('postgres:databases:'+ name +':encoding', 'UTF8') }} - - lc_ctype: {{ salt['pillar.get']('postgres:databases:'+ name +':lc_ctype', 'en_US.UTF8') }} - - lc_collate: {{ salt['pillar.get']('postgres:databases:'+ name +':lc_collate', 'en_US.UTF8') }} - - template: {{ salt['pillar.get']('postgres:databases:'+ name +':template', 'template0') }} - {% if salt['pillar.get']('postgres:databases:'+ name +':owner') %} - - owner: {{ salt['pillar.get']('postgres:databases:'+ name +':owner') }} + - encoding: {{ db.get('encoding', 'UTF8') }} + - lc_ctype: {{ db.get('lc_ctype', 'en_US.UTF8') }} + - lc_collate: {{ db.get('lc_collate', 'en_US.UTF8') }} + - template: {{ db.get('template', 'template0') }} + {% if db.get('owner') %} + - owner: {{ db.get('owner') }} {% endif %} - - runas: {{ salt['pillar.get']('postgres:databases:'+ name +':runas', 'postgres') }} - {% if salt['pillar.get']('postgres:databases:'+ name +':user') %} + - user: {{ db.get('runas', 'postgres') }} + {% if db.get('user') %} - require: - - postgres_user: postgres-user-{{ salt['pillar.get']('postgres:databases:'+ name +':user') }} + - postgres_user: postgres-user-{{ db.get('user') }} {% endif %} {% endfor%} -{% endif %} diff --git a/postgres/map.jinja b/postgres/map.jinja index 543b4ab..415e344 100644 --- a/postgres/map.jinja +++ b/postgres/map.jinja @@ -1,54 +1,22 @@ -{% set pg_version = salt['grains.filter_by']({ - 'RedHat' : { 'id': '9.1' }, - 'Arch' : { 'id': '9.1' }, - 'Debian' : { 'id': '9.3' }, -}, merge=salt['grains.filter_by']({ - '14.04' : { 'id': '9.3' }, - '14.10' : { 'id': '9.4' }, -}, grain='lsb_distrib_release', merge=salt['grains.filter_by']({ - 'jessie' : { 'id': '9.4' }, - 'wheezy' : { 'id': '9.1' }, -}, grain='lsb_distrib_codename', merge=salt['pillar.get']('postgres:lookup')))) %} +{% import_yaml "postgres/defaults.yaml" as defaults %} +{% import_yaml "postgres/osmap.yaml" as osmap %} +{% import_yaml "postgres/codenamemap.yaml" as codemap %} -{% set postgres = salt['grains.filter_by']({ - 'RedHat': { - 'pkg' : 'postgresql-server', - 'pkg_dev' : 'postgresql-devel', - 'pkg_libpq_dev' : 'postgresql-libs', - 'pkg_client' : 'postgresql-client', - 'python' : 'python-psycopg2', - 'service' : 'postgresql', - 'conf_dir' : '/var/lib/pgsql/data', - 'create_cluster' : False, - 'init_db' : True, - 'version' : pg_version.id, - }, - 'Arch': { - 'pkg' : 'postgresql', - 'pkg_dev' : 'postgresql-devel', - 'pkg_libpq_dev' : 'postgresql-libs', - 'pkg_client' : 'postgresql-client', - 'python' : 'python-psycopg2', - 'service' : 'postgresql', - 'conf_dir' : '/var/lib/pgsql/data', - 'create_cluster' : False, - 'init_db' : True, - 'version' : pg_version.id, - }, - 'Debian': { - 'pkg' : 'postgresql-' + pg_version.id, - 'pkg_dev' : 'postgresql-server-dev-' + pg_version.id, - 'pkg_libpq_dev' : 'libpq-dev', - 'pkg_client' : 'postgresql-client-'+ pg_version.id, - 'pkg_contrib' : 'postgresql-contrib-' + pg_version.id, - 'pkg_repo' : 'deb http://apt.postgresql.org/pub/repos/apt/ ' + grains['lsb_distrib_codename'] + '-pgdg main', - 'pkg_repo_file' : '/etc/apt/sources.list.d/pgdg.list', - 'python' : 'python-pygresql', - 'service' : 'postgresql', - 'pg_hba' : '/etc/postgresql/' + pg_version.id + '/main/pg_hba.conf', - 'conf_dir' : '/etc/postgresql/' + pg_version.id + '/main', - 'create_cluster' : False, - 'init_db' : False, - 'version' : pg_version.id, - }, -}, merge=salt['pillar.get']('postgres:lookup')) %} +{# get the settings for the os_family grain #} +{% set osfam = salt['grains.filter_by'](osmap) 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 {} %} + +{# merge the os family/codename specific data over the defaults #} +{% do defaults.postgres.update(oscode) %} + +{# 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) %} diff --git a/postgres/osmap.yaml b/postgres/osmap.yaml new file mode 100644 index 0000000..dc4224e --- /dev/null +++ b/postgres/osmap.yaml @@ -0,0 +1,6 @@ +RedHat: + pkg_repo: pgdg94 + repo_baseurl: http://yum.postgresql.org/9.4/redhat/rhel-$releasever-$basearch +Arch: {} +Debian: + pkg_repo_file: /etc/apt/sources.list.d/pgdg.list diff --git a/postgres/upstream.sls b/postgres/upstream.sls index d5da6da..14ec148 100644 --- a/postgres/upstream.sls +++ b/postgres/upstream.sls @@ -1,11 +1,14 @@ {% from "postgres/map.jinja" import postgres with context %} -{% if grains['os'] == 'Ubuntu' %} # Other distro support should be added here +{% if grains['os_family'] == 'Debian' %} install-postgresql-repo: pkgrepo.managed: - humanname: PostgreSQL Official Repository - - name: {{ postgres.pkg_repo }} + - name: {{ postgres.pkg_repo }} {{ postgres.version }} - keyid: B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 - keyserver: keyserver.ubuntu.com - file: {{ postgres.pkg_repo_file }} + - require_in: + - pkg: {{ postgres.pkg }} {% endif %} + From 17baaa9f8c04530d2cf3a013619ec878af5af9db Mon Sep 17 00:00:00 2001 From: Brian Jackson Date: Thu, 23 Apr 2015 11:17:39 -0500 Subject: [PATCH 19/21] Add _states/_modules for tablespaces and add tablespace support to formula Add temporary states/modules for handling tablespaces in Postgresql. These will be fed back upstream, but they can live here for now. Add support for the states/modules to the formula. --- _modules/postgres_ext.py | 261 +++++++++++++++++++++++++++++++++ _states/postgres_tablespace.py | 140 ++++++++++++++++++ postgres/init.sls | 19 +++ 3 files changed, 420 insertions(+) create mode 100644 _modules/postgres_ext.py create mode 100644 _states/postgres_tablespace.py diff --git a/_modules/postgres_ext.py b/_modules/postgres_ext.py new file mode 100644 index 0000000..89666fa --- /dev/null +++ b/_modules/postgres_ext.py @@ -0,0 +1,261 @@ +from __future__ import absolute_import +import logging +try: + import pipes + import csv + HAS_ALL_IMPORTS = True +except ImportError: + HAS_ALL_IMPORTS = False + +# All this can be removed when we merge this stuff upstream +import salt.utils + +log = logging.getLogger(__name__) + +def __virtual__(): + ''' + Only load this module if the postgres module is already loaded + ''' + if 'postgres.psql_query' in __salt__: + return True + return False + +# Copied directly from salt/modules/postgres.py, remove when upstreaming +def _run_psql(cmd, runas=None, password=None, host=None, port=None, user=None): + ''' + Helper function to call psql, because the password requirement + makes this too much code to be repeated in each function below + ''' + kwargs = { + 'reset_system_locale': False, + 'clean_env': True, + } + if runas is None: + if not host: + host = __salt__['config.option']('postgres.host') + if not host or host.startswith('/'): + if 'FreeBSD' in __grains__['os_family']: + runas = 'pgsql' + if 'OpenBSD' in __grains__['os_family']: + runas = '_postgresql' + else: + runas = 'postgres' + + if user is None: + user = runas + + if runas: + kwargs['runas'] = runas + + if password is None: + password = __salt__['config.option']('postgres.pass') + if password is not None: + pgpassfile = salt.utils.mkstemp(text=True) + with salt.utils.fopen(pgpassfile, 'w') as fp_: + fp_.write('{0}:{1}:*:{2}:{3}'.format( + 'localhost' if not host or host.startswith('/') else host, + port if port else '*', + user if user else '*', + password, + )) + __salt__['file.chown'](pgpassfile, runas, '') + kwargs['env'] = {'PGPASSFILE': pgpassfile} + + ret = __salt__['cmd.run_all'](cmd, python_shell=False, **kwargs) + + if ret.get('retcode', 0) != 0: + log.error('Error connecting to Postgresql server') + if password is not None and not __salt__['file.remove'](pgpassfile): + log.warning('Remove PGPASSFILE failed') + + return ret + + +def _connection_defaults(user=None, host=None, port=None, maintenance_db=None, + password=None): + ''' + Returns a tuple of (user, host, port, db) with config, pillar, or default + values assigned to missing values. + ''' + if not user: + user = __salt__['config.option']('postgres.user') + if not host: + host = __salt__['config.option']('postgres.host') + if not port: + port = __salt__['config.option']('postgres.port') + if not maintenance_db: + maintenance_db = __salt__['config.option']('postgres.maintenance_db') + if password is None: + password = __salt__['config.option']('postgres.pass') + + return (user, host, port, maintenance_db, password) + + +def _psql_cmd(*args, **kwargs): + ''' + Return string with fully composed psql command. + Accept optional keyword arguments: user, host and port as well as any + number or positional arguments to be added to the end of command. + ''' + (user, host, port, maintenance_db, password) = _connection_defaults( + kwargs.get('user'), + kwargs.get('host'), + kwargs.get('port'), + kwargs.get('maintenance_db'), + kwargs.get('password')) + + cmd = [salt.utils.which('psql'), + '--no-align', + '--no-readline', + '--no-password'] # It is never acceptable to issue a password prompt. + if user: + cmd += ['--username', user] + if host: + cmd += ['--host', host] + if port: + cmd += ['--port', str(port)] + if not maintenance_db: + maintenance_db = 'postgres' + cmd += ['--dbname', maintenance_db] + cmd += args + cmdstr = ' '.join([pipes.quote(c) for c in cmd]) + return cmdstr + + +def _psql_prepare_and_run(cmd, + host=None, + port=None, + maintenance_db=None, + password=None, + runas=None, + user=None): + rcmd = _psql_cmd( + host=host, user=user, port=port, + maintenance_db=maintenance_db, password=password, + *cmd) + cmdret = _run_psql( + rcmd, runas=runas, password=password, host=host, port=port, user=user) + return cmdret + +def tablespace_list(user=None, host=None, port=None, maintenance_db=None, + password=None, runas=None): + ''' + Return dictionary with information about tablespaces of a Postgres server. + CLI Example: + .. code-block:: bash + salt '*' postgres_ext.tablespace_list + ''' + + ret = {} + + query = ( + 'SELECT spcname as "Name", pga.rolname as "Owner", spcacl as "ACL", ' + 'spcoptions as "Opts", pg_tablespace_location(pgts.oid) as "Location" ' + 'FROM pg_tablespace pgts, pg_roles pga WHERE pga.oid = pgts.spcowner' + ) + + rows = __salt__['postgres.psql_query'](query, runas=runas, host=host, + user=user, port=port, + maintenance_db=maintenance_db, + password=password) + + for row in rows: + ret[row['Name']] = row + ret[row['Name']].pop('Name') + + return ret + + +def tablespace_exists(name, user=None, host=None, port=None, maintenance_db=None, + password=None, runas=None): + ''' + Checks if a tablespace exists on the Postgres server. + CLI Example: + .. code-block:: bash + salt '*' postgres_ext.tablespace_exists 'dbname' + ''' + + tablespaces = tablespace_list(user=user, host=host, port=port, + maintenance_db=maintenance_db, + password=password, runas=runas) + return name in tablespaces + + +def tablespace_create(name, location, user=None, host=None, port=None, + maintenance_db=None, password=None, runas=None): + ''' + Adds a tablespace to the Postgres server. + + CLI Example: + + .. code-block:: bash + + salt '*' postgres_ext.tablespace_create tablespacename '/path/datadir' + ''' + query = 'CREATE TABLESPACE {0} LOCATION \'{1}\''.format(name, location) + + # Execute the command + ret = _psql_prepare_and_run(['-c', query], + user=user, host=host, port=port, + maintenance_db=maintenance_db, + password=password, runas=runas) + return ret['retcode'] == 0 + + +def tablespace_alter(name, user=None, host=None, port=None, maintenance_db=None, + password=None, new_name=None, new_owner=None, + set_option=None, reset_option=None, runas=None): + ''' + Change tablespace name, owner, or options. + CLI Example: + .. code-block:: bash + salt '*' postgres_ext.tablespace_alter tsname new_owner=otheruser + salt '*' postgres_ext.tablespace_alter index_space new_name=fast_raid + salt '*' postgres_ext.tablespace_alter tsname reset_option=seq_page_cost + ''' + if not any([new_name, new_owner, set_option, reset_option]): + return True # Nothing todo? + + queries = [] + + if new_name: + queries.append('ALTER TABLESPACE {} RENAME TO {}'.format( + name, new_name)) + if new_owner: + queries.append('ALTER TABLESPACE {} OWNER TO {}'.format( + name, new_owner)) + if set_option: + queries.append('ALTER TABLESPACE {} SET ({} = {})'.format( + name, set_option[0], set_option[1])) + if reset_option: + queries.append('ALTER TABLESPACE {} RESET ({})'.format( + name, reset_option)) + + for query in queries: + ret = _psql_prepare_and_run(['-c', query], + user=user, host=host, port=port, + maintenance_db=maintenance_db, + password=password, runas=runas) + if ret['retcode'] != 0: + return False + + return True + + +def tablespace_remove(name, user=None, host=None, port=None, + maintenance_db=None, password=None, runas=None): + ''' + Removes a tablespace from the Postgres server. + CLI Example: + .. code-block:: bash + salt '*' postgres_ext.tablespace_remove tsname + ''' + query = 'DROP TABLESPACE {}'.format(name) + ret = _psql_prepare_and_run(['-c', query], + user=user, + host=host, + port=port, + runas=runas, + maintenance_db=maintenance_db, + password=password) + return ret['retcode'] == 0 diff --git a/_states/postgres_tablespace.py b/_states/postgres_tablespace.py new file mode 100644 index 0000000..fc3524f --- /dev/null +++ b/_states/postgres_tablespace.py @@ -0,0 +1,140 @@ +''' +Management of PostgreSQL tablespace +================================== +The postgres_tablespace module is used to create and manage Postgres +tablespaces. +Tablespaces can be set as either absent or present. +.. code-block:: yaml + ssd-tablespace: + postgres_tablespace.present: + - name: indexes + - path: +''' +from __future__ import absolute_import + + +def __virtual__(): + ''' + Only load if the postgres_ext module is present + ''' + return 'postgres_ext.tablespace_exists' in __salt__ + +def present(name, + directory, + options=None, + owner=None, + user=None, + maintenance_db=None, + db_password=None, + db_host=None, + db_port=None, + db_user=None): + ''' + Ensure that the named tablespace is present with the specified properties. + For more information about all of these options see man create_tablespace(1) + name + The name of the tablespace to manage + directory + The directory where the tablespace will be located + db_user + database username if different from config or defaul + db_password + user password if any password for a specified user + db_host + Database host if different from config or default + db_port + Database port if different from config or default + user + System user all operations should be performed on behalf of + .. versionadded:: Beryllium + ''' + ret = {'name': name, + 'changes': {}, + 'result': True, + 'comment': 'Tablespace {0} is already present'.format(name)} + dbargs = { + 'maintenance_db': maintenance_db, + 'runas': user, + 'host': db_host, + 'user': db_user, + 'port': db_port, + 'password': db_password, + } + tblspaces = __salt__['postgres_ext.tablespace_list'](**dbargs) + if name not in tblspaces: + # not there, create it + if __opts__['test']: + ret['result'] = None + ret['comment'] = 'Tablespace {0} is set to be created'.format(name) + return ret + if __salt__['postgres_ext.tablespace_create'](name, directory, **dbargs): + ret['comment'] = 'The tablespace {0} has been created'.format(name) + ret['changes'][name] = 'Present' + return ret + + # already exists, make sure it's got the right path + if tblspaces[name]['Location'] != directory: + ret['comment'] = 'Tablespace {0} isn\'t at the right path'.format( + name) + ret['result'] = False + return ret # This isn't changeable, they need to remove/remake + + if (not owner and not tblspaces[name]['Owner'] == owner): + if __salt__['postgres_ext.tablespace_alter'](name, new_owner=owner): + ret['comment'] = 'Tablespace {0} owner changed'.format(name) + ret['result'] = True + + return ret + + +def absent(name, + user=None, + maintenance_db=None, + db_password=None, + db_host=None, + db_port=None, + db_user=None): + ''' + Ensure that the named database is absent + name + The name of the database to remove + db_user + database username if different from config or defaul + db_password + user password if any password for a specified user + db_host + Database host if different from config or default + db_port + Database port if different from config or default + user + System user all operations should be performed on behalf of + .. versionadded:: Beryllium + ''' + ret = {'name': name, + 'changes': {}, + 'result': True, + 'comment': ''} + + db_args = { + 'maintenance_db': maintenance_db, + 'runas': user, + 'host': db_host, + 'user': db_user, + 'port': db_port, + 'password': db_password, + } + #check if tablespace exists and remove it + if __salt__['postgres_ext.tablespace_exists'](name, **db_args): + if __opts__['test']: + ret['result'] = None + ret['comment'] = 'Tablespace {0} is set to be removed'.format(name) + return ret + if __salt__['postgres_ext.tablespace_remove'](name, **db_args): + ret['comment'] = 'Tablespace {0} has been removed'.format(name) + ret['changes'][name] = 'Absent' + return ret + + # fallback + ret['comment'] = 'Tablespace {0} is not present, so it cannot ' \ + 'be removed'.format(name) + return ret diff --git a/postgres/init.sls b/postgres/init.sls index 2d444ed..58b4f95 100644 --- a/postgres/init.sls +++ b/postgres/init.sls @@ -112,3 +112,22 @@ postgres-db-{{ name }}: - postgres_user: postgres-user-{{ db.get('user') }} {% endif %} {% endfor%} + +{% for name, directory in postgres.tablespaces.items() %} +postgres-tablespace-dir-perms-{{ directory}}: + file.directory: + - name: {{ directory }} + - user: postgres + - group: postgres + - makedirs: True + - recurse: + - user + - group + +postgres-tablespace-{{ name }}: + postgres_tablespace.present: + - name: {{ name }} + - directory: {{ directory }} + - require: + - service: {{ postgres.service }} +{% endfor%} From e23f4bf8e84c0b248ad4be21680aaf5b767ad683 Mon Sep 17 00:00:00 2001 From: Brian Jackson Date: Thu, 23 Apr 2015 12:04:20 -0500 Subject: [PATCH 20/21] Handle test=True correctly for tablespace owner We weren't handling test=True correctly for when the table already existed, but the owner was changing. --- _states/postgres_tablespace.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/_states/postgres_tablespace.py b/_states/postgres_tablespace.py index fc3524f..bf0953c 100644 --- a/_states/postgres_tablespace.py +++ b/_states/postgres_tablespace.py @@ -79,7 +79,11 @@ def present(name, ret['result'] = False return ret # This isn't changeable, they need to remove/remake - if (not owner and not tblspaces[name]['Owner'] == owner): + if (owner and not tblspaces[name]['Owner'] == owner): + if __opts__['test']: + ret['result'] = None + ret['comment'] = 'Tablespace {0} owner to be altered'.format(name) + return ret if __salt__['postgres_ext.tablespace_alter'](name, new_owner=owner): ret['comment'] = 'Tablespace {0} owner changed'.format(name) ret['result'] = True From 997bc3563a27c6f51f946dee6d402dead9512088 Mon Sep 17 00:00:00 2001 From: Brian Jackson Date: Fri, 24 Apr 2015 13:15:59 -0500 Subject: [PATCH 21/21] Fix backward compat This should fix the backwards compatibility breakage. Missed in my testing because I had a pre-existing pillar setup (from an old way I did tablespace support). --- postgres/defaults.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/postgres/defaults.yaml b/postgres/defaults.yaml index fe9c3fa..5c4c27e 100644 --- a/postgres/defaults.yaml +++ b/postgres/defaults.yaml @@ -14,5 +14,6 @@ postgres: users: {} acls: [] databases: {} + tablespaces: {} postgresconf: "" pg_hba.conf: salt://postgres/pg_hba.conf