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 22, 2016
1 parent 1e87f91 commit b4edec9
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 51 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.
16 changes: 8 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,14 @@
#}

{# 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:
name: '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 +36,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
14 changes: 11 additions & 3 deletions postgres/macros.jinja
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{%- from "postgres/map.jinja" import postgres with context -%}

{%- macro format_kwargs(kwarg) -%}

{%- filter indent(4) %}
{%- for k, v in kwarg|dictsort() %}
- {{ k }}: {{ v }}
{%- endfor %}
{%- endfilter %}

{%- endmacro %}

{%- macro format_state(name, state, kwarg) %}

{%- do kwarg.update({'name': name}) %}
Expand All @@ -12,9 +22,7 @@

{{ state }}-{{ name }}:
{{ state }}.{{ ensure|default('present') }}:
{%- for k, v in kwarg|dictsort() %}
- {{ k }}: {{ v }}
{%- endfor %}
{{- format_kwargs(kwarg) }}

{%- endmacro %}

Expand Down
23 changes: 16 additions & 7 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,7 +12,11 @@ Arch:
pkg_dev: postgresql

Debian:
pkg_repo_file: /etc/apt/sources.list.d/pgdg.list
pkg_repo:
humanname: PostgreSQL Official Repository
key_url: 'https://www.postgresql.org/media/keys/ACCC4CF8.asc'
file: /etc/apt/sources.list.d/pgdg.list
pkg_repo_keyid: ACCC4CF8
pkg_dev: postgresql-server-dev-all
pkg_libpq_dev: libpq-dev

Expand All @@ -20,13 +27,15 @@ 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:
name: pgdg{{ release }}
humanname: PostgreSQL {{ version }} $releasever - $basearch
baseurl: 'https://download.postgresql.org/pub/repos/yum/{{ version }}/redhat/rhel-$releasever-$basearch'
gpgcheck: 1
gpgkey: 'https://download.postgresql.org/pub/repos/yum/RPM-GPG-KEY-PGDG-{{ release }}'

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

pkg_repo: pgdg{{ release }}
pkg_repo_humanname: PostgreSQL {{ version }} $releasever - $basearch
pkg_repo_url: https://download.postgresql.org/pub/repos/yum/{{ version }}/redhat/rhel-$releasever-$basearch
pkg: postgresql{{ release }}-server
pkg_client: postgresql{{ release }}
conf_dir: /var/lib/pgsql/{{ version }}/data
Expand Down
44 changes: 24 additions & 20 deletions postgres/upstream.sls
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
{%- from "postgres/map.jinja" import postgres with context %}
{%- from "postgres/map.jinja" import postgres with context -%}
{%- from "postgres/macros.jinja" import format_kwargs with context -%}
{%- if grains['os_family'] == 'Debian' -%}
{%- if 'pkg_repo' in postgres -%}
{%- if postgres.use_upstream_repo -%}
# Add upstream repository for your distro
install-postgresql-repo:
pkgrepo.managed:
- humanname: {{ postgres.pkg_repo_humanname }}
- name: {{ postgres.pkg_repo }}
- keyid: B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
- keyserver: keyserver.ubuntu.com
- file: {{ postgres.pkg_repo_file }}
{{- format_kwargs(postgres.pkg_repo) }}
{%- else -%}
# Remove the repo configuration (and GnuPG key) as requested
remove-postgresql-repo:
pkgrepo.absent:
- name: {{ postgres.pkg_repo.name }}
{%- if 'pkg_repo_keyid' in postgres %}
- keyid: {{ postgres.pkg_repo_keyid }}
{%- endif %}
{%- elif grains['os_family'] == 'RedHat' -%}
{%- endif -%}
{%- else -%}
# Notify that we don't manage this distro
install-postgresql-repo:
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
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
- require:
- file: install-postgresql-repo
test.show_notification:
- text: |
PostgreSQL does not provide package repository for {{ grains['osfinger'] }}
{%- endif %}

0 comments on commit b4edec9

Please sign in to comment.