Skip to content

Commit

Permalink
refactor: extract function for mtree lines
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Oct 4, 2023
1 parent 311553d commit 79f65ad
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions oci/private/tarball.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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}"

0 comments on commit 79f65ad

Please sign in to comment.