-
Notifications
You must be signed in to change notification settings - Fork 20
/
rhoai-dih.sh
executable file
·498 lines (443 loc) · 14.9 KB
/
rhoai-dih.sh
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
set -o nounset
set -o pipefail
set_defaults() {
org_url_base="https://api.github.com/orgs/red-hat-data-services/repos?per_page=100&page="
excluded_repos=("rhods-disconnected-install-helper" "odh-manifests" "openshift-ai-handbook")
rhods_version="${rhods_version:-}"
repository_folder="${repository_folder:-.odh-manifests}"
notebooks_folder="${notebooks_folder:-.odh-notebooks}"
notebooks_branch="${rhods_version:-main}"
file_name="${file_name:-$rhods_version.md}"
skip_tls="${skip_tls:-false}"
mirror_url="${mirror_url:-registry.example.com:5000/mirror/oc-mirror-metadata}"
repository_url="${repository_url:-https://github.com/red-hat-data-services/odh-manifests}"
notebooks_url="${notebooks_url:-https://github.com/red-hat-data-services/notebooks}"
openshift_version="${openshift_version:-v4.14}"
skip_image_verification="${skip_image_verification:-false}"
channel="${channel:-fast}"
}
# Other additional images
must_gather_image=""
function help() {
echo "Usage: script.sh [-h] [-v] [--skip-image-verification] [--skip-tls]"
echo " -h, --help Display this help message"
echo " -v, --rhods-version RHODS version. Valid format: rhods-X.Y"
echo " --skip-image-verification Skip image verification"
echo " --skip-tls Skip TLS verification"
echo " --set-repository-source Set the repository source"
echo " --set-file-name Set the file name"
echo " --set-registry Set the registry"
echo " --set-openshift-version Set the OpenShift version"
echo " --set-channel Set the channel"
}
function get_latest_rhods_version() {
local rhods_version
rhods_version=$(git ls-remote --heads https://github.com/red-hat-data-services/rhods-operator | grep 'rhoai' | awk -F'/' '{print $NF}' | sort -V | tail -1)
echo "$rhods_version"
}
is_rhods_version_greater_or_equal_to() {
local version=$1
major_version=$(echo "$version" | cut -d'-' -f2 | cut -d'.' -f1)
minor_version=$(echo "$version" | cut -d'-' -f2 | cut -d'.' -f2)
actual_major_version=$(echo "$rhods_version" | cut -d'-' -f2 | cut -d'.' -f1)
actual_minor_version=$(echo "$rhods_version" | cut -d'-' -f2 | cut -d'.' -f2)
if [ "$actual_major_version" -gt "$major_version" ] || ([ "$actual_major_version" -eq "$major_version" ] && [ "$actual_minor_version" -ge "$minor_version" ]); then
return 0
else
return 1
fi
}
function get_supported_versions() {
pushd "$repository_folder" || echo "Error: Directory $repository_folder does not exist"
latest_rhods_version=$(get_latest_rhods_version)
popd || exit 1
major_version=$(echo $latest_rhods_version | cut -d'-' -f2 | cut -d'.' -f1)
minor_version=$(echo $latest_rhods_version | cut -d'-' -f2 | cut -d'.' -f2)
for i in {1..4}; do
pushd "$repository_folder" || echo "Error: Directory $repository_folder does not exist"
if [ $i == 1 ]; then
minor_version=$((minor_version))
else
minor_version=$((minor_version - 1))
fi
if [ $minor_version -lt 0 ]; then
major_version=$((major_version - 1))
minor_version=99
fi
version="rhods-$major_version.$minor_version"
rhods_version=$version
file_name="$rhods_version.md"
change_rhods_version
popd || exit 1
image_set_configuration
done
}
function verify_image_exists() {
local image=$1
local image_name
local image_digest
local image_sha256
image_name=$(echo "$image" | awk -F '@' '{print $1}')
image_digest=$(echo "$image" | awk -F '@' '{print $2}')
echo "docker image: $image"
image_sha256=$(skopeo inspect docker://"$image" | jq -r '.Digest')
echo "Verifying image $image_name"
echo "Image variable: $image"
if [ "$image_digest" != "$image_sha256" ]; then
echo "Error: Image $image_name does not exist"
#exit 1
fi
echo "Image $image_name exists with digest $image_sha256"
}
function image_tag_to_digest() {
local image=$1
local image_name
local image_digest
image_name=$(echo "$image" | awk -F ':' '{print $1}')
image_digest=$(skopeo inspect docker://"$image" | jq -r '.Digest')
echo "$image_name@$image_digest"
}
function find_images(){
local openvino=""
if is_rhods_version_greater_or_equal_to rhods-2.4; then
if is_rhods_version_greater_or_equal_to rhods-2.14; then
find "$repository_folder" -maxdepth 2 -type d \( -name "manifests" -o -name "config" -o -name "jupyter" \) -exec bash -c 'grep -hrEo "quay\.io/[^/]+/[^@\{\},]+@sha256:[a-f0-9]+" "$0"' {} \; | grep -v 'quay\.io/opendatahub' | grep -v 'quay\.io/integreatly/prometheus-blackbox-exporter' | sort -u
else
find "$repository_folder" -maxdepth 2 -type d \( -name "manifests" -o -name "config" -o -name "jupyter" \) -exec bash -c 'grep -hrEo "quay\.io/[^/]+/[^@\{\},]+@sha256:[a-f0-9]+" "$0"' {} \; | grep -v 'quay\.io/opendatahub' | sort -u
fi
else
grep -hrEo 'quay\.io/[^/]+/[^@{},]+@sha256:[a-f0-9]+' "$repository_folder" | sort -u
fi
if is_rhods_version_greater_or_equal_to rhods-2.10; then
# local openvino_path="$repository_folder/odh-model-controller/config/base/params.env"
# while IFS= read -r line || [[ -n "$line" ]]; do
# imagename_tag="${line#*=}"
# if [[ "$imagename_tag" == quay.io/modh/* ]]; then
# echo "$imagename_tag"
# fi
# done < "$openvino_path"
#additional images changes
# Path to the YAML file
IMAGES_FILE="$repository_folder/rhoai-additional-images/rhoai-disconnected-images.yaml"
if [ -f "$IMAGES_FILE" ]; then
# Read the YAML file and parse it using yq
ADDITIONAL_IMAGES=$(yq e '.additional-images[]' "$IMAGES_FILE")
# Display the images
echo "$ADDITIONAL_IMAGES"
fi
else
# search openvino image
local manifests_folder=$( is_rhods_version_greater_or_equal_to rhods-2.4 && echo "/manifests" || echo "" )
local openvino_path="$repository_folder/odh-dashboard$manifests_folder/overlays/modelserving/kustomization.yaml"
if [ -f "$openvino_path" ]; then
#local image_name=$(yq -r .images[0].newName "$openvino_path")
local image_name_tag=$(yq eval '.images[] | .newName + "@" + .digest' "$openvino_path")
echo "$image_name_tag"
elif [ ! -f "$openvino_path" ]; then
openvino=$(grep -hrEo 'quay\.io/[^/]+/[^@{},]+:[^@{},]+' "$repository_folder" | sort -u | sed -n '/openvino/p')
if [ -z "$openvino" ]; then
echo "Error: openvino image not found"
exit 1
fi
image_tag_to_digest $(echo "$openvino")
fi
fi
}
function find_notebooks_images() {
grep -hrEo 'quay\.io/[^/]+/[^@{},]+@sha256:[a-f0-9]+' "$notebooks_folder" | sort -u
}
function image_set_configuration() {
if [ "$skip_image_verification" == "false" ]; then
echo "Verify images"
while read -r image; do
if [[ $image =~ [{}]+ ]]; then
continue
fi
verify_image_exists "$image"
done < <(find_images)
if ! is_rhods_version_greater_or_equal_to rhods-2.4; then
while read -r image; do
if [[ $image =~ [{}]+ ]]; then
continue
fi
verify_image_exists "$image"
done < <(find_notebooks_images)
fi
verify_image_exists "$(image_tag_to_digest $must_gather_image)"
else
echo "Skipping image verification"
fi
min_max_version=""
if [ -z "$branch_main" ]; then
rhods_semver="${rhods_version/rhoai-/}.0"
min_max_version="minVersion: $rhods_semver
maxVersion: $rhods_semver"
fi
if [ -n "${minmax_version:-}" ]; then
min_max_version="minVersion: $minmax_version
maxVersion: $minmax_version"
fi
cat <<EOF >"$file_name"
# Additional images:
$(find_images | sed 's/^/ - /')
$(image_tag_to_digest "$must_gather_image" | sed 's/^/ - /')
$(if [ -n "$branch_main" ]; then echo " - quay.io/modh/kserve-agent:nightly"
echo " - quay.io/modh/kserve-controller:nightly"
echo " - quay.io/modh/kserve-router:nightly"
echo " - quay.io/modh/kserve-storage-initializer:nightly"
fi)
$(if ! is_rhods_version_greater_or_equal_to rhods-2.4; then
find_notebooks_images | sed 's/^/ - name: /'
fi)
# ImageSetConfiguration example:
\`\`\`yaml
kind: ImageSetConfiguration
apiVersion: mirror.openshift.io/v1alpha2
archiveSize: 4
storageConfig:
registry:
imageURL: $mirror_url
skipTLS: $skip_tls
mirror:
operators:
- catalog: registry.redhat.io/redhat/redhat-operator-index:$openshift_version
packages:
- name: rhods-operator
channels:
- name: $channel
$min_max_version
additionalImages:
$(find_images | sed 's/^/ - name: /')
$(image_tag_to_digest "$must_gather_image" | sed 's/^/ - name: /')
$(if [ -n "$branch_main" ]; then echo " - name: quay.io/modh/kserve-agent:nightly"
echo " - name: quay.io/modh/kserve-controller:nightly"
echo " - name: quay.io/modh/kserve-router:nightly"
echo " - name: quay.io/modh/kserve-storage-initializer:nightly"
fi)
$(if ! is_rhods_version_greater_or_equal_to rhods-2.4; then
find_notebooks_images | sed 's/^/ - name: /'
fi)
\`\`\`
EOF
}
function change_rhods_version() {
echo "Change rhods version $rhods_version branch"
if [[ ! $rhods_version =~ ^rhods-[0-9]+\.[0-9]+$ ]]; then
echo "Error: Invalid version format $rhods_version. Valid format: rhods-X.Y"
exit 1
fi
if ! git branch -a | grep -q "$rhods_version"; then
echo "Error: Version $rhods_version does not exist"
exit 1
fi
echo "Switching to $rhods_version"
git switch "$rhods_version"
return 0
}
function fetch_repository() {
if is_rhods_version_greater_or_equal_to rhods-2.4; then
echo "Cloning repositories"
clone_all_repos
else
if [ -d "$repository_folder" ]; then
echo "Update $repository_folder"
pushd "$repository_folder" || echo "Error: Directory $repository_folder does not exist"
git pull
popd || echo "Error: Directory $repository_folder does not exist"
else
echo "Clone $repository_folder"
git clone "$repository_url" "$repository_folder"
fi
fi
}
function fetch_notebooks_repository() {
if [ -d "$notebooks_folder" ]; then
echo "Update $notebooks_folder"
pushd "$notebooks_folder" || echo "Error: Directory $notebooks_folder does not exist"
git checkout $notebooks_branch
git pull origin $notebooks_branch
popd || echo "Error: Directory $notebooks_folder does not exist"
else
echo "Clone $notebooks_folder"
git clone "$notebooks_url" "$notebooks_folder"
pushd "$notebooks_folder" || echo "Error: Directory $notebooks_folder does not exist"
git checkout "$notebooks_branch"
popd || echo "Error: Directory $notebooks_folder does not exist"
fi
}
# Check github rate limit
check_github_rate_limit() {
response=$(curl -s https://api.github.com/rate_limit)
limit=$(echo "$response" | jq -r '.resources.core.limit')
remaining=$(echo "$response" | jq -r '.resources.core.remaining')
reset=$(echo "$response" | jq -r '.resources.core.reset')
reset_date=$(date -d @$reset)
if [ "$remaining" -eq 0 ]; then
echo "GitHub rate limit has been reached. Wait until $reset_date to continue."
echo "Rate limit: $limit"
echo "Remaining requests: $remaining"
echo "Reset time: $reset_date"
exit 1
fi
}
function get_next_page_url() {
local org_url=$1
curl -sI "$org_url" | awk '/Link:/ {match($0,/\<(https[^;]*)\>; rel="next"/,a); print a[1]}'
}
function branch_exists() {
local repo=$1
local version=$2
git ls-remote --heads "https://github.com/red-hat-data-services/$repo.git" "$version" | grep -q "$version"
}
function clone_repo() {
local repo=$1
local version=$2
echo "cloning $repo with version $version"
git clone --depth 1 -b "$version" "https://github.com/red-hat-data-services/$repo.git" "$repository_folder/$repo"
if [ $? -ne 0 ]; then
echo "Error: Failed to access $repo"
return 1
fi
}
function clone_default_repo() {
local repo=$1
echo "cloning $repo repo"
git clone "https://github.com/red-hat-data-services/$repo.git" "$repository_folder/$repo"
if [ $? -ne 0 ]; then
echo "Error: Failed to access $repo"
return 1
fi
}
function is_repo_excluded() {
local repo=$1
for excluded_repo in "${excluded_repos[@]}"; do
if [[ "$repo" == "$excluded_repo" ]]; then
return 0
fi
done
return 1
}
function clone_all_repos() {
local org_url="${org_url_base}"
check_github_rate_limit
while :; do
local repos
repos=$(curl -s "$org_url" | jq -r '.[] | .name')
if [ -z "$repos" ]; then
break
fi
org_url=$(get_next_page_url "$org_url")
for repo in $repos; do
if ! is_repo_excluded "$repo"; then
if branch_exists "$repo" "$rhods_version"; then
if [ -z "$branch_main" ]; then
clone_repo "$repo" "$rhods_version"
else
clone_default_repo "$repo"
fi
fi
fi
done
done
}
function find_quay_images() {
local repository_folder=$repository_folder
find "$repository_folder" -maxdepth 2 -type d \( -name "manifests" -o -name "config" -o -name "jupyter" \) -exec bash -c 'grep -hrEo "quay\.io/[^/]+/[^@\{\},]+@sha256:[a-f0-9]+" "$0"' {} \; | grep -v 'quay\.io/opendatahub' | sort -u
}
function count_number_images() {
find_quay_images | wc -l
}
function cleanup() {
if [ -d "$repository_folder" ]; then
echo "Remove $repository_folder"
rm -rf "$repository_folder"
fi
if [ -d "$notebooks_folder" ]; then
echo "Remove $notebooks_folder"
rm -rf "$notebooks_folder"
fi
}
function test_current_branch_name() {
branchName=$1
repo_folder="$repository_folder/kubeflow"
# Check if the repository folder exists
if [ -d "$repo_folder" ]; then
# Navigate to the repository folder
cd "$repo_folder" || exit
# Check the git branch
branch=$(git rev-parse --abbrev-ref HEAD)
echo "Current branch for $branchName: $branch"
else
echo "Repository folder not found"
fi
}
parse_args() {
while [ "$#" -gt 0 ]; do
case "$1" in
-h | --help)
help
exit
;;
--rhoai-version | -v)
rhods_version="$2"
file_name="$rhods_version.md"
shift
shift
;;
--skip-image-verification)
skip_image_verification=true
shift
;;
--skip-tls)
skip_tls="true"
shift
;;
--set-file-name)
file_name="$2"
shift
shift
;;
--set-registry)
mirror_url="$2"
shift
shift
;;
--set-repository-folder)
repository_folder="$2"
shift
shift
;;
--set-channel)
channel="$2"
shift
shift
;;
--set-openshift-version)
openshift_version="$2"
shift
shift
;;
--supported-versions)
fetch_notebooks_repository
fetch_repository
get_supported_versions
exit
;;
--)
shift
break
;;
*)
echo "Invalid option: $1" >&2
exit 1
;;
esac
done
}
function update_must_gather() {
if is_rhods_version_greater_or_equal_to rhods-2.10; then
must_gather_image="quay.io/modh/must-gather:$rhods_version"
else
must_gather_image="quay.io/modh/must-gather:stable"
fi
}