2020-12-23 18:39:02 +05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# The Docker Image Mirror script.
|
|
|
|
|
|
|
|
WHAT_TO_MIRROR=()
|
2022-06-26 23:53:26 +05:00
|
|
|
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
|
|
|
|
2022-06-26 23:53:26 +05:00
|
|
|
MIRROR_CONFIGS=$(ls ./images/*.sh)
|
|
|
|
for file in "${MIRROR_CONFIGS[@]}"; do
|
2020-12-23 18:39:02 +05:00
|
|
|
echo "Importing ${file}..."
|
2022-06-26 23:53:26 +05:00
|
|
|
# shellcheck disable=SC2086
|
|
|
|
WHAT_TO_MIRROR=( "${WHAT_TO_MIRROR[@]}" "$(bash ${file})" )
|
2020-12-23 18:39:02 +05:00
|
|
|
done
|
|
|
|
|
2022-06-26 23:53:26 +05:00
|
|
|
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 00:13:31 +05:00
|
|
|
if ! docker pull "${image}"; then
|
2022-06-27 00:12:17 +05:00
|
|
|
echo "PULL FAILED"
|
2022-06-27 00:08:44 +05:00
|
|
|
exit 1
|
2020-12-23 18:39:02 +05:00
|
|
|
fi
|
|
|
|
|
2022-06-26 23:53:26 +05:00
|
|
|
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 00:13:31 +05:00
|
|
|
if ! docker tag "${image_hash}" "${DESTINATION_REGISTRY}/${image}"; then
|
2022-06-27 00:12:17 +05:00
|
|
|
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 00:13:31 +05:00
|
|
|
if ! docker push "${DESTINATION_REGISTRY}/${image}"; then
|
2022-06-27 00:12:17 +05:00
|
|
|
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 00:13:31 +05:00
|
|
|
docker image rm "${image}"
|
|
|
|
docker image rm "${DESTINATION_REGISTRY}/${image}"
|
2020-12-23 18:39:02 +05:00
|
|
|
}
|
|
|
|
|
2022-06-26 23:53:26 +05:00
|
|
|
# Login to registry.
|
2022-06-27 00:13:31 +05:00
|
|
|
echo "Logging into '${DESTINATION_REGISTRY}' as '${USER}'..."
|
2022-06-27 00:10:08 +05:00
|
|
|
docker login -u "${USER}" -p "${PASSWORD}" "${DESTINATION_REGISTRY}"
|
2022-06-26 23:53:26 +05:00
|
|
|
|
2020-12-23 18:39:02 +05:00
|
|
|
# Mirror images.
|
2022-06-26 23:53:26 +05:00
|
|
|
for image in "${WHAT_TO_MIRROR[@]}"; do
|
|
|
|
mirror "${image}"
|
2020-12-23 18:39:02 +05:00
|
|
|
done
|