diff --git a/README.rst b/README.rst index f28a0b19..b88bddd0 100644 --- a/README.rst +++ b/README.rst @@ -31,7 +31,7 @@ Creates such DB objects as: users, tablespaces, databases, schemas and extension See ``pillar.example`` file for details. ``postgres.python`` -------------------- +---------------------- Installs the PostgreSQL adapter for Python on Linux. @@ -83,11 +83,36 @@ The state relies on the ``postgres:use_upstream_repo`` Pillar value which could The ``postgres:version`` Pillar controls which version of the PostgreSQL packages should be installed from the upstream Linux repository. Defaults to ``9.5``. + +Removal states +=============== + +``postgres.dropped`` +-------------------- + +Meta state to remove Postgres software. By default the release specified, or installed by, the formula is targeted only. To target multiple releases, set pillar ``postgres.removal.multiple_releases: True``. + +``postgres.server.remove`` +------------------------ + +Remove server, lib, and contrib packages. The ``postgres.server.remove`` will retain data by default (no data loss) - set pillar ``postgres.remove.data: True`` to remove data and configuration directories also. + +``postgres.client.remove`` +------------------------ + +Remove client package. + +``postgres.dev.remove`` +---------------------- + +Remove development and python packages. + + Testing ======= -The postgres state was tested on MacOS (El Capitan 10.11.6) +The ``postgres`` state was tested on MacOS (El Capitan 10.11.6), and ``remove`` states on Ubuntu, Centos, and Fedora. -Testing is done with the ``kitchen-salt``. +Linux testing is done with the ``kitchen-salt``. ``kitchen converge`` -------------------- diff --git a/pillar.example b/pillar.example index 24b05568..1c273870 100644 --- a/pillar.example +++ b/pillar.example @@ -184,4 +184,9 @@ postgres: maintenance_db: db1 #postgis: {} + remove: + data: True + multiple_releases: True + releases: ['9.6', '10',] + # vim: ft=yaml ts=2 sts=2 sw=2 et diff --git a/postgres/client/remove.sls b/postgres/client/remove.sls new file mode 100644 index 00000000..751179c3 --- /dev/null +++ b/postgres/client/remove.sls @@ -0,0 +1,47 @@ +{%- from "postgres/map.jinja" import postgres with context -%} + +#remove release installed by formula +postgresql-client-removed: + pkg.removed: + - pkgs: + {% if postgres.pkg_client %} + - {{ postgres.pkg_client }} + {% endif %} + +{%- if postgres.remove.multiple_releases %} + #search for and cleandown multiple releases + + {% for release in postgres.remove.releases %} + {% if 'bin_dir' in postgres %} + {%- for bin in postgres.client_bins %} + {% set path = '/usr/pgsql-' + release|string + '/bin/' + bin %} + +postgresql{{ release }}-client-{{ bin }}-alternative-remove: + alternatives.remove: + - name: {{ bin }} + - path: {{ path }} + {% if grains.os in ('Fedora', 'CentOS',) %} + {# bypass bug #} + - onlyif: alternatives --display {{ bin }} + {% else %} + - onlyif: test -f {{ path }} + {% endif %} + - require_in: + - pkg: postgresql{{ release }}-client-pkgs-removed + {%- endfor %} + {%- endif %} + +postgresql{{ release }}-client-pkgs-removed: + pkg.purged: + - pkgs: + - postgresql + - postgresql-{{ release }} + - postgresql-{{ release|replace('.', '') }} + - postgresql{{ release }}-common + - postgresql{{ release }}-jdbc + - postgresql{{ release }} + - postgresql{{ release|replace('.', '') }} + + {% endfor %} + +{%- endif %} diff --git a/postgres/defaults.yaml b/postgres/defaults.yaml index 2f470fb9..71f473df 100644 --- a/postgres/defaults.yaml +++ b/postgres/defaults.yaml @@ -80,3 +80,8 @@ postgres: linux: #Alternatives system are disabled by a 'altpriority=0' pillar. altpriority: 0 + + remove: + data: False + multiple_releases: False + releases: ['9.2', '9.3', '9.4', '9.5', '9.6', '10',] diff --git a/postgres/dev.sls b/postgres/dev.sls index 93020d4e..47b8059f 100644 --- a/postgres/dev.sls +++ b/postgres/dev.sls @@ -23,7 +23,11 @@ postgresql-{{ bin }}-altinstall: - link: {{ salt['file.join']('/usr/bin', bin) }} - path: {{ path }} - priority: {{ postgres.linux.altpriority }} + {% if grains.os in ('Fedora', 'CentOS',) %} {# bypass bug #} + - onlyif: alternatives --display {{ bin }} + {% else %} - onlyif: test -f {{ path }} + {% endif %} {%- endfor %} {%- endif %} diff --git a/postgres/dev/remove.sls b/postgres/dev/remove.sls new file mode 100644 index 00000000..66f524a9 --- /dev/null +++ b/postgres/dev/remove.sls @@ -0,0 +1,55 @@ +{%- from "postgres/map.jinja" import postgres with context -%} + +# remove release installed by formula +postgresql-devel-removed: + pkg.removed: + - pkgs: + {% if postgres.pkg_dev %} + - {{ postgres.pkg_dev }} + {% endif %} + {% if postgres.pkg_libpq_dev %} + - {{ postgres.pkg_libpq_dev }} + {% endif %} + {% if postgres.pkg_python %} + - {{ postgres.pkg_python }} + {% endif %} + +{%- if postgres.remove.multiple_releases %} + #search for and cleandown multiple releases + + {% for release in postgres.remove.releases %} + {% if 'bin_dir' in postgres %} + {%- for bin in postgres.dev_bins %} + {% set path = '/usr/pgsql-' + release|string + '/bin/' + bin %} + +postgresql{{ release }}-devel-{{ bin }}-alternative-remove: + alternatives.remove: + - name: {{ bin }} + - path: {{ path }} + {% if grains.os in ('Fedora', 'CentOS',) %} + {# bypass bug #} + - onlyif: alternatives --display {{ bin }} + {% else %} + - onlyif: test -f {{ path }} + {% endif %} + - require_in: + - pkg: postgresql{{ release }}-devel-pkgs-removed + {%- endfor %} + {%- endif %} + +postgresql{{ release }}-devel-pkgs-removed: + pkg.purged: + - pkgs: + - postgresql-dev + - postgresql-dev-{{ release|replace('.', '') }} + - postgresql-server-dev + - postgresql-server-dev-{{ release|replace('.', '') }} + - postgresql{{ release }}-jdbc + - postgresql{{ release|replace('.', '') }}-jdbc + - postgresql-{{ release }} + - postgresql-{{ release|replace('.', '') }} + - {{ postgres.pkg_python or "postgresql-python" }} + + {% endfor %} + +{%- endif %} diff --git a/postgres/dropped.sls b/postgres/dropped.sls index 2297be63..c0a9226d 100644 --- a/postgres/dropped.sls +++ b/postgres/dropped.sls @@ -1,32 +1,5 @@ -{% from tpldir + "/map.jinja" import postgres with context %} -postgresql-dead: - service.dead: - - name: {{ postgres.service }} - -postgresql-removed: - pkg.removed: - - pkgs: - {% if postgres.pkg %} - - {{ postgres.pkg }} - {% endif %} - {% if postgres.pkg_client %} - - {{ postgres.pkg_client }} - {% endif %} - {% if postgres.pkg_dev %} - - {{ postgres.pkg_dev }} - {% endif %} - {% if postgres.pkg_libpq_dev %} - - {{ postgres.pkg_libpq_dev }} - {% endif %} - {% if postgres.pkgs_extra %} - {% for pkg in postgres.pkgs_extra %} - - {{ pkg }} - {% endfor %} - {% endif %} - -postgres-dir-absent: - file.absent: - - names: - - {{ postgres.conf_dir }} - - {{ postgres.data_dir }} +include: + - postgres.server.remove + - postgres.client.remove + - postgres.dev.remove diff --git a/postgres/osmap.yaml b/postgres/osmap.yaml index cfa60288..0e04e970 100644 --- a/postgres/osmap.yaml +++ b/postgres/osmap.yaml @@ -3,5 +3,7 @@ Fedora: pkg_repo: baseurl: 'https://download.postgresql.org/pub/repos/yum/{{ repo.version }}/fedora/fedora-$releasever-$basearch' + remove: + releases: ['9.4', '9.5', '9.6', '10',] # vim: ft=sls diff --git a/postgres/server/init.sls b/postgres/server/init.sls index a757056b..7ff492ab 100644 --- a/postgres/server/init.sls +++ b/postgres/server/init.sls @@ -36,10 +36,10 @@ postgresql-server: - group: wheel - require_in: - service: postgresql-running - {%- else %} + # Alternatives system. Make server binaries available in $PATH - {%- if 'bin_dir' in postgres and postgres.linux.altpriority %} + {%- elif 'bin_dir' in postgres and postgres.linux.altpriority %} {%- for bin in postgres.server_bins %} {%- set path = salt['file.join'](postgres.bin_dir, bin) %} @@ -56,9 +56,7 @@ postgresql-{{ bin }}-altinstall: - cmd: postgresql-cluster-prepared {%- endfor %} - {%- endif %} - -{%- endif %} + {%- endif %} postgresql-cluster-prepared: file.directory: diff --git a/postgres/server/remove.sls b/postgres/server/remove.sls new file mode 100644 index 00000000..eb739183 --- /dev/null +++ b/postgres/server/remove.sls @@ -0,0 +1,89 @@ +{%- from "postgres/map.jinja" import postgres with context %} + +postgresql-dead: + service.dead: + - name: {{ postgres.service }} + - enable: False + +postgresql-repo-removed: + pkgrepo.absent: + - name: {{ postgres.pkg_repo.name }} + {%- if 'pkg_repo_keyid' in postgres %} + - keyid: {{ postgres.pkg_repo_keyid }} + {%- endif %} + +#remove release installed by formula +postgresql-server-removed: + pkg.removed: + - pkgs: + {% if postgres.pkg %} + - {{ postgres.pkg }} + {% endif %} + {% if postgres.pkgs_extra %} + {% for pkg in postgres.pkgs_extra %} + - {{ pkg }} + {% endfor %} + {% endif %} + +{%- if postgres.remove.multiple_releases %} + #search for and cleandown multiple releases + + {% for release in postgres.remove.releases %} +postgresql{{ release }}-server-pkgs-removed: + pkg.purged: + - pkgs: + - {{ postgres.pkg if postgres.pkg else "postgresql" }} + - postgresql-server + - postgresql-libs + - postgresql-contrib + - postgresql-server-{{ release }} + - postgresql-libs-{{ release }} + - postgresql-contrib-{{ release }} + - postgresql{{ release }}-contrib + - postgresql{{ release }}-server + - postgresql{{ release }}-libs + - postgresql{{ release }}-contrib + - postgresql{{ release|replace('.', '') }}-contrib + - postgresql{{ release|replace('.', '') }}-server + - postgresql{{ release|replace('.', '') }}-libs + - postgresql{{ release|replace('.', '') }}-contrib + + {% if 'bin_dir' in postgres %} + {% for bin in postgres.server_bins %} + {% set path = '/usr/pgsql-' + release|string + '/bin/' + bin %} + +postgresql{{ release }}-server-{{ bin }}-alternative-remove: + alternatives.remove: + - name: {{ bin }} + - path: {{ path }} + {% if grains.os in ('Fedora', 'CentOS',) %} + {# bypass bug #} + - onlyif: alternatives --display {{ bin }} + {% else %} + - onlyif: test -f {{ path }} + {% endif %} + + {% endfor %} + {% endif %} + + {%- if postgres.remove.data %} + #allow data loss? default is no +postgresql{{ release }}-dataconf-removed: + file.absent: + - names: + - {{ postgres.conf_dir }} + - {{ postgres.data_dir }} + - /var/lib/postgresql + - /var/lib/pgsql + + {% for name, tblspace in postgres.tablespaces|dictsort() %} +postgresql{{ release }}-tablespace-dir-{{ name }}-removed: + file.absent: + - name: {{ tblspace.directory }} + - require: + - file: postgresql{{ release }}-dataconf-removed + {% endfor %} + {% endif %} + + {% endfor %} +{%- endif %}