-
Notifications
You must be signed in to change notification settings - Fork 170
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
Prune update #839
Merged
Merged
Prune update #839
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
@@ -1,78 +1,114 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
# This is just a thin wrapper around prune_builds. That way we still get the | ||
# preflight checks to make sure the workdir looks sane. | ||
|
||
dn=$(dirname "$0") | ||
# shellcheck source=src/cmdlib.sh | ||
. "${dn}"/cmdlib.sh | ||
|
||
print_help() { | ||
cat 1>&2 <<'EOF' | ||
Usage: coreos-assembler prune --help | ||
coreos-assembler prune [--keep=N] [--keep-last-days=N] | ||
|
||
Delete older untagged build artifacts. By default, only the last 3 untagged | ||
builds are kept. This can be overridden with the `--keep` option. | ||
EOF | ||
} | ||
|
||
# Parse options | ||
KEEP_LAST_N= | ||
KEEP_LAST_DAYS= | ||
rc=0 | ||
options=$(getopt --options h --longoptions help,keep:,keep-last-days: -- "$@") || rc=$? | ||
[ $rc -eq 0 ] || { | ||
print_help | ||
exit 1 | ||
} | ||
eval set -- "$options" | ||
while true; do | ||
case "$1" in | ||
-h | --help) | ||
print_help | ||
exit 0 | ||
;; | ||
--keep) | ||
shift | ||
KEEP_LAST_N="$1" | ||
;; | ||
--keep-last-days) | ||
shift | ||
KEEP_LAST_DAYS="$1" | ||
;; | ||
--) | ||
shift | ||
break | ||
;; | ||
*) | ||
fatal "$0: unrecognized option: $1" | ||
exit 1 | ||
;; | ||
esac | ||
shift | ||
done | ||
|
||
if [ $# -ne 0 ]; then | ||
print_help | ||
fatal "ERROR: Too many arguments" | ||
exit 1 | ||
fi | ||
|
||
# just support one of the two for now | ||
if [ -n "${KEEP_LAST_N:-}" ] && [ -n "${KEEP_LAST_DAYS:-}" ]; then | ||
fatal "ERROR: Only one of --keep or --keep-last-days allowed" | ||
elif [ -z "${KEEP_LAST_N:-}" ] && [ -z "${KEEP_LAST_DAYS:-}" ]; then | ||
KEEP_LAST_N=3 | ||
fi | ||
|
||
if [ -n "${KEEP_LAST_DAYS:-}" ]; then | ||
set -- --keep-last-days "${KEEP_LAST_DAYS}" | ||
else | ||
set -- --keep-last-n "${KEEP_LAST_N}" | ||
fi | ||
|
||
prepare_build | ||
|
||
"${dn}"/prune_builds --workdir "${workdir:?}" "$@" | ||
#!/usr/bin/python3 -u | ||
|
||
''' | ||
This script removes previous builds. DO NOT USE on production pipelines | ||
''' | ||
|
||
import argparse | ||
import os | ||
import sys | ||
|
||
|
||
from datetime import timedelta, datetime, timezone | ||
from shutil import rmtree | ||
|
||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | ||
from cosalib.builds import Builds, get_local_builds | ||
|
||
# Let's just hardcode this here for now | ||
DEFAULT_KEEP_LAST_N = 3 | ||
DEFAULT_KEEP_LAST_DAYS = 7 | ||
|
||
|
||
parser = argparse.ArgumentParser(prog="coreos-assembler prune") | ||
parser.add_argument("--workdir", default='.', help="Path to workdir") | ||
parser.add_argument("--dry-run", help="Don't actually delete anything", | ||
action='store_true') | ||
keep_options = parser.add_mutually_exclusive_group() | ||
keep_options.add_argument("--keep-last-n", type=int, metavar="N", | ||
default=DEFAULT_KEEP_LAST_N, | ||
help="Number of untagged builds to keep (0 for all)") | ||
keep_options.add_argument("--keep-last-days", metavar="N", type=int, | ||
default=DEFAULT_KEEP_LAST_DAYS, | ||
help="Keep untagged builds within number of days") | ||
args = parser.parse_args() | ||
|
||
keep_younger_than = None | ||
if args.keep_last_days != DEFAULT_KEEP_LAST_DAYS: | ||
if args.keep_last_days <= 0: | ||
raise argparse.ArgumentTypeError("value must be positive: %d" % | ||
args.keep_last_days) | ||
keep_younger_than = (datetime.now(timezone.utc) - | ||
timedelta(days=args.keep_last_days)) | ||
|
||
skip_pruning = (not keep_younger_than and args.keep_last_n == 0) | ||
|
||
builds = Builds(args.workdir) | ||
# collect all builds being pointed to by tags | ||
tagged_builds = set([tag['target'] for tag in builds.raw().get('tags', [])]) | ||
|
||
builds_dir = os.path.join(args.workdir, "builds") | ||
scanned_builds = get_local_builds(builds_dir) | ||
|
||
# sort by timestamp, newest first | ||
scanned_builds = sorted(scanned_builds, | ||
key=lambda x: x.timestamp, | ||
reverse=True) | ||
|
||
new_builds = [] | ||
builds_to_delete = [] | ||
|
||
# Don't prune known builds | ||
if skip_pruning: | ||
new_builds = scanned_builds | ||
else: | ||
if keep_younger_than: | ||
for build in scanned_builds: | ||
if build.id in tagged_builds: | ||
print(f"Skipping tagged build {build.id}") | ||
new_builds.append(build) | ||
continue | ||
|
||
if build.timestamp < keep_younger_than: | ||
builds_to_delete.append(build) | ||
else: | ||
new_builds.append(build) | ||
else: | ||
n = args.keep_last_n | ||
assert(n > 0) | ||
for build in scanned_builds: | ||
if n == 0: | ||
builds_to_delete.append(build) | ||
else: | ||
new_builds.append(build) | ||
n = n - 1 | ||
|
||
print(f"prune: new builds: {new_builds}") | ||
|
||
if args.dry_run: | ||
print(f"prune: not removing any builds") | ||
sys.exit(0) | ||
|
||
# create a new builds list | ||
builds.raw()['builds'] = [] | ||
for build in reversed(new_builds): | ||
for arch in build.basearches: | ||
builds.insert_build(build.id, arch) | ||
|
||
builds.bump_timestamp() | ||
|
||
buildids = [x.id for x in builds_to_delete] | ||
print(f"prune: removing {' '.join(buildids)}") | ||
|
||
# now delete other build dirs not in the manifest | ||
error_during_pruning = False | ||
for build in builds_to_delete: | ||
print(f"Pruning {build}") | ||
try: | ||
rmtree(os.path.join(builds_dir, build.id)) | ||
except Exception as e: | ||
error_during_pruning = True | ||
print(f"{e}") | ||
|
||
if error_during_pruning: | ||
sys.exit(1) |
This file contains 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,55 @@ | ||
#!/usr/bin/python3 -u | ||
|
||
''' | ||
This script removes unreferenced builds from s3 bucket | ||
''' | ||
|
||
import argparse | ||
import sys | ||
|
||
from cosalib.builds import Builds | ||
from cosalib.prune import fetch_build_meta, get_unreferenced_s3_builds, delete_build | ||
|
||
parser = argparse.ArgumentParser(prog="coreos-assembler remote-prune") | ||
parser.add_argument("--workdir", default='.', help="Path to workdir") | ||
parser.add_argument("--dry-run", help="Don't actually delete anything", | ||
action='store_true') | ||
|
||
subparsers = parser.add_subparsers(dest='cmd', title='subcommands') | ||
subparsers.required = True | ||
|
||
s3 = subparsers.add_parser('s3', help='Prune s3 buckets') | ||
s3.add_argument("--bucket", help="Bucket name") | ||
s3.add_argument("--prefix", help="Key prefix") | ||
|
||
args = parser.parse_args() | ||
|
||
builds = Builds(args.workdir) | ||
|
||
scanned_builds = [] | ||
for build in builds.raw()["builds"]: | ||
for arch in build['arches']: | ||
build = fetch_build_meta(build['id'], arch) | ||
if build: | ||
scanned_builds.append(build) | ||
|
||
new_builds = [] | ||
builds_to_delete = [] | ||
|
||
# Find unreferenced builds in the bucket and remove them | ||
buildids = [x['id'] for x in scanned_builds] | ||
unreferenced_s3_builds = get_unreferenced_s3_builds(buildids, args.bucket, args.prefix) | ||
|
||
error_during_pruning = False | ||
for unmatched_build_id in unreferenced_s3_builds: | ||
# TODO: fetch arches from s3 | ||
build = fetch_build_meta(unmatched_build_id, 'x86_64') | ||
if build and not args.dry_run: | ||
try: | ||
delete_build(build) | ||
except Exception as e: | ||
error_during_pruning = True | ||
print(f"{e}") | ||
|
||
if error_during_pruning: | ||
sys.exit(1) |
This file contains 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 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmmm. I think it's a bit more nuanced than that. For RHCOS we can prune bootimages not pinned by the installer, for example.
But...this gets to an interesting topic. I've been thinking we should explicitly copy/promote bootimages we want to pin to a separate "stream". But anyways, just noting this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prune won't touch "tagged" builds - seems this field should be updated with pinned images used in installer