mirror/mirror.sh

60 lines
1.4 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=()
DESTINATION_REGISTRY=code.pztrn.name/containers/mirror
2022-06-27 00:10:08 +05:00
USER=${REGISTRY_USER}
PASSWORD=${REGISTRY_PASSWORD}
2020-12-23 18:39:02 +05:00
MIRROR_CONFIGS=$(ls ./images/*.sh)
# shellcheck disable=SC2068
for file in ${MIRROR_CONFIGS[@]}; do
2020-12-23 18:39:02 +05:00
echo "Importing ${file}..."
# shellcheck disable=SC2086,SC2207,SC2206
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}... "
2022-06-27 01:36:27 +05:00
if ! docker pull "${image}" &>/dev/null; then
echo "PULL FAILED"
2022-06-27 00:08:44 +05:00
exit 1
2020-12-23 18:39:02 +05:00
fi
# shellcheck disable=SC1083
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... "
2022-06-27 01:36:27 +05:00
if ! docker tag "${image_hash}" "${DESTINATION_REGISTRY}/${image}" &>/dev/null; then
echo "TAGGING FAILED"
2022-06-27 00:08:44 +05:00
exit 1
2020-12-23 18:39:02 +05:00
fi
echo -n "pushing... "
2022-06-27 01:36:27 +05:00
if ! docker push "${DESTINATION_REGISTRY}/${image}" &>/dev/null; then
echo "FAILED"
2022-06-27 00:08:44 +05:00
exit 1
2020-12-23 18:39:02 +05:00
fi
echo "OK"
2022-06-27 01:36:27 +05:00
docker image rm "${image}" &>/dev/null
docker image rm "${DESTINATION_REGISTRY}/${image}" &>/dev/null
2020-12-23 18:39:02 +05:00
}
# Login to registry.
echo "Logging into '${DESTINATION_REGISTRY}' as '${USER}'..."
2022-06-27 00:10:08 +05:00
docker login -u "${USER}" -p "${PASSWORD}" "${DESTINATION_REGISTRY}"
2020-12-23 18:39:02 +05:00
# Mirror images.
for image in "${WHAT_TO_MIRROR[@]}"; do
mirror "${image}"
2020-12-23 18:39:02 +05:00
done