-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.sh
More file actions
152 lines (138 loc) · 5.68 KB
/
common.sh
File metadata and controls
152 lines (138 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env bash
# scripts/common.sh — shared utilities for tekton-dag scripts.
# Source this at the top of any script:
# SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# source "$SCRIPT_DIR/common.sh"
set -euo pipefail
# ── Path resolution ──────────────────────────────────────────────────
# SCRIPT_DIR must be set by the caller before sourcing (for sibling script paths).
# REPO_ROOT must be the real repo root even when scripts are invoked via a symlink
# (e.g. docs/demos/scripts → ../../scripts for VHS tapes with cwd docs/demos).
_COMMON_SH_DIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="${REPO_ROOT:-$(cd "${_COMMON_SH_DIR}/.." && pwd)}"
STACKS_DIR="${STACKS_DIR:-$REPO_ROOT/stacks}"
# ── Configurable defaults (override via env or flags) ────────────────
NAMESPACE="${NAMESPACE:-tekton-pipelines}"
IMAGE_REGISTRY="${IMAGE_REGISTRY:-localhost:5001}"
GIT_SSH_SECRET_NAME="${GIT_SSH_SECRET_NAME:-git-ssh-key}"
GIT_URL="${GIT_URL:-https://github.com/jmjava/tekton-dag.git}"
GIT_REVISION="${GIT_REVISION:-main}"
# ── Logging / errors ────────────────────────────────────────────────
die() { echo "ERROR: $*" >&2; exit 1; }
need() {
command -v "$1" >/dev/null 2>&1 || die "$1 is required but not found in PATH"
}
info() { echo "==> $*"; }
warn() { echo "WARNING: $*" >&2; }
# ── Port-forward helpers ─────────────────────────────────────────────
# Usage: start_port_forward <service> <local:remote> [namespace]
# Sets PF_PID. Caller should trap cleanup_port_forward EXIT.
PF_PID=""
start_port_forward() {
local svc="$1" ports="$2" ns="${3:-$NAMESPACE}"
kubectl port-forward "$svc" "$ports" -n "$ns" &
PF_PID=$!
sleep 3
if ! kill -0 "$PF_PID" 2>/dev/null; then
die "port-forward to $svc ($ports) failed to start"
fi
}
cleanup_port_forward() {
if [[ -n "${PF_PID:-}" ]] && kill -0 "$PF_PID" 2>/dev/null; then
kill "$PF_PID" 2>/dev/null || true
fi
}
# ── TCP port prep (stale kubectl port-forward, dev servers, etc.) ─────
# Usage: free_tcp_port <port> [<do_free>: 1=kill listeners (default), 0=skip]
free_tcp_port() {
local port="$1"
local do_free="${2:-1}"
[[ "$do_free" == "0" ]] && return 0
local killed=false
if command -v fuser >/dev/null 2>&1; then
if fuser "${port}/tcp" >/dev/null 2>&1; then
echo " [free_tcp_port] ${port}/tcp in use — fuser -k..."
fuser -k "${port}/tcp" >/dev/null 2>&1 || true
killed=true
fi
fi
if command -v lsof >/dev/null 2>&1; then
local pids
pids=$(lsof -ti ":${port}" -sTCP:LISTEN 2>/dev/null || true)
if [[ -n "$pids" ]]; then
echo " [free_tcp_port] ${port}/tcp listener PIDs: $(echo "$pids" | tr '\n' ' ') — killing..."
while IFS= read -r pid; do
[[ "$pid" =~ ^[0-9]+$ ]] || continue
kill "$pid" 2>/dev/null || true
done <<< "$pids"
killed=true
fi
fi
if [[ "$killed" == "true" ]]; then
sleep 1
fi
}
# ── Registry helpers ─────────────────────────────────────────────────
# Kind maps localhost:5001 (host) -> localhost:5000 (in-cluster).
# Compile/pipeline image refs always use the in-cluster address.
resolve_compile_registry() {
local reg="${1:-$IMAGE_REGISTRY}"
if [[ "$reg" == "localhost:5001" ]]; then
echo "localhost:5000"
else
echo "$reg"
fi
}
# ── .env loader ──────────────────────────────────────────────────────
load_env() {
local env_file="${1:-$REPO_ROOT/.env}"
if [[ -f "$env_file" ]]; then
set -a
# shellcheck disable=SC1090
source "$env_file"
set +a
fi
}
# ── YAML processor (Mike Farah yq) ────────────────────────────────────
# Some images ship the unrelated PyPI `yq` package on PATH (wrapper around jq).
# Stack scripts require https://github.com/mikefarah/yq (`yq -o=json`, etc.).
_mikefarah_yq_ok() {
local ver
command -v yq >/dev/null 2>&1 || return 1
ver=$(yq --version 2>&1 || true)
[[ "$ver" == *"mikefarah"* ]] || [[ "$ver" == *"github.com/mikefarah/yq"* ]]
}
# Install a static Mike Farah yq binary under REPO_ROOT/.tools and prepend to PATH.
# Override version with MIKEFARAH_YQ_VERSION (default v4.45.4).
ensure_mikefarah_yq() {
if _mikefarah_yq_ok; then
return 0
fi
local install_dir="${REPO_ROOT}/.tools"
local bin="${install_dir}/yq"
local want="${MIKEFARAH_YQ_VERSION:-v4.45.4}"
mkdir -p "$install_dir"
if [[ -x "$bin" ]]; then
local cached
cached=$("$bin" --version 2>&1 || true)
if [[ "$cached" == *"mikefarah"* ]] || [[ "$cached" == *"github.com/mikefarah/yq"* ]]; then
export PATH="${install_dir}:$PATH"
return 0
fi
fi
need curl
local arch
arch=$(uname -m)
case "$arch" in
x86_64) arch=amd64 ;;
aarch64 | arm64) arch=arm64 ;;
*) die "ensure_mikefarah_yq: unsupported uname -m: $arch (install yq manually)" ;;
esac
local url="https://github.com/mikefarah/yq/releases/download/${want}/yq_linux_${arch}"
info "Installing Mike Farah yq ${want} -> ${bin} (first on PATH for this process)"
curl -fsSL -o "$bin.tmp" "$url"
mv -f "$bin.tmp" "$bin"
chmod +x "$bin"
export PATH="${install_dir}:$PATH"
_mikefarah_yq_ok || die "ensure_mikefarah_yq: installed binary failed self-check"
}