Skip to content

Commit

Permalink
Merge pull request #1 from tt-gf/feature/cleanup_plugin_fix_for_reten…
Browse files Browse the repository at this point in the history
…tion

added possibility to delete artifact with specified name pattern
  • Loading branch information
wotd authored Apr 6, 2018
2 parents c193576 + f5b225d commit e632b56
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
53 changes: 37 additions & 16 deletions cleanup/artifactCleanup/artifactCleanup.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* limitations under the License.
*/

import java.util.regex.Matcher
import java.util.regex.Pattern

import org.apache.commons.lang3.StringUtils
import org.artifactory.api.repo.exception.ItemNotFoundRuntimeException

Expand All @@ -30,7 +33,7 @@ class Global {
}

// curl command example for running this plugin (Prior Artifactory 5.x, use pipe '|' and not semi-colons ';' for parameters separation).
// curl -i -uadmin:password -X POST "http://localhost:8081/artifactory/api/plugins/execute/cleanup?params=months=1;repos=libs-release-local;dryRun=true;paceTimeMS=2000;disablePropertiesSupport=true"
// curl -i -uadmin:password -X POST "http://localhost:8081/artifactory/api/plugins/execute/cleanup?params=months=1;repos=libs-release-local;dryRun=true;paceTimeMS=2000;disablePropertiesSupport=true;keepRelease=true"
//
// For a HA cluster, the following commands have to be directed at the instance running the script. Therefore it is best to invoke
// the script directly on an instance so the below commands can operate on same instance
Expand All @@ -39,16 +42,20 @@ class Global {
// curl -i -uadmin:password -X POST "http://localhost:8081/artifactory/api/plugins/execute/cleanupCtl?params=command=stop"
// curl -i -uadmin:password -X POST "http://localhost:8081/artifactory/api/plugins/execute/cleanupCtl?params=command=adjustPaceTimeMS;value=-1000"


def pluginGroup = 'cleaners'
def config = new ConfigSlurper().parse(new File(ctx.artifactoryHome.haAwareEtcDir, PROPERTIES_FILE_PATH).toURL())

executions {
cleanup(groups: [pluginGroup]) { params ->
def months = params['months'] ? params['months'][0] as int : 6
def repos = params['repos'] as String[]
def dryRun = params['dryRun'] ? params['dryRun'][0] as boolean : false
def disablePropertiesSupport = params['disablePropertiesSupport'] ? params['disablePropertiesSupport'][0] as boolean : false
def dryRun = params['dryRun'] ? params['dryRun'][0].toBoolean() : false
def disablePropertiesSupport = params['disablePropertiesSupport'] ? params['disablePropertiesSupport'][0].toBoolean() : false
Global.paceTimeMS = params['paceTimeMS'] ? params['paceTimeMS'][0] as int : 0
artifactCleanup(months, repos, log, Global.paceTimeMS, dryRun, disablePropertiesSupport)
def keepRelease = params['keepRelease'] ? params['keepRelease'][0].toBoolean() : false
def releaseRegex = config.policies[0][7] ? config.policies[0][7] as Pattern : ~/.*-\d+\.\d+\.\d+\.*/
artifactCleanup(months, repos, log, Global.paceTimeMS, dryRun, disablePropertiesSupport, keepRelease, releaseRegex)
}

cleanupCtl(groups: [pluginGroup]) { params ->
Expand Down Expand Up @@ -78,27 +85,29 @@ executions {
}
}

def config = new ConfigSlurper().parse(new File(ctx.artifactoryHome.haAwareEtcDir, PROPERTIES_FILE_PATH).toURL())
log.info "Schedule job policy list: $config.policies"

config.policies.each{ policySettings ->
def cron = policySettings[ 0 ] ? policySettings[ 0 ] as String : ["0 0 5 ? * 1"]
def repos = policySettings[ 1 ] ? policySettings[ 1 ] as String[] : ["__none__"]
def months = policySettings[ 2 ] ? policySettings[ 2 ] as int : 6
def paceTimeMS = policySettings[ 3 ] ? policySettings[ 3 ] as int : 0
def dryRun = policySettings[ 4 ] ? policySettings[ 4 ] as Boolean : false
def disablePropertiesSupport = policySettings[ 5 ] ? policySettings[ 5 ] as Boolean : false
def keepRelease = policySettings[ 6 ] ? policySettings[ 6 ] as Boolean : false
def releaseRegex = policySettings[ 7 ] ? policySettings[ 7 ] as Pattern : ~/.*-\d+\.\d+\.\d+\.*/

log.info "Schedule job policy list: $config.policies"
log.info "Schedule regex: $releaseRegex"

jobs {
"scheduledCleanup_$cron"(cron: cron) {
log.info "Policy settings for scheduled run at($cron): repo list($repos), months($months), paceTimeMS($paceTimeMS) dryrun($dryRun) disablePropertiesSupport($disablePropertiesSupport)"
artifactCleanup( months, repos, log, paceTimeMS, dryRun, disablePropertiesSupport )
log.info "Policy settings for scheduled run at($cron): repo list($repos), months($months), paceTimeMS($paceTimeMS) dryrun($dryRun) disablePropertiesSupport($disablePropertiesSupport) keepRelease($keepRelease), releaseRegex($releaseRegex)"
artifactCleanup( months, repos, log, paceTimeMS, dryRun, disablePropertiesSupport, keepRelease, releaseRegex )
}
}
}

private def artifactCleanup(int months, String[] repos, log, paceTimeMS, dryRun = false, disablePropertiesSupport = false) {
log.info "Starting artifact cleanup for repositories $repos, until $months months ago with pacing interval $paceTimeMS ms, dryrun: $dryRun, disablePropertiesSupport: $disablePropertiesSupport"
private def artifactCleanup(int months, String[] repos, log, paceTimeMS, dryRun = false, disablePropertiesSupport = false, keepRelease = false, releaseRegex = ~/.*-\d+\.\d+\.\d+\.*/) {
log.info "Starting artifact cleanup for repositories $repos, until $months months ago with pacing interval $paceTimeMS ms, dryrun: $dryRun, disablePropertiesSupport: $disablePropertiesSupport, keepRelease: $keepRelease, releaseRegex: $releaseRegex"

// Create Map(repo, paths) of skiped paths (or others properties supported in future ...)
def skip = [:]
Expand Down Expand Up @@ -141,17 +150,25 @@ private def artifactCleanup(int months, String[] repos, log, paceTimeMS, dryRun
cntNoDeletePermissions++
}
if (dryRun) {
log.info "Found $it, $cntFoundArtifacts/$artifactsCleanedUp.size total $bytesFound bytes"
log.info "\t==> currentUser: ${security.currentUser().getUsername()}"
log.info "\t==> canDelete: ${security.canDelete(it)}"

if (checkName(keepRelease, releaseRegex, it)) {
log.info "Found $it, $cntFoundArtifacts/$artifactsCleanedUp.size total $bytesFound bytes"
log.info "\t==> currentUser: ${security.currentUser().getUsername()}"
log.info "\t==> canDelete: ${security.canDelete(it)}"
}
else {
log.info "Found $it, $cntFoundArtifacts/$artifactsCleanedUp.size total $bytesFound bytes"
log.info "\t==> currentUser: ${security.currentUser().getUsername()}"
log.info "\t==> canDelete: ${security.canDelete(it)}"
log.info "\t==> protected by regex: ${releaseRegex}"
}
} else {
if (security.canDelete(it)) {
if (security.canDelete(it) && (checkName(keepRelease, releaseRegex, it))) {
log.info "Deleting $it, $cntFoundArtifacts/$artifactsCleanedUp.size total $bytesFound bytes"
repositories.delete it
} else {
log.info "Can't delete $it (user ${security.currentUser().getUsername()} has no delete permissions), " +
"$cntFoundArtifacts/$artifactsCleanedUp.size total $bytesFound bytes"
cntNoDeletePermissions++
}
}
} catch (ItemNotFoundRuntimeException ex) {
Expand Down Expand Up @@ -218,3 +235,7 @@ private def getSkippedPaths(String[] repos) {
log.info "Elapsed time to retrieve paths to skip: " + duration
return skip
}

private def checkName(keepRelease, releaseRegex, artifactName) {
return !keepRelease || !((artifactName =~ releaseRegex).count > 0)
}
6 changes: 3 additions & 3 deletions cleanup/artifactCleanup/artifactCleanup.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Support different policies for different repos. Parameters between '|' are optional
// Policy syntax: [ <cron time>, [ <repo list> ], months |, <pause time>, <true|false dryrun> |, <true|false disablePropertiesSupport> | }
// Example: [ "0 0 5 ? * 1", [ "libs-releases-local" ], 3, 500, true, true ],
// Policy syntax: [ <cron time>, [ <repo list> ], months |, <pause time>, <true|false dryrun> |, <true|false disablePropertiesSupport> |, <true|false> keepRelease |, releaseRegex | }
// Example: [ "0 0 5 ? * 1", [ "libs-releases-local" ], 3, 500, true, true, true, ~/.*-\d+\.\d+\.\d+\.*/ ],
policies = [
[ "0 0 5 ? * 1", [ "libs-releases-local" ], 3 ],
[ "0 0 5 ? * 1", [ "docker-local" ], 3, 500, true, true, true ],
]

0 comments on commit e632b56

Please sign in to comment.