Skip to content

Commit

Permalink
Ensure upstream repo is configured or absent depending on `use_upstre…
Browse files Browse the repository at this point in the history
…am_repo`
  • Loading branch information
Denys Havrysh committed Sep 20, 2016
1 parent b972237 commit 855e31b
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 61 deletions.
36 changes: 25 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,54 @@ Available states
``postgres``
------------

Installs the postgresql package.
Installs the PostgreSQL server package and prepares the DB cluster.

``postgres.python``
``postgres.client``
-------------------

Installs the postgresql python module
Installs the PostgreSQL client binaries and libraries.

``postgres.client``
``postgres.python``
-------------------

Installs the postgresql client
Installs the PostgreSQL adapter for Python.

``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:

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

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

Testing
=======

Testing is done wit kitchen-salt
Testing is done with the ``kitchen-salt``.

``kitchen converge``
--------------------

Runs the postgres main state
Runs the ``postgres`` main state.

``kitchen verify``
------------------

Runs serverspec tests on the actual instance
Runs ``serverspec`` tests on the actual instance.

``kitchen test``
----------------

Builds and runs test from scratch
Builds and runs tests from scratch.

``kitchen login``
-----------------

Gives you ssh to the vagrant machine for manual testing

Gives you ssh to the vagrant machine for manual testing.
25 changes: 13 additions & 12 deletions postgres/client.sls
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{% from "postgres/map.jinja" import postgres with context %}
{%- from "postgres/map.jinja" import postgres with context -%}
{%- set pkgs = [] %}
{%- for pkg in (postgres.pkg_client, postgres.pkg_libpq_dev) %}
{%- if pkg %}
{%- do pkgs.append(pkg) %}
{%- endif %}
{%- endfor -%}
{% if postgres.use_upstream_repo %}
include:
- postgres.upstream
{% endif %}
install-postgresql-client:
pkg.installed:
- name: {{ postgres.pkg_client }}
- refresh: {{ postgres.use_upstream_repo }}
{% if postgres.pkg_libpq_dev %}
install-postgres-libpq-dev:
postgresql-client:
pkg.installed:
- name: {{ postgres.pkg_libpq_dev }}
{% endif %}
- pkgs: {{ pkgs }}
- refresh: {{ 'pkg_repo' in postgres }}
- require:
- sls: postgres.upstream
15 changes: 7 additions & 8 deletions postgres/codenamemap.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
### Set parameters based on PostgreSQL version supplied with particular distro

{% set use_upstream_repo = salt['pillar.get']('postgres:use_upstream_repo', false) %}
{% set upstream_version = salt['pillar.get']('postgres:version', '9.5') %}

