forked from janus-idp/helm-backstage
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathsync-lightspeed-configs.sh
More file actions
executable file
·178 lines (152 loc) · 4.53 KB
/
sync-lightspeed-configs.sh
File metadata and controls
executable file
·178 lines (152 loc) · 4.53 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
#!/usr/bin/env bash
set -euo pipefail
DEFAULT_REPO="redhat-ai-dev/lightspeed-configs"
DEFAULT_REF="main"
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
LIGHTSPEED_DIR="${REPO_ROOT}/charts/backstage/files/lightspeed"
# Format: upstream_path|destination_path|transform_function
TARGETS=(
"lightspeed-core-configs/lightspeed-stack.yaml|${LIGHTSPEED_DIR}/lightspeed-stack.yaml|copy_fetched_file"
"llama-stack-configs/config.yaml|${LIGHTSPEED_DIR}/config.yaml|copy_fetched_file"
"lightspeed-core-configs/rhdh-profile.py|${LIGHTSPEED_DIR}/rhdh-profile.py|copy_fetched_file"
"env/default-values.env|${LIGHTSPEED_DIR}/secret.yaml|render_secret_yaml_from_env"
)
copy_fetched_file() {
local source_file=$1
local destination_file=$2
cp "${source_file}" "${destination_file}"
}
render_secret_yaml_from_env() {
local source_file=$1
local destination_file=$2
awk '
/^[[:space:]]*$/ { next }
# Skip comments from the upstream .env file.
/^[[:space:]]*#/ { next }
{
separator = index($0, "=")
if (separator == 0) {
printf "error: unsupported env line: %s\n", $0 > "/dev/stderr"
exit 1
}
key = substr($0, 1, separator - 1)
# These image settings are intentionally not part of the chart-managed secret payload.
if (key == "LIGHTSPEED_CORE_IMAGE" || key == "RAG_CONTENT_IMAGE") {
next
}
value = substr($0, separator + 1)
gsub(/\\/, "\\\\", value)
gsub(/"/, "\\\"", value)
printf "%s: \"%s\"\n", key, value
}
' "${source_file}" > "${destination_file}"
}
usage() {
cat <<EOF
Usage: $0 [OPTIONS]
Sync vendored Lightspeed config files from an upstream repo/ref.
Options:
--repo REPO GitHub repo in owner/name form (default: ${DEFAULT_REPO})
--ref REF Git ref to fetch from: branch, tag, or commit SHA (default: ${DEFAULT_REF})
--check Check whether local vendored files already match the selected upstream ref
-h, --help Show this help message and exit
EOF
}
fetch_file() {
local url=$1
local destination=$2
if ! curl -fsSL -A "rhdh-chart-lightspeed-sync" "${url}" -o "${destination}"; then
echo "error: failed to fetch ${url}" >&2
exit 1
fi
}
print_diff() {
local existing_file=$1
local fetched_file=$2
local relative_path=$3
if [[ -f "${existing_file}" ]]; then
diff -u \
--label "${relative_path}" \
--label "${relative_path}" \
"${existing_file}" "${fetched_file}" || true
else
diff -u \
--label "${relative_path}" \
--label "${relative_path}" \
/dev/null "${fetched_file}" || true
fi
}
repo="${DEFAULT_REPO}"
ref="${DEFAULT_REF}"
check_only=false
while [[ $# -gt 0 ]]; do
case "$1" in
--repo)
if [[ $# -lt 2 ]]; then
echo "error: --repo requires a value" >&2
usage
exit 1
fi
repo=$2
shift 2
;;
--ref)
if [[ $# -lt 2 ]]; then
echo "error: --ref requires a value" >&2
usage
exit 1
fi
ref=$2
shift 2
;;
--check)
check_only=true
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "error: unknown option: $1" >&2
usage
exit 1
;;
esac
done
tmpdir="$(mktemp -d)"
cleanup() {
rm -rf "${tmpdir}"
}
trap cleanup EXIT
changed_count=0
for target in "${TARGETS[@]}"; do
IFS='|' read -r source_path destination_path transform_function <<< "${target}"
relative_destination=${destination_path#"${REPO_ROOT}/"}
upstream_url="https://raw.githubusercontent.com/${repo}/${ref}/${source_path}"
fetched_file="${tmpdir}/$(basename "${destination_path}").upstream"
rendered_file="${tmpdir}/$(basename "${destination_path}")"
fetch_file "${upstream_url}" "${fetched_file}"
"${transform_function}" "${fetched_file}" "${rendered_file}"
if [[ -f "${destination_path}" ]] && cmp -s "${destination_path}" "${rendered_file}"; then
echo "up to date: ${relative_destination}"
continue
fi
if [[ "${check_only}" == true ]]; then
print_diff "${destination_path}" "${rendered_file}" "${relative_destination}"
else
mv "${rendered_file}" "${destination_path}"
echo "updated: ${relative_destination} <- ${upstream_url}"
fi
changed_count=$((changed_count + 1))
done
if [[ "${check_only}" == true ]]; then
if [[ "${changed_count}" -gt 0 ]]; then
echo "lightspeed config sync is required for ${changed_count} file(s)" >&2
exit 1
fi
echo "lightspeed config files are already synced"
else
echo "sync complete from ${repo}@${ref}"
fi