1
0
mirror of https://gitlab.alpinelinux.org/alpine/cloud/tiny-cloud.git synced 2025-12-15 11:22:43 +03:00

Install UserData Handlers

Also attempt to tweak some logging...  Probably some more refinement with that.
This commit is contained in:
Jake Buchholz Göktürk 2023-05-13 13:50:07 -07:00
parent 13b0eef9fe
commit 9a00dd686d
7 changed files with 50 additions and 37 deletions

View File

@ -14,6 +14,10 @@ core:
lib/tiny-cloud/init \
lib/tiny-cloud/mdev \
lib/tiny-cloud/tiny-cloud.conf
install -Dm644 -t "$(PREFIX)"/lib/tiny-cloud/user-data" \
lib/tiny-cloud/user-data/missing \
lib/tiny-cloud/user-data/script \
lib/tiny-cloud/user-data/unknown
install -Dm644 lib/tiny-cloud/tiny-cloud.conf \
"$(PREFIX)"/etc/tiny-cloud.conf
install -Dm755 -t "$(PREFIX)"/sbin \
@ -60,6 +64,8 @@ alpine:
install -Dm644 -t $(PREFIX)/lib/tiny-cloud/cloud/alpine \
lib/tiny-cloud/cloud/alpine/init
ln -s ../nocloud/imds $(PREFIX)/lib/tiny-cloud/cloud/alpine/imds
install -Dm644 -t "$(PREFIX)"/lib/tiny-cloud/user-data" \
lib/tiny-cloud/user-data/alpine-config
check: tests/Kyuafile Kyuafile
kyua test || (kyua report --verbose && exit 1)

View File

@ -1,6 +1,9 @@
#!/bin/sh
# vim:set ts=2 et:
# NOTE: The mdev-conf APK handles this now, but only for xvd or sd links (not
# both)
: "${LIBDIR:=$PREFIX/lib}"
. "$LIBDIR/tiny-cloud/common"

View File

@ -10,10 +10,11 @@
log() {
local facility="local7"
local stderr
local stderr init
local tag=$(basename "$0")
while [ "${1#-}" != "$1" ]; do
case "$1" in
-i) init=1 ;; # TODO: value = indent?
-f) facility="$2"; shift ;;
-s) stderr=-s ;;
-t) tag="$tag/$2"; shift ;;
@ -24,6 +25,7 @@ log() {
[ -z "$DEBUG" ] && [ "$level" = debug ] && return
shift
[ -n "$init" ] && echo "$0" >&2
logger $stderr -p "$facility.$level" -t "$tag[$$]" "$@"
case "$level" in
crit|alert|emerg) exit 1 ;;

View File

