From d0716b65c0adb7bc5eee82786305b48f1c2c798c Mon Sep 17 00:00:00 2001 From: Ricardo Pardini Date: Thu, 20 Apr 2023 18:36:00 +0200 Subject: [PATCH] docker/sudo: better control/info over CLI relaunch args; introduce CLI relaunch envs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- lib/functions/cli/cli-docker.sh | 5 +--- lib/functions/cli/commands.sh | 1 + lib/functions/cli/utils-cli.sh | 43 ++++++++++++++++++++++++--------- lib/functions/host/docker.sh | 18 +++++++++++--- 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/lib/functions/cli/cli-docker.sh b/lib/functions/cli/cli-docker.sh index f781a22c74..33b6bb850f 100644 --- a/lib/functions/cli/cli-docker.sh +++ b/lib/functions/cli/cli-docker.sh @@ -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. diff --git a/lib/functions/cli/commands.sh b/lib/functions/cli/commands.sh index d5406e751c..1eec34e51b 100644 --- a/lib/functions/cli/commands.sh +++ b/lib/functions/cli/commands.sh @@ -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=() diff --git a/lib/functions/cli/utils-cli.sh b/lib/functions/cli/utils-cli.sh index faaafc93fd..9de6666630 100644 --- a/lib/functions/cli/utils-cli.sh +++ b/lib/functions/cli/utils-cli.sh @@ -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 } diff --git a/lib/functions/host/docker.sh b/lib/functions/host/docker.sh index d6136f83b0..60494259a8 100755 --- a/lib/functions/host/docker.sh +++ b/lib/functions/host/docker.sh @@ -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