docker/sudo: better control/info over CLI relaunch args; introduce CLI relaunch envs

- allow adding re-launch environments by populating `ARMBIAN_CLI_RELAUNCH_PARAMS` dict
- similar to `ARMBIAN_CLI_RELAUNCH_PARAMS`
- rename/consolidate `()`'s output globals:
  - `ARMBIAN_CLI_FINAL_RELAUNCH_ARGS` (rename)
  - `ARMBIAN_CLI_FINAL_RELAUNCH_ENVS` (new)
    - includes `ARMBIAN_HIDE_REPEAT_PARAMS` automatically
- `sudo`: include envs when relaunching, use bash explicitly
- `docker`: better logging
- `docker`: include envs when relaunching via `--env` args
This commit is contained in:
Ricardo Pardini
2023-04-20 18:36:00 +02:00
parent 9a073d06fb
commit d0716b65c0
4 changed files with 49 additions and 18 deletions

View File

@@ -55,9 +55,6 @@ function cli_docker_run() {
ARMBIAN_CLI_RELAUNCH_PARAMS+=(["ARMBIAN_BUILD_UUID"]="${ARMBIAN_BUILD_UUID}") # pass down our uuid to the docker instance
ARMBIAN_CLI_RELAUNCH_PARAMS+=(["SKIP_LOG_ARCHIVE"]="yes") # launched docker instance will not cleanup logs.
declare -g ARMBIAN_CLI_RELAUNCH_ARGS=()
produce_relaunch_parameters # produces ARMBIAN_CLI_RELAUNCH_ARGS
case "${DOCKER_SUBCMD}" in
shell)
display_alert "Launching Docker shell" "docker-shell" "info"
@@ -73,7 +70,7 @@ function cli_docker_run() {
# this does NOT exit with the same exit code as the docker instance.
# instead, it sets the docker_exit_code variable.
declare -i docker_exit_code docker_produced_logs=0
docker_cli_launch "${ARMBIAN_CLI_RELAUNCH_ARGS[@]}" # MARK: this "re-launches" using the passed params.
docker_cli_launch # MARK: this "re-launches"
# Set globals to avoid:
# 1) showing the controlling host's log; we only want to show a ref to the Docker logfile, unless it didn't produce one.

View File

@@ -106,6 +106,7 @@ function armbian_register_commands() {
# Keep a running dict of params/variables. Can't repeat stuff here. Dict.
declare -g -A ARMBIAN_CLI_RELAUNCH_PARAMS=(["ARMBIAN_RELAUNCHED"]="yes")
declare -g -A ARMBIAN_CLI_RELAUNCH_ENVS=(["ARMBIAN_RELAUNCHED"]="yes")
# Keep a running array of config files needed for relaunch.
declare -g -a ARMBIAN_CLI_RELAUNCH_CONFIGS=()

View File

@@ -181,24 +181,44 @@ function parse_each_cmdline_arg_as_command_param_or_config() {
}
# Produce relaunch parameters. Add the running configs, arguments, and command.
# Declare and use ARMBIAN_CLI_RELAUNCH_ARGS as "${ARMBIAN_CLI_RELAUNCH_ARGS[@]}"
# Declare and use ARMBIAN_CLI_FINAL_RELAUNCH_ARGS as "${ARMBIAN_CLI_FINAL_RELAUNCH_ARGS[@]}"
# Also ARMBIAN_CLI_FINAL_RELAUNCH_ENVS as "${ARMBIAN_CLI_FINAL_RELAUNCH_ENVS[@]}"
function produce_relaunch_parameters() {
declare -g -a ARMBIAN_CLI_RELAUNCH_ARGS=()
declare -g -a ARMBIAN_CLI_FINAL_RELAUNCH_ARGS=()
declare -g -a ARMBIAN_CLI_FINAL_RELAUNCH_ENVS=()
declare hide_repeat_params=()
# add the running parameters from ARMBIAN_CLI_RELAUNCH_PARAMS dict
for param in "${!ARMBIAN_CLI_RELAUNCH_PARAMS[@]}"; do
ARMBIAN_CLI_RELAUNCH_ARGS+=("${param}=${ARMBIAN_CLI_RELAUNCH_PARAMS[${param}]}")
ARMBIAN_CLI_FINAL_RELAUNCH_ARGS+=("${param}=${ARMBIAN_CLI_RELAUNCH_PARAMS[${param}]}")
# If the param is not a key of ARMBIAN_PARSED_CMDLINE_PARAMS (eg was added for re-launching), add it to the hide list
if [[ -z "${ARMBIAN_PARSED_CMDLINE_PARAMS[${param}]}" ]]; then
hide_repeat_params+=("${param}")
fi
done
# add the running configs
for config in "${ARMBIAN_CLI_RELAUNCH_CONFIGS[@]}"; do
ARMBIAN_CLI_RELAUNCH_ARGS+=("${config}")
ARMBIAN_CLI_FINAL_RELAUNCH_ARGS+=("${config}")
done
# add the command; defaults to the last command, but can be changed by the last pre-run.
if [[ -n "${ARMBIAN_CLI_RELAUNCH_COMMAND}" ]]; then
ARMBIAN_CLI_RELAUNCH_ARGS+=("${ARMBIAN_CLI_RELAUNCH_COMMAND}")
ARMBIAN_CLI_FINAL_RELAUNCH_ARGS+=("${ARMBIAN_CLI_RELAUNCH_COMMAND}")
else
ARMBIAN_CLI_RELAUNCH_ARGS+=("${ARMBIAN_COMMAND}")
ARMBIAN_CLI_FINAL_RELAUNCH_ARGS+=("${ARMBIAN_COMMAND}")
fi
display_alert "Produced relaunch args:" "ARMBIAN_CLI_RELAUNCH_ARGS: ${ARMBIAN_CLI_RELAUNCH_ARGS[*]}" "debug"
# These two envs are always included.
ARMBIAN_CLI_FINAL_RELAUNCH_ENVS+=("ARMBIAN_ORIGINAL_BUILD_UUID=${ARMBIAN_BUILD_UUID}")
ARMBIAN_CLI_FINAL_RELAUNCH_ENVS+=("ARMBIAN_HIDE_REPEAT_PARAMS=${hide_repeat_params[*]}")
# Add all values from ARMBIAN_CLI_RELAUNCH_ENVS dict
for env in "${!ARMBIAN_CLI_RELAUNCH_ENVS[@]}"; do
ARMBIAN_CLI_FINAL_RELAUNCH_ENVS+=("${env}=${ARMBIAN_CLI_RELAUNCH_ENVS[${env}]}")
done
display_alert "Produced relaunch args:" "ARMBIAN_CLI_FINAL_RELAUNCH_ARGS: ${ARMBIAN_CLI_FINAL_RELAUNCH_ARGS[*]}" "debug"
display_alert "Produced relaunch envs:" "ARMBIAN_CLI_FINAL_RELAUNCH_ENVS: ${ARMBIAN_CLI_FINAL_RELAUNCH_ENVS[*]}" "debug"
}
function cli_standard_relaunch_docker_or_sudo() {
@@ -250,10 +270,11 @@ function cli_standard_relaunch_docker_or_sudo() {
fi
display_alert "This script requires root privileges; Docker is unavailable" "trying to use sudo" "wrn"
declare -g ARMBIAN_CLI_RELAUNCH_ARGS=()
produce_relaunch_parameters # produces ARMBIAN_CLI_RELAUNCH_ARGS
declare -g ARMBIAN_CLI_FINAL_RELAUNCH_ARGS=()
declare -g ARMBIAN_CLI_FINAL_RELAUNCH_ENVS=()
produce_relaunch_parameters # produces ARMBIAN_CLI_FINAL_RELAUNCH_ARGS and ARMBIAN_CLI_FINAL_RELAUNCH_ENVS
# shellcheck disable=SC2093 # re-launching under sudo: replace the current shell, and never return.
exec sudo --preserve-env "${SRC}/compile.sh" "${ARMBIAN_CLI_RELAUNCH_ARGS[@]}" # MARK: relaunch done here!
display_alert "AFTER SUDO!!!" "AFTER SUDO!!!" "warn"
exec sudo --preserve-env "${ARMBIAN_CLI_FINAL_RELAUNCH_ENVS[@]}" bash "${SRC}/compile.sh" "${ARMBIAN_CLI_FINAL_RELAUNCH_ARGS[@]}" # MARK: relaunch done here!
display_alert "AFTER SUDO!!!" "AFTER SUDO!!!" "warn" # This should _never_ happen
fi
}

View File

@@ -247,6 +247,7 @@ function docker_cli_prepare_dockerfile() {
# initialize the extension manager; enable all extensions; only once..
if [[ "${docker_prepare_cli_skip_exts:-no}" != "yes" ]]; then
display_alert "Docker launcher" "enabling all extensions looking for Docker dependencies" "info"
enable_all_extensions_builtin_and_user
initialize_extension_manager
fi
@@ -546,7 +547,8 @@ function docker_cli_prepare_launch() {
}
function docker_cli_launch() {
display_alert "Showing Docker cmdline" "Docker args: '${DOCKER_ARGS[*]}'" "debug"
# rpardini: This debug, although useful, might include very long/multiline strings, which make it very confusing.
# display_alert "Showing Docker cmdline" "Docker args: '${DOCKER_ARGS[*]}'" "debug"
# Hack: if we're running on a Mac/Darwin, get rid of .DS_Store files in critical directories.
if [[ "${OSTYPE}" == "darwin"* ]]; then
@@ -557,11 +559,21 @@ function docker_cli_launch() {
run_host_command_logged find "${SRC}/userpatches" -name ".DS_Store" -type f -delete "||" true
fi
display_alert "Relaunching in Docker" "${*}" "debug"
# Produce the re-launch params.
declare -g ARMBIAN_CLI_FINAL_RELAUNCH_ARGS=()
declare -g ARMBIAN_CLI_FINAL_RELAUNCH_ENVS=()
produce_relaunch_parameters # produces ARMBIAN_CLI_FINAL_RELAUNCH_ARGS and ARMBIAN_CLI_FINAL_RELAUNCH_ENVS
# Add the relaunch envs to DOCKER_ARGS.
for env in "${ARMBIAN_CLI_FINAL_RELAUNCH_ENVS[@]}"; do
display_alert "Adding Docker env" "${env}" "debug"
DOCKER_ARGS+=("--env" "${env}")
done
display_alert "-----------------Relaunching in Docker after ${SECONDS}s------------------" "here comes the 🐳" "info"
local -i docker_build_result
if docker run "${DOCKER_ARGS[@]}" "${DOCKER_ARMBIAN_INITIAL_IMAGE_TAG}" /bin/bash "${DOCKER_ARMBIAN_TARGET_PATH}/compile.sh" "$@"; then
if docker run "${DOCKER_ARGS[@]}" "${DOCKER_ARMBIAN_INITIAL_IMAGE_TAG}" /bin/bash "${DOCKER_ARMBIAN_TARGET_PATH}/compile.sh" "${ARMBIAN_CLI_FINAL_RELAUNCH_ARGS[@]}"; then
docker_build_result=$? # capture exit code of test done in the line above.
display_alert "-------------Docker run finished after ${SECONDS}s------------------------" "🐳 successfull" "info"
else