This repository has been archived on 2021-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
abra-hetzner/abra-hetzner

165 lines
7.8 KiB
Bash
Executable File

#!/usr/bin/env bash
DOC="
Hetzner provider plugin for abra
Usage:
abra-hetzner <name> <type> <image> [--ssh-keys=<ssh-keys>] [--location=<location>]
Reads your Hetzner API token from the \$HETZNER_API_TOKEN environment variable
or prompts for it.
"
# docopt parser below, refresh this parser with `docopt.sh abra-hetzner`
# shellcheck disable=2016,1075
docopt() { parse() { if ${DOCOPT_DOC_CHECK:-true}; then local doc_hash
if doc_hash=$(printf "%s" "$DOC" | (sha256sum 2>/dev/null || shasum -a 256)); then
if [[ ${doc_hash:0:5} != "$digest" ]]; then
stderr "The current usage doc (${doc_hash:0:5}) does not match \
what the parser was generated with (${digest})
Run \`docopt.sh\` to refresh the parser."; _return 70; fi; fi; fi
local root_idx=$1; shift; argv=("$@"); parsed_params=(); parsed_values=()
left=(); testdepth=0; local arg; while [[ ${#argv[@]} -gt 0 ]]; do
if [[ ${argv[0]} = "--" ]]; then for arg in "${argv[@]}"; do
parsed_params+=('a'); parsed_values+=("$arg"); done; break
elif [[ ${argv[0]} = --* ]]; then parse_long
elif [[ ${argv[0]} = -* && ${argv[0]} != "-" ]]; then parse_shorts
elif ${DOCOPT_OPTIONS_FIRST:-false}; then for arg in "${argv[@]}"; do
parsed_params+=('a'); parsed_values+=("$arg"); done; break; else
parsed_params+=('a'); parsed_values+=("${argv[0]}"); argv=("${argv[@]:1}"); fi
done; local idx; if ${DOCOPT_ADD_HELP:-true}; then
for idx in "${parsed_params[@]}"; do [[ $idx = 'a' ]] && continue
if [[ ${shorts[$idx]} = "-h" || ${longs[$idx]} = "--help" ]]; then
stdout "$trimmed_doc"; _return 0; fi; done; fi
if [[ ${DOCOPT_PROGRAM_VERSION:-false} != 'false' ]]; then
for idx in "${parsed_params[@]}"; do [[ $idx = 'a' ]] && continue
if [[ ${longs[$idx]} = "--version" ]]; then stdout "$DOCOPT_PROGRAM_VERSION"
_return 0; fi; done; fi; local i=0; while [[ $i -lt ${#parsed_params[@]} ]]; do
left+=("$i"); ((i++)) || true; done
if ! required "$root_idx" || [ ${#left[@]} -gt 0 ]; then error; fi; return 0; }
parse_shorts() { local token=${argv[0]}; local value; argv=("${argv[@]:1}")
[[ $token = -* && $token != --* ]] || _return 88; local remaining=${token#-}
while [[ -n $remaining ]]; do local short="-${remaining:0:1}"
remaining="${remaining:1}"; local i=0; local similar=(); local match=false
for o in "${shorts[@]}"; do if [[ $o = "$short" ]]; then similar+=("$short")
[[ $match = false ]] && match=$i; fi; ((i++)) || true; done
if [[ ${#similar[@]} -gt 1 ]]; then
error "${short} is specified ambiguously ${#similar[@]} times"
elif [[ ${#similar[@]} -lt 1 ]]; then match=${#shorts[@]}; value=true
shorts+=("$short"); longs+=(''); argcounts+=(0); else value=false
if [[ ${argcounts[$match]} -ne 0 ]]; then if [[ $remaining = '' ]]; then
if [[ ${#argv[@]} -eq 0 || ${argv[0]} = '--' ]]; then
error "${short} requires argument"; fi; value=${argv[0]}; argv=("${argv[@]:1}")
else value=$remaining; remaining=''; fi; fi; if [[ $value = false ]]; then
value=true; fi; fi; parsed_params+=("$match"); parsed_values+=("$value"); done
}; parse_long() { local token=${argv[0]}; local long=${token%%=*}
local value=${token#*=}; local argcount; argv=("${argv[@]:1}")
[[ $token = --* ]] || _return 88; if [[ $token = *=* ]]; then eq='='; else eq=''
value=false; fi; local i=0; local similar=(); local match=false
for o in "${longs[@]}"; do if [[ $o = "$long" ]]; then similar+=("$long")
[[ $match = false ]] && match=$i; fi; ((i++)) || true; done
if [[ $match = false ]]; then i=0; for o in "${longs[@]}"; do
if [[ $o = $long* ]]; then similar+=("$long"); [[ $match = false ]] && match=$i
fi; ((i++)) || true; done; fi; if [[ ${#similar[@]} -gt 1 ]]; then
error "${long} is not a unique prefix: ${similar[*]}?"
elif [[ ${#similar[@]} -lt 1 ]]; then
[[ $eq = '=' ]] && argcount=1 || argcount=0; match=${#shorts[@]}
[[ $argcount -eq 0 ]] && value=true; shorts+=(''); longs+=("$long")
argcounts+=("$argcount"); else if [[ ${argcounts[$match]} -eq 0 ]]; then
if [[ $value != false ]]; then
error "${longs[$match]} must not have an argument"; fi
elif [[ $value = false ]]; then
if [[ ${#argv[@]} -eq 0 || ${argv[0]} = '--' ]]; then
error "${long} requires argument"; fi; value=${argv[0]}; argv=("${argv[@]:1}")
fi; if [[ $value = false ]]; then value=true; fi; fi; parsed_params+=("$match")
parsed_values+=("$value"); }; required() { local initial_left=("${left[@]}")
local node_idx; ((testdepth++)) || true; for node_idx in "$@"; do
if ! "node_$node_idx"; then left=("${initial_left[@]}"); ((testdepth--)) || true
return 1; fi; done; if [[ $((--testdepth)) -eq 0 ]]; then
left=("${initial_left[@]}"); for node_idx in "$@"; do "node_$node_idx"; done; fi
return 0; }; optional() { local node_idx; for node_idx in "$@"; do
"node_$node_idx"; done; return 0; }; value() { local i
for i in "${!left[@]}"; do local l=${left[$i]}
if [[ ${parsed_params[$l]} = "$2" ]]; then
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
[[ $testdepth -gt 0 ]] && return 0; local value
value=$(printf -- "%q" "${parsed_values[$l]}"); if [[ $3 = true ]]; then
eval "var_$1+=($value)"; else eval "var_$1=$value"; fi; return 0; fi; done
return 1; }; stdout() { printf -- "cat <<'EOM'\n%s\nEOM\n" "$1"; }; stderr() {
printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() {
[[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() {
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:223}
usage=${DOC:35:91}; digest=0f15e; shorts=('' ''); longs=(--ssh-keys --location)
argcounts=(1 1); node_0(){ value __ssh_keys 0; }; node_1(){ value __location 1
}; node_2(){ value _name_ a; }; node_3(){ value _type_ a; }; node_4(){
value _image_ a; }; node_5(){ optional 0; }; node_6(){ optional 1; }; node_7(){
required 2 3 4 5 6; }; node_8(){ required 7; }; cat <<<' docopt_exit() {
[[ -n $1 ]] && printf "%s\n" "$1" >&2; printf "%s\n" "${DOC:35:91}" >&2; exit 1
}'; unset var___ssh_keys var___location var__name_ var__type_ var__image_
parse 8 "$@"; local prefix=${DOCOPT_PREFIX:-''}; unset "${prefix}__ssh_keys" \
"${prefix}__location" "${prefix}_name_" "${prefix}_type_" "${prefix}_image_"
eval "${prefix}"'__ssh_keys=${var___ssh_keys:-}'
eval "${prefix}"'__location=${var___location:-}'
eval "${prefix}"'_name_=${var__name_:-}'
eval "${prefix}"'_type_=${var__type_:-}'
eval "${prefix}"'_image_=${var__image_:-}'; local docopt_i=1
[[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2; for ((;docopt_i>0;docopt_i--)); do
declare -p "${prefix}__ssh_keys" "${prefix}__location" "${prefix}_name_" \
"${prefix}_type_" "${prefix}_image_"; done; }
# docopt parser above, complete command for generating this parser is `docopt.sh abra-hetzner`
ABRA_DIR="${ABRA_DIR:-$HOME/.abra}"
ABRA_VENDOR_DIR="$ABRA_DIR/vendor"
JQ="$ABRA_VENDOR_DIR/jq"
abra_hetzner() {
declare abra_hetzner__name_ abra_hetzner__type_ abra_hetzner__image_ \
abra_hetzner___ssh_keys abra_hetzner___location
DOCOPT_PREFIX=abra_hetzner_
eval "$(docopt "$@")"
if ! type curl > /dev/null 2>&1; then
error "curl program is not installed"
fi
if [[ -z "$HETZNER_API_TOKEN" ]]; then
read -rp "Hetzner API token: " HETZNER_API_TOKEN
fi
NAME="$abra_hetzner__name_"
TYPE="$abra_hetzner__type_"
IMAGE="$abra_hetzner__image_"
SSH_KEYS="$abra_hetzner___ssh_keys"
LOCATION="${abra_hetzner___location:-hel1}"
# shellcheck disable=SC2001
ssh_keys=$(echo "$SSH_KEYS" | sed 's/[^[:space:],]\+/"&"/g')
response=$(curl --silent \
-X POST \
-H "Authorization: Bearer $HETZNER_API_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"name\":\"$NAME\", \
\"server_type\": \"$TYPE\", \
\"image\": \"$IMAGE\", \
\"ssh_keys\": [$ssh_keys], \
\"location\": \"$LOCATION\" \
}" \
'https://api.hetzner.cloud/v1/servers')
ip=$(echo "$response" | $JQ -r ".server .public_net .ipv4 .ip")
if [[ "$ip" == "null" ]]; then
error_msg=$(echo "$response" | jq -r ".error .message")
echo "Oops, something went wrong. Hetzner Cloud API responded with: $error_msg"
exit 1
fi
echo "You new Hetzner Cloud VPS is up on $ip. Enjoy!"
}
abra_hetzner "$@"