From 158230f7a69494d406a1f89aa3acf4288382d1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jake=20Buchholz=20G=C3=B6kt=C3=BCrk?= Date: Sun, 24 Nov 2024 10:53:50 -0800 Subject: [PATCH] Autodetect for Hetzner and Scaleway --- lib/tiny-cloud/cloud/hetzner/autodetect | 5 +++++ lib/tiny-cloud/cloud/hetzner/imds copy | 18 ++++++++++++++++++ lib/tiny-cloud/cloud/scaleway/autodetect | 6 ++++++ lib/tiny-cloud/cloud/scaleway/imds copy | 18 ++++++++++++++++++ tests/init.test | 22 ++++++++++++++++++++++ 5 files changed, 69 insertions(+) create mode 100755 lib/tiny-cloud/cloud/hetzner/autodetect create mode 100644 lib/tiny-cloud/cloud/hetzner/imds copy create mode 100755 lib/tiny-cloud/cloud/scaleway/autodetect create mode 100644 lib/tiny-cloud/cloud/scaleway/imds copy diff --git a/lib/tiny-cloud/cloud/hetzner/autodetect b/lib/tiny-cloud/cloud/hetzner/autodetect new file mode 100755 index 0000000..cc949b5 --- /dev/null +++ b/lib/tiny-cloud/cloud/hetzner/autodetect @@ -0,0 +1,5 @@ +#!/bin/sh +# vim:set filetype=sh: +# shellcheck shell=sh + +grep -qi hetzner "$ROOT"/sys/class/dmi/id/modalias 2>/dev/null && echo "10 hetzner" diff --git a/lib/tiny-cloud/cloud/hetzner/imds copy b/lib/tiny-cloud/cloud/hetzner/imds copy new file mode 100644 index 0000000..f7b877d --- /dev/null +++ b/lib/tiny-cloud/cloud/hetzner/imds copy @@ -0,0 +1,18 @@ +# AWS Instance MetaData Service variables and functions +# vim:set filetype=sh: +# shellcheck shell=sh + +IMDS_HEADER="X-aws-ec2-metadata-token" +IMDS_TOKEN_TTL_HEADER="X-aws-ec2-metadata-token-ttl-seconds" +: "${IMDS_TOKEN_TTL:=5}" +IMDS_URI="latest" + +_imds_token() { + printf "PUT /latest/api/token HTTP/1.0\r\n%s: %s\r\n\r\n" \ + "$IMDS_TOKEN_TTL_HEADER" "$IMDS_TOKEN_TTL" \ + | nc -w 1 "$IMDS_ENDPOINT" 80 | tail -n 1 +} + +_imds_header() { + echo "$IMDS_HEADER: $(_imds_token)" +} diff --git a/lib/tiny-cloud/cloud/scaleway/autodetect b/lib/tiny-cloud/cloud/scaleway/autodetect new file mode 100755 index 0000000..5d516bb --- /dev/null +++ b/lib/tiny-cloud/cloud/scaleway/autodetect @@ -0,0 +1,6 @@ +#!/bin/sh +# vim:set filetype=sh: +# shellcheck shell=sh + +# TODO: verify that this works +grep -qi scaleway "$ROOT"/sys/class/dmi/id/modalias 2>/dev/null && echo "10 scaleway" diff --git a/lib/tiny-cloud/cloud/scaleway/imds copy b/lib/tiny-cloud/cloud/scaleway/imds copy new file mode 100644 index 0000000..f7b877d --- /dev/null +++ b/lib/tiny-cloud/cloud/scaleway/imds copy @@ -0,0 +1,18 @@ +# AWS Instance MetaData Service variables and functions +# vim:set filetype=sh: +# shellcheck shell=sh + +IMDS_HEADER="X-aws-ec2-metadata-token" +IMDS_TOKEN_TTL_HEADER="X-aws-ec2-metadata-token-ttl-seconds" +: "${IMDS_TOKEN_TTL:=5}" +IMDS_URI="latest" + +_imds_token() { + printf "PUT /latest/api/token HTTP/1.0\r\n%s: %s\r\n\r\n" \ + "$IMDS_TOKEN_TTL_HEADER" "$IMDS_TOKEN_TTL" \ + | nc -w 1 "$IMDS_ENDPOINT" 80 | tail -n 1 +} + +_imds_header() { + echo "$IMDS_HEADER: $(_imds_token)" +} diff --git a/tests/init.test b/tests/init.test index a6e7f36..b88e8ef 100755 --- a/tests/init.test +++ b/tests/init.test @@ -28,11 +28,13 @@ init_tests \ autodetect_aws_xen \ autodetect_azure \ autodetect_gcp \ + autodetect_hetzner \ autodetect_incus \ autodetect_nocloud_cmdline \ autodetect_nocloud_dmi \ autodetect_nocloud_volume \ autodetect_oci \ + autodetect_scaleway \ autodetect_unknown PROVIDERS="aws azure gcp nocloud oci scaleway hetzner" @@ -284,6 +286,16 @@ autodetect_gcp_body() { sh -c ". \"$lib\"; echo \$CLOUD" } +autodetect_hetzner_body() { + mkdir -p sys/class/dmi/id + cat > sys/class/dmi/id/modalias <<-EOT + dmi:bvnHetzner:bvrHetzner:bd10/22/2024:br1.0:svnHetzner:pnHetzner:pvr:rvnKVM:rnHetzner:rvr:cvnHetzner:ct1:cvr:sku: + EOT + atf_check \ + -o match:"hetzner" \ + sh -c ". \"$lib\"; echo \$CLOUD" +} + autodetect_incus_body() { mkdir -p dev/incus touch dev/incus/sock @@ -330,3 +342,13 @@ autodetect_oci_body() { -o match:"oci" \ sh -c ". \"$lib\"; echo \$CLOUD" } + +autodetect_scaleway_body() { + mkdir -p sys/class/dmi/id + cat > sys/class/dmi/id/modalias <<-EOT + dmi:bvnScaleway:bvrScaleway:bd10/22/2024:br1.0:svnScaleway:pnScaleway:pvr:rvnKVM:rnScaleway:rvr:cvnScaleway:ct1:cvr:sku: + EOT + atf_check \ + -o match:"scaleway" \ + sh -c ". \"$lib\"; echo \$CLOUD" +}