Skip to content

Commit

Permalink
dry run for move, also exposes dry run to the plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Alix Cook committed Oct 11, 2019
1 parent 37eba1c commit 859e5b1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ requests
humanfriendly
party
pluginbase
django
4 changes: 2 additions & 2 deletions src/lavatory/commands/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ def apply_purge_policies(selected_repos, policies_path=None, dryrun=True, defaul
plugin_source = setup_pluginbase(extra_policies_path=policies_path)
LOG.info("Applying retention policies to %s", ', '.join(selected_repos))
for repository in selected_repos:
artifactory_repo = Artifactory(repo_name=repository)
artifactory_repo = Artifactory(repo_name=repository, dryrun=dryrun)
policy = get_policy(plugin_source, repository, default=default)
if not policy:
continue
LOG.info("Policy Docs: %s", inspect.getdoc(policy.purgelist))
artifacts = policy.purgelist(artifactory_repo)
purged_count = artifactory_repo.purge(dryrun, artifacts)
purged_count = artifactory_repo.purge(artifacts)
LOG.info("Processed %s, Purged %s", repository, purged_count)


Expand Down
14 changes: 9 additions & 5 deletions src/lavatory/utils/artifactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
class Artifactory:
"""Artifactory purger class."""

def __init__(self, repo_name=None):
def __init__(self, repo_name=None, dryrun=True):
self.repo_name = repo_name
self.dryrun = dryrun
self.credentials = load_credentials()
self.base_url = self.credentials['artifactory_url']
self.artifactory = party.Party()
Expand Down Expand Up @@ -58,7 +59,7 @@ def repos(self, repo_type='local'):

return repos

def purge(self, dry_run, artifacts):
def purge(self, artifacts):
""" Purge artifacts from the specified repo.
Args:
Expand All @@ -69,15 +70,15 @@ def purge(self, dry_run, artifacts):
purged (int): Count purged.
"""
purged = 0
mode = 'DRYRUN' if dry_run else 'LIVE'
mode = 'DRYRUN' if self.dryrun else 'LIVE'
LOG.info('Running mode: %s', mode)

artifacts = sorted(artifacts, key=lambda k: k['path'])
for artifact in artifacts:
artifact_path = '{}/{}/{}'.format(self.repo_name, artifact['path'], artifact['name'])
LOG.info('%s purge %s', mode, artifact_path)
full_artifact_url = '{}/{}'.format(self.base_url, artifact_path)
if dry_run:
if self.dryrun:
purged += 1
else:
try:
Expand All @@ -99,8 +100,11 @@ def move_artifacts(self, artifacts=None, dest_repository=None):
dest_prefix = "?to=/{}".format(dest_repository)
artifacts = sorted(artifacts, key=lambda k: k['path'])
for artifact in artifacts:
move_url = "{0}/{1}/{2}{3}/{1}/{2}".format(base_endpoint, artifact['path'], artifact['name'], dest_prefix)
LOG.info("Moving %s to repository %s", artifact['name'], dest_repository)
move_url = "{0}/{1}/{2}{3}/{1}/{2}".format(base_endpoint, artifact['path'], artifact['name'], dest_prefix)
if self.dryrun:
LOG.info("DRYRUN: would have made request to %s" % move_url)
continue
request = self.artifactory.post(move_url)
if not request.ok:
LOG.warning("error moving artifact %s: %s", artifact['name'], request.text)
Expand Down

0 comments on commit 859e5b1

Please sign in to comment.