Skip to content

Instantly share code, notes, and snippets.

@mlegenhausen
Created November 21, 2017 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlegenhausen/e1d3681c888e70f08771e7a7d89ee5aa to your computer and use it in GitHub Desktop.
Save mlegenhausen/e1d3681c888e70f08771e7a7d89ee5aa to your computer and use it in GitHub Desktop.
Set of command line tools to work with the google container registry (GCR)
#!/bin/bash
# Copyright © 2017 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
function gcrgc {
if [[ "$#" -ne 2 || "${1}" == '-h' || "${1}" == '--help' ]]; then
cat >&2 <<"EOF"
gcrgc cleans up tagged or untagged images pushed before specified date
for a given repository (an image name without a tag/digest).
USAGE:
gcrgc REPOSITORY DATE
EXAMPLE
gcrgc gcr.io/ahmet/my-app 2017-04-01
would clean up everything under the gcr.io/ahmet/my-app repository
pushed before 2017-04-01.
EOF
return 1
elif [[ "${#2}" -ne 10 ]]; then
echo "wrong DATE format; use YYYY-MM-DD." >&2
return 1
fi
local count=0
local image="${1}"
local date="${2}"
for digest in $(gcloud container images list-tags ${image} --limit=999999 --sort-by=TIMESTAMP \
--filter="timestamp.datetime < '${date}'" --format='get(digest)'); do
(
set -x
gcloud container images delete -q --force-delete-tags "${image}@${digest}"
)
let count=count+1
done
echo "Deleted ${count} images in ${image}." >&2
}
function gcrgcu {
if [[ "$#" -ne 1 || "${1}" == '-h' || "${1}" == '--help' ]]; then
cat >&2 <<"EOF"
gcrgcu cleans up all untagged images for a given registry.
USAGE:
gcrgcu REGISTRY
EXAMPLE
gcrgcu gcr.io/my-project-id
would remove all untagged images from the project registry my-project-id.
EOF
return 1
fi
local repository="${1}"
for image in $(gcloud container images list --repository="$repository" --limit=999999 --format='get(name)'); do
for digest in $(gcloud container images list-tags "$image" --limit=999999 --format json | jq -r '.[] | select(.tags == []) | .digest'); do
echo gcloud container images delete "$image@$digest" -q
gcloud container images delete "$image@$digest" -q
done
done
}
function gcrdu {
if [[ "$#" -ne 1 || "${1}" == '-h' || "${1}" == '--help' ]]; then
cat >&2 <<"EOF"
gcrdu returns the total size of a google cloud registry
USAGE:
gcrdu PROJECT
EXAMPLE
gcrdu my-project-id
would return the total allocated size of the gcr.
EOF
return 1
fi
local project="${1}"
gsutil du -sh "gs://eu.artifacts.${project}.appspot.com"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment