mirror/mirror.sh
Stanislav N. aka pztrn 12c0d3771d
Some checks failed
continuous-integration/drone/push Build is failing
Remove output redirection to /dev/null for debugging purposes.
2022-06-27 00:13:31 +05:00

58 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# The Docker Image Mirror script.
WHAT_TO_MIRROR=()
DESTINATION_REGISTRY=code.pztrn.name/containers/mirror
USER=${REGISTRY_USER}
PASSWORD=${REGISTRY_PASSWORD}
MIRROR_CONFIGS=$(ls ./images/*.sh)
for file in "${MIRROR_CONFIGS[@]}"; do
echo "Importing ${file}..."
# shellcheck disable=SC2086
WHAT_TO_MIRROR=( "${WHAT_TO_MIRROR[@]}" "$(bash ${file})" )
done
echo "Images to mirror: ${WHAT_TO_MIRROR[*]}"
function mirror() {
image=$1
image_name=$(echo "${image}" | cut -d":" -f 1)
image_version=$(echo "${image}" | cut -d":" -f 2)
echo -n "Mirroring ${image}... "
if ! docker pull "${image}"; then
echo "PULL FAILED"
exit 1
fi
image_hash=$(docker images -a | grep "^${image_name}" | grep "${image_version}" | awk "{' print $3 '}")
echo -n "${image_hash} received... "
if ! docker tag "${image_hash}" "${DESTINATION_REGISTRY}/${image}"; then
echo "TAGGING FAILED"
exit 1
fi
echo -n "pushing... "
if ! docker push "${DESTINATION_REGISTRY}/${image}"; then
echo "FAILED"
exit 1
fi
echo "OK"
docker image rm "${image}"
docker image rm "${DESTINATION_REGISTRY}/${image}"
}
# Login to registry.
echo "Logging into '${DESTINATION_REGISTRY}' as '${USER}'..."
docker login -u "${USER}" -p "${PASSWORD}" "${DESTINATION_REGISTRY}"
# Mirror images.
for image in "${WHAT_TO_MIRROR[@]}"; do
mirror "${image}"
done