Last active
January 25, 2016 06:11
-
-
Save CoRfr/3216ab29a15bf237dcbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This shell script allows you to maintain a local cache of public images. | |
# | |
# Just add your images in CACHE_IMAGES, and it will push the image to the | |
# registry specified in LOCAL_REGISTRY. | |
# The / in the origin image name is replaced by a -. | |
# google/cadvisor => local_registry:5000/google-cadvisor | |
# | |
# If you want to rename the destination image, just specify the destination name | |
# after a comma. For instance: | |
# 'google/cadvisor,cadvisor' | |
# | |
# You can specify a specific version to sync like: | |
# 'google/cadvisor:0.5.0' | |
# Otherwise, it will default to latest. | |
# | |
# You can probably use this as a cronjob to sync every hour or so. | |
COLOR_MSG="\033[1;36m" | |
COLOR_ERROR="\033[1;31m" | |
COLOR_WARN="\033[1;93m" | |
COLOR_RESET="\033[0m" | |
declare -a CACHE_IMAGES=( | |
'clue/h5ai' | |
'nickstenning/graphite' | |
'google/cadvisor' | |
'kubernetes/heapster_influxdb' | |
'kubernetes/heapster_grafana' | |
'kubernetes/heapster' | |
'million12/toolbox' | |
'ruby:2.1' | |
'ruby:2.2' | |
'corfr/calamari-server' | |
'corfr/calamari-minion' | |
'corfr/coreos-pxe-spoke' | |
'corfr/coreos-pxe-hub' | |
'corfr/jenkins-swarm-docker-client' | |
'corfr/jenkins-swarm-docker-client:1.7.1' | |
'corfr/jenkins-swarm-docker-client:1.6.2' | |
'corfr/jenkins-job-builder' | |
'corfr/git-autoupdate' | |
'corfr/yocto-dev' | |
'corfr/subgit' | |
'corfr/sensu-client' | |
'corfr/vmware-tools' | |
'ceph/daemon' | |
'ceph/config' | |
'pataquets/gearmand' | |
'mribeiro/xmllint' | |
'python:2.7.9' | |
'busybox' | |
'sstarcher/sensu' | |
'sstarcher/uchiwa' | |
) | |
REMOTE_REGISTRY=${REMOTE_REGISTRY:-"registry.hub.docker.com"} | |
REMOTE_REGISTRY_PROTOCOL=${REMOTE_REGISTRY_PROTOCOL:-"https"} | |
LOCAL_REGISTRY=${LOCAL_REGISTRY:-"registry-legato:5000"} | |
LOCAL_REGISTRY_PROTOCOL=${LOCAL_REGISTRY_PROTOCOL:-"http"} | |
function check_ret { | |
RETVAL=$? | |
if [ $RETVAL -ne 0 ]; then | |
echo -e $COLOR_ERROR "Exit Code $RETVAL" $COLOR_RESET | |
exit $RETVAL | |
fi | |
} | |
function get_registry_image_id { | |
registry=$1 | |
http_protocol=$2 | |
repo=$3 | |
version=$4 | |
curl ${http_protocol}://$registry/v1/repositories/$repo/tags/$version > /tmp/registry-img.id 2> /dev/null | |
if grep pk /tmp/registry-img.id > /dev/null; then | |
jq -r .[0].id /tmp/registry-img.id | |
else | |
jq -r . /tmp/registry-img.id | cut -c 1-8 | |
fi | |
return 0 | |
} | |
function sync_image { | |
img=$1 | |
dst=$2 | |
src=$(echo $img | sed 's/,.*//g') | |
if [ -z "$dst" ]; then | |
dst=$(echo $img | sed 's/.*,//g') | |
fi | |
version=$(echo $src | sed 's/.*:\([^:]*\)$/\1/g') | |
src_orig=$src | |
# Version specified ? | |
if [[ "$version" == "$src" ]]; then | |
version="latest" | |
else | |
src=$(echo $src | sed "s/:$version//g") | |
fi | |
# Destination specified ? | |
if [[ "$src_orig" == "$dst" ]]; then | |
dst=$(echo $src | sed "s^/^-^g") | |
fi | |
dst_img="${LOCAL_REGISTRY}/$dst" | |
echo -e $COLOR_MSG "Syncing $src -> $dst_img ($version)" $COLOR_RESET | |
current_remote_id=$(get_registry_image_id $REMOTE_REGISTRY $REMOTE_REGISTRY_PROTOCOL $src $version) | |
echo $current_remote_id | |
current_local_id=$(get_registry_image_id $LOCAL_REGISTRY $LOCAL_REGISTRY_PROTOCOL $dst $version) | |
echo $current_local_id | |
if [[ "$current_remote_id" == "$current_local_id" ]]; then | |
return 0 | |
fi | |
docker pull "$src:$version" | |
check_ret | |
src_hash=$(docker images | grep $version | grep $src | awk '{print $3}' | head -1) | |
echo -e $COLOR_MSG "$src -> $src_hash" $COLOR_RESET | |
if [ -z "$src_hash" ]; then | |
echo "Unable to find hash for $src" | |
exit 1 | |
fi | |
docker tag -f $src_hash $dst_img:$version | |
check_ret | |
docker push $dst_img:$version | |
check_ret | |
} | |
if [ -z "$1" ]; then | |
for img in ${CACHE_IMAGES[@]}; do | |
sync_image $img | |
done | |
else | |
sync_image $1 $2 | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment