|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +info() |
| 4 | +{ |
| 5 | + echo '[INFO] ' "$@" |
| 6 | +} |
| 7 | +warn() |
| 8 | +{ |
| 9 | + echo '[WARN] ' "$@" >&2 |
| 10 | +} |
| 11 | +fatal() |
| 12 | +{ |
| 13 | + echo '[ERROR] ' "$@" >&2 |
| 14 | + exit 1 |
| 15 | +} |
| 16 | + |
| 17 | +update_chart_version() { |
| 18 | + info "updating chart ${1} in ${CHART_VERSIONS_FILE}" |
| 19 | + CURRENT_VERSION=$(yq -r '.charts[] | select(.filename == "/charts/'"${1}"'.yaml") | .version' ${CHART_VERSIONS_FILE}) |
| 20 | + NEW_VERSION=${2} |
| 21 | + if [ "${CURRENT_VERSION}" != "${NEW_VERSION}" ]; then |
| 22 | + info "found version ${CURRENT_VERSION}, updating to ${NEW_VERSION}" |
| 23 | + chart_updated=true |
| 24 | + if test "$DRY_RUN" == "false"; then |
| 25 | + sed -i "s/${CURRENT_VERSION}/${NEW_VERSION}/g" ${CHART_VERSIONS_FILE} |
| 26 | + else |
| 27 | + info "dry-run is enabled, no changes will occur" |
| 28 | + fi |
| 29 | + else |
| 30 | + info "no new version found" |
| 31 | + fi |
| 32 | +} |
| 33 | + |
| 34 | +update_chart_images() { |
| 35 | + info "downloading chart ${1} version ${2} to extract image versions" |
| 36 | + tempdir=$(mktemp -d) |
| 37 | + CHART_URL="https://github.com/rancher/rke2-charts/raw/main/assets/${1}/${1}-${2}.tgz" |
| 38 | + curl -s -L -o $tempdir/${1}-${2}.tgz ${CHART_URL} |
| 39 | + if test "$chart_updated" == "true"; then |
| 40 | + # get all images and tags for the latest constraint |
| 41 | + cni=$(echo "${1}" | sed -nE 's/rke2-(.*)/\1/p') |
| 42 | + IMAGES_TAG=$(helm template $tempdir/${1}-${2}.tgz | grep -E image:) |
| 43 | + LIST_IMAGES="" |
| 44 | + while IFS= read -r line ; do |
| 45 | + IMAGE=$(echo "${line}" | sed -nE 's/image: (.*)/\1/p' | sed 's/\//\\\//g' | tr -dc '[:alnum:]:.\-/\\') |
| 46 | + LIST_IMAGES=$(echo "$LIST_IMAGES\${REGISTRY}/${IMAGE}\n") |
| 47 | + done <<< "$IMAGES_TAG" |
| 48 | + if test "$DRY_RUN" == "false"; then |
| 49 | + awk -v images_list="$LIST_IMAGES" "BEGIN{split(images_list, images_array, \"\n\"); for (i in images_array) {n = split(images_array[i], image_tag, \":\"); if (n == 2) images_list_array[image_tag[1]] = image_tag[2];}}/.*build\/images-${cni}.txt.*/ {print; getline; while (\$1 != \"EOF\") { n = split(\$1, current_image, \":\"); if (n == 2) print \" \"current_image[1]\":\"images_list_array[current_image[1]]; getline;}}; {print}" ${CHART_AIRGAP_IMAGES_FILE} > $tempdir/images_file.sh |
| 50 | + cp $tempdir/images_file.sh ${CHART_AIRGAP_IMAGES_FILE} |
| 51 | + else |
| 52 | + info "dry-run is enabled, no changes will occur" |
| 53 | + fi |
| 54 | + else |
| 55 | + info "no new version found" |
| 56 | + fi |
| 57 | + # removing downloaded artifacts |
| 58 | + rm -rf $tempdir/ |
| 59 | +} |
| 60 | + |
| 61 | +CHART_VERSIONS_FILE="charts/chart_versions.yaml" |
| 62 | +CHART_AIRGAP_IMAGES_FILE="scripts/build-images" |
| 63 | + |
| 64 | + |
| 65 | +CHART_NAME=${1} |
| 66 | +CHART_VERSION=${2} |
| 67 | +chart_updated=false |
| 68 | + |
| 69 | +update_chart_version ${CHART_NAME} ${CHART_VERSION} |
| 70 | +update_chart_images ${CHART_NAME} ${CHART_VERSION} |
0 commit comments