# # # ## NOTE ## # 1. Registry service must be running # 2. Volume must be mounted locally ( -v /docker/registry/latest:/tmp ) # #export _REGISTRY_HOST=192.168.140.208 #export _REGISTRY_PORT=5000 export _REGISTRY_HOST=localhost export _REGISTRY_PORT=$2 export _REGISTRY_HOME=/docker/registry/latest export _LIBRARY_HOME=${_REGISTRY_HOME}/registry/repositories/library export _IMAGES_HOME=${_REGISTRY_HOME}/registry/images _get_registry_images () { for i in `docker_registry ${_REGISTRY_HOST} ${_REGISTRY_PORT} | perl -p -e "s|library/||g" | perl -p -e "s| ||g"`; do echo "$i" done } _help () { echo "Please execute this way" echo "$0 -q " echo "$0 -d :" exit } _delete_image () { #http://192.168.140.208:5000/v1/repositories/ubuntu/tags #http://192.168.140.208:5000/v1/images/90a0ff921ef27c15382e8d04e0d43a726af4b4cb40fef056bcbe7432d46ffe84/ancestry if [ `_get_registry_images | grep -w "$_IMAGE$" | grep -c "$_IMAGE"` -eq 0 ]; then echo "This '$_IMAGE' image is not in this '$_REGISTRY_HOST:$_REGISTRY_PORT' registry" exit 1 else _IMAGE_NAME=`echo "$_IMAGE" | awk -F':' '{print $1}'` _IMAGE_TAG=`echo "$_IMAGE" | awk -F':' '{print $2}'` _IMAGE_COUNT=`_get_registry_images | grep -c "$_IMAGE_NAME"` _OUTPUT=`curl -s -S "http://${_REGISTRY_HOST}:${_REGISTRY_PORT}/v1/repositories/${_IMAGE_NAME}/tags" | python -mjson.tool | grep "$_IMAGE_TAG\"\:" | awk '{print $2}' | perl -p -e "s|\"||g" | perl -p -e "s|\,||g"` _OUTPUT_OTHER_TAGS=`curl -s -S "http://${_REGISTRY_HOST}:${_REGISTRY_PORT}/v1/repositories/${_IMAGE_NAME}/tags" | python -mjson.tool | grep -v "$_IMAGE_TAG\"\:" | awk '{print $2}' | perl -p -e "s|\"||g" | perl -p -e "s|\,||g"` _ANCESTRY=`curl -s -S "http://${_REGISTRY_HOST}:${_REGISTRY_PORT}/v1/images/${_OUTPUT}/ancestry" | python -mjson.tool | grep '"' | perl -p -e "s|\"||g" | perl -p -e "s|\,||g"` _OTHER_TAG_COUNT=`echo $_OUTPUT_OTHER_TAGS | wc -l` if [ ${_IMAGE_COUNT} -eq 1 ]; then #cp -pr ${_REGISTRY_HOME}/docker-registry.db ${_REGISTRY_HOME}/docker-registry.db_bkp sqlite3 ${_REGISTRY_HOME}/docker-registry.db "delete from repository where name = 'library/${_IMAGE_NAME}' " fi if [ `echo $_OTHER_TAG_COUNT` -ne 0 ]; then for i in `echo $_OUTPUT_OTHER_TAGS`; do _TEMP_VAR=`curl -s -S "http://${_REGISTRY_HOST}:${_REGISTRY_PORT}/v1/images/${i}/ancestry" | python -mjson.tool | grep '"' | perl -p -e "s|\"||g" | perl -p -e "s|\,||g"` _ANCESTRY_OTHER_TAG=`echo $_ANCESTRY_OTHER_TAG$_TEMP_VAR` done fi for i in `echo ${_ANCESTRY}`; do _dependent_count=`find ${_IMAGES_HOME} -name "ancestry" | xargs grep -l "$i" | wc -l` if [ $_dependent_count -eq 1 ]; then #cp -pr ${_IMAGES_HOME}/$i ${_IMAGES_HOME}/123_$i rm -fr ${_IMAGES_HOME}/$i fi if [ `echo "$_ANCESTRY_OTHER_TAG" | grep -c "$i"` -eq 0 ] ; then cat ${_LIBRARY_HOME}/${_IMAGE_NAME}/_index_images | perl -p -e "s|\{\"id\"\: \"$i\"\}||g" | perl -p -e "s|\[\,|\[|g" | perl -p -e "s|\,\]|\]|g" | perl -p -e "s|, , |, |g" > ${_LIBRARY_HOME}/${_IMAGE_NAME}/_index_images.modified cp -pr ${_LIBRARY_HOME}/${_IMAGE_NAME}/_index_images ${_LIBRARY_HOME}/${_IMAGE_NAME}/_index_images.backup mv ${_LIBRARY_HOME}/${_IMAGE_NAME}/_index_images.modified ${_LIBRARY_HOME}/${_IMAGE_NAME}/_index_images rm -f ${_LIBRARY_HOME}/${_IMAGE_NAME}/tag${_IMAGE_TAG}_json rm -f ${_LIBRARY_HOME}/${_IMAGE_NAME}/tag_${_IMAGE_TAG} fi done if [ ${_IMAGE_COUNT} -eq 1 ]; then rm -fr ${_LIBRARY_HOME}/${_IMAGE_NAME} fi echo "Deleted" fi } if [ "$1" == "-q" ]; then if [ "$#" -ne 2 ]; then _help else _get_registry_images fi elif [ "$1" == "-d" ]; then if [ "$#" -lt 3 ]; then _help else export _IMAGE=$3 _delete_image fi else _help fi