From 2713aac43f233d688652be02dfbf6be1d699ba7d Mon Sep 17 00:00:00 2001 From: Hugo Rodrigues Date: Sun, 10 Mar 2024 22:28:36 +0000 Subject: [PATCH] Added support to Hetzner --- lib/tiny-cloud/cloud/hetzner/imds | 23 +++++++++++++++++++++++ tests/imds.test | 17 ++++++++++++++++- tests/init.test | 2 +- tests/test_env.sh | 6 ++++++ tests/tiny-cloud.test | 2 +- 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 lib/tiny-cloud/cloud/hetzner/imds diff --git a/lib/tiny-cloud/cloud/hetzner/imds b/lib/tiny-cloud/cloud/hetzner/imds new file mode 100644 index 0000000..428e1e7 --- /dev/null +++ b/lib/tiny-cloud/cloud/hetzner/imds @@ -0,0 +1,23 @@ +# Hetzner Instance MetaData Service variables and functions +# vim:set filetype=sh: +# shellcheck shell=sh + +IMDS_BASE_URI="hetzner/v1" +IMDS_URI="$IMDS_BASE_URI/metadata" + +IMDS_HOSTNAME="hostname" +IMDS_LOCAL_HOSTNAME="$IMDS_HOSTNAME" +IMDS_SSH_KEYS="public-keys" +IMDS_USERDATA="userdata" + +_imds_header() { + echo "" +} + +_imds_ssh_keys() { + _imds "$IMDS_SSH_KEYS" | sed 's/"/\n/g' | egrep -v '(\[|\]|,)' | sort -u +} + +_imds_userdata() { + wget --quiet --timeout 1 --output-document - --header "$(_imds_header)" "http://$IMDS_ENDPOINT/$IMDS_BASE_URI/$IMDS_USERDATA" +} diff --git a/tests/imds.test b/tests/imds.test index aa8b140..fa3db05 100755 --- a/tests/imds.test +++ b/tests/imds.test @@ -18,6 +18,7 @@ init_tests \ imds_hostname_nocloud \ imds_hostname_oci \ imds_hostname_scaleway \ + imds_hostname_hetzner \ \ imds_local_hostname_alpine \ imds_local_hostname_aws \ @@ -26,6 +27,7 @@ init_tests \ imds_local_hostname_nocloud \ imds_local_hostname_oci \ imds_local_hostname_scaleway \ + imds_local_hostname_hetzner \ \ imds_ssh_keys_alpine \ imds_ssh_keys_aws \ @@ -34,6 +36,7 @@ init_tests \ imds_ssh_keys_nocloud \ imds_ssh_keys_oci \ imds_ssh_keys_scaleway \ + imds_ssh_keys_hetzner \ \ imds_nocloud_cmdline_local_hostname \ imds_nocloud_smbios_local_hostname \ @@ -54,7 +57,7 @@ imds_space_body() { check_hostname() { fake_metadata "$1" <<-EOF - # nocloud, alpine, aws + # nocloud, alpine, aws, hetzner hostname: myhostname # azure compute: @@ -77,11 +80,14 @@ HOSTNAME=myhostname EOF CLOUD="scaleway" atf_check -o match:"myhostname" imds @hostname } +imds_hostname_hetzner_body() { check_hostname hetzner; } check_local_hostname() { fake_metadata "$1" <<-EOF # nocloud, alpine, aws local-hostname: myhostname + # hetzner + hostname: myhostname # azure compute: name: myhostname @@ -103,6 +109,7 @@ HOSTNAME=myhostname EOF CLOUD="scaleway" atf_check -o match:"myhostname" imds @local-hostname } +imds_local_hostname_hetzner_body() { check_local_hostname hetzner; } check_ssh_keys() { local key="ssh-ed25519 keydata" @@ -148,6 +155,14 @@ SSH_PUBLIC_KEYS_0_IP= EOF CLOUD="scaleway" atf_check -o match:"$key" imds @ssh-keys } +imds_ssh_keys_hetzner_body() { + local key="ssh-ed25519 keydata" + fake_metadata "hetzner" <<-EOF + # hetzner + public-keys: '["$key"]' + EOF + CLOUD="hetzner" atf_check -o match:"$key" imds @ssh-keys +} imds_nocloud_cmdline_local_hostname_body() { atf_require_prog yx diff --git a/tests/init.test b/tests/init.test index 84a43eb..21a42ea 100755 --- a/tests/init.test +++ b/tests/init.test @@ -25,7 +25,7 @@ init_tests \ userdata_type \ run_userdata -PROVIDERS="alpine aws azure gcp nocloud oci scaleway" +PROVIDERS="alpine aws azure gcp nocloud oci scaleway hetzner" expand_root_body() { mkdir proc diff --git a/tests/test_env.sh b/tests/test_env.sh index 9e0de37..14278ac 100644 --- a/tests/test_env.sh +++ b/tests/test_env.sh @@ -93,6 +93,11 @@ fake_userdata_scaleway() { cat > "169.254.42.42.txt" } +fake_metadata_hetzner() { + cat > "169.254.169.254.yaml" + export WGET_STRIP_PREFIX="/hetzner/v1/metadata" +} + fake_metadata() { case "${1:-$CLOUD}" in alpine|nocloud) fake_metadata_nocloud;; @@ -101,6 +106,7 @@ fake_metadata() { gcp) fake_metadata_gcp;; oci) fake_metadata_oci;; scaleway) fake_metadata_scaleway;; + hetzner) fake_metadata_hetzner;; *) echo "TODO: fake_metadata_$CLOUD" >&2;; esac } diff --git a/tests/tiny-cloud.test b/tests/tiny-cloud.test index 425effc..cde611f 100755 --- a/tests/tiny-cloud.test +++ b/tests/tiny-cloud.test @@ -6,7 +6,7 @@ export PREFIX="$srcdir" export MOCK=echo -PROVIDERS="alpine aws azure gcp nocloud oci scaleway" +PROVIDERS="alpine aws azure gcp nocloud oci scaleway hetzner" init_tests \ tiny_cloud_help \