forked from CCCHH/ansible-infra
Vendor Galaxy Roles and Collections
This commit is contained in:
parent
c1e1897cda
commit
2aed20393f
3553 changed files with 387444 additions and 2 deletions
|
|
@ -0,0 +1,19 @@
|
|||
debops.dpkg_cleanup - Prepare a dpkg cleanup hook using Ansible
|
||||
|
||||
Copyright (C) 2020 Maciej Delmanowski <drybjed@gmail.com>
|
||||
Copyright (C) 2020 DebOps <https://debops.org/>
|
||||
SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
This Ansible role is part of DebOps.
|
||||
|
||||
DebOps is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 3, as
|
||||
published by the Free Software Foundation.
|
||||
|
||||
DebOps is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with DebOps. If not, see https://www.gnu.org/licenses/.
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
---
|
||||
# .. vim: foldmarker=[[[,]]]:foldmethod=marker
|
||||
|
||||
# .. Copyright (C) 2020 Maciej Delmanowski <drybjed@gmail.com>
|
||||
# .. Copyright (C) 2020 DebOps <https://debops.org/>
|
||||
# .. SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
# .. _dpkg_cleanup__ref_defaults:
|
||||
|
||||
# debops.dpkg_cleanup default variables
|
||||
# =====================================
|
||||
|
||||
# .. contents:: Sections
|
||||
# :local:
|
||||
#
|
||||
# .. include:: ../../../../includes/global.rst
|
||||
|
||||
|
||||
# Global options [[[
|
||||
# ------------------
|
||||
|
||||
# .. envvar:: dpkg_cleanup__enabled [[[
|
||||
#
|
||||
# Enable or disable support for the role. By default it's only enabled on hosts
|
||||
# which use the APT package manager with assumption that :command:`dpkg`
|
||||
# command is present.
|
||||
dpkg_cleanup__enabled: '{{ True if (ansible_pkg_mgr == "apt") else False }}'
|
||||
# ]]]
|
||||
# ]]]
|
||||
# Configuration paths [[[
|
||||
# -----------------------
|
||||
|
||||
# .. envvar:: dpkg_cleanup__facts_path [[[
|
||||
#
|
||||
# Absolute path where Ansible local fact scripts are stored. The hook script
|
||||
# will automatically remove the fact created by a given role.
|
||||
dpkg_cleanup__facts_path: '/etc/ansible/facts.d'
|
||||
|
||||
# ]]]
|
||||
# .. envvar:: dpkg_cleanup__scripts_path [[[
|
||||
#
|
||||
# Absolute path where the dpkg hook scripts are stored.
|
||||
dpkg_cleanup__scripts_path: '/usr/local/lib/dpkg-cleanup'
|
||||
|
||||
# ]]]
|
||||
# .. envvar:: dpkg_cleanup__hooks_path [[[
|
||||
#
|
||||
# Absolute path where :command:`dpkg` configuration snippets are stored.
|
||||
dpkg_cleanup__hooks_path: '/etc/dpkg/dpkg.cfg.d'
|
||||
# ]]]
|
||||
# ]]]
|
||||
# Hook script definitions [[[
|
||||
# ---------------------------
|
||||
|
||||
# .. envvar:: dpkg_cleanup__dependent_packages [[[
|
||||
#
|
||||
# List which sets the contents of the hook scripts generated by the role,
|
||||
# defined by other Ansible roles via role dependent variables.
|
||||
# See :ref:`dpkg_cleanup__ref_packages` for more details.
|
||||
dpkg_cleanup__dependent_packages: []
|
||||
# ]]]
|
||||
# ]]]
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
---
|
||||
# Copyright (C) 2020 Maciej Delmanowski <drybjed@gmail.com>
|
||||
# Copyright (C) 2020-2022 DebOps <https://debops.org/>
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
# Ensure that custom Ansible plugins and modules included in the main DebOps
|
||||
# collection are available to roles in other collections.
|
||||
collections: [ 'debops.debops' ]
|
||||
|
||||
dependencies: []
|
||||
|
||||
galaxy_info:
|
||||
|
||||
author: 'Maciej Delmanowski'
|
||||
description: 'Prepare a dpkg cleanup hook executed on package removal'
|
||||
company: 'DebOps'
|
||||
license: 'GPL-3.0-only'
|
||||
min_ansible_version: '2.9.0'
|
||||
|
||||
platforms:
|
||||
|
||||
- name: 'Ubuntu'
|
||||
versions: [ 'all' ]
|
||||
|
||||
- name: 'Debian'
|
||||
versions: [ 'all' ]
|
||||
|
||||
galaxy_tags:
|
||||
- cleanup
|
||||
- system
|
||||
- dpkg
|
||||
- apt
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
---
|
||||
# Copyright (C) 2020 Maciej Delmanowski <drybjed@gmail.com>
|
||||
# Copyright (C) 2020 DebOps <https://debops.org/>
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
- name: Ensure that the cleanup script directory exists
|
||||
ansible.builtin.file:
|
||||
path: '{{ dpkg_cleanup__scripts_path }}'
|
||||
state: 'directory'
|
||||
mode: '0755'
|
||||
when: dpkg_cleanup__enabled | bool
|
||||
|
||||
- name: Remove cleanup scripts if requested
|
||||
ansible.builtin.file:
|
||||
path: '{{ dpkg_cleanup__scripts_path + "/" + item.name }}'
|
||||
state: 'absent'
|
||||
loop: '{{ q("flattened", dpkg_cleanup__dependent_packages) }}'
|
||||
loop_control:
|
||||
label: '{{ {"package": item.name} }}'
|
||||
when: dpkg_cleanup__enabled | bool and item.name | d() and
|
||||
item.state | d('present') == 'absent'
|
||||
|
||||
- name: Remove cleanup hooks if requested
|
||||
ansible.builtin.file:
|
||||
path: '{{ dpkg_cleanup__hooks_path + "/dpkg-cleanup-" + item.name }}'
|
||||
state: 'absent'
|
||||
loop: '{{ q("flattened", dpkg_cleanup__dependent_packages) }}'
|
||||
loop_control:
|
||||
label: '{{ {"package": item.name} }}'
|
||||
when: dpkg_cleanup__enabled | bool and item.name | d() and
|
||||
item.state | d('present') == 'absent'
|
||||
|
||||
- name: Generate cleanup scripts
|
||||
ansible.builtin.template:
|
||||
src: 'usr/local/lib/dpkg-cleanup/package.j2'
|
||||
dest: '{{ dpkg_cleanup__scripts_path + "/" + item.name }}'
|
||||
mode: '0755'
|
||||
loop: '{{ q("flattened", dpkg_cleanup__dependent_packages) }}'
|
||||
loop_control:
|
||||
label: '{{ {"package": item.name} }}'
|
||||
when: dpkg_cleanup__enabled | bool and item.name | d() and
|
||||
item.state | d('present') != 'absent'
|
||||
|
||||
- name: Generate cleanup hooks
|
||||
ansible.builtin.template:
|
||||
src: 'etc/dpkg/dpkg.cfg.d/dpkg-cleanup-package.j2'
|
||||
dest: '{{ dpkg_cleanup__hooks_path + "/dpkg-cleanup-" + item.name }}'
|
||||
mode: '0644'
|
||||
loop: '{{ q("flattened", dpkg_cleanup__dependent_packages) }}'
|
||||
loop_control:
|
||||
label: '{{ {"package": item.name} }}'
|
||||
when: dpkg_cleanup__enabled | bool and item.name | d() and
|
||||
item.state | d('present') != 'absent'
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
{# Copyright (C) 2020 Maciej Delmanowski <drybjed@gmail.com>
|
||||
# Copyright (C) 2020 DebOps <https://debops.org/>
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
#}
|
||||
# {{ ansible_managed }}
|
||||
|
||||
pre-invoke={{ dpkg_cleanup__scripts_path + "/" + item.name }}
|
||||
|
|
@ -0,0 +1,164 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# {{ ansible_managed }}
|
||||
|
||||
# Copyright (C) 2020 Maciej Delmanowski <drybjed@gmail.com>
|
||||
# Copyright (C) 2020 DebOps <https://debops.org/>
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
# Clean up Ansible modifications on the '{{ item.name }}' package removal
|
||||
# The script is executed by dpkg pre-invoke hook
|
||||
|
||||
|
||||
set -o nounset -o pipefail -o errexit
|
||||
|
||||
# Name of the package which will trigger the hook script
|
||||
apt_package="{{ item.name }}"
|
||||
|
||||
# List of files which should be reverted via the dpkg-divert script
|
||||
readarray -t revert_files << EOF
|
||||
{% if item.revert_files | d() %}
|
||||
{% for path in (([ item.revert_files ]
|
||||
if (item.revert_files is string)
|
||||
else item.revert_files)
|
||||
| flatten | sort) %}
|
||||
{% if path.startswith("/") %}
|
||||
{{ path }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
EOF
|
||||
|
||||
# List of files which should be removed
|
||||
readarray -t remove_files << EOF
|
||||
{% if item.remove_files | d() %}
|
||||
{% for path in (([ item.remove_files ]
|
||||
if (item.remove_files is string)
|
||||
else item.remove_files)
|
||||
| flatten | sort) %}
|
||||
{% if path.startswith("/") %}
|
||||
{{ path }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{{ dpkg_cleanup__facts_path + "/" + (item.ansible_fact | d(item.name)) + ".fact" }}
|
||||
{{ dpkg_cleanup__scripts_path + "/" + item.name }}
|
||||
{{ dpkg_cleanup__hooks_path + "/dpkg-cleanup-" + item.name }}
|
||||
EOF
|
||||
|
||||
# List of directories which should be removed
|
||||
readarray -t remove_directories << EOF
|
||||
{% if item.remove_directories | d() %}
|
||||
{% for path in (([ item.remove_directories ]
|
||||
if (item.remove_directories is string)
|
||||
else item.remove_directories)
|
||||
| flatten | sort) %}
|
||||
{% if path.startswith("/") %}
|
||||
{{ path }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
EOF
|
||||
|
||||
# List of service names which should be reloaded via 'systemctl' command
|
||||
readarray -t reload_services << EOF
|
||||
{% if item.reload_services | d() %}
|
||||
{% for service in (([ item.reload_services ]
|
||||
if (item.reload_services is string)
|
||||
else item.reload_services)
|
||||
| flatten | sort) %}
|
||||
{{ service }}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
EOF
|
||||
|
||||
# List of service names which should be restarted via 'systemctl' command
|
||||
readarray -t restart_services << EOF
|
||||
{% if item.restart_services | d() %}
|
||||
{% for service in (([ item.restart_services ]
|
||||
if (item.restart_services is string)
|
||||
else item.restart_services)
|
||||
| flatten | sort) %}
|
||||
{{ service }}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
EOF
|
||||
|
||||
|
||||
divert_cleanup () {
|
||||
for revert_file in "${revert_files[@]}" ; do
|
||||
if [ -f "${revert_file}.dpkg-divert" ] ; then
|
||||
rm -fv "${revert_file}"
|
||||
dpkg-divert --local --rename --remove "${revert_file}"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
file_cleanup () {
|
||||
for remove_file in "${remove_files[@]}" ; do
|
||||
test ! -f "${remove_file}" || rm -fv "${remove_file}"
|
||||
done
|
||||
}
|
||||
|
||||
directory_cleanup () {
|
||||
for remove_directory in "${remove_directories[@]}" ; do
|
||||
test ! -d "${remove_directory}" || rm -rfv "${remove_directory}"
|
||||
done
|
||||
}
|
||||
|
||||
service_reload () {
|
||||
for service in "${reload_services[@]}" ; do
|
||||
if pidof systemd > /dev/null 2>&1 ; then
|
||||
if systemctl is-active "${service}" > /dev/null 2>&1 ; then
|
||||
printf "Reloading %s service ...\\n" "${service}"
|
||||
systemctl reload "${service}.service"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
service_restart () {
|
||||
for service in "${restart_services[@]}" ; do
|
||||
if pidof systemd > /dev/null 2>&1 ; then
|
||||
if systemctl is-active "${service}" > /dev/null 2>&1 ; then
|
||||
printf "Restarting %s service ...\\n" "${service}"
|
||||
systemctl restart "${service}.service"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
# Get the PID of the parent 'dpkg' process and check its command line arguments
|
||||
# to get the list of packages currently being worked on
|
||||
dpkg_pid="$(ps -o ppid= ${PPID} | tr -d '[:space:]')"
|
||||
IFS=' ' read -r -a arguments <<< "$(tr '\0' ' ' < "/proc/${dpkg_pid}/cmdline")"
|
||||
|
||||
if [ -n "${DPKG_HOOK_ACTION:-}" ] ; then
|
||||
case ${DPKG_HOOK_ACTION} in
|
||||
purge|remove)
|
||||
for x in "${arguments[@]}"; do
|
||||
case ${x} in
|
||||
${apt_package}:*)
|
||||
|
||||
if [ -n "${revert_files[*]}" ] ; then
|
||||
divert_cleanup
|
||||
fi
|
||||
if [ -n "${remove_files[*]}" ] ; then
|
||||
file_cleanup
|
||||
fi
|
||||
if [ -n "${remove_directories[*]}" ] ; then
|
||||
directory_cleanup
|
||||
fi
|
||||
if [ -n "${reload_services[*]}" ] ; then
|
||||
service_reload
|
||||
fi
|
||||
if [ -n "${restart_services[*]}" ] ; then
|
||||
service_restart
|
||||
fi
|
||||
|
||||
;;
|
||||
esac
|
||||
done
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
Loading…
Add table
Add a link
Reference in a new issue