From bc7d388b493a9793b4d69ef3d02fa3ce465d5cd6 Mon Sep 17 00:00:00 2001 From: Ricardo Pardini Date: Fri, 28 Oct 2022 00:08:43 +0200 Subject: [PATCH] armbian-next: `cli`: a bit more untangling of re-launching; introduce `ARMBIAN_CLI_RELAUNCH_COMMAND` and `cli_standard_relaunch_docker_or_sudo()` - `configdump` cli: force root/docker --- lib/functions/cli/cli-build.sh | 33 ++--------------------- lib/functions/cli/cli-configdump.sh | 4 ++- lib/functions/cli/cli-docker.sh | 3 +-- lib/functions/cli/cli-requirements.sh | 6 ++--- lib/functions/cli/commands.sh | 17 ++++++------ lib/functions/cli/utils-cli.sh | 38 +++++++++++++++++++++++++-- lib/functions/host/docker.sh | 8 +++++- 7 files changed, 59 insertions(+), 50 deletions(-) diff --git a/lib/functions/cli/cli-build.sh b/lib/functions/cli/cli-build.sh index 7cfbd2394b..8e0d154361 100644 --- a/lib/functions/cli/cli-build.sh +++ b/lib/functions/cli/cli-build.sh @@ -1,37 +1,8 @@ function cli_standard_build_pre_run() { declare -g ARMBIAN_COMMAND_REQUIRE_BASIC_DEPS="yes" # Require prepare_host_basic to run before the command. - # Super early handling. If no command and not root, become root by using sudo. Some exceptions apply. - if [[ "${EUID}" == "0" ]]; then # we're already root. Either running as real root, or already sudo'ed. - display_alert "Already running as root" "great" "debug" - else # not root. - # Pass the current UID to any further relaunchings (under docker or sudo). - ARMBIAN_CLI_RELAUNCH_PARAMS+=(["SET_OWNER_TO_UID"]="${EUID}") # add params when relaunched under docker - - # We've a few options. - # 1) We could check if Docker is working, and do everything under Docker. Users who can use Docker, can "become" root inside a container. - # 2) We could ask for sudo (which _might_ require a password)... - # @TODO: GitHub actions can do both. Sudo without password _and_ Docker; should we prefer Docker? Might have unintended consequences... - if is_docker_ready_to_go; then - # add the current user EUID as a parameter when it's relaunched under docker. SET_OWNER_TO_UID="${EUID}" - display_alert "Trying to build, not root, but Docker is ready to go" "delegating to Docker" "debug" - ARMBIAN_CLI_RELAUNCH_PARAMS+=(["DOCKER_CLI_CMD"]="build") # add params when relaunched under docker - ARMBIAN_CHANGE_COMMAND_TO="docker" - return 0 - fi - - # check if we're on Linux via uname. if not, refuse to do anything. - if [[ "$(uname)" != "Linux" ]]; then - display_alert "Not running on Linux; Docker is not available" "refusing to run" "err" - exit 1 - 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 - sudo --preserve-env "${SRC}/compile.sh" "${ARMBIAN_CLI_RELAUNCH_ARGS[@]}" # MARK: relaunch done here! - display_alert "AFTER SUDO!!!" "AFTER SUDO!!!" "warn" - fi + # "gimme root on a Linux machine" + cli_standard_relaunch_docker_or_sudo } function cli_standard_build_run() { diff --git a/lib/functions/cli/cli-configdump.sh b/lib/functions/cli/cli-configdump.sh index bea3677ba9..e3585e3ad5 100644 --- a/lib/functions/cli/cli-configdump.sh +++ b/lib/functions/cli/cli-configdump.sh @@ -1,9 +1,11 @@ function cli_config_dump_pre_run() { declare -g CONFIG_DEFS_ONLY='yes' + # "gimme root on a Linux machine" + cli_standard_relaunch_docker_or_sudo } function cli_config_dump_run() { # configuration etc - it initializes the extension manager do_capturing_defs prepare_and_config_main_build_single # this sets CAPTURED_VARS - echo "${CAPTURED_VARS}" # to stdout! + echo "${CAPTURED_VARS}" # to stdout! } diff --git a/lib/functions/cli/cli-docker.sh b/lib/functions/cli/cli-docker.sh index 92b02e3d9a..c61f45b01d 100644 --- a/lib/functions/cli/cli-docker.sh +++ b/lib/functions/cli/cli-docker.sh @@ -6,8 +6,7 @@ function cli_docker_pre_run() { # make sure we're not _ALREADY_ running under docker... otherwise eternal loop? if [[ "${ARMBIAN_RUNNING_IN_CONTAINER}" == "yes" ]]; then - display_alert "DOCKER_CLI_CMD" "asking for docker... inside docker; turning to '${DOCKER_CLI_CMD}' command" "warn" - ARMBIAN_CHANGE_COMMAND_TO="${DOCKER_CLI_CMD}" + exit_with_error "asking for docker... inside docker. how did this happen? so sorry." fi } diff --git a/lib/functions/cli/cli-requirements.sh b/lib/functions/cli/cli-requirements.sh index 2a8598509f..fe848a7a5e 100644 --- a/lib/functions/cli/cli-requirements.sh +++ b/lib/functions/cli/cli-requirements.sh @@ -15,10 +15,8 @@ function cli_requirements_pre_run() { } function cli_requirements_run() { - declare -g REQUIREMENTS_DEFS_ONLY='yes' # @TODO: decide, this is already set in ARMBIAN_COMMANDS_TO_VARS_DICT - declare -a -g host_dependencies=() early_prepare_host_dependencies # tests itself for REQUIREMENTS_DEFS_ONLY=yes too - install_host_dependencies "for REQUIREMENTS_DEFS_ONLY=yes" - display_alert "Done with" "REQUIREMENTS_DEFS_ONLY" "cachehit" + LOG_SECTION="install_host_dependencies" do_with_logging install_host_dependencies "for requirements command" + display_alert "Done with" "@host dependencies" "cachehit" } diff --git a/lib/functions/cli/commands.sh b/lib/functions/cli/commands.sh index fe5efa3bab..9b2700e7dc 100644 --- a/lib/functions/cli/commands.sh +++ b/lib/functions/cli/commands.sh @@ -2,10 +2,10 @@ function armbian_register_commands() { # More than one command can map to the same handler. In that case, use ARMBIAN_COMMANDS_TO_VARS_DICT for specific vars. declare -g -A ARMBIAN_COMMANDS_TO_HANDLERS_DICT=( ["docker"]="docker" # thus requires cli_docker_pre_run and cli_docker_run - ["docker-purge"]="docker" # idem - ["dockerpurge"]="docker" # idem - ["docker-shell"]="docker" # idem - ["dockershell"]="docker" # idem + ["docker-purge"]="docker" # idem @TODO unimplemented!!! + ["dockerpurge"]="docker" # idem @TODO unimplemented!!! + ["docker-shell"]="docker" # idem @TODO unimplemented!!! + ["dockershell"]="docker" # idem @TODO unimplemented!!! ["generate-dockerfile"]="docker" # idem ["vagrant"]="vagrant" # thus requires cli_vagrant_pre_run and cli_vagrant_run @@ -22,11 +22,10 @@ function armbian_register_commands() { # Vars to be set for each command. Optional. declare -g -A ARMBIAN_COMMANDS_TO_VARS_DICT=( - ["docker-purge"]="DOCKER_SUBCMD='purge'" - ["dockerpurge"]="DOCKER_SUBCMD='purge'" - - ["docker-shell"]="DOCKER_SUBCMD='shell'" - ["dockershell"]="DOCKER_SUBCMD='shell'" + ["docker-purge"]="DOCKER_SUBCMD='purge'" # @TODO unimplemented! + ["dockerpurge"]="DOCKER_SUBCMD='purge'" # @TODO unimplemented! + ["docker-shell"]="DOCKER_SUBCMD='shell'" # @TODO unimplemented! + ["dockershell"]="DOCKER_SUBCMD='shell'" # @TODO unimplemented! ["generate-dockerfile"]="DOCKERFILE_GENERATE_ONLY='yes'" diff --git a/lib/functions/cli/utils-cli.sh b/lib/functions/cli/utils-cli.sh index 7ec6fe9c6a..c349500add 100644 --- a/lib/functions/cli/utils-cli.sh +++ b/lib/functions/cli/utils-cli.sh @@ -178,6 +178,40 @@ function produce_relaunch_parameters() { ARMBIAN_CLI_RELAUNCH_ARGS+=("${config}") done display_alert "Produced relaunch args:" "ARMBIAN_CLI_RELAUNCH_ARGS: ${ARMBIAN_CLI_RELAUNCH_ARGS[*]}" "debug" - # @TODO: add the command. if we have one. - + # 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}") + fi +} + +function cli_standard_relaunch_docker_or_sudo() { + if [[ "${EUID}" == "0" ]]; then # we're already root. Either running as real root, or already sudo'ed. + display_alert "Already running as root" "great, running '${ARMBIAN_COMMAND}' normally" "debug" + else # not root. + # Pass the current UID to any further relaunchings (under docker or sudo). + ARMBIAN_CLI_RELAUNCH_PARAMS+=(["SET_OWNER_TO_UID"]="${EUID}") # add params when relaunched under docker + + # We've a few options. + # 1) We could check if Docker is working, and do everything under Docker. Users who can use Docker, can "become" root inside a container. + # 2) We could ask for sudo (which _might_ require a password)... + # @TODO: GitHub actions can do both. Sudo without password _and_ Docker; should we prefer Docker? Might have unintended consequences... + if is_docker_ready_to_go; then + display_alert "Trying to build, not root, but Docker is ready to go" "delegating to Docker" "debug" + ARMBIAN_CHANGE_COMMAND_TO="docker" + ARMBIAN_CLI_RELAUNCH_COMMAND="${ARMBIAN_COMMAND}" # add params when relaunched under docker + return 0 + fi + + # check if we're on Linux via uname. if not, refuse to do anything. + if [[ "$(uname)" != "Linux" ]]; then + display_alert "Not running on Linux; Docker is not available" "refusing to run" "err" + exit 1 + 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 + sudo --preserve-env "${SRC}/compile.sh" "${ARMBIAN_CLI_RELAUNCH_ARGS[@]}" # MARK: relaunch done here! + display_alert "AFTER SUDO!!!" "AFTER SUDO!!!" "warn" + fi } diff --git a/lib/functions/host/docker.sh b/lib/functions/host/docker.sh index 4b85dce596..27b7cfca4b 100755 --- a/lib/functions/host/docker.sh +++ b/lib/functions/host/docker.sh @@ -39,7 +39,7 @@ function is_docker_ready_to_go() { display_alert "Can't use Docker" "Actually ALREADY UNDER DOCKER!" "debug" return 1 fi - if [[ ! -n "$(command -v docker)" ]]; then + if [[ -z "$(command -v docker)" ]]; then display_alert "Can't use Docker" "docker command not found" "debug" return 1 fi @@ -353,6 +353,12 @@ function docker_cli_prepare_launch() { display_alert "Skipping /dev/loop* hacks for" "${DOCKER_ARMBIAN_HOST_OS_UNAME}" "debug" fi + # if DOCKER_EXTRA_ARGS is an array and has more than zero elements, add its contents to the DOCKER_ARGS array + if [[ "${DOCKER_EXTRA_ARGS[*]+isset}" == "isset" && "${#DOCKER_EXTRA_ARGS[@]}" -gt 0 ]]; then + display_alert "Adding extra Docker arguments" "${DOCKER_EXTRA_ARGS[*]}" "debug" + DOCKER_ARGS+=("${DOCKER_EXTRA_ARGS[@]}") + fi + } function docker_cli_launch() {