Skip to content

Commit d7ebb69

Browse files
authoredJul 21, 2023
CASMINST-6528 Adds script to dynamically update tag references to containers 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.
1 parent 51275db commit d7ebb69

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
 

‎docs-csm.spec

+4
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ cat INSTALLED_FILES | xargs -i sh -c 'test -L {} && exit || test -f $RPM_BUILD_R
5252
%docdir /usr/share/doc/csm
5353
%license LICENSE
5454
%defattr(-,root,root)
55+
56+
%post
57+
/usr/share/doc/csm/workflows/update_tags.sh
58+

‎workflows/scripts/upload-rebuild-templates.sh

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ set -euo pipefail
2626
basedir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
2727

2828
function main() {
29+
cat << EOO
30+
$($basedir/../update_tags.sh)
31+
EOO
2932
upload_worker_rebuild_template
3033
upload_worker_rebuild_hooks
3134
upload_storage_rebuild_template

‎workflows/update_tags.sh

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)
Please sign in to comment.