Skip to content

Commit

Permalink
CASMINST-6528 Adds script to dynamically update tag references to con…
Browse files Browse the repository at this point in the history
…tainers in scr… (#3955)

* Adds script to dynamically update tag references to containers in scripts and argo files.

* Modifies the update_tags scripts to specifically look for every reference needing an update in just the yaml files in the workflows directory.

* Adds checks confirming nexus is up.

* Improves an error message.

* Adds a post install script to dynamically update workflows images tag references when docs-csm is installed on a CSM environment.

* Improves update_tags logging messages. Fixes the registry.local lookup detection.

* typo

* Fixes registry.local name lookup test in update_tags for workflows script.

* Inserts dynamic tag update into upload-rebuild script so any time we are uploading argo templates into k8s, the tags are latest.

* Fixes path to update_tags script during post install.

* Shell check fixes.

* Fix

* Adds PIT check.
  • Loading branch information
denniswalker authored Jul 21, 2023
1 parent 51275db commit d7ebb69
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs-csm.spec
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ cat INSTALLED_FILES | xargs -i sh -c 'test -L {} && exit || test -f $RPM_BUILD_R
%docdir /usr/share/doc/csm
%license LICENSE
%defattr(-,root,root)

%post
/usr/share/doc/csm/workflows/update_tags.sh

3 changes: 3 additions & 0 deletions workflows/scripts/upload-rebuild-templates.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ set -euo pipefail
basedir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

function main() {
cat << EOO
$($basedir/../update_tags.sh)
EOO
upload_worker_rebuild_template
upload_worker_rebuild_hooks
upload_storage_rebuild_template
Expand Down
82 changes: 82 additions & 0 deletions workflows/update_tags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash
# MIT License
#
# (C) Copyright 2021-2023 Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

# This script updates all image tag references in the workflows directory.
# This is necessary since a new docs-csm version could land before or after a CSM update.

set -e
REGISTRY="registry.local"
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd $SCRIPT_DIR

if [[ "$(basename ${SCRIPT_DIR})" != "workflows" ]]; then
echo "ERROR: This script must be located in the workflows directory to run."
exit 1
fi

if [[ $(nslookup $REGISTRY >> /dev/null; echo $?) != 0 ]]; then
echo "INFO: Skipping update_tags.sh script given docs were installed outside of a CSM environment."
exit 0
fi

if [[ -f /etc/pit-release ]]; then
echo "INFO: Skipping update_tags.sh script given docs were installed on PIT."
exit 0
fi

if [[ ! $(curl -s https://${REGISTRY}) ]]; then
echo "ERROR: Is this a CSM environment? Check that ${REGISTRY} is accessible over https before continuing."
exit 1
fi

function get_list_of_images_to_update() {
grep -rhPo "(?<=image: )[a-z].*(?=:)" . | sort | uniq
}

function get_latest_tag_for_image() {
THIS_IMAGE=$1
THIS_PREFIX=$([[ $(echo $THIS_IMAGE | grep -e "^${REGISTRY}/") ]] && echo "" || echo "${REGISTRY}/")
podman search $THIS_PREFIX$THIS_IMAGE --list-tags --format=json | jq -r '.[0].Tags | sort_by(.) | last'
}

function get_filenames_referring_to_image() {
grep -RHl -e "${THIS_IMAGE}:" .
}

function update_tags_in_file() {
THIS_IMAGE=$1
LATEST_TAG=$2
THIS_FILE=$3
echo "Updating tag of ${THIS_IMAGE} in ${SCRIPT_DIR}/${THIS_FILE} to ${LATEST_TAG}."
sed -i -e "s|${THIS_IMAGE}:.*|${THIS_IMAGE}:${LATEST_TAG}|g" $THIS_FILE
}

# For each image found, lookup the latest tag and update the references in every file.
for THIS_IMAGE in $(get_list_of_images_to_update); do
LATEST_TAG=$(get_latest_tag_for_image $THIS_IMAGE)
for FILE in $(get_filenames_referring_to_image); do
update_tags_in_file $THIS_IMAGE $LATEST_TAG $FILE
done
done

cd $OLDPWD

0 comments on commit d7ebb69

Please sign in to comment.