@ -70,21 +70,21 @@ init__install_hotplugs() {
printf ': ' >&2
for module in $HOTPLUG_MODULES; do
result='?'
result='unknown'
level='err'
printf "$module" >&2
log info "$phase $ACTION $module"
printf " >> " >&2
log -i -t "$phase/$ACTION" info "$module: installing"
if type "mod__$module" | grep -q -w "function"; then
if "mod__$module"; then
result='+'
result='installed'
level='info'
else
result='!'
result='failed'
rc=1
fi
fi
printf '(%s) ' $result >&2
log "$level" "$phase $ACTION $module ($result)"
printf " >> " >&2
log -i -t "$phase/$ACTION" info "$module: $result"
done
return $rc
}
@ -145,8 +145,7 @@ auto_detect_ethernet_interface() {
init__set_default_interfaces() {
if [ -f "$ROOT"/etc/network/interfaces ]; then
echo "already set up" >&2
log info "$phase $ACTION - already set up"
log -i -t "$phase" info "$ACTION: already set up"
return
fi
@ -171,8 +170,7 @@ init__create_default_user() {
local user="$CLOUD_USER"
# don't do anything if it already exists
if getent passwd "$user" >/dev/null; then
echo "already exists" >&2
log info "$phase $ACTION - already exists"
log -i -t "$phase" info "$ACTION: already exists"
return
fi
@ -202,15 +200,13 @@ init__enable_sshd() {
init__set_hostname() {
local fqdn=$(imds @hostname)
if [ -z "$fqdn" ]; then
echo "no hostname set" >&2
log info "No hostname set"
log -i -t "$phase" info "$ACTION: no hostname set"
return
fi
local host="${fqdn%%\.*}"
if [ -z "$host" ]; then
echo "$hostname is not a valid FQDN" >&2
log err "$fqdn is not a valid FQDN"
log -i -t "$phase" warn "$ACTION: invalid hostname '$fqdn'"
return 1
fi
@ -224,8 +220,7 @@ init__set_ssh_keys() {
local user="$CLOUD_USER"
local pwent="$(getent passwd "$user")"
if [ -z "$pwent" ]; then
echo "failed to find $user" >&2
log err "Failed to find user $user"
log -i -t "$phase" err "$ACTION: failed to find user $user"
return 1
fi
local group=$(echo "$pwent" | cut -d: -f4)
@ -246,7 +241,7 @@ init__set_ssh_keys() {
init__save_userdata() {
local userdata="$TINY_CLOUD_VAR/user-data"
if [ -f "$userdata" ]; then
log info "user-data already saved"
log -i -t "$phase" info "$ACTION: user-data already saved"
return
fi
local tmpfile=$(mktemp "$userdata.XXXXXX")
@ -285,7 +280,6 @@ fi
### load user-data type-specific init functions / vars (potentially overriding)
# this should be non-overrideable, but need this before we...
userdata_type() {
if [ ! -f "$TINY_CLOUD_VAR/user-data" ]; then
echo missing
@ -302,5 +296,6 @@ userdata_type() {
USERDATA_TYPE="$(userdata_type)"
if [ -f "$LIBDIR/tiny-cloud/user-data/$USERDATA_TYPE" ]; then
. "$LIBDIR/tiny-cloud/user-data/$USERDATA_TYPE"
else
log -i warn "no user-data handler found for '$USERDATA_TYPE'"
fi
# TODO: some indication that the user-data type is unsupported?

View File

@ -1,4 +1,8 @@
# Missing UserData Functions
# vim:set ts=4 et ft=sh:
# TODO: what to do if we have NO user-data yet
init__missing_userdata() {
log -i -t "$phase" notice "$ACTION: no user-data found"
}
INIT_ACTIONS_MAIN="missing_userdata ${INIT_ACTIONS_MAIN}"

View File

@ -1,4 +1,9 @@
# Unknown UserData Functions
# vim:set ts=4 et ft=sh:
# TODO: this would probably be mostly NOOPs
init__unknown_userdata() {
local type="$(userdata_type)"
log -i -t "$phase" warn "$ACTION: unable to process '$type' user-data"
}
INIT_ACTIONS_MAIN="unknown_userdata ${INIT_ACTIONS_MAIN}"

View File

@ -34,13 +34,12 @@ while true; do
case "$1" in
complete) # indicate bootstrap is done
init__bootstrap_complete
log warn 'bootstrap marked complete';;
log -i notice 'bootstrap marked complete';;
incomplete) # indicate bootstrap isn't done
bootstrap_incomplete
log warn 'bootstrap marked incomplete';;
log -i warn 'bootstrap marked incomplete';;
*) usage >&2; exit 1;;
esac
printf ' bootstrap marked "%s"\n' "$1" >&2
exit 0;;
-s|--setup) # just openrc for now
for phase in -early -net '' -final; do
@ -67,8 +66,7 @@ esac
# is initial bootstrap already done?
if [ -f "$TINY_CLOUD_VAR/.bootstrap-complete" ]; then
printf ' already bootstrapped\n' >&2
log info "$phase - already bootstrapped"
log -i -t "$phase" info "already bootstrapped"
exit 0;
fi
@ -93,7 +91,7 @@ INIT_ACTIONS_FINAL="${INIT_ACTIONS_FINAL} bootstrap_complete"
case "$phase" in
early) INIT_ACTIONS="$INIT_ACTIONS_EARLY";;
net) INIT_ACTIONS="$INIT_ACTIONS_NET";;
net) INIT_ACTIONS="$INIT_ACTIONS_NET";;
main) INIT_ACTIONS="$INIT_ACTIONS_MAIN";;
final) INIT_ACTIONS="$INIT_ACTIONS_FINAL";;
*) usage >&2; exit 1
@ -101,23 +99,23 @@ esac
for ACTION in $INIT_ACTIONS; do
if skip_action "$ACTION"; then
printf '\n -- %s : [SKIP]' $ACTION >&2
log warn "$phase - $ACTION - SKIPPED"
printf '\n -- ' >&2
log -i -t "$phase" notice "$ACTION: skipped"
continue
fi
printf '\n ++ %s ' $ACTION >&2
log info "$phase - $ACTION - START"
RESULT="UNKNOWN"
printf '\n ++ ' >&2
log -i -t "$phase" info "$ACTION"
RESULT="unknown"
LEVEL="err"
if type "init__$ACTION" | grep -q -w "function"; then
if "init__$ACTION" "$@"; then
RESULT="DONE"
RESULT="done"
LEVEL="info"
else
RESULT="FAIL"
fi
fi
printf ': [%s]' $RESULT >&2
log "$LEVEL" "$phase - $ACTION - $RESULT"
printf ' ++ ' >&2
log -i -t "$phase" "$LEVEL" "$ACTION: $RESULT"
done
echo >&2