-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.sh
More file actions
executable file
·159 lines (144 loc) · 4.95 KB
/
common.sh
File metadata and controls
executable file
·159 lines (144 loc) · 4.95 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
153
154
155
156
157
158
159
#!/usr/bin/env bash
# Shared helpers for RHOAI 2.25.4 pre-migration install scripts.
set -Eeuo pipefail
: "${SCRIPT_DIR:?SCRIPT_DIR must be set by caller}"
: "${ROOT_DIR:=$(cd "$SCRIPT_DIR/.." && pwd)}"
export ROOT_DIR
log() { printf '\033[1;34m[%s]\033[0m %s\n' "$(date +%H:%M:%S)" "$*"; }
warn() { printf '\033[1;33m[%s]\033[0m WARN: %s\n' "$(date +%H:%M:%S)" "$*" >&2; }
err() { printf '\033[1;31m[%s]\033[0m ERR: %s\n' "$(date +%H:%M:%S)" "$*" >&2; }
die() { err "$*"; exit 1; }
require_cmd() {
for cmd in "$@"; do
command -v "$cmd" >/dev/null 2>&1 || die "required command not found: $cmd"
done
}
require_oc_login() {
oc whoami >/dev/null 2>&1 || die "not logged into an OpenShift cluster (run 'oc login')"
}
apply_manifest() {
local path="$1"
log "apply: $path"
oc apply -f "$path"
}
apply_dir() {
local dir="$1"
if [[ ! -d "$dir" ]]; then
return 0
fi
shopt -s nullglob
local files=("$dir"/*.yaml)
shopt -u nullglob
if ((${#files[@]} == 0)); then
return 0
fi
for f in "${files[@]}"; do
apply_manifest "$f"
done
}
wait_for_csv_succeeded() {
local ns="$1" csv_prefix="$2" timeout="${3:-900}"
log "waiting for CSV ${csv_prefix}* in ns/${ns} to reach Succeeded (timeout ${timeout}s)"
local deadline=$(( $(date +%s) + timeout ))
while (( $(date +%s) < deadline )); do
local csv phase
csv=$(oc -n "$ns" get csv -o name 2>/dev/null | grep -E "clusterserviceversion.operators.coreos.com/${csv_prefix}" | head -n1 || true)
if [[ -n "$csv" ]]; then
phase=$(oc -n "$ns" get "$csv" -o jsonpath='{.status.phase}' 2>/dev/null || true)
if [[ "$phase" == "Succeeded" ]]; then
log " ${csv} phase=Succeeded"
return 0
fi
[[ -n "$phase" ]] && log " ${csv} phase=${phase}"
fi
sleep 10
done
die "timeout waiting for CSV ${csv_prefix}* in ns/${ns}"
}
approve_installplan() {
# Approves the first pending InstallPlan in the namespace — used for Manual subscriptions
# where we want to pin the installed CSV.
local ns="$1" timeout="${2:-300}"
log "waiting for pending InstallPlan in ns/${ns} (timeout ${timeout}s)"
local deadline=$(( $(date +%s) + timeout ))
while (( $(date +%s) < deadline )); do
local ip
ip=$(oc -n "$ns" get installplan -o json 2>/dev/null \
| jq -r '.items[] | select(.spec.approved==false) | .metadata.name' \
| head -n1 || true)
if [[ -n "$ip" ]]; then
log " approving InstallPlan ${ip}"
oc -n "$ns" patch installplan "$ip" --type=merge -p '{"spec":{"approved":true}}'
return 0
fi
sleep 5
done
die "no pending InstallPlan appeared in ns/${ns}"
}
wait_for_crd() {
local crd="$1" timeout="${2:-300}"
log "waiting for CRD ${crd} (timeout ${timeout}s)"
local deadline=$(( $(date +%s) + timeout ))
while (( $(date +%s) < deadline )); do
if oc get crd "$crd" >/dev/null 2>&1; then
log " CRD ${crd} present"
return 0
fi
sleep 5
done
die "timeout waiting for CRD ${crd}"
}
wait_for_dsc_ready() {
local name="${1:-default-dsc}" timeout="${2:-1800}"
log "waiting for DataScienceCluster/${name} phase=Ready (timeout ${timeout}s)"
local deadline=$(( $(date +%s) + timeout ))
while (( $(date +%s) < deadline )); do
local phase
phase=$(oc get dsc "$name" -o jsonpath='{.status.phase}' 2>/dev/null || true)
if [[ "$phase" == "Ready" ]]; then
log " DSC/${name} is Ready"
return 0
fi
[[ -n "$phase" ]] && log " DSC/${name} phase=${phase}"
sleep 15
done
die "timeout waiting for DSC/${name}"
}
wait_for_dsci_ready() {
local name="${1:-default-dsci}" timeout="${2:-900}"
log "waiting for DSCInitialization/${name} phase=Ready (timeout ${timeout}s)"
local deadline=$(( $(date +%s) + timeout ))
while (( $(date +%s) < deadline )); do
local phase
phase=$(oc get dsci "$name" -o jsonpath='{.status.phase}' 2>/dev/null || true)
if [[ "$phase" == "Ready" ]]; then
log " DSCI/${name} is Ready"
return 0
fi
[[ -n "$phase" ]] && log " DSCI/${name} phase=${phase}"
sleep 10
done
die "timeout waiting for DSCI/${name}"
}
wait_for_rollout() {
local kind="$1" name="$2" ns="$3" timeout="${4:-600}"
log "waiting for ${kind}/${name} -n ${ns} rollout (timeout ${timeout}s)"
oc -n "$ns" rollout status "${kind}/${name}" --timeout="${timeout}s"
}
render_and_apply() {
# Renders envsubst on a file, then applies. Caller is responsible for exporting vars.
local path="$1"
log "apply (envsubst): $path"
envsubst < "$path" | oc apply -f -
}
preflight_cluster() {
require_cmd oc jq envsubst
require_oc_login
local ocp
ocp=$(oc get clusterversion version -o jsonpath='{.status.desired.version}' 2>/dev/null || echo "unknown")
log "OCP version: ${ocp}"
local def_sc
def_sc=$(oc get sc -o json | jq -r '.items[] | select(.metadata.annotations."storageclass.kubernetes.io/is-default-class"=="true") | .metadata.name' | head -n1)
[[ -n "$def_sc" ]] || die "no default StorageClass found"
log "default StorageClass: ${def_sc}"
}