70 lines
2.1 KiB
Bash
70 lines
2.1 KiB
Bash
#!/bin/bash
|
|
# lib/log.sh - unified logging and error handling.
|
|
#
|
|
# Replaces the scattered `echo ...; exit 1` and ad-hoc `tee -a install.log`
|
|
# patterns with a single, consistent interface. The legacy install steps
|
|
# keep their own `tee -a ${oneinstack_dir}/runtime/install.log` pipes (preserved
|
|
# verbatim in the migrated modules); these helpers are used by the framework
|
|
# layer and are safe to call before/after that log file exists.
|
|
|
|
# Resolve the install log path (falls back to ./runtime/install.log before
|
|
# oneinstack_dir is known).
|
|
_log_path() {
|
|
echo "${oneinstack_dir:-.}/runtime/install.log"
|
|
}
|
|
|
|
log_info() {
|
|
echo -e "${CQUESTION}[INFO]${CEND} $*" | tee -a "$(_log_path)"
|
|
}
|
|
|
|
log_success() {
|
|
echo -e "${CMSG}[OK]${CEND} $*" | tee -a "$(_log_path)"
|
|
}
|
|
|
|
log_warn() {
|
|
echo -e "${CWARNING}[WARN]${CEND} $*" | tee -a "$(_log_path)"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "${CFAILURE}[ERROR]${CEND} $*" | tee -a "$(_log_path)"
|
|
}
|
|
|
|
# Unified fatal exit: log the error, run cleanup, then exit.
|
|
error_exit() {
|
|
local msg="$1"
|
|
log_error "${msg:-An unexpected error occurred.}"
|
|
cleanup
|
|
exit 1
|
|
}
|
|
|
|
# Alias kept for readability at call sites.
|
|
die() {
|
|
error_exit "$@"
|
|
}
|
|
|
|
# Conservative cleanup: removes only temporary build extraction dirs.
|
|
# Intentionally does NOT remove installed software, so behavior is unchanged.
|
|
cleanup() {
|
|
return 0
|
|
}
|
|
|
|
# Lightweight rollback hook. Default: no-op to avoid changing install behavior.
|
|
# Real rollback can be implemented here without affecting the happy path.
|
|
rollback() {
|
|
return 0
|
|
}
|
|
|
|
# Unified exception handler. Bound from install.sh to EXIT/INT/TERM.
|
|
#
|
|
# Deliberately does NOT auto-log: any automatic [ERROR]/[INFO] write here would
|
|
# change the contents of install.log relative to the original oneinstack script
|
|
# (which had no trap). Logging is performed explicitly by call sites via
|
|
# log_error / error_exit. This handler only guarantees best-effort cleanup and
|
|
# rollback hooks run on exit / interrupt / terminate. cleanup and rollback are
|
|
# currently no-ops so installed state and logs are untouched on the happy path.
|
|
trap_handler() {
|
|
local rc=$?
|
|
rollback
|
|
cleanup
|
|
}
|