Skip to content

Commit 9a233dd

Browse files
Merge pull request #5839 from ggiguash/cache-del-last
USHIFT-6364: Cache management new functionality
2 parents 430e2c6 + dae9048 commit 9a233dd

File tree

1 file changed

+57
-15
lines changed

1 file changed

+57
-15
lines changed

test/bin/manage_build_cache.sh

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ source "${SCRIPTDIR}/common.sh"
1010
AWS_BUCKET_NAME="${AWS_BUCKET_NAME:-microshift-build-cache}"
1111
BCH_SUBDIR=
1212
TAG_SUBDIR=
13+
ARCH_SUBDIR="${UNAME_M}"
1314

1415
usage() {
1516
cat - <<EOF
@@ -24,29 +25,33 @@ The cache directory structure is '${AWS_BUCKET_NAME}/<branch>/<arch>/<tag>'.
2425
-h Show this help.
2526
2627
upload: Upload build artifacts from the local disk to the specified
27-
'${AWS_BUCKET_NAME}/<branch>/${UNAME_M}/<tag>' AWS S3 bucket.
28+
'${AWS_BUCKET_NAME}/<branch>/<arch>/<tag>' AWS S3 bucket.
2829
The 'verify' operation is run before the upload attempt. If
2930
data already exists at the destination, the upload is skipped
3031
and the script exits with 0 status.
3132
3233
download: Download build artifacts from the specified
33-
'${AWS_BUCKET_NAME}/<branch>/${UNAME_M}/<tag>' AWS S3 bucket
34+
'${AWS_BUCKET_NAME}/<branch>/<arch>/<tag>' AWS S3 bucket
3435
to the local disk.
3536
3637
verify: Exit with 0 status if the specified
37-
'${AWS_BUCKET_NAME}/<branch>/${UNAME_M}/<tag>' sub-directory
38+
'${AWS_BUCKET_NAME}/<branch>/<arch>/<tag>' sub-directory
3839
exists and contains files, 1 otherwise.
3940
40-
setlast: Update the '${AWS_BUCKET_NAME}/<branch>/${UNAME_M}/last' file
41+
setlast: Update the '${AWS_BUCKET_NAME}/<branch>/<arch>/last' file
4142
contents in the AWS S3 bucket with the specified '<tag>'.
4243
43-
getlast: Retrieve the '${AWS_BUCKET_NAME}/<branch>/${UNAME_M}/last'
44+
getlast: Retrieve the '${AWS_BUCKET_NAME}/<branch>/<arch>/last'
4445
file contents from the AWS S3 bucket. The output format is
4546
"LAST: <tag>" for easy parsing. The script returns the
4647
specified '<tag>' as a fallback if the bucket file does
4748
not exist.
4849
49-
keep: Delete all data from the '${AWS_BUCKET_NAME}/<branch>/${UNAME_M}'
50+
dellast: Delete the data pointed by the '${AWS_BUCKET_NAME}/<branch>/<arch>/last'
51+
file contents and update the 'last' file contents in the AWS S3 bucket
52+
with the specified '<tag>'
53+
54+
keep: Delete all data from the '${AWS_BUCKET_NAME}/<branch>/<arch>'
5055
AWS S3 bucket, only keeping the 'last' and the specified
5156
'<tag>' sub-directories.
5257
@@ -56,6 +61,7 @@ Options:
5661
5762
-t <tag> The tag sub-directory in the AWS S3 bucket.
5863
64+
-a <arch> The architecture sub-directory in the AWS S3 bucket (default: ${ARCH_SUBDIR}).
5965
EOF
6066
}
6167

@@ -68,7 +74,7 @@ run_aws_cli() {
6874
}
6975

