mirror of
https://codeberg.org/pierreprinetti/forgejo-hetzner-runner.git
synced 2025-06-27 16:25:53 +00:00
111 lines
2.3 KiB
Bash
Executable file
111 lines
2.3 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
set -Eeuo pipefail
|
|
|
|
random_string() {
|
|
declare -r length="$1"
|
|
tr -dc a-z </dev/random \
|
|
| head -c "$length"
|
|
}
|
|
|
|
declare \
|
|
image='debian-12' \
|
|
location='fsn1' \
|
|
name="runner-$(random_string 5)" \
|
|
server_type='cx11' \
|
|
cloud_init_path='runner.cloud-init.yaml' \
|
|
ssh_key_id='' \
|
|
runner_token="${FORGEJO_TOKEN:-}" \
|
|
|
|
while getopts "hi:l:n:t:c:r:s:" arg; do
|
|
case $arg in
|
|
h)
|
|
echo 'runner-up'
|
|
echo 'https://codeberg.org/pierreprinetti/forgejo-hetzner-runner'
|
|
echo
|
|
echo -e 'Usage:'
|
|
echo -e "\t-i: image (default: '${image}')"
|
|
echo -e "\t-l: location (default: '${location}')"
|
|
echo -e "\t-n: name (default: '${name}')"
|
|
echo -e "\t-t: type (default: '${server_type}')"
|
|
echo -e "\t-c: cloud-init path (default: '${cloud_init_path}')"
|
|
echo -e "\t-r: Forgejo runner token (default: '${runner_token}')"
|
|
echo -e "\t-s: SSH key ID (default: none)"
|
|
echo
|
|
exit 0
|
|
;;
|
|
i)
|
|
image=$OPTARG
|
|
;;
|
|
l)
|
|
location=$OPTARG
|
|
;;
|
|
n)
|
|
name=$OPTARG
|
|
;;
|
|
t)
|
|
server_type=$OPTARG
|
|
;;
|
|
c)
|
|
cloud_init_path=$OPTARG
|
|
;;
|
|
r)
|
|
runner_token=$OPTARG
|
|
;;
|
|
s)
|
|
ssh_key_id=$OPTARG
|
|
;;
|
|
*)
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
readonly \
|
|
image \
|
|
location \
|
|
name \
|
|
server_type \
|
|
cloud_init_path \
|
|
runner_token \
|
|
ssh_key_id
|
|
shift $((OPTIND-1))
|
|
|
|
if [[ -z "$runner_token" ]]; then
|
|
echo 'Set a runner token'
|
|
exit 1
|
|
fi
|
|
|
|
declare authorized_ssh_key
|
|
declare data
|
|
data="$(jq -c \
|
|
--arg image "$image" \
|
|
--arg location "$location" \
|
|
--arg name "$name" \
|
|
--arg server_type "$server_type" \
|
|
'.
|
|
| .image=$image
|
|
| .location=$location
|
|
| .name=$name
|
|
| .server_type=$server_type
|
|
| .labels.role="runner"
|
|
' <<< '{}')"
|
|
if [[ -n $ssh_key_id ]]; then
|
|
data="$(jq -c --argjson ssh_key_id "$ssh_key_id" '.ssh_keys=[$ssh_key_id]' <<< "$data")"
|
|
authorized_ssh_key="$(curl -sS -X GET \
|
|
-H "Authorization: Bearer $HETZNER_API_TOKEN" \
|
|
"https://api.hetzner.cloud/v1/ssh_keys/${ssh_key_id}" \
|
|
| jq -r '.ssh_key.public_key')"
|
|
fi
|
|
readonly authorized_ssh_key
|
|
|
|
export runner_token
|
|
export authorized_ssh_key
|
|
data="$(jq -c --arg cloud_init "$(envsubst '$authorized_ssh_key $runner_token' < "$cloud_init_path")" '.user_data=$cloud_init' <<< "$data")"
|
|
readonly data
|
|
|
|
curl -isS \
|
|
-X POST \
|
|
-H "Authorization: Bearer ${HETZNER_API_TOKEN}" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$data" \
|
|
'https://api.hetzner.cloud/v1/servers'
|