{% macro debian_codename(name, version, codename=none) %}
{#
Generate lookup dictionary map for Debian and derivative distributions
Expand All @@ -14,12 +17,13 @@
#}

{# use upstream version if configured #}
{% set version = upstream_version|default(version) %}
{% if use_upstream_repo %}
{% set version = upstream_version %}
{% endif %}

{{ codename|default(name, true) }}:
# PostgreSQL packages are mostly downloaded from `main` repo component
pkg_repo: 'deb http://apt.postgresql.org/pub/repos/apt/ {{ name }}-pgdg main {{ version }}'
pkg_repo_humanname: PostgreSQL Official Repository
pkg_repo: 'deb http://apt.postgresql.org/pub/repos/apt {{ name }}-pgdg main {{ upstream_version }}'
pkg: postgresql-{{ version }}
pkg_client: postgresql-client-{{ version }}
conf_dir: /etc/postgresql/{{ version }}/main
Expand All @@ -31,11 +35,6 @@

{% endmacro %}

{% if salt['pillar.get']('postgres:use_upstream_repo', false) %}
{# upstream version will always override all versions given below #}
{% set upstream_version = salt['pillar.get']('postgres:version', '9.5') %}
{% endif %}

## Debian GNU/Linux
{{ debian_codename('wheezy', '9.1') }}
{{ debian_codename('jessie', '9.4') }}
Expand Down
4 changes: 2 additions & 2 deletions postgres/defaults.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
postgres:
use_upstream_repo: False
pkg: postgresql
pkgs_extra: []
pkg_client: postgresql-client
pkg_dev: postgresql-devel
pkg_libpq_dev: postgresql-libs
pkg_client: postgresql-client
pkgs_extra: []
python: python-psycopg2
user: postgres
group: postgres
Expand Down
12 changes: 5 additions & 7 deletions postgres/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
{%- from "postgres/map.jinja" import postgres with context -%}
{%- from "postgres/macros.jinja" import format_state with context -%}

{%- if postgres.use_upstream_repo %}

include:
- postgres.upstream

{%- endif %}

### Installation states

postgresql-installed:
postgresql-server:
pkg.installed:
- name: {{ postgres.pkg }}
- refresh: {{ postgres.use_upstream_repo }}
- refresh: {{ 'pkg_repo' in postgres }}
- require:
- sls: postgres.upstream

# make sure the data directory and contents have been initialized
postgresql-cluster-prepared:
Expand All @@ -27,7 +25,7 @@ postgresql-cluster-prepared:
- unless:
- {{ postgres.prepare_cluster.test }}
- require:
- pkg: postgresql-installed
- pkg: postgresql-server

postgresql-config-dir:
file.directory:
Expand Down
21 changes: 16 additions & 5 deletions postgres/osmap.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{% set version = salt['pillar.get']('postgres:version', '9.5') %}
{% set release = version|replace('.', '') %}

Arch:
conf_dir: /var/lib/postgres/data
prepare_cluster:
Expand All @@ -9,6 +12,9 @@ Arch:
pkg_dev: postgresql

Debian:
pkg_repo_humanname: PostgreSQL Official Repository
pkg_repo_keyid: ACCC4CF8
pkg_repo_keyurl: 'https://www.postgresql.org/media/keys/ACCC4CF8.asc'
pkg_repo_file: /etc/apt/sources.list.d/pgdg.list
pkg_dev: postgresql-server-dev-all
pkg_libpq_dev: libpq-dev
Expand All @@ -20,13 +26,18 @@ OpenBSD:
user: _postgresql

RedHat:
{% if salt['pillar.get']('postgres:use_upstream_repo', False) %}
{% set version = salt['pillar.get']('postgres:version', '9.5') %}
{% set release = version|replace('.', '') %}

pkg_repo: pgdg{{ release }}
pkg_repo_keyfile: /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG-{{ release }}
pkg_repo_keyurl:
- 'https://download.postgresql.org/pub/repos/yum/RPM-GPG-KEY-PGDG-{{ release }}'
# Fallback key URL
- 'https://download.postgresql.org/pub/repos/yum/RPM-GPG-KEY-PGDG'
pkg_repo_keyhash: md5=78b5db170d33f80ad5a47863a7476b22
pkg_repo_humanname: PostgreSQL {{ version }} $releasever - $basearch
pkg_repo_url: https://download.postgresql.org/pub/repos/yum/{{ version }}/redhat/rhel-$releasever-$basearch
pkg_repo_url: 'https://download.postgresql.org/pub/repos/yum/{{ version }}/redhat/rhel-$releasever-$basearch'

{% if salt['pillar.get']('postgres:use_upstream_repo', false) %}

pkg: postgresql{{ release }}-server
pkg_client: postgresql{{ release }}
conf_dir: /var/lib/pgsql/{{ version }}/data
Expand Down
55 changes: 39 additions & 16 deletions postgres/upstream.sls
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
{%- from "postgres/map.jinja" import postgres with context %}
{%- from "postgres/map.jinja" import postgres with context -%}
{%- if grains['os_family'] == 'Debian' -%}
{%- if not postgres.use_upstream_repo and 'pkg_repo' in postgres -%}
install-postgresql-repo:
postgres-repo:
pkgrepo.absent:
- name: {{ postgres.pkg_repo }}
{%- if 'pkg_repo_keyid' in postgres %}
- keyid: {{ postgres.pkg_repo_keyid }}
{%- endif %}
{%- if 'pkg_repo_keyfile' in postgres %}
postgresql-repo-keyfile:
file.absent:
- name: {{ postgres.pkg_repo_keyfile }}
- require:
- pkgrepo: postgres-repo
{%- endif %}
{%- elif grains['os_family'] == 'Debian' -%}
postgresql-repo:
pkgrepo.managed:
- humanname: {{ postgres.pkg_repo_humanname }}
- name: {{ postgres.pkg_repo }}
- keyid: B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
- keyserver: keyserver.ubuntu.com
- humanname: {{ postgres.pkg_repo_humanname }}
- key_url: {{ postgres.pkg_repo_keyurl }}
- file: {{ postgres.pkg_repo_file }}
- require_in:
- pkg: postgresql-installed
{%- elif grains['os_family'] == 'RedHat' -%}
install-postgresql-repo:
postgresql-repo-keyfile:
file.managed:
- name: /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
- source: https://download.postgresql.org/pub/repos/yum/RPM-GPG-KEY-PGDG
- source_hash: md5=78b5db170d33f80ad5a47863a7476b22
- name: {{ postgres.pkg_repo_keyfile }}
- source: {{ postgres.pkg_repo_keyurl }}
- source_hash: {{ postgres.pkg_repo_keyhash }}
postgresql-repo:
pkgrepo.managed:
- name: {{ postgres.pkg_repo }}
- humanname: {{ postgres.pkg_repo_humanname }}
- baseurl: {{ postgres.pkg_repo_url }}
- gpgcheck: 1
- gpgkey: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
- gpgkey: 'file://{{ postgres.pkg_repo_keyfile }}'
- require:
- file: install-postgresql-repo
- require_in:
- pkg: postgresql-installed
- file: postgresql-repo-keyfile
{%- else -%}
postgresql-repo:
test.show_notification:
- text: |
PostgreSQL does not provide package repository for {{ grains['os_family'] }}
{%- endif %}

0 comments on commit 855e31b

Please sign in to comment.