7076
check_contents(){
71-
local -r src_dir="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${UNAME_M}/${TAG_SUBDIR}"
77+
local -r src_dir="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}/${TAG_SUBDIR}"
7278
local -r must_contain_array=("mirror-registry\.tar$" "brew-rpms\.tar$" "repo\.tar$" "${VM_POOL_BASENAME}/.*\.iso$")
7379

7480
echo "Checking contents of '${src_dir}'"
@@ -86,7 +92,7 @@ check_contents(){
8692

8793
action_upload() {
8894
local -r src_base="${IMAGEDIR}"
89-
local -r dst_base="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${UNAME_M}/${TAG_SUBDIR}"
95+
local -r dst_base="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}/${TAG_SUBDIR}"
9096

9197
if check_contents ; then
9298
echo "The '${dst_base}' already exists with valid contents"
@@ -120,7 +126,7 @@ action_upload() {
120126
}
121127

122128
action_download() {
123-
local -r src_base="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${UNAME_M}/${TAG_SUBDIR}"
129+
local -r src_base="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}/${TAG_SUBDIR}"
124130
local -r dst_base="${IMAGEDIR}"
125131

126132
# Download ISO images
@@ -166,7 +172,7 @@ action_verify() {
166172

167173
action_setlast() {
168174
local -r src_file="$(mktemp /tmp/setlast.XXXXXXXX)"
169-
local -r dst_file="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${UNAME_M}/last"
175+
local -r dst_file="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}/last"
170176

171177
if [ "${TAG_SUBDIR}" = "last" ] ; then
172178
echo "ERROR: Cannot set 'last' tag to itself"
@@ -180,7 +186,7 @@ action_setlast() {
180186
}
181187

182188
action_getlast() {
183-
local -r src_file="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${UNAME_M}/last"
189+
local -r src_file="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}/last"
184190
local -r dst_file="$(mktemp /tmp/getlast.XXXXXXXX)"
185191

186192
echo "Reading '${src_file}' tag contents"
@@ -193,12 +199,45 @@ action_getlast() {
193199
rm -f "${dst_file}"
194200
}
195201

202+
action_dellast() {
203+
local -r top_dir="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}"
204+
# Get the last contents with the ${TAG_SUBDIR} default
205+
local -r last_dir="$(action_getlast | awk '/LAST:/ {print $NF}')"
206+
207+
if [ -z "${last_dir}" ] ; then
208+
echo "ERROR: Cannot delete 'last' tag because it is not set or empty"
209+
exit 1
210+
fi
211+
if [ "${last_dir}" = "${TAG_SUBDIR}" ] ; then
212+
echo "ERROR: Cannot delete 'last' tag because it is the same as the new '${TAG_SUBDIR}' tag"
213+
exit 1
214+
fi
215+
216+
# Verify that the new tag exists
217+
if ! check_contents ; then
218+
echo "ERROR: Cannot reset 'last' tag to a non-existent '${TAG_SUBDIR}' tag"
219+
exit 1
220+
fi
221+
222+
# Reset the last tag and delete the old contents
223+
action_setlast
224+
run_aws_cli s3 rm --recursive "${top_dir}/${last_dir}"
225+
}
226+
196227
action_keep() {
197-
local -r top_dir="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${UNAME_M}"
228+
local -r top_dir="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}"
198229
# Get the last contents with the ${TAG_SUBDIR} default
199230
local -r last_dir="$(action_getlast | awk '/LAST:/ {print $NF}')"
200231

201-
for sub_dir in $(run_aws_cli s3 ls "${top_dir}/" | awk '{print $NF}'); do
232+
# Get all sub-directories in the top directory
233+
local -r sub_dirs=$(run_aws_cli s3 ls "${top_dir}/" | grep '/$' | awk '{print $NF}')
234+
# Skip if only one sub-directory exists
235+
if [ "$(echo "${sub_dirs}" | wc -w)" -eq 1 ] ; then
236+
echo "Only one sub-directory found in '${top_dir}', keeping it"
237+
return 0
238+
fi
239+
240+
for sub_dir in ${sub_dirs}; do
202241
if [ "${sub_dir}" = "last" ] ; then
203242
continue
204243
fi
@@ -223,7 +262,7 @@ action="${1}"
223262
shift
224263

225264
# Parse options
226-
while getopts "b:t:h" opt; do
265+
while getopts "b:t:a:h" opt; do
227266
case "${opt}" in
228267
h)
229268
usage
@@ -235,6 +274,9 @@ while getopts "b:t:h" opt; do
235274
t)
236275
TAG_SUBDIR="${OPTARG}"
237276
;;
277+
a)
278+
ARCH_SUBDIR="${OPTARG}"
279+
;;
238280
*)
239281
usage
240282
exit 1
@@ -262,7 +304,7 @@ fi
262304

263305
# Run actions
264306
case "${action}" in
265-
upload|download|verify|setlast|getlast|keep)
307+
upload|download|verify|setlast|getlast|dellast|keep)
266308
"action_${action}" "$@"
267309
;;
268310
-h|help)

0 commit comments

Comments
 (0)