From 79f65ad002e47155ca2ba51dbdcda26663590643 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Wed, 4 Oct 2023 13:07:24 -0700 Subject: [PATCH] refactor: extract function for mtree lines --- oci/private/tarball.sh.tpl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/oci/private/tarball.sh.tpl b/oci/private/tarball.sh.tpl index 0fdc8cc8..74136621 100644 --- a/oci/private/tarball.sh.tpl +++ b/oci/private/tarball.sh.tpl @@ -12,17 +12,23 @@ readonly TAGS_FILE="{{tags}}" # https://man.freebsd.org/cgi/man.cgi?mtree(8) # so that tar produces a deterministic output. mtree=$(mktemp) +function add_to_tar() { + tar_path=$1 + content=$2 + readonly mtree_flags="uid=0 gid=0 mode=0755 time=1672560000 type=file" + echo >>"${mtree}" "blobs/${LAYER}.tar.gz ${mtree_flags} content=${IMAGE_DIR}/blobs/${LAYER}" +} MANIFEST_DIGEST=$(${YQ} eval '.manifests[0].digest | sub(":"; "/")' "${IMAGE_DIR}/index.json" | tr -d '"') MANIFEST_BLOB_PATH="${IMAGE_DIR}/blobs/${MANIFEST_DIGEST}" CONFIG_DIGEST=$(${YQ} eval '.config.digest | sub(":"; "/")' ${MANIFEST_BLOB_PATH}) CONFIG_BLOB_PATH="${IMAGE_DIR}/blobs/${CONFIG_DIGEST}" -echo >>"${mtree}" "blobs/${CONFIG_DIGEST} uid=0 gid=0 mode=0755 time=1672560000 type=file content=${CONFIG_BLOB_PATH}" +add_to_tar "blobs/${CONFIG_DIGEST}" "${CONFIG_BLOB_PATH}" LAYERS=$(${YQ} eval '.layers | map(.digest | sub(":"; "/"))' ${MANIFEST_BLOB_PATH}) for LAYER in $(${YQ} ".[]" <<< $LAYERS); do - echo >>"${mtree}" "blobs/${LAYER}.tar.gz uid=0 gid=0 mode=0755 time=0 type=file content=${IMAGE_DIR}/blobs/${LAYER}" + add_to_tar "blobs/${LAYER}.tar.gz" "${IMAGE_DIR}/blobs/${LAYER}" done # Replace newlines (unix or windows line endings) with % character. @@ -36,7 +42,7 @@ layers="${LAYERS}" \ --null-input '.[0] = {"Config": env(config), "RepoTags": "${repo_tags}" | envsubst | split("%") | map(select(. != "")) , "Layers": env(layers) | map( "blobs/" + . + ".tar.gz") }' \ --output-format json > "${STAGING_DIR}/manifest.json" -echo >>"${mtree}" "manifest.json uid=0 gid=0 mode=0644 time=1672560000 type=file content=${STAGING_DIR}/manifest.json" +add_to_tar "manifest.json" "${STAGING_DIR}/manifest.json" # We've created the manifest, now hand it off to tar to create our final output "${TAR}" --create --file "${TARBALL_PATH}" "@${mtree}"