makedist/bin/makedist-docker

108 lines
3.2 KiB
Bash
Executable File

#!/bin/bash
# makedist-docker - push rootfs images to docker hub
# Copyright (c) 2024 by Silvan Calarco <silvan.calarco@mambasoft.it>
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
makedist_me="${0##*/}"
copyleft="\
makedist-docker - push rootfs images to docker hub
Copyright (c) 2024 by Silvan Calarco <silvan.calarco@mambasoft.it>
"
echo "$copyleft"
function usage() {
echo "\
usage: $makedist_me target language
Example:
$makedist_me rootfs-base en
" >&2
}
TARGET=$1
LANGUAGE=$2
[ "${TARGET}" -a "${LANGUAGE}" ] || {
usage
exit 1
}
. /etc/sysconfig/makedist
BASEDIR=${REPODIR_LOCAL}/media/rolling/${TARGET}/${LANGUAGE}
[ "${DOCKER_USER}" -a "${DOCKER_TOKEN}" ] || {
echo "ERROR: missing docker credentials; exiting."
exit 1
}
[ "${DOCKER_REPOSITORY}" ] || {
echo "ERROR: missing DOCKER_REPOSITORY declaration; exiting."
exit 1
}
create_and_push_manifest() {
local MANIFEST_TAG="$1"
local MANIFESTS_LIST="$2"
local SNAPSHOT_VERSION="$3"
local PLATFORM
docker manifest rm ${MANIFEST_TAG}
docker manifest create ${MANIFEST_TAG} ${MANIFESTS_LIST}
for ARCH in x86_64 aarch64 i586; do
if [ "${ARCH}" == "aarch64" ]; then
PLATFORM="arm64"
elif [ "${ARCH}" == "x86_64" ]; then
PLATFORM="amd64"
elif [ "${ARCH}" == "i586" ]; then
PLATFORM="386"
fi
docker manifest annotate ${MANIFEST_TAG} \
${DOCKER_REPOSITORY}:snapshot-${SNAPSHOT_VERSION}-${ARCH} \
--os linux --arch ${PLATFORM}
done
echo "Pushing manifest with tag ${MANIFEST_TAG}..."
docker manifest push ${MANIFEST_TAG}
}
docker login -u ${DOCKER_USER} -p ${DOCKER_TOKEN}
MANIFESTS_LIST=""
for ARCH in x86_64 aarch64 i586; do
if [ "${ARCH}" == "aarch64" ]; then
PLATFORM="linux/arm64"
elif [ "${ARCH}" == "x86_64" ]; then
PLATFORM="linux/amd64"
elif [ "${ARCH}" == "i586" ]; then
PLATFORM="linux/386"
fi
ROOTFS_ARCHIVE=`readlink -f ${BASEDIR}/openmamba-${TARGET}-${LANGUAGE}-snapshot.${ARCH}.tar.xz`
SNAPSHOT_VERSION=`echo ${ROOTFS_ARCHIVE} | sed "s|.*snapshot-\([0-9]*\).*|\1|"`
if [ "$SNAPSHOT_VERSION" == "" ]; then
echo "ERROR: unable to find snapshot version from ${ROOTFS_ARCHIVE}; skipping ${ARCH}..."
continue
fi
IMAGE_TAG="snapshot-${SNAPSHOT_VERSION}-${ARCH}"
if [ -z "$(docker images -q ${DOCKER_REPOSITORY}:${IMAGE_TAG} 2>/dev/null)" ]; then
echo "Importing `basename ${ROOTFS_ARCHIVE}` (arch:${ARCH};version:${SNAPSHOT_VERSION})..."
xzcat ${ROOTFS_ARCHIVE} | docker import --platform ${PLATFORM} - ${DOCKER_REPOSITORY}:${IMAGE_TAG} || exit 1
else
echo "Image ${DOCKER_REPOSITORY}:${IMAGE_TAG} already exists locally"
fi
docker push ${DOCKER_REPOSITORY}:snapshot-${SNAPSHOT_VERSION}-${ARCH}
MANIFESTS_LIST="${MANIFESTS_LIST} ${DOCKER_REPOSITORY}:snapshot-${SNAPSHOT_VERSION}-${ARCH}"
done
# Prepare multiplatform manifest
create_and_push_manifest ${DOCKER_REPOSITORY}:snapshot-${SNAPSHOT_VERSION} "${MANIFESTS_LIST}" ${SNAPSHOT_VERSION}
create_and_push_manifest ${DOCKER_REPOSITORY}:latest "${MANIFESTS_LIST}" ${SNAPSHOT_VERSION}