Last active
November 18, 2018 22:29
-
-
Save Sharpie/1e1bfc162498d102b5e38d367f642096 to your computer and use it in GitHub Desktop.
Clean up puppet-agent services left by a SYSV init script after upgrade to SystemD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# A reaper script that scans the PIDs of services belonging to the puppet-agent | |
# package. When invoked with --check, the script exits non-zero if any such | |
# PIDs are found and also executes `systemctl daemon-reload` if needed. When | |
# invoked with --clean, the script kills any such PIDs and re-starts the | |
# affected services. | |
PUPPET_SERVICES=(puppet pxp-agent mcollective) | |
PARENT=$PPID | |
ls_deleted() { | |
local pid exe | |
pgrep "${1?}" | while IFS= read -r pid; do | |
if [[ $pid -eq $PARENT ]]; then | |
# This script will be invoked from a puppet worker process that may be | |
# running from a deleted ruby executable. Don't kill our parent. | |
continue | |
fi | |
exe=$(readlink "/proc/${pid}/exe") | |
if [[ "${exe}" = *"deleted"* ]]; then | |
printf '%s\n' "${pid}" | |
fi | |
done | |
} | |
check() { | |
local systemctl_result zombie_services=() \ | |
systemctl_failed='Failed to get properties: Access denied' | |
systemctl_result=$(systemctl status "${PUPPET_SERVICES[@]}" 2>&1) | |
if [[ "${systemctl_result}" = *"${systemctl_failed}"* ]]; then | |
logger -s 'Executing systemctl-daemon reload to clear SELinux denial.' | |
# Make systemd aware of new el-7 unit files. | |
systemctl daemon-reload | |
fi | |
for service in "${PUPPET_SERVICES[@]}"; do | |
zombie_services+=($(ls_deleted "${service}")) | |
done | |
if [[ "${#zombie_services[@]}" -gt 0 ]]; then | |
logger -s "Found PIDs from removed el-6 package: ${zombie_services[*]}" | |
exit 1 | |
else | |
exit 0 | |
fi | |
} | |
clean() { | |
local zombie_services=() | |
for service in "${PUPPET_SERVICES[@]}"; do | |
zombie_services=($(ls_deleted "${service}")) | |
if [[ "${#zombie_services[@]}" -gt 0 ]]; then | |
logger -s "Sending SIGTERM to ${service} PIDs from obsolete el-6 package: ${zombie_services[*]}" | |
kill -TERM "${zombie_services[@]}" | |
logger -s "Re-starting ${service}." | |
systemctl restart "${service}" | |
fi | |
done | |
} | |
case "${1}" in | |
--check) | |
check | |
;; | |
--clean) | |
clean | |
;; | |
*) | |
printf 'This script must be called with a --check or --clean flag.\n' | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment