-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Cirrus: Print images that should be pruned #3335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| FROM libpod/imgts:latest | ||
|
|
||
| RUN yum -y update && \ | ||
| yum clean all | ||
|
|
||
| COPY /contrib/imgprune/entrypoint.sh /usr/local/bin/entrypoint.sh | ||
| RUN chmod 755 /usr/local/bin/entrypoint.sh |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
|  | ||
|
|
||
| A container image for maintaining the collection of | ||
| VM images used by CI/CD on this project and several others. | ||
| Acts upon metadata maintained by the imgts container. | ||
|
|
||
| Example build (from repository root): | ||
|
|
||
| ```bash | ||
| sudo podman build -t $IMAGE_NAME -f contrib/imgprune/Dockerfile . | ||
| ``` | ||
cevich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
|
|
||
| source /usr/local/bin/lib_entrypoint.sh | ||
|
|
||
| req_env_var GCPJSON GCPNAME GCPPROJECT IMGNAMES | ||
|
|
||
| gcloud_init | ||
|
|
||
| # For safety's sake + limit nr background processes | ||
| PRUNE_LIMIT=10 | ||
| THEFUTURE=$(date --date='+1 hour' +%s) | ||
| TOO_OLD='90 days ago' | ||
| THRESHOLD=$(date --date="$TOO_OLD" +%s) | ||
| # Format Ref: https://cloud.google.com/sdk/gcloud/reference/topic/formats | ||
| FORMAT='value[quote](name,selfLink,creationTimestamp,labels)' | ||
| PROJRE="/v1/projects/$GCPPROJECT/global/" | ||
| BASE_IMAGE_RE='cloud-base' | ||
| RECENTLY=$(date --date='30 days ago' --iso-8601=date) | ||
| EXCLUDE="$IMGNAMES $IMAGE_BUILDER_CACHE_IMAGE_NAME" # whitespace separated values | ||
| # Filter Ref: https://cloud.google.com/sdk/gcloud/reference/topic/filters | ||
| FILTER="selfLink~$PROJRE AND creationTimestamp<$RECENTLY AND NOT name=($EXCLUDE)" | ||
| TODELETE=$(mktemp -p '' todelete.XXXXXX) | ||
|
|
||
| echo "Searching images for pruning candidates older than $TOO_OLD ($THRESHOLD):" | ||
| $GCLOUD compute images list --format="$FORMAT" --filter="$FILTER" | \ | ||
| while read name selfLink creationTimestamp labels | ||
| do | ||
| created_ymd=$(date --date=$creationTimestamp --iso-8601=date) | ||
| last_used=$(egrep --only-matching --max-count=1 'last-used=[[:digit:]]+' <<< $labels || true) | ||
| markmsgpfx="Marking $name (created $created_ymd) for deletion" | ||
| if [[ -z "$last_used" ]] | ||
| then # image pre-dates addition of tracking labels | ||
| echo "$markmsgpfx: Missing 'last-used' metadata, labels: '$labels'" | ||
| echo "$name" >> $TODELETE | ||
| continue | ||
| fi | ||
|
|
||
| last_used_timestamp=$(date --date=@$(cut -d= -f2 <<< $last_used || true) +%s || true) | ||
| last_used_ymd=$(date --date=@$last_used_timestamp --iso-8601=date) | ||
| if [[ -z "$last_used_timestamp" ]] || [[ "$last_used_timestamp" -ge "$THEFUTURE" ]] | ||
| then | ||
| echo "$markmsgpfx: Missing or invalid last-used timestamp: '$last_used_timestamp'" | ||
| echo "$name" >> $TODELETE | ||
| continue | ||
| fi | ||
|
|
||
| if [[ "$last_used_timestamp" -le "$THRESHOLD" ]] | ||
| then | ||
| echo "$markmsgpfx: Used over $TOO_OLD on $last_used_ymd" | ||
| echo "$name" >> $TODELETE | ||
| continue | ||
| fi | ||
|
|
||
| echo "NOT $markmsgpfx: last used on $last_used_ymd)" | ||
| done | ||
|
|
||
| echo "Pruning up to $PRUNE_LIMIT images that were marked for deletion:" | ||
| for image_name in $(tail -$PRUNE_LIMIT $TODELETE | sort --random-sort) | ||
| do | ||
| # This can take quite some time (minutes), run in parallel disconnected from terminal | ||
| echo "TODO: Would have: $GCLOUD compute images delete $image_name &" | ||
| sleep "$[1+RANDOM/1000]s" & # Simlate background operation | ||
| done | ||
|
|
||
| wait || true # Nothing to delete: No background jobs |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/bin/bash | ||
cevich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| set -e | ||
|
|
||
| RED="\e[1;36;41m" | ||
| YEL="\e[1;33;44m" | ||
| NOR="\e[0m" | ||
| SENTINEL="__unknown__" # default set in dockerfile | ||
| # Disable all input prompts | ||
| # https://cloud.google.com/sdk/docs/scripting-gcloud | ||
| GCLOUD="gcloud --quiet" | ||
|
|
||
| die() { | ||
| EXIT=$1 | ||
| PFX=$2 | ||
| shift 2 | ||
| MSG="$@" | ||
| echo -e "${RED}${PFX}:${NOR} ${YEL}$MSG${NOR}" | ||
| [[ "$EXIT" -eq "0" ]] || exit "$EXIT" | ||
| } | ||
|
|
||
| # Pass in a list of one or more envariable names; exit non-zero with | ||
| # helpful error message if any value is empty | ||
| req_env_var() { | ||
| for i; do | ||
| if [[ -z "${!i}" ]] | ||
| then | ||
| die 1 FATAL entrypoint.sh requires \$$i to be non-empty. | ||
| elif [[ "${!i}" == "$SENTINEL" ]] | ||
| then | ||
| die 2 FATAL entrypoint.sh requires \$$i to be explicitly set. | ||
| fi | ||
| done | ||
| } | ||
|
|
||
| gcloud_init() { | ||
| set +xe | ||
| TMPF=$(mktemp -p '' .$(uuidgen)XXXX) | ||
| trap "rm -f $TMPF" EXIT | ||
| echo "$GCPJSON" > $TMPF && \ | ||
| $GCLOUD auth activate-service-account --project "$GCPPROJECT" --key-file=$TMPF || \ | ||
| die 5 FATAL auth | ||
| rm -f $TMPF | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.