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

            
3
set -euo pipefail
4

            
5
PROJECT_ID="thunderbolts"
6
ORG_ID="xdev"
7
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8
MODE="install"
9
REMOTE_USER="root"
10
LOCAL_MODE=0
11
TARGETS=()
12

            
13
get_mgmt_ip() {
14
    case "$1" in
15
        baobab) echo "192.168.2.91" ;;
16
        ebony) echo "192.168.2.92" ;;
17
        tapia) echo "192.168.2.93" ;;
18
        *) echo "" ;;
19
    esac
20
}
21

            
22
resolve_target() {
23
    local host="$1"
24
    local ip=""
25

            
26
    if [[ "$host" == *@* ]]; then
27
        echo "$host"
28
        return 0
29
    fi
30

            
31
    ip="$(get_mgmt_ip "$host")"
32
    if [[ -n "$ip" ]]; then
33
        echo "${REMOTE_USER}@${ip}"
34
    else
35
        echo "${REMOTE_USER}@${host}"
36
    fi
37
}
38

            
39
show_help() {
40
    cat <<EOF
41
${PROJECT_ID} setup wrapper
42

            
43
Usage: $0 [OPTIONS] [host...]
44

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

            
51
Without explicit hosts, remote mode defaults to: baobab ebony tapia
52
EOF
53
}
54

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

            
59
run_local_uninstall() {
60
    local canonical="/usr/local/lib/${ORG_ID}/${PROJECT_ID}/uninstall.sh"
61
    if [[ -x "${canonical}" ]]; then
62
        "${canonical}"
63
    else
64
        bash "${SCRIPT_DIR}/scripts/uninstall.sh"
65
    fi
66
}
67

            
68
copy_remote_tree() {
69
    local target="$1"
70
    local remote_tmp="$2"
71

            
72
    ssh "${target}" "rm -rf '${remote_tmp}' && mkdir -p '${remote_tmp}/scripts' '${remote_tmp}/deploy/attempt1/common/sbin' '${remote_tmp}/deploy/attempt1/common/systemd/system' '${remote_tmp}/deploy/attempt1/common/udev/rules.d'"
73
    scp -q "${SCRIPT_DIR}/scripts/install.sh" "${target}:${remote_tmp}/scripts/"
74
    scp -q "${SCRIPT_DIR}/scripts/uninstall.sh" "${target}:${remote_tmp}/scripts/"
75
    scp -q "${SCRIPT_DIR}/README.md" "${target}:${remote_tmp}/"
76
    scp -q "${SCRIPT_DIR}/INSTALL.md" "${target}:${remote_tmp}/"
77
    scp -q "${SCRIPT_DIR}/CHANGELOG.md" "${target}:${remote_tmp}/"
78
    scp -q "${SCRIPT_DIR}/deploy/attempt1/common/sbin/tb-recover.sh" "${target}:${remote_tmp}/deploy/attempt1/common/sbin/"
79
    scp -q "${SCRIPT_DIR}/deploy/attempt1/common/systemd/system/tb-bridge.service" "${target}:${remote_tmp}/deploy/attempt1/common/systemd/system/"
80
    scp -q "${SCRIPT_DIR}/deploy/attempt1/common/systemd/system/tb-enlist@.service" "${target}:${remote_tmp}/deploy/attempt1/common/systemd/system/"
81
    scp -q "${SCRIPT_DIR}/deploy/attempt1/common/systemd/system/tb-recover.service" "${target}:${remote_tmp}/deploy/attempt1/common/systemd/system/"
82
    scp -q "${SCRIPT_DIR}/deploy/attempt1/common/systemd/system/tb-recover.timer" "${target}:${remote_tmp}/deploy/attempt1/common/systemd/system/"
83
    scp -q "${SCRIPT_DIR}/deploy/attempt1/common/udev/rules.d/90-thunderbolt-net-systemd.rules" "${target}:${remote_tmp}/deploy/attempt1/common/udev/rules.d/"
84
}
85

            
86
run_remote_install() {
87
    local target="$1"
88
    local remote_tmp="/tmp/${PROJECT_ID}.$$"
89
    local remote_prefix=""
90

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

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

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

            
103
    ssh "${target}" "rm -rf '${remote_tmp}' && mkdir -p '${remote_tmp}/scripts'"
104
    scp -q "${SCRIPT_DIR}/scripts/uninstall.sh" "${target}:${remote_tmp}/scripts/"
105
    if [[ "${REMOTE_USER}" == "root" ]]; then
106
        ssh "${target}" "if [ -x '${canonical}' ]; then '${canonical}'; else bash '${remote_tmp}/scripts/uninstall.sh'; fi"
107
    else
108
        ssh "${target}" "sudo bash -lc \"if [ -x '${canonical}' ]; then '${canonical}'; else bash '${remote_tmp}/scripts/uninstall.sh'; fi\""
109
    fi
110
    ssh "${target}" "rm -rf '${remote_tmp}'"
111
}
112

            
113
while [[ $# -gt 0 ]]; do
114
    case "$1" in
115
        -h|--help)
116
            show_help
117
            exit 0
118
            ;;
119
        -l|--local)
120
            LOCAL_MODE=1
121
            shift
122
            ;;
123
        -u|--uninstall)
124
            MODE="uninstall"
125
            shift
126
            ;;
127
        --user)
128
            REMOTE_USER="$2"
129
            shift 2
130
            ;;
131
        -*)
132
            echo "ERROR: unknown option: $1" >&2
133
            show_help
134
            exit 1
135
            ;;
136
        *)
137
            TARGETS+=("$1")
138
            shift
139
            ;;
140
    esac
141
done
142

            
143
if [[ ${#TARGETS[@]} -eq 0 && ${LOCAL_MODE} -eq 0 ]]; then
144
    TARGETS=(baobab ebony tapia)
145
fi
146

            
147
echo "================================"
148
echo "${PROJECT_ID} - ${MODE}"
149
echo "================================"
150

            
151
if [[ ${LOCAL_MODE} -eq 1 ]]; then
152
    if [[ "${MODE}" == "install" ]]; then
153
        run_local_install
154
    else
155
        run_local_uninstall
156
    fi
157
    exit 0
158
fi
159

            
160
for host in "${TARGETS[@]}"; do
161
    if [[ "${MODE}" == "install" ]]; then
162
        run_remote_install "$(resolve_target "${host}")"
163
    else
164
        run_remote_uninstall "$(resolve_target "${host}")"
165
    fi
166
done