#!/bin/bash # ModelGrid Uninstaller # # install.sh installs this script beside the executable, in the installation directory # (/opt/modelgrid unless --install-dir chose another). It removes the systemd service, the # command link and that installation directory: the executable, its third-party notices and # this script. The configuration in /etc/modelgrid is removed only on request; the cluster state # in /var/lib/modelgrid and the logs in /var/log/modelgrid stay. # # Usage: # sudo modelgrid uninstall # sudo bash /opt/modelgrid/uninstall.sh # # Environment (set by `modelgrid uninstall`): # REMOVE_CONFIG=yes|no remove /etc/modelgrid without asking # MODELGRID_CLI_CALL=true called by the CLI, which has already asked its questions set -euo pipefail if [[ "$EUID" -ne 0 ]]; then echo "Please run as root (sudo modelgrid uninstall or sudo bash $0)" exit 1 fi REMOVE_CONFIG="${REMOVE_CONFIG:-no}" MODELGRID_CLI_CALL="${MODELGRID_CLI_CALL:-}" SERVICE_FILE="/etc/systemd/system/modelgrid.service" CONFIG_DIR="/etc/modelgrid" BIN_LINKS=("/usr/local/bin/modelgrid" "/usr/bin/modelgrid") # The installation directory is the directory this script lives in. INSTALL_DIR="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd -P)" EXECUTABLE="$INSTALL_DIR/modelgrid" # Refuse to delete a directory that is not a ModelGrid installation, such as a source checkout. if [[ ! -f "$EXECUTABLE" ]]; then echo "Error: $INSTALL_DIR holds no ModelGrid executable (modelgrid); refusing to remove it." >&2 echo "Run the uninstall.sh that install.sh placed in the installation directory." >&2 exit 1 fi if [[ "$INSTALL_DIR" == "/" ]]; then echo "Error: refusing to remove /" >&2 exit 1 fi echo "ModelGrid Uninstaller" echo "=====================" echo "Removing ModelGrid installed in $INSTALL_DIR" # 1. The systemd service. if command -v systemctl >/dev/null 2>&1; then if systemctl is-active --quiet modelgrid.service 2>/dev/null; then echo "Stopping the ModelGrid service..." systemctl stop modelgrid.service fi if systemctl is-enabled --quiet modelgrid.service 2>/dev/null; then echo "Disabling the ModelGrid service..." systemctl disable modelgrid.service fi fi if [[ -f "$SERVICE_FILE" ]]; then echo "Removing $SERVICE_FILE" rm -f "$SERVICE_FILE" if command -v systemctl >/dev/null 2>&1; then systemctl daemon-reload fi fi # 2. The command links that point into this installation. for link in "${BIN_LINKS[@]}"; do if [[ -L "$link" ]]; then target="$(readlink -f "$link" || true)" if [[ "$target" == "$INSTALL_DIR/"* ]]; then echo "Removing $link" rm -f "$link" fi fi done # 3. The configuration, on request. if [[ "$REMOVE_CONFIG" == "yes" ]]; then echo "Removing $CONFIG_DIR" rm -rf "$CONFIG_DIR" elif [[ -z "$MODELGRID_CLI_CALL" && -d "$CONFIG_DIR" && -t 0 ]]; then read -r -p "Remove the ModelGrid configuration in $CONFIG_DIR? (y/N) " reply || reply="" if [[ "$reply" =~ ^[Yy]$ ]]; then echo "Removing $CONFIG_DIR" rm -rf "$CONFIG_DIR" fi fi # 4. The installation directory: the executable, its notices and this script. Bash has the # script open, so it finishes after the file is gone. echo "Removing $INSTALL_DIR" rm -rf -- "$INSTALL_DIR" echo echo "ModelGrid has been uninstalled." if [[ -d "$CONFIG_DIR" ]]; then echo "The configuration in $CONFIG_DIR was kept." fi if [[ -d /var/lib/modelgrid || -d /var/log/modelgrid ]]; then echo "Cluster state in /var/lib/modelgrid and logs in /var/log/modelgrid were kept; remove them by hand if no longer needed." fi