autoNAS / setup.sh
Newer Older
141 lines | 3.523kb
Bogdan Timofte authored 2 weeks ago
1
#!/bin/bash
2

            
3
set -euo pipefail
4

            
5
PROJECT_ID="autonas"
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" install
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/autonas-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}'"
45
    scp -q -r \
46
        "${SCRIPT_DIR}/config" \
47
        "${SCRIPT_DIR}/scripts" \
48
        "${SCRIPT_DIR}/README.md" \
49
        "${SCRIPT_DIR}/DEVELOPMENT.md" \
50
        "${SCRIPT_DIR}/CHANGELOG.md" \
51
        "${SCRIPT_DIR}/INSTALL.md" \
52
        "${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' install"
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/autonas-uninstall.sh" "${target}:${remote_tmp}/scripts/"
74

            
75
    if [[ "${REMOTE_USER}" == "root" ]]; then
76
        ssh "${target}" "if [ -x '${canonical}' ]; then '${canonical}'; else bash '${remote_tmp}/scripts/autonas-uninstall.sh'; fi"
77
    else
78
        ssh "${target}" "sudo bash -lc \"if [ -x '${canonical}' ]; then '${canonical}'; else bash '${remote_tmp}/scripts/autonas-uninstall.sh'; fi\""
79
    fi
80

            
81
    ssh "${target}" "rm -rf '${remote_tmp}'"
82
}
83

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

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

            
118
echo "================================"
119
echo "${PROJECT_ID} - ${MODE}"
120
echo "================================"
121

            
122
if [[ ${LOCAL_MODE} -eq 1 ]]; then
123
    if [[ "${MODE}" == "install" ]]; then
124
        run_local_install
125
    else
126
        run_local_uninstall
127
    fi
128
    exit 0
129
fi
130

            
131
TARGET="${REMOTE_USER}@${REMOTE_NODE}"
132
if ! ping -c 1 "${REMOTE_NODE}" >/dev/null 2>&1; then
133
    echo "ERROR: cannot reach ${REMOTE_NODE}" >&2
134
    exit 1
135
fi
136

            
137
if [[ "${MODE}" == "install" ]]; then
138
    run_remote_install "${TARGET}"
139
else
140
    run_remote_uninstall "${TARGET}"
141
fi