| 1 |
#!/bin/bash |
|
| 2 | ||
| 3 |
set -euo pipefail |
|
| 4 | ||
| 5 |
PROJECT_ID="pve-net-hang-watchdog" |
|
| 6 |
ORG_ID="xdev" |
|
| 7 |
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
| 8 |
MODE="install" |
|
| 9 |
REMOTE_NODE="" |
|
| 10 |
REMOTE_USER="root" |
|
| 11 |
LOCAL_MODE=0 |
|
| 12 | ||
| 13 |
show_help() {
|
|
| 14 |
cat <<EOF |
|
| 15 |
${PROJECT_ID} setup wrapper
|
|
| 16 | ||
| 17 |
Usage: $0 [OPTIONS] [<target_node>] |
|
| 18 | ||
| 19 |
Options: |
|
| 20 |
-h, --help Show this help message |
|
| 21 |
-l, --local Run on localhost |
|
| 22 |
-u, --uninstall Uninstall instead of install |
|
| 23 |
--user <user> Remote SSH user (default: root) |
|
| 24 |
EOF |
|
| 25 |
} |
|
| 26 | ||
| 27 |
run_local_install() {
|
|
| 28 |
bash "${SCRIPT_DIR}/scripts/install.sh" --source-dir "${SCRIPT_DIR}"
|
|
| 29 |
} |
|
| 30 | ||
| 31 |
run_local_uninstall() {
|
|
| 32 |
local canonical="/usr/local/lib/${ORG_ID}/${PROJECT_ID}/uninstall.sh"
|
|
| 33 |
if [[ -x "${canonical}" ]]; then
|
|
| 34 |
"${canonical}"
|
|
| 35 |
else |
|
| 36 |
bash "${SCRIPT_DIR}/scripts/uninstall.sh"
|
|
| 37 |
fi |
|
| 38 |
} |
|
| 39 | ||
| 40 |
copy_remote_tree() {
|
|
| 41 |
local target="$1" |
|
| 42 |
local remote_tmp="$2" |
|
| 43 | ||
| 44 |
ssh "${target}" "rm -rf '${remote_tmp}' && mkdir -p '${remote_tmp}/bin' '${remote_tmp}/scripts' '${remote_tmp}/systemd' '${remote_tmp}/config'"
|
|
| 45 |
scp -q "${SCRIPT_DIR}/bin/pve-net-hang-watchdog.sh" "${target}:${remote_tmp}/bin/"
|
|
| 46 |
scp -q "${SCRIPT_DIR}/scripts/install.sh" "${target}:${remote_tmp}/scripts/"
|
|
| 47 |
scp -q "${SCRIPT_DIR}/scripts/uninstall.sh" "${target}:${remote_tmp}/scripts/"
|
|
| 48 |
scp -q "${SCRIPT_DIR}/systemd/pve-net-hang-watchdog.service" "${target}:${remote_tmp}/systemd/"
|
|
| 49 |
scp -q "${SCRIPT_DIR}/config/xdev-pve-net-hang-watchdog" "${target}:${remote_tmp}/config/"
|
|
| 50 |
scp -q "${SCRIPT_DIR}/README.md" "${target}:${remote_tmp}/"
|
|
| 51 |
scp -q "${SCRIPT_DIR}/INSTALL.md" "${target}:${remote_tmp}/"
|
|
| 52 |
scp -q "${SCRIPT_DIR}/CHANGELOG.md" "${target}:${remote_tmp}/"
|
|
| 53 |
} |
|
| 54 | ||
| 55 |
run_remote_install() {
|
|
| 56 |
local target="$1" |
|
| 57 |
local remote_tmp="/tmp/${PROJECT_ID}.$$"
|
|
| 58 |
local remote_prefix="" |
|
| 59 | ||
| 60 |
[[ "${REMOTE_USER}" != "root" ]] && remote_prefix="sudo "
|
|
| 61 | ||
| 62 |
copy_remote_tree "${target}" "${remote_tmp}"
|
|
| 63 |
ssh "${target}" "${remote_prefix}bash '${remote_tmp}/scripts/install.sh' --source-dir '${remote_tmp}'"
|
|
| 64 |
ssh "${target}" "rm -rf '${remote_tmp}'"
|
|
| 65 |
} |
|
| 66 | ||
| 67 |
run_remote_uninstall() {
|
|
| 68 |
local target="$1" |
|
| 69 |
local remote_tmp="/tmp/${PROJECT_ID}-uninstall.$$"
|
|
| 70 |
local canonical="/usr/local/lib/${ORG_ID}/${PROJECT_ID}/uninstall.sh"
|
|
| 71 | ||
| 72 |
ssh "${target}" "rm -rf '${remote_tmp}' && mkdir -p '${remote_tmp}/scripts'"
|
|
| 73 |
scp -q "${SCRIPT_DIR}/scripts/uninstall.sh" "${target}:${remote_tmp}/scripts/"
|
|
| 74 |
if [[ "${REMOTE_USER}" == "root" ]]; then
|
|
| 75 |
ssh "${target}" "if [ -x '${canonical}' ]; then '${canonical}'; else bash '${remote_tmp}/scripts/uninstall.sh'; fi"
|
|
| 76 |
else |
|
| 77 |
ssh "${target}" "sudo bash -lc \"if [ -x '${canonical}' ]; then '${canonical}'; else bash '${remote_tmp}/scripts/uninstall.sh'; fi\""
|
|
| 78 |
fi |
|
| 79 |
ssh "${target}" "rm -rf '${remote_tmp}'"
|
|
| 80 |
} |
|
| 81 | ||
| 82 |
while [[ $# -gt 0 ]]; do |
|
| 83 |
case "$1" in |
|
| 84 |
-h|--help) |
|
| 85 |
show_help |
|
| 86 |
exit 0 |
|
| 87 |
;; |
|
| 88 |
-l|--local) |
|
| 89 |
LOCAL_MODE=1 |
|
| 90 |
shift |
|
| 91 |
;; |
|
| 92 |
-u|--uninstall) |
|
| 93 |
MODE="uninstall" |
|
| 94 |
shift |
|
| 95 |
;; |
|
| 96 |
--user) |
|
| 97 |
REMOTE_USER="$2" |
|
| 98 |
shift 2 |
|
| 99 |
;; |
|
| 100 |
-*) |
|
| 101 |
echo "ERROR: unknown option: $1" >&2 |
|
| 102 |
show_help |
|
| 103 |
exit 1 |
|
| 104 |
;; |
|
| 105 |
*) |
|
| 106 |
REMOTE_NODE="$1" |
|
| 107 |
shift |
|
| 108 |
;; |
|
| 109 |
esac |
|
| 110 |
done |
|
| 111 | ||
| 112 |
if [[ -z "${REMOTE_NODE}" && ${LOCAL_MODE} -eq 0 ]]; then
|
|
| 113 |
LOCAL_MODE=1 |
|
| 114 |
fi |
|
| 115 | ||
| 116 |
echo "================================" |
|
| 117 |
echo "${PROJECT_ID} - ${MODE}"
|
|
| 118 |
echo "================================" |
|
| 119 | ||
| 120 |
if [[ ${LOCAL_MODE} -eq 1 ]]; then
|
|
| 121 |
if [[ "${MODE}" == "install" ]]; then
|
|
| 122 |
run_local_install |
|
| 123 |
else |
|
| 124 |
run_local_uninstall |
|
| 125 |
fi |
|
| 126 |
exit 0 |
|
| 127 |
fi |
|
| 128 | ||
| 129 |
TARGET="${REMOTE_USER}@${REMOTE_NODE}"
|
|
| 130 |
if ! ping -c 1 "${REMOTE_NODE}" >/dev/null 2>&1; then
|
|
| 131 |
echo "ERROR: cannot reach ${REMOTE_NODE}" >&2
|
|
| 132 |
exit 1 |
|
| 133 |
fi |
|
| 134 | ||
| 135 |
if [[ "${MODE}" == "install" ]]; then
|
|
| 136 |
run_remote_install "${TARGET}"
|
|
| 137 |
else |
|
| 138 |
run_remote_uninstall "${TARGET}"
|
|
| 139 |
fi |