From bf5afc7d266bed58aad423b29876db2c825c4b70 Mon Sep 17 00:00:00 2001 From: "l.kirchner" Date: Thu, 21 May 2026 23:03:26 +0200 Subject: [PATCH] Add lib/install.func: shared in-container helpers --- lib/install.func | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 lib/install.func diff --git a/lib/install.func b/lib/install.func new file mode 100644 index 0000000..b4fc48f --- /dev/null +++ b/lib/install.func @@ -0,0 +1,79 @@ +#!/usr/bin/env bash +# lib/install.func — shared in-container helpers for install/*-install.sh +# +# Sourced via: +# source <(curl -fsSL https://gitea.luki-net.org/luki-net/proxmox-scripts/raw/branch/main/lib/install.func) + +# ── colors / logging ───────────────────────────────────────────────────────── +if [[ -z "${_COLORS_LOADED:-}" ]]; then + RED="\033[0;31m"; GREEN="\033[0;32m"; YELLOW="\033[1;33m"; BLUE="\033[0;34m"; NC="\033[0m" + _COLORS_LOADED=1 +fi +msg_info() { echo -e "${BLUE}[i]${NC} $*"; } +msg_ok() { echo -e "${GREEN}[✓]${NC} $*"; } +msg_warn() { echo -e "${YELLOW}[!]${NC} $*"; } +msg_err() { echo -e "${RED}[✗]${NC} $*" >&2; } + +# ── apt ────────────────────────────────────────────────────────────────────── +# Always installs: ca-certificates curl openssl tzdata gnupg +# Additional packages can be passed as args. +setup_base_apt() { + export DEBIAN_FRONTEND=noninteractive + msg_info "Updating apt index..." + apt-get update -qq + if [[ $# -gt 0 ]]; then + msg_info "Installing base packages + $*..." + else + msg_info "Installing base packages..." + fi + apt-get install -y -qq \ + ca-certificates curl openssl tzdata gnupg \ + "$@" \ + >/dev/null + msg_ok "apt setup complete" +} + +apt_cleanup() { + apt-get autoremove -y -qq >/dev/null || true + apt-get autoclean -qq >/dev/null || true +} + +# ── users / dirs ───────────────────────────────────────────────────────────── +create_system_user() { + local user="$1" home="$2" + if id -u "$user" >/dev/null 2>&1; then + msg_warn "User $user already exists, skipping" + return 0 + fi + msg_info "Creating system user $user (home: $home)" + useradd --system --create-home --home-dir "$home" --shell /usr/sbin/nologin "$user" +} + +# ── systemd ────────────────────────────────────────────────────────────────── +# write_systemd_unit NAME CONTENT +# Writes /etc/systemd/system/NAME.service, daemon-reloads, enables + starts. +write_systemd_unit() { + local name="$1" content="$2" + msg_info "Writing systemd unit: $name.service" + printf '%s\n' "$content" >"/etc/systemd/system/$name.service" + systemctl daemon-reload + systemctl enable --now "$name.service" + msg_ok "$name.service enabled and started" +} + +# ── http wait ──────────────────────────────────────────────────────────────── +# wait_for_http URL [TIMEOUT_SECONDS] +wait_for_http() { + local url="$1" timeout="${2:-30}" + msg_info "Waiting for $url..." + local i + for ((i=0; i/dev/null 2>&1; then + msg_ok "$url responding" + return 0 + fi + sleep 1 + done + msg_warn "$url did not respond within ${timeout}s" + return 1 +}