lib/common.sh: fix refresh_os_packages contract, drop set -e, add ensure_template_present

- refresh_os_packages()'s header claimed it was callable both host-side (via
  pct exec) and inside the LXC; it's a plain bash function in this process,
  it cannot cross a pct exec boundary. Corrected to match the comment
  already present at its one host-side non-call-site in openbao/install.sh.
- Removed `set -euo pipefail`: a sourced file must not impose shell options
  on the caller. Both install scripts already set these before sourcing.
- Added ensure_template_present(), ported from openbao/install.sh, so
  gitea-runner can reuse it (issue #12 follow-up) instead of duplicating a
  template-download check that skipped `pveam update` and could silently
  settle for a stale cached template list.
This commit is contained in:
Damien
2026-07-30 14:05:43 +02:00
parent 3e4ede907e
commit 936ab2c2fb
+27 -3
View File
@@ -17,8 +17,10 @@
# config`. Reusable as-is by any LXC creator script. # config`. Reusable as-is by any LXC creator script.
# - refresh_os_packages(): Alpine only (apk update && apk upgrade). A # - refresh_os_packages(): Alpine only (apk update && apk upgrade). A
# future Debian-based script needs its own apt-get variant. # future Debian-based script needs its own apt-get variant.
#
set -euo pipefail # Does not set shell options (set -e/-u/-o pipefail): a sourced file must
# not impose those on the caller's shell. Both openbao/install.sh and
# gitea-runner/install.sh already set them before sourcing this file.
# ============================================================ # ============================================================
# #12 - Detect newest Alpine LXC template available from the Proxmox repos. # #12 - Detect newest Alpine LXC template available from the Proxmox repos.
@@ -65,6 +67,23 @@ enable_tty1_autologin() {
pkill -KILL -f '(getty|agetty).*tty1' 2>/dev/null || true pkill -KILL -f '(getty|agetty).*tty1' 2>/dev/null || true
} }
# ============================================================
# #12 - Ensure the given template is downloaded to TEMPLATE_STORAGE, doing a
# `pveam update` first so a stale local cache doesn't silently settle for an
# older version than the one detect_latest_alpine_template() just picked.
# Expects TEMPLATE_STORAGE to be set by the caller.
# ============================================================
ensure_template_present() {
local tmpl="$1"
if ! pveam list "$TEMPLATE_STORAGE" 2>/dev/null | grep -q "$tmpl"; then
log_info "Downloading template ${tmpl} to storage ${TEMPLATE_STORAGE}..."
pveam update >/dev/null
pveam download "$TEMPLATE_STORAGE" "$tmpl"
else
log_info "Template ${tmpl} already present on ${TEMPLATE_STORAGE}."
fi
}
# ============================================================ # ============================================================
# #15 - Find an existing LXC by tag or hostname (host-side, requires pct). # #15 - Find an existing LXC by tag or hostname (host-side, requires pct).
# Echoes the CTID on match, returns 1 if none found. # Echoes the CTID on match, returns 1 if none found.
@@ -88,7 +107,12 @@ find_existing_lxc() {
# ============================================================ # ============================================================
# #15 - Refresh OS packages (Alpine: apk update && apk upgrade). # #15 - Refresh OS packages (Alpine: apk update && apk upgrade).
# Callable both host-side (via pct exec) and inside the LXC. # Callable only from inside the LXC: this is a plain bash function in the
# current process, so it cannot run across a `pct exec ... sh -c` boundary
# without shipping its definition into the container. Host-side callers
# (see openbao/install.sh's update_lxc()) invoke apk update/upgrade inline
# via `pct exec` instead — do not try to dedupe that call site onto this
# function.
# ============================================================ # ============================================================
refresh_os_packages() { refresh_os_packages() {
log_info "Refreshing Alpine packages..." log_info "Refreshing Alpine packages..."