Skip to content
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

Add LRU cleanup for generic repositories #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ policies:
regex_pattern: "\d"
```

- `DeleteLeastRecentlyUsedFiles` - delete the least recently used files until the total kept size is greater than passed value

```yaml
- rule: DeleteLeastRecentlyUsedFiles
keep_at_most_mb: 512
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should combine LRU and sized rule here.
Almost all rules right now works on "number of files" idea - so for LRU it'd be something like

- rule: DeleteLeastRecentlyUsedFiles
  keep: 10

where 10 - number of artifacts.

Imagine the case when you setup 512MB limit with regular size for each artifact for 10Mb.
But accidentally, you put some large zip/deb/msi in the repo, like 640Mb.
The rule with Mb will remove all other artifacts, but will keep only the last one.

```

## Keep

- `KeepLatestNFiles` - Leaves the last (by creation time) files in the amount of N pieces. WITHOUT accounting
Expand Down Expand Up @@ -562,4 +569,3 @@ In order to provide a new release of `artifactory-cleanup`, there are two steps
1. Bump the version in the [setup.py](setup.py)
2. Bump the version in the [__init__.py](./artifactory_cleanup/__init__.py)
3. Create a Git release tag (in format `1.0.1`) by creating a release on GitHub

34 changes: 29 additions & 5 deletions artifactory_cleanup/rules/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,36 @@ class DeleteByRegexpName(Rule):
def __init__(self, regex_pattern):
self.regex_pattern = rf"{regex_pattern}"

def aql_add_filter(self, filters):
print("Here's filters that we get\n", filters)
return filters

def filter(self, artifacts: ArtifactsList) -> ArtifactsList:
for artifact in artifacts[:]:
if re.match(self.regex_pattern, artifact["name"]) is None:
artifacts.remove(artifact)
return artifacts
return artifacts


class DeleteLeastRecentlyUsedFiles(Rule):
"""
Delete the least recently used files until the total kept size is greater than ``keep_at_most_mb`` megabytes.
Creation is interpreted as a first usage.
"""

def __init__(self, keep_at_most_mb: int):
self.keepAtMostBytes = keep_at_most_mb * 1024 * 1024

def filter(self, artifacts: ArtifactsList) -> ArtifactsList:
# List will contain fresh files at the beginning
artifacts.sort(key=utils.sort_by_usage, reverse=True)

keptSize = 0
keptArtifacts = []
for artifact in artifacts:
keptSize += artifact["size"]
if keptSize > self.keepAtMostBytes:
# No need to keep files if overflow occurs
break

keptArtifacts.append(artifact)

artifacts.keep(keptArtifacts)

return artifacts
9 changes: 8 additions & 1 deletion artifactory_cleanup/rules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from treelib import Node, Tree

from artifactory_cleanup.rules.base import ArtifactsList
from artifactory_cleanup.rules.base import ArtifactDict, ArtifactsList


def is_repository(data):
Expand Down Expand Up @@ -188,3 +188,10 @@ def to_masks(masks: Union[str, List[str]]):
return masks
else:
raise AttributeError("'masks' argument must by list of string OR string")


def sort_by_usage(artifact: ArtifactDict) -> str:
try:
return artifact["stats"]["downloaded"]
except:
return artifact["created"]