Skip to content

Commit

Permalink
ds-identify: Improve ds-identify testing flexibility (#5047)
Browse files Browse the repository at this point in the history
- DI_MAIN: if DI_MAIN isn't a builtin function, exec() it
- /usr/libexec/ds-identify-env may be sourced to set variables

The purpose of these changes is to improve ds-identify testing capabilities and to allow testing alternative ds-identify implementations. Use of these capabilities is not supported in cloud-init deployments and therefore generates warnings in the ds-identify log.

Systemd gives generators no ability to run with custom environment variables. To set ds-identify variables at runtime, sourcing an environment file is required.

Example no-op side-loading with env var:

    $ DI_MAIN='echo' ./tools/ds-identify "hello world"
    WARN: side-loading alternate implementation: [echo]
    hello world

Example no-op side-loading with environment variable config:

    $ PATH_ROOT=. ./tools/ds-identify "test"
    WARN: loading environment file [./usr/libexec/ds-identify-env]
    WARN: side-loading alternate implementation: [echo]
    test
    $ cat ./usr/libexec/ds-identify-env
    export DI_MAIN=echo

Example side-loader that can only identify one cloud:

    $ cat nocloud-identifier.sh
    #!/bin/sh
    echo "creating nocloud config"
    echo "datasource_list: [ NoCloud ]" > cloud.cfg
    $ DI_MAIN=./nocloud-identifier.sh ./tools/ds-identify
    WARN: side-loading alternate implementation: [./nocloud-identifier.sh]
    creating nocloud config
    $ cat cloud.cfg
    datasource_list: [ NoCloud ]
  • Loading branch information
holmanb committed Mar 18, 2024
1 parent b1bfa59 commit d8e3a4b
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion tools/ds-identify
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ PATH_SYS_CLASS_BLOCK=${PATH_SYS_CLASS_BLOCK:-${PATH_ROOT}/sys/class/block}
PATH_DEV_DISK="${PATH_DEV_DISK:-${PATH_ROOT}/dev/disk}"
PATH_VAR_LIB_CLOUD="${PATH_VAR_LIB_CLOUD:-${PATH_ROOT}/var/lib/cloud}"
PATH_DI_CONFIG="${PATH_DI_CONFIG:-${PATH_ROOT}/etc/cloud/ds-identify.cfg}"
PATH_DI_ENV="${PATH_DI_ENV:-${PATH_ROOT}/usr/libexec/ds-identify-env}"
PATH_PROC_CMDLINE="${PATH_PROC_CMDLINE:-${PATH_ROOT}/proc/cmdline}"
PATH_PROC_1_CMDLINE="${PATH_PROC_1_CMDLINE:-${PATH_ROOT}/proc/1/cmdline}"
PATH_PROC_1_ENVIRON="${PATH_PROC_1_ENVIRON:-${PATH_ROOT}/proc/1/environ}"
Expand Down Expand Up @@ -1961,6 +1962,16 @@ set_run_path() {
DI_LOG="${DI_LOG:-${PATH_RUN_CI}/ds-identify.log}"
}

# set ds-identify internal variables by providing an env file
# testing only - NOT use for production code, it is NOT supported
get_environment() {
if [ -f "$PATH_DI_ENV" ]; then
debug 0 "WARN: loading environment file [${PATH_DI_ENV}]";
# shellcheck source=/dev/null
. "$PATH_DI_ENV"
fi
}

_main() {
local dscheck_fn="" ret_dis=1 ret_en=0

Expand Down Expand Up @@ -2087,6 +2098,7 @@ _main() {

main() {
local ret=""
get_environment
ensure_sane_path
read_uname_info
set_run_path
Expand Down Expand Up @@ -2116,7 +2128,14 @@ noop() {
:
}

get_environment
case "${DI_MAIN}" in
# builtin DI_MAIN implementations
main|print_info|noop) "${DI_MAIN}" "$@";;
*) error "unexpected value for DI_MAIN"; exit 1;;

# side-load an alternate implementation
# testing only - NOT use for production code, it is NOT supported
*)
debug 0 "WARN: side-loading alternate implementation: [${DI_MAIN}]";
exec "${DI_MAIN}" "$@";;
esac

0 comments on commit d8e3a4b

Please sign in to comment.