Newer Older
f16725e 3 months ago History
153 lines | 4.138kb
Bogdan Timofte authored 3 months ago
1
#!/bin/bash
2

            
3
set -euo pipefail
4

            
5
PROJECT_ID="pve-guests-state"
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
PGS 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

            
25
Examples:
26
  $0 --local
27
  $0 pve-node-2
28
  $0 --user admin pve-node-2
29
  $0 --uninstall pve-node-2
30
  $0 --local --uninstall
31
EOF
32
}
33

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

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

            
41
    if [[ -x "${canonical}" ]]; then
42
        "${canonical}"
43
    else
44
        bash "${SCRIPT_DIR}/scripts/uninstall.sh"
45
    fi
46
}
47

            
48
copy_remote_tree() {
49
    local target="$1"
50
    local remote_tmp="$2"
51

            
52
    ssh "${target}" "rm -rf '${remote_tmp}' && mkdir -p '${remote_tmp}/bin' '${remote_tmp}/scripts' '${remote_tmp}/docs'"
53
    scp -q "${SCRIPT_DIR}/bin/pgs" "${target}:${remote_tmp}/bin/"
54
    scp -q "${SCRIPT_DIR}/scripts/install.sh" "${target}:${remote_tmp}/scripts/"
55
    scp -q "${SCRIPT_DIR}/scripts/uninstall.sh" "${target}:${remote_tmp}/scripts/"
56
    scp -q "${SCRIPT_DIR}/README.md" "${target}:${remote_tmp}/"
57
    scp -q "${SCRIPT_DIR}/INSTALL.md" "${target}:${remote_tmp}/"
58
    scp -q "${SCRIPT_DIR}/CHANGELOG.md" "${target}:${remote_tmp}/"
59
    scp -q "${SCRIPT_DIR}/LICENSE" "${target}:${remote_tmp}/"
60
    scp -q "${SCRIPT_DIR}/docs/DECISIONS.md" "${target}:${remote_tmp}/docs/"
61
    scp -q "${SCRIPT_DIR}/docs/TECHNICAL.md" "${target}:${remote_tmp}/docs/"
62
}
63

            
64
run_remote_install() {
65
    local target="$1"
66
    local remote_tmp="/tmp/${PROJECT_ID}.$$"
67
    local remote_prefix=""
68

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

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

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

            
81
    ssh "${target}" "rm -rf '${remote_tmp}' && mkdir -p '${remote_tmp}/scripts'"
82
    scp -q "${SCRIPT_DIR}/scripts/uninstall.sh" "${target}:${remote_tmp}/scripts/"
83
    if [[ "${REMOTE_USER}" == "root" ]]; then
84
        ssh "${target}" "if [ -x '${canonical}' ]; then '${canonical}'; else bash '${remote_tmp}/scripts/uninstall.sh'; fi"
85
    else
86
        ssh "${target}" "sudo bash -lc \"if [ -x '${canonical}' ]; then '${canonical}'; else bash '${remote_tmp}/scripts/uninstall.sh'; fi\""
87
    fi
88
    ssh "${target}" "rm -rf '${remote_tmp}'"
89
}
90

            
91
while [[ $# -gt 0 ]]; do
92
    case "$1" in
93
        -h|--help)
94
            show_help
95
            exit 0
96
            ;;
97
        -l|--local)
98
            LOCAL_MODE=1
99
            shift
100
            ;;
101
        -u|--uninstall)
102
            MODE="uninstall"
103
            shift
104
            ;;
105
        --user)
106
            REMOTE_USER="$2"
107
            shift 2
108
            ;;
109
        -*)
110
            echo "ERROR: unknown option: $1" >&2
111
            show_help
112
            exit 1
113
            ;;
114
        *)
115
            REMOTE_NODE="$1"
116
            shift
117
            ;;
118
    esac
119
done
120

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

            
125
echo "================================"
126
echo "PGS - ${MODE}"
127
echo "================================"
128

            
129
if [[ ${LOCAL_MODE} -eq 1 ]]; then
130
    echo "Target: localhost"
131
    echo ""
132
    if [[ "${MODE}" == "install" ]]; then
133
        run_local_install
134
    else
135
        run_local_uninstall
136
    fi
137
    exit 0
138
fi
139

            
140
TARGET="${REMOTE_USER}@${REMOTE_NODE}"
141
echo "Target: ${TARGET}"
142
echo ""
143

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

            
149
if [[ "${MODE}" == "install" ]]; then
150
    run_remote_install "${TARGET}"
151
else
152
    run_remote_uninstall "${TARGET}"
153
fi