Skip to content

Commit

Permalink
add flag to ignore and continue if artifact not found (#131)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: allburov <allburov@gmail.com>
  • Loading branch information
Rommmmm and allburov authored Feb 19, 2024
1 parent c2fa03b commit b461468
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions artifactory_cleanup/artifactorycleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ def __init__(
policies: List[CleanupPolicy],
destroy: bool,
today: date,
ignore_not_found: bool,
):
self.session = session
self.policies = policies
self.destroy = destroy
self.ignore_not_found = ignore_not_found

self._init_policies(today)

Expand Down
9 changes: 9 additions & 0 deletions artifactory_cleanup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ class ArtifactoryCleanupCLI(cli.Application):
envname="ARTIFACTORY_CLEANUP_DESTROY",
)

_ignore_not_found = cli.Flag(
"--ignore-not-found",
help="Ignores 404 errors when deleting artifacts",
mandatory=False,
default=False,
envname="ARTIFACTORY_CLEANUP_IGNORE_NOT_FOUND",
)

_days_in_future = cli.SwitchAttr(
"--days-in-future",
help="Simulate future behaviour",
Expand Down Expand Up @@ -153,6 +161,7 @@ def main(self):
policies=policies,
destroy=self._destroy,
today=today,
ignore_not_found=self._ignore_not_found,
)

# Filter policies by name
Expand Down
20 changes: 16 additions & 4 deletions artifactory_cleanup/rules/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import date
from typing import Optional, Union, List, Dict
from urllib.parse import quote
from requests import HTTPError

import cfgv
from hurry.filesize import size
Expand Down Expand Up @@ -293,21 +294,32 @@ def filter(self, artifacts: ArtifactsList) -> ArtifactsList:
print()
return artifacts

def delete(self, artifact: ArtifactDict, destroy: bool) -> None:
def delete(self, artifact: ArtifactDict, destroy: bool, ignore_not_found: bool = False) -> None:
"""
Delete the artifact
:param artifact: artifact to remove
:param destroy: if False - just log the action, do not actually remove the artifact
:param ignore_not_found: if True - do not raise an error if the artifact is not found
"""
path = "{repo}/{name}" if artifact["path"] == "." else "{repo}/{path}/{name}"

if artifact["path"] == ".":
path = "{repo}/{name}"
else:
path = "{repo}/{path}/{name}"

artifact_path = path.format(**artifact)
artifact_path = quote(artifact_path)
artifact_size = artifact.get("size", 0) or 0

if not destroy:
print(f"DEBUG - we would delete '{artifact_path}' - {size(artifact_size)}")
return

print(f"DESTROY MODE - delete '{artifact_path} - {size(artifact_size)}'")
r = self.session.delete(artifact_path)
r.raise_for_status()
try:
r.raise_for_status()
except HTTPError as e:
if e.response.status_code == 404 and ignore_not_found:
print(f"NOT FOUND - '{artifact_path}' was not found, so not deleted.")
raise

0 comments on commit b461468

Please sign in to comment.