|
| 1 | +#!/bin/bash |
| 2 | +# MIT License |
| 3 | +# |
| 4 | +# (C) Copyright 2021-2023 Hewlett Packard Enterprise Development LP |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | +# copy of this software and associated documentation files (the "Software"), |
| 8 | +# to deal in the Software without restriction, including without limitation |
| 9 | +# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | +# and/or sell copies of the Software, and to permit persons to whom the |
| 11 | +# Software is furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included |
| 14 | +# in all copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 | +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 | +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 22 | +# OTHER DEALINGS IN THE SOFTWARE. |
| 23 | + |
| 24 | +# This script updates all image tag references in the workflows directory. |
| 25 | +# This is necessary since a new docs-csm version could land before or after a CSM update. |
| 26 | + |
| 27 | +set -e |
| 28 | +REGISTRY="registry.local" |
| 29 | +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) |
| 30 | +cd $SCRIPT_DIR |
| 31 | + |
| 32 | +if [[ "$(basename ${SCRIPT_DIR})" != "workflows" ]]; then |
| 33 | + echo "ERROR: This script must be located in the workflows directory to run." |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +if [[ $(nslookup $REGISTRY >> /dev/null; echo $?) != 0 ]]; then |
| 38 | + echo "INFO: Skipping update_tags.sh script given docs were installed outside of a CSM environment." |
| 39 | + exit 0 |
| 40 | +fi |
| 41 | + |
| 42 | +if [[ -f /etc/pit-release ]]; then |
| 43 | + echo "INFO: Skipping update_tags.sh script given docs were installed on PIT." |
| 44 | + exit 0 |
| 45 | +fi |
| 46 | + |
| 47 | +if [[ ! $(curl -s https://${REGISTRY}) ]]; then |
| 48 | + echo "ERROR: Is this a CSM environment? Check that ${REGISTRY} is accessible over https before continuing." |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +function get_list_of_images_to_update() { |
| 53 | + grep -rhPo "(?<=image: )[a-z].*(?=:)" . | sort | uniq |
| 54 | +} |
| 55 | + |
| 56 | +function get_latest_tag_for_image() { |
| 57 | + THIS_IMAGE=$1 |
| 58 | + THIS_PREFIX=$([[ $(echo $THIS_IMAGE | grep -e "^${REGISTRY}/") ]] && echo "" || echo "${REGISTRY}/") |
| 59 | + podman search $THIS_PREFIX$THIS_IMAGE --list-tags --format=json | jq -r '.[0].Tags | sort_by(.) | last' |
| 60 | +} |
| 61 | + |
| 62 | +function get_filenames_referring_to_image() { |
| 63 | + grep -RHl -e "${THIS_IMAGE}:" . |
| 64 | +} |
| 65 | + |
| 66 | +function update_tags_in_file() { |
| 67 | + THIS_IMAGE=$1 |
| 68 | + LATEST_TAG=$2 |
| 69 | + THIS_FILE=$3 |
| 70 | + echo "Updating tag of ${THIS_IMAGE} in ${SCRIPT_DIR}/${THIS_FILE} to ${LATEST_TAG}." |
| 71 | + sed -i -e "s|${THIS_IMAGE}:.*|${THIS_IMAGE}:${LATEST_TAG}|g" $THIS_FILE |
| 72 | +} |
| 73 | + |
| 74 | +# For each image found, lookup the latest tag and update the references in every file. |
| 75 | +for THIS_IMAGE in $(get_list_of_images_to_update); do |
| 76 | + LATEST_TAG=$(get_latest_tag_for_image $THIS_IMAGE) |
| 77 | + for FILE in $(get_filenames_referring_to_image); do |
| 78 | + update_tags_in_file $THIS_IMAGE $LATEST_TAG $FILE |
| 79 | + done |
| 80 | +done |
| 81 | + |
| 82 | +cd $OLDPWD |
0 commit comments