1 contributor
#!/bin/bash
set -euo pipefail
PROJECT_ID="autonas"
ORG_ID="xdev"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MODE="install"
REMOTE_NODE=""
REMOTE_USER="root"
LOCAL_MODE=0
show_help() {
cat <<EOF
${PROJECT_ID} setup wrapper
Usage: $0 [OPTIONS] [<target_node>]
Options:
-h, --help Show this help message
-l, --local Run on localhost
-u, --uninstall Uninstall instead of install
--user <user> Remote SSH user (default: root)
EOF
}
run_local_install() {
bash "${SCRIPT_DIR}/scripts/install.sh" install
}
run_local_uninstall() {
local canonical="/usr/local/lib/${ORG_ID}/${PROJECT_ID}/uninstall.sh"
if [[ -x "${canonical}" ]]; then
"${canonical}"
else
bash "${SCRIPT_DIR}/scripts/autonas-uninstall.sh"
fi
}
copy_remote_tree() {
local target="$1"
local remote_tmp="$2"
ssh "${target}" "rm -rf '${remote_tmp}' && mkdir -p '${remote_tmp}'"
scp -q -r \
"${SCRIPT_DIR}/config" \
"${SCRIPT_DIR}/scripts" \
"${SCRIPT_DIR}/README.md" \
"${SCRIPT_DIR}/DEVELOPMENT.md" \
"${SCRIPT_DIR}/CHANGELOG.md" \
"${SCRIPT_DIR}/INSTALL.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' install"
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/autonas-uninstall.sh" "${target}:${remote_tmp}/scripts/"
if [[ "${REMOTE_USER}" == "root" ]]; then
ssh "${target}" "if [ -x '${canonical}' ]; then '${canonical}'; else bash '${remote_tmp}/scripts/autonas-uninstall.sh'; fi"
else
ssh "${target}" "sudo bash -lc \"if [ -x '${canonical}' ]; then '${canonical}'; else bash '${remote_tmp}/scripts/autonas-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