mirror/mirror.sh
Stanislav N. aka pztrn 6af5138b90
Some checks failed
continuous-integration/drone/push Build is failing
Silence docker logging.
2022-06-27 01:36:27 +05:00

60 lines
1.4 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)
# shellcheck disable=SC2068
for file in ${MIRROR_CONFIGS[@]}; do
echo "Importing ${file}..."
# shellcheck disable=SC2086,SC2207,SC2206
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}" &>/dev/null; then
echo "PULL FAILED"
exit 1
fi
# shellcheck disable=SC1083
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}" &>/dev/null; then
echo "TAGGING FAILED"
exit 1
fi
echo -n "pushing... "
if ! docker push "${DESTINATION_REGISTRY}/${image}" &>/dev/null; then
echo "FAILED"
exit 1
fi
echo "OK"
docker image rm "${image}" &>/dev/null
docker image rm "${DESTINATION_REGISTRY}/${image}" &>/dev/null
}
# 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