57 lines
1.3 KiB
Bash
Executable File
57 lines
1.3 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# specify registry as:
|
|
# REGISTRY=my.secured.registry.com:5000
|
|
# PROTOCOL=https
|
|
#
|
|
# REGISTRY=192.168.1.33:5000
|
|
# PROTOCOL=http
|
|
#
|
|
# If you don't have SSL, be sure to add the insecure
|
|
# registry to docker configuration
|
|
|
|
REGISTRY=127.0.0.1:5000
|
|
PROTOCOL=http
|
|
|
|
|
|
|
|
IMAGE=$( echo "$1" | cut -f1 -d:)
|
|
if [ "$1" = "" ] ; then
|
|
echo "You must specify which image to pull"
|
|
exit 1
|
|
fi
|
|
|
|
TAG=$( echo "$1" | cut -s -f2 -d:)
|
|
TAG=${TAG:-latest}
|
|
|
|
## docker-search for upstream REGISTRY
|
|
# TODO: fix the grep part!
|
|
UPSTREAM=$( docker search --format="{{.Name}}" "${IMAGE}" | grep "^${IMAGE}$" )
|
|
|
|
if [ ! "${IMAGE}" = "${UPSTREAM}" ] ; then
|
|
echo "Image doesn't exists on docker hub"
|
|
exit 1
|
|
fi
|
|
|
|
# TODO: check if tag exists on upstream
|
|
|
|
## check if image exists on local registry
|
|
URL=${PROTOCOL}://${REGISTRY}/v2/${IMAGE}/tags/list
|
|
curl -sX GET "$URL" | grep '"NAME_UNKNOWN"' > /dev/null || {
|
|
## if so, check if tag exists on local registry
|
|
URL=${PROTOCOL}://${REGISTRY}/v2/${IMAGE}/manifests/${TAG}
|
|
curl -sX GET "$URL" | grep '"MANIFEST_UNKNOWN"' > /dev/null || {
|
|
## if so, download it
|
|
docker pull ${REGISTRY}/${IMAGE}:${TAG}
|
|
}
|
|
}
|
|
|
|
## then download from upstream
|
|
docker pull ${IMAGE}:${TAG}
|
|
|
|
## and update local registry
|
|
docker tag ${IMAGE}:${TAG} ${REGISTRY}/${IMAGE}:${TAG}
|
|
docker push ${REGISTRY}/${IMAGE}:${TAG}
|
|
docker rmi ${REGISTRY}/${IMAGE}:${TAG}
|
|
|