mirror/mirror.sh

53 lines
1.1 KiB
Bash
Raw Normal View History

2020-12-23 18:39:02 +05:00
#!/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}) )
2020-12-23 18:39:02 +05:00
done
echo "Images to mirror: ${WHAT_TO_MIRROR[@]}"
2020-12-23 18:39:02 +05:00
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 '})
2020-12-23 18:39:02 +05:00
echo -n "${image_hash} received... "
docker tag "${image_hash}" "${REGISTRY_TO_MIRROR}/${image}" &> /dev/null
2020-12-23 18:39:02 +05:00
if [ $? -ne 0 ]; then
echo "FAIL"
exit
fi
echo -n "pushing... "
docker push "${REGISTRY_TO_MIRROR}/${image}" &> /dev/null
2020-12-23 18:39:02 +05:00
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
2020-12-23 18:39:02 +05:00
}
# Mirror images.
for image in ${WHAT_TO_MIRROR[@]}; do
mirror ${image}
done