From c12d77c20db2cd7148850e764b6921247dbb3dc6 Mon Sep 17 00:00:00 2001 From: "Michael Ziminsky (Z)" Date: Thu, 4 Apr 2024 22:50:30 -0700 Subject: [PATCH] Record packages installed by setup for uninstall --- automated install/basic-install.sh | 91 ++++++++++++----------- automated install/uninstall.sh | 115 ++++++++++++++++------------- 2 files changed, 109 insertions(+), 97 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index 2496727bf4..78643d9273 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -53,10 +53,20 @@ Cloudflare (DNSSEC);1.1.1.1;1.0.0.1;2606:4700:4700::1111;2606:4700:4700::1001 EOM ) +declare -a INSTALLED + +# This directory is where the Pi-hole scripts will be installed +PI_HOLE_INSTALL_DIR="/opt/pihole" +PI_HOLE_CONFIG_DIR="/etc/pihole" +PI_HOLE_LOCAL_REPO="/etc/.pihole" +PI_HOLE_BIN_DIR="/usr/local/bin" + # Location for final installation log storage -installLogLoc="/etc/pihole/install.log" +installLogLoc="${PI_HOLE_CONFIG_DIR}/install.log" # This is an important file as it contains information specific to the machine it's being installed on -setupVars="/etc/pihole/setupVars.conf" +setupVars="${PI_HOLE_CONFIG_DIR}/setupVars.conf" +# File listing the packages installed by this script +pkgsFile="${PI_HOLE_CONFIG_DIR}/setup-installed.list" # Pi-hole uses lighttpd as a Web server, and this is the config file for it lighttpdConfig="/etc/lighttpd/lighttpd.conf" # This is a file used for the colorized output @@ -74,19 +84,14 @@ webroot="/var/www/html" webInterfaceGitUrl="https://github.com/pi-hole/web.git" webInterfaceDir="${webroot}/admin" piholeGitUrl="https://github.com/mgziminsky/pi-hole-alpine.git" -PI_HOLE_LOCAL_REPO="/etc/.pihole" # List of pihole scripts, stored in an array PI_HOLE_FILES=(chronometer list piholeDebug piholeLogFlush setupLCD update version gravity uninstall webpage) -# This directory is where the Pi-hole scripts will be installed -PI_HOLE_INSTALL_DIR="/opt/pihole" -PI_HOLE_CONFIG_DIR="/etc/pihole" -PI_HOLE_BIN_DIR="/usr/local/bin" FTL_CONFIG_FILE="${PI_HOLE_CONFIG_DIR}/pihole-FTL.conf" if [ -z "$useUpdateVars" ]; then useUpdateVars=false fi -adlistFile="/etc/pihole/adlists.list" +adlistFile="${PI_HOLE_CONFIG_DIR}/adlists.list" # Pi-hole needs an IP address; to begin, these variables are empty since we don't know what the IP is until this script can run IPV4_ADDRESS=${IPV4_ADDRESS} IPV6_ADDRESS=${IPV6_ADDRESS} @@ -1248,7 +1253,7 @@ version_check_dnsmasq() { # Local, named variables local dnsmasq_conf="/etc/dnsmasq.conf" local dnsmasq_conf_orig="/etc/dnsmasq.conf.orig" - local dnsmasq_pihole_id_string="addn-hosts=/etc/pihole/gravity.list" + local dnsmasq_pihole_id_string="addn-hosts=${PI_HOLE_CONFIG_DIR}/gravity.list" local dnsmasq_pihole_id_string2="# Dnsmasq config for Pi-hole's FTLDNS" local dnsmasq_original_config="${PI_HOLE_LOCAL_REPO}/advanced/dnsmasq.conf.original" local dnsmasq_pihole_01_source="${PI_HOLE_LOCAL_REPO}/advanced/01-pihole.conf" @@ -1386,6 +1391,10 @@ installConfigs() { echo "${DNS_SERVERS}" > "${PI_HOLE_CONFIG_DIR}/dns-servers.conf" chmod 644 "${PI_HOLE_CONFIG_DIR}/dns-servers.conf" + # Save or update the list of packages installed by this script + [[ $useUpdateVars == false ]] && rm -f "${pkgsFile}" + printf "%s\n" "${INSTALLED[@]}" >> "${pkgsFile}" + # Install template file if it does not exist if [[ ! -r "${FTL_CONFIG_FILE}" ]]; then install -d -m 0755 ${PI_HOLE_CONFIG_DIR} @@ -1722,18 +1731,13 @@ install_dependent_packages() { printf '%*s\n' "${c}" '' | tr " " -; "${PKG_INSTALL[@]}" "${installArray[@]}" printf '%*s\n' "${c}" '' | tr " " -; - return fi - printf "\\n" - return 0 - fi - # Install Alpine packages - if is_command apk ; then + elif is_command apk ; then # For each package, check if it's already installed (and if so, don't add it to the installArray) for i in "$@"; do printf " %b Checking for %s..." "${INFO}" "${i}" - if "${PKG_MANAGER}" info -e "${i}" &> /dev/null; then + if "${PKG_MANAGER}" info -qe "${i}"; then printf "%b %b Checking for %s\\n" "${OVER}" "${TICK}" "${i}" else printf "%b %b Checking for %s (will be installed)\\n" "${OVER}" "${INFO}" "${i}" @@ -1767,32 +1771,31 @@ install_dependent_packages() { enable_service cronie || true restart_service cronie fi - - return fi - printf "\\n" - return 0 - fi - # Install Fedora/CentOS packages - for i in "$@"; do - # For each package, check if it's already installed (and if so, don't add it to the installArray) - printf " %b Checking for %s..." "${INFO}" "${i}" - if "${PKG_MANAGER}" -q list installed "${i}" &> /dev/null; then - printf "%b %b Checking for %s\\n" "${OVER}" "${TICK}" "${i}" - else - printf "%b %b Checking for %s (will be installed)\\n" "${OVER}" "${INFO}" "${i}" - installArray+=("${i}") + else + for i in "$@"; do + # For each package, check if it's already installed (and if so, don't add it to the installArray) + printf " %b Checking for %s..." "${INFO}" "${i}" + if "${PKG_MANAGER}" -q list installed "${i}" &> /dev/null; then + printf "%b %b Checking for %s\\n" "${OVER}" "${TICK}" "${i}" + else + printf "%b %b Checking for %s (will be installed)\\n" "${OVER}" "${INFO}" "${i}" + installArray+=("${i}") + fi + done + # If there's anything to install, install everything in the list. + if [[ "${#installArray[@]}" -gt 0 ]]; then + printf " %b Processing %s install(s) for: %s, please wait...\\n" "${INFO}" "${PKG_MANAGER}" "${installArray[*]}" + printf '%*s\n' "${c}" '' | tr " " -; + "${PKG_INSTALL[@]}" "${installArray[@]}" + printf '%*s\n' "${c}" '' | tr " " -; + return fi - done - # If there's anything to install, install everything in the list. - if [[ "${#installArray[@]}" -gt 0 ]]; then - printf " %b Processing %s install(s) for: %s, please wait...\\n" "${INFO}" "${PKG_MANAGER}" "${installArray[*]}" - printf '%*s\n' "${c}" '' | tr " " -; - "${PKG_INSTALL[@]}" "${installArray[@]}" - printf '%*s\n' "${c}" '' | tr " " -; - return fi + + INSTALLED+=("${installArray[@]}") + printf "\\n" return 0 } @@ -1947,7 +1950,7 @@ finalExports() { # Install the logrotate script installLogrotate() { local str="Installing latest logrotate script" - local target=/etc/pihole/logrotate + local target=${PI_HOLE_CONFIG_DIR}/logrotate printf "\\n %b %s..." "${INFO}" "${str}" if [[ -f ${target} ]]; then @@ -2297,8 +2300,8 @@ FTLinstall() { local ftlBranch - if [[ -f "/etc/pihole/ftlbranch" ]];then - ftlBranch=$(/dev/null | grep -c "ok installed" } -elif [ -x "$(command -v rpm)" ]; then +elif is_command rpm; then # Fedora Family PKG_REMOVE=("${PKG_MANAGER}" remove -y) package_check() { rpm -qa | grep "^$1-" > /dev/null } -elif [ -x "$(command -v apk)" ]; then +elif is_command apk; then # Alpine Family PKG_REMOVE=("${PKG_MANAGER}" del) package_check() { @@ -79,10 +85,16 @@ else fi removeAndPurge() { + # Call removeNoPurge to remove Pi-hole specific files + removeNoPurge + # Purge dependencies echo "" + if [ -n "$1" ]; then + ${SUDO} "${PKG_REMOVE[@]}" "${DEPS[@]}" + else for i in "${DEPS[@]}"; do - if package_check "${i}" > /dev/null; then + if package_check "${i}" &> /dev/null; then while true; do read -rp " ${QST} Do you wish to remove ${COL_WHITE}${i}${COL_NC} from your system? [Y/N] " answer case ${answer} in @@ -98,26 +110,31 @@ removeAndPurge() { echo -e " ${INFO} Package ${i} not installed" fi done + fi # Remove dnsmasq config files ${SUDO} rm -f /etc/dnsmasq.conf /etc/dnsmasq.conf.orig /etc/dnsmasq.d/*-pihole*.conf &> /dev/null echo -e " ${TICK} Removing dnsmasq config files" - - # Call removeNoPurge to remove Pi-hole specific files - removeNoPurge } removeNoPurge() { + # Remove FTL + if is_command pihole-FTL &> /dev/null; then + echo -ne " ${INFO} Removing pihole-FTL..." + stop_service pihole-FTL + fi + # Only web directories/files that are created by Pi-hole should be removed echo -ne " ${INFO} Removing Web Interface..." - ${SUDO} rm -rf /var/www/html/admin &> /dev/null - ${SUDO} rm -rf /var/www/html/pihole &> /dev/null - ${SUDO} rm -f /var/www/html/index.lighttpd.orig &> /dev/null + # shellcheck disable=SC2154 # defined in basic-install.sh + ${SUDO} rm -rf "${webroot}/admin" &> /dev/null + ${SUDO} rm -rf "${webroot}/pihole" &> /dev/null + ${SUDO} rm -f "${webroot}/index.lighttpd.orig" &> /dev/null # If the web directory is empty after removing these files, then the parent html directory can be removed. - if [ -d "/var/www/html" ]; then - if [[ ! "$(ls -A /var/www/html)" ]]; then - ${SUDO} rm -rf /var/www/html &> /dev/null + if [ -d "${webroot}" ]; then + if [[ ! "$(ls -A "${webroot}")" ]]; then + ${SUDO} rm -rf "${webroot}" &> /dev/null fi fi echo -e "${OVER} ${TICK} Removed Web Interface" @@ -180,10 +197,10 @@ removeNoPurge() { ${SUDO} rm -f /etc/dnsmasq.d/06-rfc6761.conf &> /dev/null ${SUDO} rm -rf /var/log/*pihole* &> /dev/null ${SUDO} rm -rf /var/log/pihole/*pihole* &> /dev/null - ${SUDO} rm -rf /etc/pihole/ &> /dev/null - ${SUDO} rm -rf /etc/.pihole/ &> /dev/null - ${SUDO} rm -rf /opt/pihole/ &> /dev/null - ${SUDO} rm -f /usr/local/bin/pihole &> /dev/null + ${SUDO} rm -rf "${PI_HOLE_CONFIG_DIR}" &> /dev/null + ${SUDO} rm -rf "${PI_HOLE_FILES_DIR}" &> /dev/null + ${SUDO} rm -rf "${PI_HOLE_INSTALL_DIR}" &> /dev/null + ${SUDO} rm -f "${PI_HOLE_BIN_DIR}"/pihole &> /dev/null ${SUDO} rm -f /etc/bash_completion.d/pihole &> /dev/null ${SUDO} rm -f /etc/sudoers.d/pihole &> /dev/null echo -e " ${TICK} Removed config files" @@ -194,30 +211,21 @@ removeNoPurge() { systemctl reload-or-restart systemd-resolved fi - # Remove FTL - if command -v pihole-FTL &> /dev/null; then - echo -ne " ${INFO} Removing pihole-FTL..." - if [[ -x "$(command -v systemctl)" ]]; then - systemctl stop pihole-FTL - else - service pihole-FTL stop - fi - ${SUDO} rm -f /etc/systemd/system/pihole-FTL.service - if [[ -d '/etc/systemd/system/pihole-FTL.service.d' ]]; then - read -rp " ${QST} FTL service override directory /etc/systemd/system/pihole-FTL.service.d detected. Do you wish to remove this from your system? [y/N] " answer - case $answer in - [yY]*) - echo -ne " ${INFO} Removing /etc/systemd/system/pihole-FTL.service.d..." - ${SUDO} rm -R /etc/systemd/system/pihole-FTL.service.d - echo -e "${OVER} ${INFO} Removed /etc/systemd/system/pihole-FTL.service.d" - ;; - *) echo -e " ${INFO} Leaving /etc/systemd/system/pihole-FTL.service.d in place.";; - esac - fi - ${SUDO} rm -f /etc/init.d/pihole-FTL - ${SUDO} rm -f /usr/bin/pihole-FTL - echo -e "${OVER} ${TICK} Removed pihole-FTL" + ${SUDO} rm -f /etc/systemd/system/pihole-FTL.service + if [[ -d '/etc/systemd/system/pihole-FTL.service.d' ]]; then + read -rp " ${QST} FTL service override directory /etc/systemd/system/pihole-FTL.service.d detected. Do you wish to remove this from your system? [y/N] " answer + case $answer in + [yY]*) + echo -ne " ${INFO} Removing /etc/systemd/system/pihole-FTL.service.d..." + ${SUDO} rm -R /etc/systemd/system/pihole-FTL.service.d + echo -e "${OVER} ${INFO} Removed /etc/systemd/system/pihole-FTL.service.d" + ;; + *) echo -e " ${INFO} Leaving /etc/systemd/system/pihole-FTL.service.d in place.";; + esac fi + ${SUDO} rm -f /etc/init.d/pihole-FTL + ${SUDO} rm -f /usr/bin/pihole-FTL + echo -e "${OVER} ${TICK} Removed pihole-FTL" # If the pihole manpage exists, then delete and rebuild man-db if [[ -f /usr/local/share/man/man8/pihole.8 ]]; then @@ -242,13 +250,6 @@ removeNoPurge() { echo -e " ${CROSS} Unable to remove 'pihole' group" fi fi - - echo -e "\\n We're sorry to see you go, but thanks for checking out Pi-hole! - If you need help, reach out to us on GitHub, Discourse, Reddit or Twitter - Reinstall at any time: ${COL_WHITE}curl -sSL https://install.pi-hole.net | bash${COL_NC} - - ${COL_LIGHT_RED}Please reset the DNS on your router/clients to restore internet connectivity - ${COL_LIGHT_GREEN}Uninstallation Complete! ${COL_NC}" } ######### SCRIPT ########### @@ -260,10 +261,18 @@ while true; do echo -n "${i} " done echo "${COL_NC}" - read -rp " ${QST} Do you wish to go through each dependency for removal? (Choosing No will leave all dependencies installed) [Y/n] " answer + read -rp " ${QST} Do you wish to uninstall dependencies? ('Yes' will prompt for each, 'No' will leave all dependencies installed, 'All' will remove all) [Y/n/a] " answer case ${answer} in + [Aa]* ) removeAndPurge force; break;; [Yy]* ) removeAndPurge; break;; [Nn]* ) removeNoPurge; break;; * ) removeAndPurge; break;; esac done + +echo -e "\\n We're sorry to see you go, but thanks for checking out Pi-hole! + If you need help, reach out to us on GitHub, Discourse, Reddit or Twitter + Reinstall at any time: ${COL_WHITE}curl -sSL https://install.pi-hole.net | bash${COL_NC} + + ${COL_LIGHT_RED}Please reset the DNS on your router/clients to restore internet connectivity + ${COL_LIGHT_GREEN}Uninstallation Complete! ${COL_NC}"