mirror/mirror.sh
Stanislav N. aka pztrn 0d60c4b933
Some checks reported errors
continuous-integration/drone/push Build encountered an error
Update mirrorred images list and add Drone config.
2022-06-26 23:53:26 +05:00

57 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
REGISTRY_USER=$1
REGISTRY_PASSWORD=$2
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}" &> /dev/null; then
echo "FAIL"
exit
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}" &> /dev/null; then
echo "FAIL"
exit
fi
echo -n "pushing... "
if ! docker push "${REGISTRY_TO_MIRROR}/${image}" &> /dev/null; then
echo "FAIL"
exit
fi
echo "OK"
docker image rm "${image}" &> /dev/null
docker image rm "${REGISTRY_TO_MIRROR}/${image}" &> /dev/null
}
# Login to registry.
docker login -u "${REGISTRY_USER}" -p "${REGISTRY_PASSWORD}" "${DESTINATION_REGISTRY}"
# Mirror images.
for image in "${WHAT_TO_MIRROR[@]}"; do
mirror "${image}"
done