Repository navigation
Expand file tree
/
Copy pathresolve-pullpreview-env.sh
More file actions
executable file
·283 lines (237 loc) · 7.63 KB
/
Copy pathresolve-pullpreview-env.sh
File metadata and controls
executable file
·283 lines (237 loc) · 7.63 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env bash
# Resolve PullPreview env vars needed by helmfile on the preview VM.
# Safe to source: uses return when sourced, exit only when executed directly.
set -euo pipefail
_resolve_env_finish() {
local code="${1:?}"
if [[ "${BASH_SOURCE[0]:-}" != "${0}" ]]; then
return "${code}"
fi
exit "${code}"
}
values_file="${1:-}"
_resolve_public_dns() {
if [[ -n "${PULLPREVIEW_PUBLIC_DNS:-}" ]]; then
return 0
fi
if [[ -z "${values_file}" || ! -f "${values_file}" ]]; then
return 1
fi
local host=""
if command -v python3 >/dev/null 2>&1; then
set +e
host="$(
python3 - "${values_file}" <<'PY'
import sys
try:
import yaml
except ImportError:
raise SystemExit(2)
with open(sys.argv[1], encoding="utf-8") as handle:
data = yaml.safe_load(handle) or {}
host = (
data.get("openproject", {}).get("openproject", {}).get("host")
or data.get("openproject", {}).get("host")
)
if not host:
raise SystemExit("openproject host not found in values file")
print(host)
PY
)"
py_status=$?
set -e
if [[ "${py_status}" -eq 2 ]]; then
host=""
elif [[ "${py_status}" -ne 0 ]]; then
echo "[pullpreview] Warning: could not parse values file with python3 (${py_status})"
host=""
fi
fi
if [[ -z "${host}" ]]; then
host="$(
awk '
/^openproject:/ { in_op=1; next }
in_op && /^ openproject:/ { in_nested=1; next }
in_nested && /host:/ {
gsub(/["'\'']/, "", $2)
print $2
exit
}
in_op && /^[^ ]/ && !/^ / { exit }
' "${values_file}"
)"
fi
if [[ -n "${host}" ]]; then
export PULLPREVIEW_PUBLIC_DNS="${host}"
echo "[pullpreview] Resolved public DNS from values file: ${PULLPREVIEW_PUBLIC_DNS}"
return 0
fi
return 1
}
_resolve_enterprise_token() {
if [[ -n "${OPENPROJECT_ENTERPRISE_TOKEN:-}" ]]; then
return 0
fi
if [[ -z "${values_file}" || ! -f "${values_file}" ]]; then
return 1
fi
local token=""
if command -v python3 >/dev/null 2>&1; then
set +e
token="$(
python3 - "${values_file}" <<'PY'
import sys
try:
import yaml
except ImportError:
raise SystemExit(2)
with open(sys.argv[1], encoding="utf-8") as handle:
data = yaml.safe_load(handle) or {}
op = data.get("openproject", {}) or {}
env = op.get("environment", {}) or op.get("openproject", {}).get("environment", {}) or {}
token = env.get("OPENPROJECT_SEED__ENTERPRISE__TOKEN")
if token is None:
token = op.get("openproject", {}).get("environment", {}).get("OPENPROJECT_SEED__ENTERPRISE__TOKEN")
if not token or not str(token).strip():
raise SystemExit("enterprise token not found in values file")
print(str(token).strip("\n"))
PY
)"
py_status=$?
set -e
if [[ "${py_status}" -ne 0 ]]; then
token=""
fi
fi
if [[ -n "${token}" ]]; then
export OPENPROJECT_ENTERPRISE_TOKEN="${token}"
echo "[pullpreview] Resolved OPENPROJECT_ENTERPRISE_TOKEN from rendered values file"
return 0
fi
return 1
}
_resolve_deploy_inputs() {
if [[ -z "${values_file}" || ! -f "${values_file}" ]]; then
return 0
fi
if ! command -v python3 >/dev/null 2>&1; then
echo "[pullpreview] Warning: python3 unavailable; helmfile may use default image tags/branches"
return 0
fi
local resolved=""
set +e
resolved="$(
python3 - "${values_file}" <<'PY'
import sys
try:
import yaml
except ImportError:
raise SystemExit(2)
with open(sys.argv[1], encoding="utf-8") as handle:
data = yaml.safe_load(handle) or {}
opnc = data.get("opnc-integration", {}) or {}
op = data.get("openproject", {}) or {}
nc = data.get("nextcloud", {}) or {}
xw = data.get("xwiki", {}) or {}
values = {}
setup_method = opnc.get("integrationSetupMethod")
if setup_method:
values["INTEGRATION_SETUP_METHOD"] = str(setup_method)
op_version = (op.get("image") or {}).get("tag") or (opnc.get("openproject") or {}).get("dockerTag")
if op_version:
values["OPENPROJECT_VERSION"] = str(op_version)
op_branch = (opnc.get("openproject") or {}).get("gitSourceBranch")
if op_branch:
values["OPENPROJECT_BRANCH"] = str(op_branch)
nc_version = (nc.get("image") or {}).get("tag")
if nc_version:
values["NEXTCLOUD_VERSION"] = str(nc_version)
nc_branch = (opnc.get("nextcloud") or {}).get("gitSourceBranch")
if not nc_branch:
for env_var in (nc.get("nextcloud") or {}).get("extraEnv") or []:
if env_var.get("name") == "NC_GIT_SOURCE_BRANCH" and env_var.get("value"):
nc_branch = env_var["value"]
break
if nc_branch:
values["NEXTCLOUD_BRANCH"] = str(nc_branch)
for app in (opnc.get("nextcloud") or {}).get("enableApps") or []:
if app.get("name") != "integration_openproject":
continue
if app.get("version"):
values["INTEGRATION_OPENPROJECT_VERSION"] = str(app["version"])
if app.get("gitBranch"):
values["INTEGRATION_OPENPROJECT_GIT_BRANCH"] = str(app["gitBranch"])
break
xwiki_version = (xw.get("image") or {}).get("tag")
if xwiki_version:
values["XWIKI_VERSION"] = str(xwiki_version)
for env_var in xw.get("extraEnvVars") or []:
if env_var.get("name") == "EXTENSION_OPENPROJECT_VERSION" and env_var.get("value"):
values["XWIKI_EXTENSION_OPENPROJECT_VERSION"] = str(env_var["value"])
break
xw_enabled = (opnc.get("xwiki") or {}).get("enabled")
if xw_enabled is not None:
values["XWIKI_ENABLED"] = "true" if xw_enabled else "false"
gl_enabled = (opnc.get("gitlab") or {}).get("enabled")
if gl_enabled is not None:
values["GITLAB_ENABLED"] = "true" if gl_enabled else "false"
sq = opnc.get("squashtm") or {}
sq_enabled = sq.get("enabled")
if sq_enabled is not None:
values["SQUASHTM_ENABLED"] = "true" if sq_enabled else "false"
if sq.get("version"):
values["SQUASHTM_VERSION"] = str(sq["version"])
if sq.get("adminPassword"):
values["SQUASHTM_ADMIN_PASSWORD"] = str(sq["adminPassword"])
if opnc.get("adminPassword"):
values["PULLPREVIEW_ADMIN_PASSWORD"] = str(opnc["adminPassword"])
if opnc.get("userPassword"):
values["PULLPREVIEW_USER_PASSWORD"] = str(opnc["userPassword"])
mcp_token = (opnc.get("mcp") or {}).get("oauthToken")
if mcp_token:
values["PULLPREVIEW_MCP_TOKEN"] = str(mcp_token)
stack_profile = opnc.get("stackProfile")
if stack_profile:
values["STACK_PROFILE"] = str(stack_profile)
op_env = (op.get("environment") or {})
wiki_enh = op_env.get("OPENPROJECT_FEATURE_WIKI_ENHANCEMENTS_ACTIVE")
if str(wiki_enh).strip().lower() == "true":
values["OPENPROJECT_FEATURE_WIKI_ENHANCEMENTS"] = "true"
for key, value in values.items():
print(f"{key}={value}")
PY
)"
local py_status=$?
set -e
if [[ "${py_status}" -eq 2 ]]; then
echo "[pullpreview] Warning: PyYAML unavailable; helmfile may use default image tags/branches"
return 0
fi
if [[ "${py_status}" -ne 0 ]]; then
echo "[pullpreview] Warning: could not parse deploy inputs from values file (${py_status})"
return 0
fi
local line key value exported=0
while IFS= read -r line; do
[[ -n "${line}" ]] || continue
key="${line%%=*}"
value="${line#*=}"
if [[ -z "${!key:-}" ]]; then
export "${key}=${value}"
exported=$((exported + 1))
fi
done <<< "${resolved}"
if [[ "${exported}" -gt 0 ]]; then
echo "[pullpreview] Resolved ${exported} deploy input(s) from rendered values file"
fi
}
if ! _resolve_public_dns; then
echo "::error::PULLPREVIEW_PUBLIC_DNS is not set and could not be resolved from chart values."
_resolve_env_finish 1
fi
if ! _resolve_enterprise_token; then
echo "::error::OPENPROJECT_ENTERPRISE_TOKEN is not set and could not be resolved from rendered chart values."
_resolve_env_finish 1
fi
_resolve_deploy_inputs
_resolve_env_finish 0