#!/bin/bash set -euo pipefail PROJECT_ID="pve-net-hang-watchdog" ORG_ID="xdev" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" MODE="install" REMOTE_NODE="" REMOTE_USER="root" LOCAL_MODE=0 show_help() { cat <] Options: -h, --help Show this help message -l, --local Run on localhost -u, --uninstall Uninstall instead of install --user Remote SSH user (default: root) 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}/systemd' '${remote_tmp}/config'" scp -q "${SCRIPT_DIR}/bin/pve-net-hang-watchdog.sh" "${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}/systemd/pve-net-hang-watchdog.service" "${target}:${remote_tmp}/systemd/" scp -q "${SCRIPT_DIR}/config/xdev-pve-net-hang-watchdog" "${target}:${remote_tmp}/config/" 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}/" } 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 "${PROJECT_ID} - ${MODE}" echo "================================" if [[ ${LOCAL_MODE} -eq 1 ]]; then if [[ "${MODE}" == "install" ]]; then run_local_install else run_local_uninstall fi exit 0 fi TARGET="${REMOTE_USER}@${REMOTE_NODE}" 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