#!/bin/bash # # This script should be run on the build host to manage cache of build artifacts set -euo pipefail SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # shellcheck source=test/bin/common.sh source "${SCRIPTDIR}/common.sh" AWS_BUCKET_NAME="${AWS_BUCKET_NAME:-microshift-build-cache}" BCH_SUBDIR= TAG_SUBDIR= ARCH_SUBDIR="${UNAME_M}" usage() { cat - <//'. -h Show this help. upload: Upload build artifacts from the local disk to the specified '${AWS_BUCKET_NAME}///' AWS S3 bucket. The 'verify' operation is run before the upload attempt. If data already exists at the destination, the upload is skipped and the script exits with 0 status. download: Download build artifacts from the specified '${AWS_BUCKET_NAME}///' AWS S3 bucket to the local disk. verify: Exit with 0 status if the specified '${AWS_BUCKET_NAME}///' sub-directory exists and contains files, 1 otherwise. setlast: Update the '${AWS_BUCKET_NAME}///last' file contents in the AWS S3 bucket with the specified ''. getlast: Retrieve the '${AWS_BUCKET_NAME}///last' file contents from the AWS S3 bucket. The output format is "LAST: " for easy parsing. The script returns the specified '' as a fallback if the bucket file does not exist. dellast: Delete the data pointed by the '${AWS_BUCKET_NAME}///last' file contents and update the 'last' file contents in the AWS S3 bucket with the specified '' keep: Delete all data from the '${AWS_BUCKET_NAME}//' AWS S3 bucket, only keeping the 'last' and the specified '' sub-directories. Options: -b The branch sub-directory in the AWS S3 bucket. -t The tag sub-directory in the AWS S3 bucket. -a The architecture sub-directory in the AWS S3 bucket (default: ${ARCH_SUBDIR}). EOF } run_aws_cli() { if ! "${AWSCLI}" "$@" ; then echo "ERROR: Failed to run AWS CLI command: $*" >&2 return 1 fi return 0 } check_contents(){ local -r src_dir="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}/${TAG_SUBDIR}" local -r must_contain_array=("mirror-registry\.tar$" "brew-rpms\.tar$" "repo\.tar$" "${VM_POOL_BASENAME}/.*\.iso$") echo "Checking contents of '${src_dir}'" local s3_stdout s3_stdout=$("${AWSCLI}" s3 ls "${src_dir}" --recursive) || return 1 for item in "${must_contain_array[@]}"; do if ! echo "${s3_stdout}" | grep -qE "${item}"; then return 1 fi done return 0 } action_upload() { local -r src_base="${IMAGEDIR}" local -r dst_base="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}/${TAG_SUBDIR}" if check_contents ; then echo "The '${dst_base}' already exists with valid contents" return fi # Upload ISO images local -r iso_base="${src_base}/${VM_POOL_BASENAME}" local -r iso_size="$(du -csh "${iso_base}" | awk 'END{print $1}')" local -r iso_dest="${dst_base}/${VM_POOL_BASENAME}" echo "Uploading ${iso_size} of ISO images to '${iso_dest}'" run_aws_cli s3 sync --quiet --include '*.iso' "${iso_base}" "${iso_dest}" # Upload mirror-registry, brew-rpms and repo archives for dir in mirror-registry brew-rpms repo ; do local pkg_src="${src_base}/${dir}.tar" local pkg_dst="${dst_base}/${dir}.tar" # Archive the files before the upload rm -f "${pkg_src}" tar cf "${pkg_src}" -C "${src_base}" "${dir}" local repo_size repo_size="$(du -csh "${pkg_src}" | awk 'END{print $1}')" echo "Uploading ${repo_size} of data to '${pkg_dst}'" run_aws_cli s3 cp --quiet "${pkg_src}" "${pkg_dst}" rm -f "${pkg_src}" done } action_download() { local -r src_base="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}/${TAG_SUBDIR}" local -r dst_base="${IMAGEDIR}" # Download ISO images local -r iso_base="${src_base}/${VM_POOL_BASENAME}" local -r iso_dest="${dst_base}/${VM_POOL_BASENAME}" echo "Downloading ISO images from '${iso_base}'" run_aws_cli s3 sync --quiet --include '*.iso' "${iso_base}" "${iso_dest}" local -r iso_size="$(du -csh "${iso_dest}" | awk 'END{print $1}')" echo "Downloaded ${iso_size} of ISO images" # Download mirror-registry, brew-rpms and repo archives for dir in mirror-registry brew-rpms repo ; do local pkg_src="${src_base}/${dir}.tar" local pkg_dst="${dst_base}/${dir}.tar" local pkg_dir="${dst_base}/${dir}" echo "Downloading data from '${pkg_src}'" rm -f "${pkg_dst}" run_aws_cli s3 cp --quiet "${pkg_src}" "${pkg_dst}" # Unarchive the files after the download rm -rf "${pkg_dir}" tar xf "${pkg_dst}" -C "${dst_base}" rm -f "${pkg_dst}" local repo_size repo_size="$(du -csh "${pkg_dir}" | awk 'END{print $1}')" echo "Downloaded ${repo_size} of data" done } action_verify() { if check_contents ; then echo OK exit 0 fi echo KO exit 1 } action_setlast() { local -r src_file="$(mktemp /tmp/setlast.XXXXXXXX)" local -r dst_file="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}/last" if [ "${TAG_SUBDIR}" = "last" ] ; then echo "ERROR: Cannot set 'last' tag to itself" exit 1 fi echo "Updating '${dst_file}' with the '${TAG_SUBDIR}' tag" echo -n "${TAG_SUBDIR}" > "${src_file}" run_aws_cli s3 cp --quiet "${src_file}" "${dst_file}" rm -f "${src_file}" } action_getlast() { local -r src_file="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}/last" local -r dst_file="$(mktemp /tmp/getlast.XXXXXXXX)" echo "Reading '${src_file}' tag contents" run_aws_cli s3 cp --quiet "${src_file}" "${dst_file}" || true if [ -s "${dst_file}" ] ; then echo "LAST: $(cat "${dst_file}")" else echo "LAST: ${TAG_SUBDIR}" fi rm -f "${dst_file}" } action_dellast() { local -r top_dir="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}" # Get the last contents with the ${TAG_SUBDIR} default local -r last_dir="$(action_getlast | awk '/LAST:/ {print $NF}')" if [ -z "${last_dir}" ] ; then echo "ERROR: Cannot delete 'last' tag because it is not set or empty" exit 1 fi if [ "${last_dir}" = "${TAG_SUBDIR}" ] ; then echo "ERROR: Cannot delete 'last' tag because it is the same as the new '${TAG_SUBDIR}' tag" exit 1 fi # Verify that the new tag exists if ! check_contents ; then echo "ERROR: Cannot reset 'last' tag to a non-existent '${TAG_SUBDIR}' tag" exit 1 fi # Reset the last tag and delete the old contents action_setlast run_aws_cli s3 rm --recursive "${top_dir}/${last_dir}" } action_keep() { local -r top_dir="s3://${AWS_BUCKET_NAME}/${BCH_SUBDIR}/${ARCH_SUBDIR}" # Get the last contents with the ${TAG_SUBDIR} default local -r last_dir="$(action_getlast | awk '/LAST:/ {print $NF}')" # Get all sub-directories in the top directory local -r sub_dirs=$(run_aws_cli s3 ls "${top_dir}/" | grep '/$' | awk '{print $NF}') # Skip if only one sub-directory exists if [ "$(echo "${sub_dirs}" | wc -w)" -eq 1 ] ; then echo "Only one sub-directory found in '${top_dir}', keeping it" return 0 fi for sub_dir in ${sub_dirs}; do if [ "${sub_dir}" = "last" ] ; then continue fi if [ "${sub_dir}" = "${TAG_SUBDIR}/" ] || [ "${sub_dir}" = "${last_dir}/" ] ; then echo "Keeping '${sub_dir}' sub-directory" continue fi echo "Deleting '${sub_dir}' sub-directory" run_aws_cli s3 rm --recursive "${top_dir}/${sub_dir}" done } # # Main function # if [ $# -eq 0 ]; then usage exit 1 fi action="${1}" shift # Parse options while getopts "b:t:a:h" opt; do case "${opt}" in h) usage exit 0 ;; b) BCH_SUBDIR="${OPTARG}" ;; t) TAG_SUBDIR="${OPTARG}" ;; a) ARCH_SUBDIR="${OPTARG}" ;; *) usage exit 1 ;; esac done if [ -z "${BCH_SUBDIR}" ] || [ -z "${TAG_SUBDIR}" ] ; then echo "ERROR: The and sub-directory values are mandatory" echo usage exit 1 fi # Install AWS CLI tools if [ ! -e "${AWSCLI}" ] ; then "${ROOTDIR}/scripts/fetch_tools.sh" awscli fi # Verify the bucket can be accessed if ! run_aws_cli s3 ls "${AWS_BUCKET_NAME}" &>/dev/null ; then echo "ERROR: Cannot access the '${AWS_BUCKET_NAME}' AWS bucket" exit 1 fi # Run actions case "${action}" in upload|download|verify|setlast|getlast|dellast|keep) "action_${action}" "$@" ;; -h|help) usage exit 0 ;; *) usage exit 1 ;; esac