2023-03-09 18:30:40 +01:00
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2013-2023 Igor Pecovnik, igor@armbian.com
#
# This file is a part of the Armbian Build Framework
# https://github.com/armbian/build/
2022-10-09 17:58:23 +02:00
function cli_docker_pre_run() {
if [[ " ${ DOCKERFILE_GENERATE_ONLY } " == "yes" ]] ; then
display_alert "Dockerfile generation only" "func cli_docker_pre_run" "debug"
return 0
fi
2023-01-30 22:48:35 +01:00
case " ${ DOCKER_SUBCMD } " in
shell)
# inside-function-function: a dynamic hook, only triggered if this CLI runs.
function add_host_dependencies__ssh_client_for_docker_shell_over_ssh() {
2023-03-28 20:20:56 +02:00
declare -g EXTRA_BUILD_DEPS = " ${ EXTRA_BUILD_DEPS } openssh-client"
2023-01-30 22:48:35 +01:00
}
declare -g DOCKER_PASS_SSH_AGENT = "yes" # Pass SSH agent to docker
;;
esac
2022-10-09 17:58:23 +02:00
# make sure we're not _ALREADY_ running under docker... otherwise eternal loop?
if [[ " ${ ARMBIAN_RUNNING_IN_CONTAINER } " == "yes" ]] ; then
2023-01-29 17:07:02 +01:00
exit_with_error "asking for docker... inside docker. how did this happen? Tip: you don't need 'docker' to run armbian-next inside Docker; it's automatically detected and used when appropriate."
2022-10-09 17:58:23 +02:00
fi
}
function cli_docker_run() {
2023-02-01 15:39:19 +01:00
# Docker won't have ${SRC}/.git, so precalculate the git-info header so it can be included in the inside-Docker logs.
# It's gonna be picked up by export_ansi_logs() and included in the final log, if it exists.
declare -g GIT_INFO_ANSI
GIT_INFO_ANSI = " $( prepare_ansi_git_info_log_header) "
2025-01-03 11:44:00 +01:00
# GIT_INFO_ANSI can grow to be quite large if there are many changed files.
# If it's too big, it will cause "argument list too long" errors when launching docker.
# Limit it to 1024 characters, otherwise replace it with a simple message.
if [[ ${# GIT_INFO_ANSI } -gt 1024 ]] ; then
GIT_INFO_ANSI = "Armbian: too many git changes to list."
fi
2023-02-01 15:39:19 +01:00
2023-05-24 16:32:31 +02:00
# Same stuff for BUILD_REPOSITORY_URL and BUILD_REPOSITORY_COMMIT.
if [[ -d " ${ SRC } /.git" && " ${ CONFIG_DEFS_ONLY } " != "yes" ]] ; then # don't waste time if only gathering config defs
set_git_build_repo_url_and_commit_vars "docker launcher"
fi
2022-10-09 17:58:23 +02:00
LOG_SECTION = "docker_cli_prepare" do_with_logging docker_cli_prepare
2023-01-29 12:41:49 +01:00
# @TODO: and can be very well said that in CI, we always want FAST_DOCKER=yes, unless we're building the Docker image itself.
if [[ " ${ FAST_DOCKER :- "no" } " != "yes" ]] ; then # "no, I want *slow* docker" -- no one, ever
LOG_SECTION = "docker_cli_prepare_dockerfile" do_with_logging docker_cli_prepare_dockerfile
2023-01-30 13:25:02 +01:00
if [[ " ${ DOCKERFILE_GENERATE_ONLY } " == "yes" ]] ; then
display_alert "Dockerfile generated" "exiting" "info"
exit 0
fi
2023-01-29 12:41:49 +01:00
LOG_SECTION = "docker_cli_build_dockerfile" do_with_logging docker_cli_build_dockerfile
fi
2022-10-09 17:58:23 +02:00
LOG_SECTION = "docker_cli_prepare_launch" do_with_logging docker_cli_prepare_launch
ARMBIAN_CLI_RELAUNCH_PARAMS +=([ "SET_OWNER_TO_UID" ]= " ${ EUID } " ) # fix the owner of files to our UID
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.
2025-09-05 03:16:20 -04:00
if [[ -n " ${ DOCKER_NICE :- } " ]] ; then
ARMBIAN_CLI_RELAUNCH_PARAMS +=([ "DOCKER_NICE" ]= " ${ DOCKER_NICE } " ) # propagated `nice` value
fi
2022-10-09 17:58:23 +02:00
2024-02-01 20:10:33 +05:30
# 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
2022-11-30 11:16:17 +01:00
case " ${ DOCKER_SUBCMD } " in
shell)
display_alert "Launching Docker shell" "docker-shell" "info"
2024-07-12 23:35:29 +03:00
docker run -it " ${ DOCKER_ARGS [@] } " " ${ DOCKER_ARMBIAN_INITIAL_IMAGE_TAG } " /bin/bash
2022-11-30 11:16:17 +01:00
;;
purge)
display_alert "Purging unused Docker volumes" "docker-purge" "info"
docker_purge_deprecated_volumes
;;
*)
2023-01-18 21:03:13 +01:00
# 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
2023-04-20 18:36:00 +02:00
docker_cli_launch # MARK: this "re-launches"
2023-01-18 21:03:13 +01:00
# 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.
2023-01-21 02:45:24 +01:00
# If it did produce one, it's "link" is already shown above.
2023-01-18 21:03:13 +01:00
if [[ $docker_produced_logs -gt 0 ]] ; then
declare -g show_message_after_export = "skip" # handled by export_ansi_logs()
fi
# 2) actually exiting with the same error code as the docker instance, but without triggering an error.
declare -g -i global_final_exit_code = $docker_exit_code # handled by .... @TODO
2022-11-30 11:16:17 +01:00
;;
2023-01-18 21:03:13 +01:00
2022-11-30 11:16:17 +01:00
esac
2022-10-09 17:58:23 +02:00
}