f16725e 3 months ago History
1 contributor
153 lines | 4.138kb
#!/bin/bash

set -euo pipefail

PROJECT_ID="pve-guests-state"
ORG_ID="xdev"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MODE="install"
REMOTE_NODE=""
REMOTE_USER="root"
LOCAL_MODE=0

show_help() {
    cat <<EOF
PGS setup wrapper

Usage: $0 [OPTIONS] [<target_node>]

Options:
  -h, --help           Show this help message
  -l, --local          Run on localhost
  -u, --uninstall      Uninstall instead of install
  --user <user>        Remote SSH user (default: root)

Examples:
  $0 --local
  $0 pve-node-2
  $0 --user admin pve-node-2
  $0 --uninstall pve-node-2
  $0 --local --uninstall
EOF
}

run_local_install() {
    bash "${SCRIPT_DIR}/scripts/install.sh" --source-dir "${SCRIPT_DIR}"
}

run_local_uninstall() {
    local canonical="/usr/local/lib/${ORG_ID}/${PROJECT_ID}/uninstall.sh"

    if [[ -x "${canonical}" ]]; then
        "${canonical}"
    else
        bash "${SCRIPT_DIR}/scripts/uninstall.sh"
    fi
}

copy_remote_tree() {
    local target="$1"
    local remote_tmp="$2"

    ssh "${target}" "rm -rf '${remote_tmp}' && mkdir -p '${remote_tmp}/bin' '${remote_tmp}/scripts' '${remote_tmp}/docs'"
    scp -q "${SCRIPT_DIR}/bin/pgs" "${target}:${remote_tmp}/bin/"
    scp -q "${SCRIPT_DIR}/scripts/install.sh" "${target}:${remote_tmp}/scripts/"
    scp -q "${SCRIPT_DIR}/scripts/uninstall.sh" "${target}:${remote_tmp}/scripts/"
    scp -q "${SCRIPT_DIR}/README.md" "${target}:${remote_tmp}/"
    scp -q "${SCRIPT_DIR}/INSTALL.md" "${target}:${remote_tmp}/"
    scp -q "${SCRIPT_DIR}/CHANGELOG.md" "${target}:${remote_tmp}/"
    scp -q "${SCRIPT_DIR}/LICENSE" "${target}:${remote_tmp}/"
    scp -q "${SCRIPT_DIR}/docs/DECISIONS.md" "${target}:${remote_tmp}/docs/"
    scp -q "${SCRIPT_DIR}/docs/TECHNICAL.md" "${target}:${remote_tmp}/docs/"
}

run_remote_install() {
    local target="$1"
    local remote_tmp="/tmp/${PROJECT_ID}.$$"
    local remote_prefix=""

    [[ "${REMOTE_USER}" != "root" ]] && remote_prefix="sudo "

    copy_remote_tree "${target}" "${remote_tmp}"
    ssh "${target}" "${remote_prefix}bash '${remote_tmp}/scripts/install.sh' --source-dir '${remote_tmp}'"
    ssh "${target}" "rm -rf '${remote_tmp}'"
}

run_remote_uninstall() {
    local target="$1"
    local remote_tmp="/tmp/${PROJECT_ID}-uninstall.$$"
    local canonical="/usr/local/lib/${ORG_ID}/${PROJECT_ID}/uninstall.sh"

    ssh "${target}" "rm -rf '${remote_tmp}' && mkdir -p '${remote_tmp}/scripts'"
    scp -q "${SCRIPT_DIR}/scripts/uninstall.sh" "${target}:${remote_tmp}/scripts/"
    if [[ "${REMOTE_USER}" == "root" ]]; then
        ssh "${target}" "if [ -x '${canonical}' ]; then '${canonical}'; else bash '${remote_tmp}/scripts/uninstall.sh'; fi"
    else
        ssh "${target}" "sudo bash -lc \"if [ -x '${canonical}' ]; then '${canonical}'; else bash '${remote_tmp}/scripts/uninstall.sh'; fi\""
    fi
    ssh "${target}" "rm -rf '${remote_tmp}'"
}

while [[ $# -gt 0 ]]; do
    case "$1" in
        -h|--help)
            show_help
            exit 0
            ;;
        -l|--local)
            LOCAL_MODE=1
            shift
            ;;
        -u|--uninstall)
            MODE="uninstall"
            shift
            ;;
        --user)
            REMOTE_USER="$2"
            shift 2
            ;;
        -*)
            echo "ERROR: unknown option: $1" >&2
            show_help
            exit 1
            ;;
        *)
            REMOTE_NODE="$1"
            shift
            ;;
    esac
done

if [[ -z "${REMOTE_NODE}" && ${LOCAL_MODE} -eq 0 ]]; then
    LOCAL_MODE=1
fi

echo "================================"
echo "PGS - ${MODE}"
echo "================================"

if [[ ${LOCAL_MODE} -eq 1 ]]; then
    echo "Target: localhost"
    echo ""
    if [[ "${MODE}" == "install" ]]; then
        run_local_install
    else
        run_local_uninstall
    fi
    exit 0
fi

TARGET="${REMOTE_USER}@${REMOTE_NODE}"
echo "Target: ${TARGET}"
echo ""

if ! ping -c 1 "${REMOTE_NODE}" >/dev/null 2>&1; then
    echo "ERROR: cannot reach ${REMOTE_NODE}" >&2
    exit 1
fi

if [[ "${MODE}" == "install" ]]; then
    run_remote_install "${TARGET}"
else
    run_remote_uninstall "${TARGET}"
fi