mirror of
https://github.com/armbian/build.git
synced 2025-08-09 12:46:58 +02:00
- armbian-next: introduce `USE_LOCAL_APT_DEB_CACHE` (default `=yes`) as alternative/in addition to `apt-cacher-ng` (eg, in Docker) - this uses `cache/aptcache/${RELEASE}-${ARCH}` (in the host) for - apt cache, by bind-mounting it to `${SDCARD}/var/cache/apt` in the `chroot_sdcard_apt_get()` runner and its usages - debootstrap, by passing it `--cache-dir` - utility function to help understand what is happening to cache during usage - apt itself mantains this cache, removing old packages when new ones are installed. apt does this _by default_ - introduce `DONT_MAINTAIN_APT_CACHE=yes` to skip out of automatic apt maintenance of apt cache, eg, during `remove`s - don't do `apt clean` and such if using local cache, that would clean the cache, not the chroot - clean up `install_deb_chroot()` a little, find an unrelated bug there - WiP: the great cli entrypoint (+docker) rewrite, Phase 6: relaunching structure; re-pass ARMBIAN_BUILD_UUID; use ARMBIAN_COMMAND for log filename; fix for output/logs dir perms - WiP: the great cli entrypoint (+docker) rewrite, Phase 5: cleanups 4/x; better logging, check & force `DEST_LANG` - WiP: the great cli entrypoint (+docker) rewrite, Phase 5: cleanups 3/x; don't write to stderr in generated Dockerfile - it's `drastic red` on non-buildx dockers - WiP: the great cli entrypoint (+docker) rewrite, Phase 5: cleanups 2/x, logging - WiP: the great cli entrypoint (+docker) rewrite, Phase 5: cleanups 1/x - source configs in a logging section. - Docker: silent, fast retries to make sure `docker system df` works - shut-up `chown` (no `-v`) output related to `SET_OWNER_TO_UID` - ask user to wait while `DESTIMG` is rsync'ed to `FINALDEST` -- it's potentially very slow - use green apple for Mac logging, instead of red apple which might imply error... - WiP: the great cli entrypoint (+docker) rewrite, Phase 4: run as non-root, maybe-with-Docker - introduce `is_docker_ready_to_go()`; if it is, and we're not root, use Docker instead of sudo. <- GOOD IDEA? BAD IDEA? lol - introduce `SET_OWNER_TO_UID` var to be passed to Docker/sudo so written files are owned by the launching user, not root. - introduce `mkdir_recursive_and_set_uid_owner()` and `reset_uid_owner()` to reset owner based on `SET_OWNER_TO_UID` - use it for userpatches files created, logs, and output files, including images and debs. - @TODOs ref. `$SUDO_USER` which I think the old version of this? - add a lot of @TODOs, ref being able to relaunch something that's not `build` inside Docker, also add/change params and configs and command. - initially add `ARMBIAN_DOCKER_RELAUNCH_EXTRA_ARGS` - WiP: the great cli entrypoint (+docker) rewrite, Phase 3: rpardini is demented, v3 - WiP: the great cli entrypoint (+docker) rewrite, Phase 2: rpardini is demented - WiP: the great cli entrypoint (+docker) rewrite, Phase 1 - armbian-next: WiP: Docker: actually use the GHA-image as base; pull it every 24hs. - using image in my private repo. - this has significant speedup to "start building time" on the 1st run - move some Linux specific stuff to its own if - add comments and todo - armbian-next: WiP: Docker, high-WiP, beginnings of Armbian mount dict, with linux/darwin preferences - armbian-next: WiP: Docker, configure `BUILDKIT_COLORS` - armbian-next: WiP: Docker, make docker image from Dockerfile more compact by flattening layers - armbian-next: `logging`: add whale indicator if build running under Docker - armbian-next: WiP: `docker`: working with `bookworm`, `sid`, and `jammy` on Darwin & Linux; works with `bullseye` on Linux only - armbian-next: WiP: `docker`: force ARMBIAN_RUNNING_IN_CONTAINER both in Dockerfile and passed as `--env`; apt update and install in same layer; back to jammy - armbian-next: introduce `armbian_is_running_in_container()` and `armbian_is_host_running_systemd()`, replacing `systemd-detect-virt` in multiple spots - WiP: try with debian:bullseye -- can't detect docker at all - armbian-next: WiP: 2nd stab at new Docker support; Darwin still works; Linux `docker.io` working - gen .dockerignore together with Dockerfile - split in funcs - hacks for Linux and `/dev/loop` stuff, CONTAINER_COMPAT=yes - mac still works, Linux stuff would break it but I if'fed - armbian-next: the secrets of `CONTAINER_COMPAT` revealed; add size checking to check_loop_device() and avoid retry when `mknod`ing - this fails for the right reasons now, causing retries, which are then retried and work ;-) - this is related to building under Docker on Linux, using docker.io package (not docker-ce) - armbian-next: remove `.dockerignore` and add it to `.gitignore`; it's going to be auto-generated - armbian-next: `.dockerignore`: Docker context should only have minimal files and folders, to speed up Dockerfile build - IMPORTANT: `.dockerignore` is going to be generated from now on: so this is the last commit with changes before removal - armbian-next: WiP: initial stab at new Docker support; really run the passed cmdline; add Dockerfile to gitignore - armbian-next: WiP: initial stab at new Docker support; generate Dockerfile; introduce REQUIREMENTS_DEFS_ONLY - uses REQUIREMENTS_DEFS_ONLY - works on Docker Desktop on Mac; - linux TBA - armbian-next: don't error out if `.git` not present; other small fixes - armbian-next: general "work or at least don't misbehave when run on a very bare ubuntu:latest instance" - can't assume things, for example: - that `sudo` will be available; it might not, and might be already root, no reason to fail - that `/etc/timezone` will exist - that `systemd-detect-virt` will be available - that `git` will be available - that `locale-gen` will be available
153 lines
6.6 KiB
Bash
153 lines
6.6 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# Initialize and prepare the trap managers, one for each of ERR, INT, TERM and EXIT traps.
|
|
# Bash goes insane regarding line numbers and other stuff if we try to overwrite the traps.
|
|
# This also implements the custom "cleanup" handlers, which always run at the end of build, or when exiting prematurely for any reason.
|
|
function traps_init() {
|
|
# shellcheck disable=SC2034 # Array of cleanup handlers.
|
|
declare -a trap_manager_cleanup_handlers=()
|
|
# shellcheck disable=SC2034 # Global to avoid doubly reporting ERR/EXIT pairs.
|
|
declare -i trap_manager_error_handled=0
|
|
trap 'main_trap_handler "ERR" "$?"' ERR
|
|
trap 'main_trap_handler "EXIT" "$?"' EXIT
|
|
trap 'main_trap_handler "INT" "$?"' INT
|
|
trap 'main_trap_handler "TERM" "$?"' TERM
|
|
}
|
|
|
|
# This is setup early in compile.sh as a trap handler for ERR, EXIT and INT signals.
|
|
# There are arrays trap_manager_error_handlers=() trap_manager_exit_handlers=() trap_manager_int_handlers=()
|
|
# that will receive the actual handlers.
|
|
# First param is the type of trap, the second is the value of "$?"
|
|
# In order of occurrence.
|
|
# 1) Ctrl-C causes INT [stack unreliable], then ERR, then EXIT with trap_exit_code > 0
|
|
# 2) Stuff failing causes ERR [stack OK], then EXIT with trap_exit_code > 0
|
|
# 3) exit_with_error causes EXIT [stack OK, with extra frame] directly with trap_exit_code == 43
|
|
# 4) EXIT can also be called directly [stack unreliable], with trap_exit_code == 0 if build successful.
|
|
# So the EXIT trap will do:
|
|
# - show stack, if not previously shown (trap_manager_error_handled==0), and if trap_exit_code > 0
|
|
# - allow for debug shell, if trap_exit_code > 0
|
|
# - call all the cleanup functions (always)
|
|
function main_trap_handler() {
|
|
local trap_type="${1}"
|
|
local trap_exit_code="${2}"
|
|
local stack_caller short_stack
|
|
stack_caller="$(show_caller_full)"
|
|
short_stack="${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
|
|
|
|
display_alert "main_trap_handler" "${trap_type} and ${trap_exit_code} trap_manager_error_handled:${trap_manager_error_handled} short_stack:${short_stack}" "trap"
|
|
|
|
case "${trap_type}" in
|
|
TERM | INT)
|
|
display_alert "Build interrupted" "Build interrupted by SIG${trap_type}" "warn"
|
|
trap_manager_error_handled=1
|
|
return # Nothing else to do here. Let the ERR trap show the stack, and the EXIT trap do cleanups.
|
|
;;
|
|
|
|
ERR)
|
|
# If error occurs in subshell (eg: inside $()), we would show the error twice.
|
|
# Determine if we're in a subshell, and if so, output a single message.
|
|
# BASHPID is the current subshell; $$ is parent shell pid
|
|
if [[ "${BASHPID}" == "${$}" ]]; then
|
|
# Not in subshell, dump the error, complete with log, and show the stack.
|
|
logging_error_show_log
|
|
display_alert "Error occurred in main shell" "code ${trap_exit_code} at ${short_stack}\n${stack_caller}\n" "err"
|
|
else
|
|
# In a subshell. This trap will run again in the parent shell, so just output a message about it;
|
|
# When the parent shell trap runs, it will show the stack and log.
|
|
display_alert "Error occurred in SUBSHELL" "SUBSHELL: code ${trap_exit_code} at ${short_stack}" "err"
|
|
fi
|
|
trap_manager_error_handled=1
|
|
return # Nothing else to do here, let the EXIT trap do the cleanups.
|
|
;;
|
|
|
|
EXIT)
|
|
if [[ ${trap_manager_error_handled} -lt 1 ]] && [[ ${trap_exit_code} -gt 0 ]]; then
|
|
logging_error_show_log
|
|
display_alert "Exit with error detected" "${trap_exit_code} at ${short_stack} -\n${stack_caller}\n" "err"
|
|
trap_manager_error_handled=1
|
|
fi
|
|
|
|
if [[ ${trap_exit_code} -gt 0 ]] && [[ "${ERROR_DEBUG_SHELL}" == "yes" ]]; then
|
|
export ERROR_DEBUG_SHELL=no # dont do it twice
|
|
display_alert "MOUNT" "${MOUNT}" "debug"
|
|
display_alert "SDCARD" "${SDCARD}" "debug"
|
|
display_alert "ERROR_DEBUG_SHELL=yes, starting a shell." "ERROR_DEBUG_SHELL; exit to cleanup." "debug"
|
|
bash < /dev/tty >&2 || true
|
|
fi
|
|
|
|
# Run the cleanup handlers, always.
|
|
run_cleanup_handlers || true
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# Run the cleanup handlers, if any, and clean the cleanup list.
|
|
function run_cleanup_handlers() {
|
|
display_alert "run_cleanup_handlers! list:" "${trap_manager_cleanup_handlers[*]}" "cleanup"
|
|
if [[ ${#trap_manager_cleanup_handlers[@]} -lt 1 ]]; then
|
|
return 0 # No handlers set, just return.
|
|
else
|
|
display_alert "Cleaning up" "please wait for cleanups to finish" "debug"
|
|
fi
|
|
# Loop over the handlers, execute one by one. Ignore errors.
|
|
# IMPORTANT: cleanups are added first to the list, so cleanups run in the reverse order they were added.
|
|
local one_cleanup_handler
|
|
for one_cleanup_handler in "${trap_manager_cleanup_handlers[@]}"; do
|
|
display_alert "Running cleanup handler" "${one_cleanup_handler}" "debug"
|
|
"${one_cleanup_handler}" || true
|
|
done
|
|
# Clear the cleanup handler list, so they don't accidentally run again.
|
|
trap_manager_cleanup_handlers=()
|
|
}
|
|
|
|
# Adds a callback for trap types; first argument is function name; extra params are the types to add for.
|
|
function add_cleanup_handler() {
|
|
local callback="$1"
|
|
display_alert "Add callback as cleanup handler" "${callback}" "cleanup"
|
|
# IMPORTANT: cleanups are added first to the list, so they're executed in reverse order.
|
|
trap_manager_cleanup_handlers=("${callback}" "${trap_manager_cleanup_handlers[@]}")
|
|
}
|
|
|
|
function execute_and_remove_cleanup_handler() {
|
|
local callback="$1"
|
|
display_alert "Execute and remove cleanup handler" "${callback}" "cleanup"
|
|
# @TODO implement! loop over the list of cleanups; run the one that matches the callback; remove it from the list.
|
|
local remaning_cleanups=()
|
|
for one_cleanup_handler in "${trap_manager_cleanup_handlers[@]}"; do
|
|
if [[ "${one_cleanup_handler}" != "${callback}" ]]; then
|
|
remaning_cleanups+=("${one_cleanup_handler}")
|
|
else
|
|
"${one_cleanup_handler}" || true
|
|
fi
|
|
done
|
|
trap_manager_cleanup_handlers=("${remaning_cleanups[@]}")
|
|
}
|
|
|
|
function remove_all_trap_handlers() {
|
|
display_alert "Will remove ALL trap handlers, for a clean exit..." "" "cleanup"
|
|
}
|
|
|
|
# exit_with_error <message> <highlight>
|
|
# a way to terminate build process with verbose error message
|
|
function exit_with_error() {
|
|
# Log the error and exit.
|
|
# Everything else will be done by shared trap handling, above.
|
|
local _file="${BASH_SOURCE[1]}"
|
|
local _function=${FUNCNAME[1]}
|
|
local _line="${BASH_LINENO[0]}"
|
|
|
|
display_alert "error: ${1}" "${2} in ${_function}() at ${_file}:${_line}" "err"
|
|
|
|
# @TODO: move this into trap handler
|
|
# @TODO: integrate both overlayfs and the FD locking with cleanup logic
|
|
display_alert "Build terminating... wait for cleanups..." "" "err"
|
|
overlayfs_wrapper "cleanup"
|
|
|
|
## This does not really make sense. wtf?
|
|
## unlock loop device access in case of starvation # @TODO: hmm, say that again?
|
|
#exec {FD}> /var/lock/armbian-debootstrap-losetup
|
|
#flock -u "${FD}"
|
|
|
|
exit 43
|
|
}
|