1
0
mirror of https://gitlab.alpinelinux.org/alpine/cloud/tiny-cloud.git synced 2025-12-14 19:02:45 +03:00

Add incus support

Incus provides meta-data via unix socket /dev/incus/sock
https://linuxcontainers.org/incus/docs/main/dev-incus/
This commit is contained in:
Natanael Copa 2024-06-21 15:49:39 +02:00
parent abe98e6cbf
commit 3197427cb3
4 changed files with 72 additions and 13 deletions

View File

@ -0,0 +1,21 @@
# Incus Instance Metadata
# vim:set filetype=sh:
# shellcheck shell=sh
# https://linuxcontainers.org/incus/docs/main/dev-incus/
IMDS_URI=/1.0/meta-data
IMDS_LOCAL_HOSTNAME="local-hostname"
IMDS_HOSTNAME="local-hostname"
IMDS_ENDPOINT=local:/dev/incus/sock
_imds() {
printf "GET %s HTTP/1.0\n\n" "$IMDS_URI" \
| nc "$IMDS_ENDPOINT" | dos2unix \
| awk -F ': ' -v key="$1" '$1 == key { print $2 }'
}
_imds_userdata() {
printf "GET /1.0/config/user.user-data HTTP/1.0\n\n" \
| nc "$IMDS_ENDPOINT" | sed '1,/^\r$/d'
}

View File

@ -20,14 +20,24 @@ while [ ! -z "$1" ]; do
esac
done
# Scaleway
if [ "$CLOUD" = "scaleway" ]; then
if [ $local_port -gt 1023 ]; then
NC_CONTENT="invalid local port"
else
NC_CONTENT="$(cat ${NC_FILE:-$host.txt})"
fi
printf "HTTP/1.1 200 OK\r\n\r\n%s" "$NC_CONTENT"
else
echo "token-foo"
fi
case "$CLOUD" in
scaleway)
if [ $local_port -gt 1023 ]; then
NC_CONTENT="invalid local port"
else
NC_CONTENT="$(cat ${NC_FILE:-$host.txt})"
fi
printf "HTTP/1.1 200 OK\r\n\r\n%s" "$NC_CONTENT"
;;
incus)
set -- $input # GET $URL HTTP/1.0
url="$2"
NC_CONTENT="$(cat ${NC_FILE:-${host%:*}.txt})"
len=$(printf "%s" "$NC_CONTENT" | wc -c)
printf "HTTP/1.0 200 $url OK\r\nContent-Type: application/json\r\nContent-Length: $len\r\n\r\n%s" \
"$NC_CONTENT"
;;
*)
echo "token-foo"
;;
esac

View File

@ -18,6 +18,7 @@ init_tests \
imds_hostname_oci \
imds_hostname_scaleway \
imds_hostname_hetzner \
imds_hostname_incus \
\
imds_local_hostname_aws \
imds_local_hostname_azure \
@ -26,6 +27,7 @@ init_tests \
imds_local_hostname_oci \
imds_local_hostname_scaleway \
imds_local_hostname_hetzner \
imds_local_hostname_incus \
\
imds_ssh_keys_aws \
imds_ssh_keys_azure \
@ -38,6 +40,7 @@ init_tests \
imds_nocloud_cmdline_local_hostname \
imds_nocloud_smbios_local_hostname \
\
imds_userdata_incus \
imds_userdata_scaleway
imds_help_body() {
@ -68,6 +71,13 @@ check_hostname() {
imds_hostname_aws_body() { check_hostname aws; }
imds_hostname_azure_body() { check_hostname azure; }
imds_hostname_gcp_body() { check_hostname gcp; }
imds_hostname_incus_body() {
fake_metadata incus <<-EOF
#cloud-config
local-hostname: myhostname
EOF
CLOUD="incus" atf_check -o match:"myhostname" imds @hostname
}
imds_hostname_nocloud_body() { check_hostname nocloud; }
imds_hostname_oci_body() { check_hostname oci; }
imds_hostname_scaleway_body() {
@ -80,7 +90,7 @@ imds_hostname_hetzner_body() { check_hostname hetzner; }
check_local_hostname() {
fake_metadata "$1" <<-EOF
# nocloud, alpine, aws
# nocloud, alpine, aws, incus
local-hostname: myhostname
# hetzner
hostname: myhostname
@ -96,6 +106,7 @@ check_local_hostname() {
imds_local_hostname_aws_body() { check_local_hostname aws; }
imds_local_hostname_azure_body() { check_local_hostname azure; }
imds_local_hostname_gcp_body() { check_local_hostname gcp; }
imds_local_hostname_incus_body() { check_local_hostname incus; }
imds_local_hostname_nocloud_body() { check_local_hostname nocloud; }
imds_local_hostname_oci_body() { check_local_hostname oci; }
imds_local_hostname_scaleway_body() {
@ -106,6 +117,7 @@ EOF
CLOUD="scaleway" atf_check -o match:"^myhostname$" imds @local-hostname
}
imds_local_hostname_hetzner_body() { check_local_hostname hetzner; }
imds_local_hostname_incus_body() { check_local_hostname incus; }
check_ssh_keys() {
local key="ssh-ed25519 keydata"
@ -181,6 +193,13 @@ imds_nocloud_smbios_local_hostname_body() {
done
}
imds_userdata_incus_body() {
fake_userdata_incus <<-EOF
#alpine-config
EOF
CLOUD="incus" atf_check -o match:"#alpine-config" imds @userdata
}
imds_userdata_scaleway_body() {
local cmd="sh ./cmd"
fake_userdata_scaleway <<-EOF

View File

@ -98,15 +98,24 @@ fake_metadata_hetzner() {
export WGET_STRIP_PREFIX="/hetzner/v1/metadata"
}
fake_metadata_incus() {
cat > "local.txt"
}
fake_userdata_incus() {
cat > "local.txt"
}
fake_metadata() {
case "${1:-$CLOUD}" in
alpine|nocloud) fake_metadata_nocloud;;
aws) fake_metadata_aws;;
azure) fake_metadata_azure;;
gcp) fake_metadata_gcp;;
hetzner) fake_metadata_hetzner;;
incus) fake_metadata_incus;;
oci) fake_metadata_oci;;
scaleway) fake_metadata_scaleway;;
hetzner) fake_metadata_hetzner;;
*) echo "TODO: fake_metadata_$CLOUD" >&2;;
esac
}