~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
113 lines · 5.6 KB Raw History
#!/bin/sh
# The server's image as an OCI image layout, made with no container engine,
# as release.yml's runner gives its jobs none. It is the Dockerfile's last
# stage: debian:trixie-slim for linux/amd64, as the registry has it when
# this runs, and one layer more holding `pds` and its user, with the
# Dockerfile's user, environment, volume, port, entry point and command.
# The image's labels, and its manifest's annotations, name its version, the
# commit it was built from and the base, by digest.
#
#   SOURCE_DATE_EPOCH=<commit time> .knotless/image.sh <pds> <version> <commit> <dir>
#
# The layer's files and the image's dates are SOURCE_DATE_EPOCH's, so the
# same binary on the same base makes the same image. Needs crane, jq, GNU
# tar, gzip and sha256sum.
set -eu

bin=$1 version=$2 commit=$3 out=$4
: "${SOURCE_DATE_EPOCH:?the commit's time, in seconds}"
base=debian:trixie-slim
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT

blob() { echo "$work/base/blobs/sha256/${1#sha256:}"; }
digest=$(crane digest --platform linux/amd64 "$base")
crane pull --format=oci "$base@$digest" "$work/base"
manifest=$(blob "$(jq -r '.manifests[0].digest' "$work/base/index.json")")
jq -e '.mediaType == "application/vnd.oci.image.manifest.v1+json"
  and all(.layers[]; .mediaType == "application/vnd.oci.image.layer.v1.tar+gzip")' \
  "$manifest" >/dev/null || { echo "$base@$digest: not an OCI image of gzipped layers" >&2; exit 1; }

# The layer: what the Dockerfile's last stage adds to the base. Its
# /etc/passwd and /etc/group are the base's with the user pds, 10001, and
# its group, as the Dockerfile's groupadd and useradd make them; the
# licenses are the repository's, beside this script.
root="$work/root"
mkdir -p "$root/usr/local/bin" "$root/usr/share/doc/monolith" "$root/data"
install -m 755 "$bin" "$root/usr/local/bin/pds"
install -m 644 "$(dirname "$0")/../LICENSE-APACHE" "$(dirname "$0")/../LICENSE-MIT" "$root/usr/share/doc/monolith/"
for layer in $(jq -r '.layers[].digest' "$manifest"); do
  tar -xzf "$(blob "$layer")" -C "$root" --wildcards 'etc/passwd' 'etc/group' 2>/dev/null || true
done
[ -s "$root/etc/passwd" ] && [ -s "$root/etc/group" ] || { echo "no /etc/passwd or /etc/group in $base" >&2; exit 1; }
echo 'pds:x:10001:10001::/data:/usr/sbin/nologin' >> "$root/etc/passwd"
echo 'pds:x:10001:' >> "$root/etc/group"
chown -R 0:0 "$root"
chown 10001:10001 "$root/data"
tar -C "$root" -cf "$work/layer.tar" --sort=name --mtime="@$SOURCE_DATE_EPOCH" \
  --numeric-owner --format=posix --pax-option=delete=atime,delete=ctime etc usr data
diff_id=$(sha256sum "$work/layer.tar" | cut -d' ' -f1)
gzip -9n < "$work/layer.tar" > "$work/layer.tar.gz"

created=$(date -u -d "@$SOURCE_DATE_EPOCH" +%Y-%m-%dT%H:%M:%SZ)
jq --arg diff "sha256:$diff_id" --arg created "$created" \
  --arg version "$version" --arg commit "$commit" --arg base "docker.io/library/$base" --arg digest "$digest" '
  .created = $created
  | .rootfs.diff_ids += [$diff]
  | .history = (.history // []) + [{created: $created, created_by: "pds, and its user (.knotless/image.sh)"}]
  | .config.User = "pds"
  | .config.Env = (.config.Env // []) + ["PDS_PORT=2583", "PDS_BIND_ADDRESS=::", "PDS_DATA_DIR=/data"]
  | .config.Volumes = {"/data": {}}
  | .config.ExposedPorts = {"2583/tcp": {}}
  | .config.Entrypoint = ["pds"]
  | .config.Cmd = ["serve"]
  | .config.Labels = (.config.Labels // {}) + {
      "org.opencontainers.image.title": "monolith",
      "org.opencontainers.image.licenses": "MIT OR Apache-2.0",
      "org.opencontainers.image.version": $version,
      "org.opencontainers.image.revision": $commit,
      "org.opencontainers.image.source": "https://knotless.dev/chadtmiller.com/monolith",
      "org.opencontainers.image.base.name": $base,
      "org.opencontainers.image.base.digest": $digest
    }' "$(blob "$(jq -r '.config.digest' "$manifest")")" > "$work/config.json"

# The layout: the base's layers, the new one, the config, the manifest and
# an index naming it alone.
rm -rf "$out"
mkdir -p "$out/blobs/sha256"
put() {
  d=$(sha256sum "$1" | cut -d' ' -f1)
  cp "$1" "$out/blobs/sha256/$d"
  echo "$d $(wc -c < "$1" | tr -d ' ')"
}
for layer in $(jq -r '.layers[].digest' "$manifest"); do cp "$(blob "$layer")" "$out/blobs/sha256/"; done
read -r layer_d layer_n <<EOF
$(put "$work/layer.tar.gz")
EOF
read -r config_d config_n <<EOF
$(put "$work/config.json")
EOF
jq --arg ld "sha256:$layer_d" --argjson ln "$layer_n" --arg cd "sha256:$config_d" --argjson cn "$config_n" \
  --arg version "$version" --arg commit "$commit" --arg base "docker.io/library/$base" --arg digest "$digest" '
  .config = {mediaType: "application/vnd.oci.image.config.v1+json", digest: $cd, size: $cn}
  | .layers += [{mediaType: "application/vnd.oci.image.layer.v1.tar+gzip", digest: $ld, size: $ln}]
  | .annotations = {
      "org.opencontainers.image.version": $version,
      "org.opencontainers.image.revision": $commit,
      "org.opencontainers.image.base.name": $base,
      "org.opencontainers.image.base.digest": $digest
    }' "$manifest" > "$work/manifest.json"
read -r manifest_d manifest_n <<EOF
$(put "$work/manifest.json")
EOF
jq -n --arg d "sha256:$manifest_d" --argjson n "$manifest_n" --arg version "$version" '{
  schemaVersion: 2,
  mediaType: "application/vnd.oci.image.index.v1+json",
  manifests: [{
    mediaType: "application/vnd.oci.image.manifest.v1+json", digest: $d, size: $n,
    platform: {architecture: "amd64", os: "linux"},
    annotations: {"org.opencontainers.image.ref.name": $version}
  }]
}' > "$out/index.json"
echo '{"imageLayoutVersion":"1.0.0"}' > "$out/oci-layout"
echo "$out: monolith $version at $commit on $base@$digest, manifest sha256:$manifest_d"