mirror/mirror.sh

53 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# The Docker Image Mirror script.
WHAT_TO_MIRROR=()
REGISTRY_TO_MIRROR=registry.gitlab.pztrn.name/containers/mirror
for file in $(ls ./images/*.sh); do
echo "Importing ${file}..."
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}... "
docker pull "${image}" &> /dev/null
if [ $? -ne 0 ]; then
echo "FAIL"
exit
fi
image_hash=$(docker images -a | grep "^${image_name}" | grep "${image_version}" | awk {' print $3 '})
echo -n "${image_hash} received... "
docker tag "${image_hash}" "${REGISTRY_TO_MIRROR}/${image}" &> /dev/null
if [ $? -ne 0 ]; then
echo "FAIL"
exit
fi
echo -n "pushing... "
docker push "${REGISTRY_TO_MIRROR}/${image}" &> /dev/null
if [ $? -ne 0 ]; then
echo "FAIL"
exit
fi
echo "OK"
docker image rm "${image}" &> /dev/null
docker image rm "${REGISTRY_TO_MIRROR}/${image}" &> /dev/null
}
# Mirror images.
for image in ${WHAT_TO_MIRROR[@]}; do
mirror ${image}
done