1
0
mirror of https://gitlab.alpinelinux.org/alpine/cloud/tiny-cloud.git synced 2025-12-16 03:42:44 +03:00
Jake Buchholz Göktürk 64a6c4ea4f * initial round of nocloud should be just about complete, pending tests
* update README, example config
* nocloud and aws share IMDS layout, move vars and funcs from aws imds lib to main imds script
* IMDS_USERDATA value is no longer configurable
* note that azure and oci are missing some network bits yet
2022-07-10 20:22:59 -07:00

57 lines
1.5 KiB
Bash

# Tiny Cloud - Final Phase Functions
# vim:set ts=4 et ft=sh:
source /lib/tiny-cloud/init-common
match_header() {
local bytes=$(echo -en "$1")
[ "$bytes" = $(dd bs=1 count=${#bytes} if="$2" 2>/dev/null) ]
}
save_userdata() {
skip_action save_userdata && return
local userdata="$TINY_CLOUD_VAR/$CLOUD_USERDATA"
local tmpfile=$(mktemp "$userdata.XXXXXX")
local cmd
imds -e @userdata > "$tmpfile"
cmd="cat"
if ! skip_action decompress_userdata; then
if match_header '\037\213\010' "$tmpfile"; then
cmd="gzip -dc"
elif match_header 'BZh' "$tmpfile"; then
cmd="bzip2 -dc"
elif match_header '\3757zXZ\000' "$tmpfile"; then
cmd="unxz -c"
elif match_header '\135\0\0\0' "$tmpfile"; then
cmd="lzma -dc"
elif match_header '\211\114\132' "$tmpfile"; then
cmd="lzop -dc"
elif match_header '\002!L\030' "$tmpfile"; then
cmd="lz4 -dc"
elif match_header '(\265/\375' "$tmpfile"; then
cmd="zstd -dc"
fi
fi
$cmd "$tmpfile" > "$userdata"
rm "$tmpfile"
}
is_userdata_script() {
head -n1 "$TINY_CLOUD_VAR/$CLOUD_USERDATA" | grep -q "#!/"
}
run_userdata() {
skip_action run_userdata && return
local log="$TINY_CLOUD_LOGS/$CLOUD_USERDATA.log"
local exit="$TINY_CLOUD_LOGS/$CLOUD_USERDATA.exit"
local userdata="$TINY_CLOUD_VAR/$CLOUD_USERDATA"
chmod +x "$userdata"
{ "$userdata" 2>& 1; echo $? > "$exit"; } | tee "$log"
return $(cat "$exit")
}