80 lines
3.2 KiB
Bash
80 lines
3.2 KiB
Bash
#!/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<timeout; i++)); do
|
|
if curl -fsS "$url" >/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
|
|
}
|