Skip to content

Commit

Permalink
Record packages installed by setup for uninstall
Browse files Browse the repository at this point in the history
  • Loading branch information
mgziminsky committed Apr 8, 2024
1 parent 22052eb commit c12d77c
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 97 deletions.
91 changes: 47 additions & 44 deletions automated install/basic-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2297,8 +2300,8 @@ FTLinstall() {

local ftlBranch

if [[ -f "/etc/pihole/ftlbranch" ]];then
ftlBranch=$(</etc/pihole/ftlbranch)
if [[ -f "${PI_HOLE_CONFIG_DIR}/ftlbranch" ]];then
ftlBranch=$(<${PI_HOLE_CONFIG_DIR}/ftlbranch)
else
ftlBranch="master"
fi
Expand Down Expand Up @@ -2483,8 +2486,8 @@ FTLcheckUpdate() {

local ftlBranch

if [[ -f "/etc/pihole/ftlbranch" ]];then
ftlBranch=$(</etc/pihole/ftlbranch)
if [[ -f "${PI_HOLE_CONFIG_DIR}/ftlbranch" ]];then
ftlBranch=$(<${PI_HOLE_CONFIG_DIR}/ftlbranch)
else
ftlBranch="master"
fi
Expand Down Expand Up @@ -2677,7 +2680,7 @@ main() {
# Display welcome dialogs
welcomeDialogs
# Create directory for Pi-hole storage
install -d -m 755 /etc/pihole/
install -d -m 755 ${PI_HOLE_CONFIG_DIR}/
# Determine available interfaces
get_available_interfaces
# Find interfaces and let the user choose one
Expand Down
115 changes: 62 additions & 53 deletions automated install/uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if [[ ${EUID} -eq 0 ]]; then
else
# Check if sudo is actually installed
# If it isn't, exit because the uninstall can not complete
if [ -x "$(command -v sudo)" ]; then
if is_command sudo; then
export SUDO="sudo"
else
echo -e " ${CROSS} ${str}
Expand All @@ -48,26 +48,32 @@ source "${setupVars}"
package_manager_detect

# Uninstall packages used by the Pi-hole
DEPS=("${INSTALLER_DEPS[@]}" "${PIHOLE_DEPS[@]}" "${OS_CHECK_DEPS[@]}")
if [[ "${INSTALL_WEB_SERVER}" == true ]]; then
# Install the Web dependencies
DEPS+=("${PIHOLE_WEB_DEPS[@]}")
declare -a DEPS
# shellcheck disable=SC2154 # defined in basic-install.sh
if [ -r "${pkgsFile}" ]; then
readarray -t DEPS < "${pkgsFile}"
else
DEPS=("${INSTALLER_DEPS[@]}" "${PIHOLE_DEPS[@]}" "${OS_CHECK_DEPS[@]}")
if [[ "${INSTALL_WEB_SERVER}" == true ]]; then
# Install the Web dependencies
DEPS+=("${PIHOLE_WEB_DEPS[@]}")
fi
fi

# Compatibility
if [ -x "$(command -v apt-get)" ]; then
if is_command apt-get; then
# Debian Family
PKG_REMOVE=("${PKG_MANAGER}" -y remove --purge)
package_check() {
dpkg-query -W -f='${Status}' "$1" 2>/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() {
Expand All @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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 ###########
Expand All @@ -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}"

0 comments on commit c12d77c

Please sign in to comment.