|
| 1 | +# lib/run-mode.sh — Dual-mode execution preamble for k3s-lab scripts. |
| 2 | +# |
| 3 | +# Source this file at the very top of any script (after set -euo pipefail): |
| 4 | +# |
| 5 | +# K3S_LAB_RAW="${K3S_LAB_RAW:-https://raw.githubusercontent.com/KevinDeBenedetti/k3s-lab/main}" |
| 6 | +# # shellcheck source=lib/run-mode.sh |
| 7 | +# source "$(dirname "${BASH_SOURCE[0]:-run-mode.sh}")/../lib/run-mode.sh" |
| 8 | +# |
| 9 | +# Why this exists |
| 10 | +# ─────────────── |
| 11 | +# Scripts can be executed in two ways: |
| 12 | +# LOCAL — `bash /path/to/script.sh` (BASH_SOURCE[0] is a real file) |
| 13 | +# REMOTE — `bash <(curl -fsSL ...)` (BASH_SOURCE[0] is /dev/fd/NN — a pipe) |
| 14 | +# |
| 15 | +# In remote mode the usual "cd $(dirname ${BASH_SOURCE[0]})" trick fails because |
| 16 | +# /dev/fd/NN is not a directory. This preamble detects the mode and exposes |
| 17 | +# three helper functions that transparently resolve paths in either case. |
| 18 | +# |
| 19 | +# Exposed helpers |
| 20 | +# ─────────────── |
| 21 | +# _lib <name> Source a file from lib/ (e.g. _lib log.sh) |
| 22 | +# _k8s <rel-path> Echo an absolute path or URL to a kubernetes/ manifest |
| 23 | +# (safe to pass to `kubectl apply -f`) |
| 24 | +# _helm_val <rel-path> Echo a local path to a helm values file; in remote |
| 25 | +# mode the file is downloaded to a temp path first. |
| 26 | +# Temp files are cleaned up automatically on EXIT. |
| 27 | +# |
| 28 | +# Prerequisites |
| 29 | +# ───────────── |
| 30 | +# K3S_LAB_RAW must be set before sourcing this file (or it defaults to main). |
| 31 | +# ────────────────────────────────────────────────────────────────────────────── |
| 32 | + |
| 33 | +K3S_LAB_RAW="${K3S_LAB_RAW:-https://raw.githubusercontent.com/KevinDeBenedetti/k3s-lab/main}" |
| 34 | + |
| 35 | +# Detect execution context. |
| 36 | +_run_src="${BASH_SOURCE[1]:-}" # [1] = the script that sourced us |
| 37 | +if [[ -n "${_run_src}" && "${_run_src}" != /dev/fd/* && -f "${_run_src}" ]]; then |
| 38 | + _RUN_REPO="$(cd "$(dirname "${_run_src}")/.." && pwd)" |
| 39 | + _RUN_REMOTE=0 |
| 40 | +else |
| 41 | + # Could also be triggered when K3S_LAB is explicitly empty (remote mode). |
| 42 | + _RUN_REPO="" |
| 43 | + _RUN_REMOTE=1 |
| 44 | +fi |
| 45 | + |
| 46 | +# Also allow K3S_LAB env var to override — set by the local-mode Make macro. |
| 47 | +if [[ -n "${K3S_LAB:-}" ]]; then |
| 48 | + _RUN_REPO="${K3S_LAB}" |
| 49 | + _RUN_REMOTE=0 |
| 50 | +fi |
| 51 | + |
| 52 | +# _lib <name> |
| 53 | +# Source a helper from lib/. Local: from disk. Remote: streamed via curl. |
| 54 | +_lib() { |
| 55 | + local name="$1" |
| 56 | + if [[ "${_RUN_REMOTE}" -eq 0 ]]; then |
| 57 | + # shellcheck disable=SC1090 |
| 58 | + source "${_RUN_REPO}/lib/${name}" |
| 59 | + else |
| 60 | + # shellcheck disable=SC1090 |
| 61 | + source <(curl -fsSL "${K3S_LAB_RAW}/lib/${name}") |
| 62 | + fi |
| 63 | +} |
| 64 | + |
| 65 | +# _k8s <rel-path> |
| 66 | +# Return the path (or URL) to a kubernetes/ manifest. |
| 67 | +# Local: absolute filesystem path → kubectl apply -f, helm --values, etc. |
| 68 | +# Remote: raw GitHub URL → kubectl apply -f supports URLs natively. |
| 69 | +_k8s() { |
| 70 | + if [[ "${_RUN_REMOTE}" -eq 0 ]]; then |
| 71 | + echo "${_RUN_REPO}/kubernetes/$1" |
| 72 | + else |
| 73 | + echo "${K3S_LAB_RAW}/kubernetes/$1" |
| 74 | + fi |
| 75 | +} |
| 76 | + |
| 77 | +# _k8s_file <rel-path> |
| 78 | +# Return a local filesystem path to any kubernetes/ file. |
| 79 | +# Local: absolute filesystem path (no download needed). |
| 80 | +# Remote: download to a temp file and return its path. |
| 81 | +# All temp files are registered for cleanup on EXIT. |
| 82 | +# |
| 83 | +# Use this (not _k8s) when you need a real file on disk: |
| 84 | +# helm upgrade --values "$(_k8s_file ingress/traefik-values.yaml)" |
| 85 | +# envsubst < "$(_k8s_file cert-manager/clusterissuer.yaml)" | kubectl apply -f - |
| 86 | +_run_tmp_files=() |
| 87 | +_k8s_file() { |
| 88 | + if [[ "${_RUN_REMOTE}" -eq 0 ]]; then |
| 89 | + echo "${_RUN_REPO}/kubernetes/$1" |
| 90 | + else |
| 91 | + local tmpf |
| 92 | + tmpf="$(mktemp /tmp/k8s-file-XXXXXXXX.yaml)" |
| 93 | + _run_tmp_files+=("${tmpf}") |
| 94 | + curl -fsSL "${K3S_LAB_RAW}/kubernetes/$1" -o "${tmpf}" |
| 95 | + echo "${tmpf}" |
| 96 | + fi |
| 97 | +} |
| 98 | + |
| 99 | +# Register cleanup of any temp files created by _helm_val. |
| 100 | +_run_mode_cleanup() { |
| 101 | + [[ "${#_run_tmp_files[@]}" -gt 0 ]] && rm -f "${_run_tmp_files[@]}" 2>/dev/null || true |
| 102 | +} |
| 103 | +trap _run_mode_cleanup EXIT |
0 commit comments