From e930fc50cac580fcdeec25a9efc88a80be60dc0c Mon Sep 17 00:00:00 2001 From: Oleksandr Andriienko Date: Tue, 7 Jul 2020 09:15:34 +0300 Subject: [PATCH] Search required images from env for both csv openshift and kubernetes. Fix some shellchecks. Signed-off-by: Oleksandr Andriienko --- olm/addDigests.sh | 31 ++++++++++++++++--------------- olm/buildDigestMap.sh | 37 +++++++++++++++++++------------------ olm/images.sh | 8 ++++---- 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/olm/addDigests.sh b/olm/addDigests.sh index f72e743d30..d4f450b162 100755 --- a/olm/addDigests.sh +++ b/olm/addDigests.sh @@ -58,20 +58,21 @@ echo "find ${CSV_FILES_DIR} -regextype posix-egrep -regex \"${CSV_FILES_DIR}/?${ CSV_FILES=( $(find ${CSV_FILES_DIR} -regextype posix-egrep -regex "${CSV_FILES_DIR}/?${CSV_FILE_PATH_REGEXP}") ) RELATED_IMAGE_PREFIX="RELATED_IMAGE_" -rm -Rf ${BASE_DIR}/generated/csv -mkdir -p ${BASE_DIR}/generated/csv +rm -Rf "${BASE_DIR}/generated/csv" +mkdir -p "${BASE_DIR}/generated/csv" # Copy original csv files -for CSV_FILE in ${CSV_FILES[@]} +for CSV_FILE in "${CSV_FILES[@]}" do echo "CSV file: ${CSV_FILE}" - cp -pR ${CSV_FILE} ${BASE_DIR}/generated/csv + cp -pR "${CSV_FILE}" "${BASE_DIR}/generated/csv" + csvs_args="${csvs_args} -c ${CSV_FILE}" done -# Collect list digest only once to save time. We expected that digest list is the same for "openshift" and "kubernetes" platforms. -source ${SCRIPTS_DIR}/buildDigestMap.sh -w ${BASE_DIR} -c ${CSV_FILES[0]} -t ${IMAGE_TAG} ${QUIET} +# shellcheck source=buildDigestMap.sh +eval "${SCRIPTS_DIR}/buildDigestMap.sh" -w "${BASE_DIR}" -t "${IMAGE_TAG}" "${csvs_args}" ${QUIET} -if [[ ! "${QUIET}" ]]; then cat ${BASE_DIR}/generated/digests-mapping.txt; fi -for CSV_FILE in ${CSV_FILES[@]} +if [[ ! "${QUIET}" ]]; then cat "${BASE_DIR}"/generated/digests-mapping.txt; fi +for CSV_FILE in "${CSV_FILES[@]}" do CSV_FILE_COPY=${BASE_DIR}/generated/csv/$(basename ${CSV_FILE}) @@ -97,7 +98,7 @@ do # Image tag could contains invalid for Env variable name characters, so let's encode it using base32. # But alphabet of base32 uses one invalid for env variable name character '=' at the end of the line, so let's replace it by '_'. # To recovery original tag should be done opposite actions: replace '_' to '=', and decode string using 'base32 -d'. - encodedTag=$(echo ${tagOrDigest} | base32 -w 0 | tr = _) + encodedTag=$(echo "${tagOrDigest}" | base32 -w 0 | tr "=" "_") relatedImageEnvName=$(echo "${RELATED_IMAGE_PREFIX}${name}_${imageLabel}_${encodedTag}" | sed -r 's/[-.]/_/g') ENV="{ name: \"${relatedImageEnvName}\", value: \"${dest}\"}" if [[ -z ${RELATED_IMAGES_ENV} ]]; then @@ -114,16 +115,16 @@ do RELATED_IMAGES="${RELATED_IMAGES}, ${RELATED_IMAGE}" fi - sed -i -e "s;${source};${dest};" ${CSV_FILE_COPY} + sed -i -e "s;${source};${dest};" "${CSV_FILE_COPY}" done - mv ${CSV_FILE_COPY} ${CSV_FILE_COPY}.old + mv "${CSV_FILE_COPY}" "${CSV_FILE_COPY}.old" yq -ryY " ( .spec.relatedImages ) += [${RELATED_IMAGES}] | ( .spec.install.spec.deployments[0].spec.template.spec.containers[0].env ) += [${RELATED_IMAGES_ENV}] - " ${CSV_FILE_COPY}.old > ${CSV_FILE_COPY} - sed -i ${CSV_FILE_COPY} -r -e "s|tag: |# tag: |" - rm -f ${CSV_FILE_COPY}.old + " "${CSV_FILE_COPY}.old" > "${CSV_FILE_COPY}" + sed -i "${CSV_FILE_COPY}" -r -e "s|tag: |# tag: |" + rm -f "${CSV_FILE_COPY}.old" # update original file with generated changes mv "${CSV_FILE_COPY}" "${CSV_FILE}" @@ -131,4 +132,4 @@ do done # cleanup -rm -fr ${BASE_DIR}/generated +rm -fr "${BASE_DIR}/generated" diff --git a/olm/buildDigestMap.sh b/olm/buildDigestMap.sh index 52d9df5229..4902a36d6e 100755 --- a/olm/buildDigestMap.sh +++ b/olm/buildDigestMap.sh @@ -10,7 +10,7 @@ # Contributors: # Red Hat, Inc. - initial API and implementation -SCRIPTS_DIR=$(cd "$(dirname "$0")"; pwd) +SCRIPTS_DIR=$(cd "$(dirname "$0")" || exit 1; pwd) BASE_DIR="$1" QUIET="" @@ -35,7 +35,7 @@ if [[ $# -lt 1 ]]; then usage; exit; fi while [[ "$#" -gt 0 ]]; do case $1 in '-w') BASE_DIR="$2"; shift 1;; - '-c') CSV="$2"; shift 1;; + '-c') CSV="$2"; CSVS+=("${CSV}");shift 1;; '-t') IMAGE_TAG="$2"; shift 1;; '-q') QUIET="-q"; shift 0;; '--help'|'-h') usage; exit;; @@ -45,42 +45,43 @@ done if [[ ! $CSV ]] || [[ ! $IMAGE_TAG ]]; then usage; exit 1; fi -mkdir -p ${BASE_DIR}/generated +mkdir -p "${BASE_DIR}/generated" echo "[INFO] Get images from CSV ${CSV}" -source ${SCRIPTS_DIR}/images.sh +# shellcheck source=images.sh +. "${SCRIPTS_DIR}"/images.sh # todo create init method setImagesFromDeploymentEnv setOperatorImage -echo ${OPERATOR_IMAGE} +echo "${OPERATOR_IMAGE}" setPluginRegistryList -echo ${PLUGIN_REGISTRY_LIST} +echo "${PLUGIN_REGISTRY_LIST}" setDevfileRegistryList -echo ${DEVFILE_REGISTRY_LIST} +echo "${DEVFILE_REGISTRY_LIST}" writeDigest() { image=$1 imageType=$2 - echo ${image} case ${image} in *@sha256:*) - withDigest="${image}";; + withDigest=${image};; *@) - continue;; + return;; *) # for other build methods or for falling back to other registries when not found, can apply transforms here - orig_image="${image}" + orig_image=${image} if [[ -x ${SCRIPTS_DIR}/buildDigestMapAlternateURLs.sh ]]; then + # shellcheck source=buildDigestMapAlternateURLs.sh . ${SCRIPTS_DIR}/buildDigestMapAlternateURLs.sh fi if [[ ${digest} ]]; then if [[ ! "${QUIET}" ]]; then echo -n "[INFO] Got digest"; fi - echo " $digest # ${image}" + echo " $digest \# ${image}" else image="${orig_image}" digest="$(skopeo inspect --tls-verify=false docker://${image} 2>/dev/null | jq -r '.Digest')" @@ -107,19 +108,19 @@ writeDigest() { } DIGEST_FILE=${BASE_DIR}/generated/digests-mapping.txt -rm -Rf ${DIGEST_FILE} -touch ${DIGEST_FILE} +rm -Rf "${DIGEST_FILE}" +touch "${DIGEST_FILE}" -writeDigest ${OPERATOR_IMAGE} "operator-image" +writeDigest "${OPERATOR_IMAGE}" "operator-image" for image in ${REQUIRED_IMAGES}; do - writeDigest ${image} "required-image" + writeDigest "${image}" "required-image" done for image in ${PLUGIN_REGISTRY_LIST}; do - writeDigest ${image} "plugin-registry-image" + writeDigest "${image}" "plugin-registry-image" done for image in ${DEVFILE_REGISTRY_LIST}; do - writeDigest ${image} "devfile-registry-image" + writeDigest "${image}" "devfile-registry-image" done diff --git a/olm/images.sh b/olm/images.sh index 45c8009abe..3503b5717e 100644 --- a/olm/images.sh +++ b/olm/images.sh @@ -11,7 +11,7 @@ # Red Hat, Inc. - initial API and implementation setImagesFromDeploymentEnv() { - REQUIRED_IMAGES=$(yq -r '.spec.install.spec.deployments[].spec.template.spec.containers[].env[] | select(.name | test("RELATED_IMAGE_.*"; "g")) | .value' "${CSV}") + REQUIRED_IMAGES=$(yq -r '.spec.install.spec.deployments[].spec.template.spec.containers[].env[] | select(.value) | select(.name | test("RELATED_IMAGE_.*"; "g")) | .value' "${CSVS[@]}" | sort | uniq) } setOperatorImage() { @@ -20,7 +20,7 @@ setOperatorImage() { setPluginRegistryList() { registry=$(yq -r '.spec.install.spec.deployments[].spec.template.spec.containers[].env[] | select(.name | test("RELATED_IMAGE_.*plugin_registry"; "g")) | .value' "${CSV}") - setRegistryImages ${registry} + setRegistryImages "${registry}" PLUGIN_REGISTRY_LIST=${registryImages} } @@ -28,7 +28,7 @@ setPluginRegistryList() { setDevfileRegistryList() { registry=$(yq -r '.spec.install.spec.deployments[].spec.template.spec.containers[].env[] | select(.name | test("RELATED_IMAGE_.*devfile_registry"; "g")) | .value' "${CSV}") - setRegistryImages ${registry} + setRegistryImages "${registry}" DEVFILE_REGISTRY_LIST=${registryImages} } @@ -39,6 +39,6 @@ setRegistryImages() { echo -n "[INFO] Pull container ${registry} ..." ${PODMAN} pull ${registry} ${QUIET} - registryImages="$(${PODMAN} run --rm --entrypoint /bin/sh ${registry} -c "cat /var/www/html/*/external_images.txt")" + registryImages="$(${PODMAN} run --rm --entrypoint /bin/sh "${registry}" -c "cat /var/www/html/*/external_images.txt")" echo "[INFO] Found $(echo "${registryImages}" | wc -l) images in registry" }