project-nomad/install/uninstall_nomad.sh
2026-01-15 15:54:59 -08:00

135 lines
6.6 KiB
Bash

#!/bin/bash
# Project N.O.M.A.D. Uninstall Script
###################################################################################################################################################################################################
# Script | Project N.O.M.A.D. Uninstall Script
# Version | 1.0.0
# Author | Crosstalk Solutions, LLC
# Website | https://crosstalksolutions.com
###################################################################################################################################################################################################
# #
# Constants & Variables #
# #
###################################################################################################################################################################################################
NOMAD_DIR="/opt/project-nomad"
MANAGEMENT_COMPOSE_FILE="${NOMAD_DIR}/compose.yml"
COLLECT_DISK_INFO_PID="/var/run/nomad-collect-disk-info.pid"
DISK_INFO_FILE="/tmp/nomad-disk-info.json"
###################################################################################################################################################################################################
# #
# Functions #
# #
###################################################################################################################################################################################################
check_has_sudo() {
if sudo -n true 2>/dev/null; then
echo -e "${GREEN}#${RESET} User has sudo permissions.\\n"
else
echo "User does not have sudo permissions"
header_red
echo -e "${RED}#${RESET} This script requires sudo permissions to run. Please run the script with sudo.\\n"
echo -e "${RED}#${RESET} For example: sudo bash $(basename "$0")"
exit 1
fi
}
check_current_directory(){
if [ "$(pwd)" == "${NOMAD_DIR}" ]; then
echo "Please run this script from a directory other than ${NOMAD_DIR}."
exit 1
fi
}
ensure_management_compose_file_exists(){
if [ ! -f "${MANAGEMENT_COMPOSE_FILE}" ]; then
echo "Unable to find the management Docker Compose file at ${MANAGEMENT_COMPOSE_FILE}. There may be a problem with your Project N.O.M.A.D. installation."
exit 1
fi
}
get_uninstall_confirmation(){
read -p "This script will remove ALL Project N.O.M.A.D. files and containers. THIS CANNOT BE UNDONE. Are you sure you want to continue? (y/n): " choice
case "$choice" in
y|Y )
echo -e "User chose to continue with the uninstallation."
;;
n|N )
echo -e "User chose not to continue with the uninstallation."
exit 0
;;
* )
echo "Invalid Response"
echo "User chose not to continue with the uninstallation."
exit 0
;;
esac
}
ensure_docker_installed() {
if ! command -v docker &> /dev/null; then
echo "Unable to find Docker. There may be a problem with your Docker installation."
exit 1
fi
}
try_remove_disk_info_script() {
echo "Checking for running collect-disk-info script..."
if [ -f "$COLLECT_DISK_INFO_PID" ]; then
echo "Stopping collect-disk-info script..."
kill "$(cat "$COLLECT_DISK_INFO_PID")"
rm -f "$COLLECT_DISK_INFO_PID"
echo "collect-disk-info script stopped."
fi
}
try_remove_disk_info_file() {
if [ -f "$DISK_INFO_FILE" ]; then
echo "Removing disk info file..."
rm -f "$DISK_INFO_FILE"
echo "Disk info file removed."
fi
}
uninstall_nomad() {
echo "Stopping and removing Project N.O.M.A.D. management containers..."
docker compose -p project-nomad -f "${MANAGEMENT_COMPOSE_FILE}" down
echo "Allowing some time for management containers to stop..."
sleep 5
# Stop and remove all containers where name starts with "nomad_"
echo "Stopping and removing all Project N.O.M.A.D. app containers..."
docker ps -a --filter "name=^nomad_" --format "{{.Names}}" | xargs -r docker rm -f
echo "Allowing some time for app containers to stop..."
sleep 5
echo "Containers should be stopped now."
# Try to stop the collect-disk-info script if it's running
try_remove_disk_info_script
# Try to remove the disk info file if it exists
try_remove_disk_info_file
echo "Removing Project N.O.M.A.D. files..."
rm -rf "${NOMAD_DIR}"
echo "Project N.O.M.A.D. has been uninstalled. We hope to see you again soon!"
}
###################################################################################################################################################################################################
# #
# Main #
# #
###################################################################################################################################################################################################
check_has_sudo
check_current_directory
ensure_management_compose_file_exists
ensure_docker_installed
get_uninstall_confirmation
uninstall_nomad