Skip to content

Commit

Permalink
Merge pull request #15 from tt-gf/bugfix/undefined-var
Browse files Browse the repository at this point in the history
fix: several fixes around the keepArtifactsRegex feature
  • Loading branch information
martinm82 authored Oct 1, 2021
2 parents 56c8308 + 28a9252 commit 97ff199
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
34 changes: 30 additions & 4 deletions cleanup/artifactCleanup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The following properties are supported by each policy descriptor object:
- `paceTimeMS`: The number of milliseconds to delay between delete operations. Default *0*.
- `disablePropertiesSupport`: Disable the support of Artifactory Properties (see above *Artifactory Properties support* section).
- `keepArtifacts`: Enable protection of artifacts that should not be deleted. Default *false*.
- `keepArtifactsRegex`: Regular expression for protecting artifacts to not be deleted. Default `/.*-[\.\d+]*[-+][\.\d+]*\..*/`.
- `keepArtifactsRegex`: Regular expression for protecting artifacts to not be deleted. Default `~/.*-[\.\d+]*[-+][\.\d+]*\..*/`.

An example file could contain the following json:

Expand All @@ -71,12 +71,21 @@ An example file could contain the following json:
"paceTimeMS": 500,
"disablePropertiesSupport": true,
"keepArtifacts": true,
"keepArtifactsRegex": "~/.\_-\d+\.\d+\.\d+\-\d{2}\.\d\.\*/"
"keepArtifactsRegex": "~/.*-[.\\d+]+-\\d{2}\\.[1-4][\\.\\w+]*/"
}
]
}
```

The regular expression used in the above example prevents the plugin from deleting artifacts that contain a version X.Y.Z and release number based on year and quarter (eg. 21.3).

```bash
my-artifact-1.2.0-21.3
my-artifact-1.2.0-21.3.zip
my-artifact-1.2.0-21.3+4.5.6
my-artifact-1.2.0-21.3+4.5.6.zip
```

**Note**: If a deprecated `artifactCleanup.properties` is defined it will only be applied if no `artifactCleanup.json` is present.

`artifactCleanup.properties` (DEPRECATED)
Expand All @@ -97,7 +106,7 @@ The following properties are supported by each policy descriptor object:

An example file could contain the following policy:

```
```groovy
policies = [
[ "0 0 5 ? * 1", [ "libs-release-local" ], 3, 500, true, true, true, ~/.*-\d+\.\d+\.\d+\.*/ ],
]
Expand All @@ -117,14 +126,31 @@ For Artifactory 5.x or higher:
For Artifactory 5.x or higher, using the depracted `months` parameter:
`curl -X POST -v -u admin:password "http://localhost:8080/artifactory/api/plugins/execute/cleanup?params=months=1;repos=libs-release-local;dryRun=true;paceTimeMS=2000;disablePropertiesSupport=true"`

For Artifactory 5.x or higher, using the `keepArtifactsRegex` parameter:

In order to pass a regular expression to a POST request we need to encode the expression to an URL-encoded format. You can either do this by using this
Python3 one-liner:

```bash
python3 -c "import urllib.parse, sys; print(urllib.parse.quote_plus(sys.argv[1]))" "~/.*-[\.\d+]*[-+][\.\d+]*\..*/"
```

or use [www.urlencoder.org](https://www.urlencoder.org).

Once you have encoded the regular expression you can call the cleanup script:

```bash
curl -X POST -uadmin:password "http://localhost:8080/artifactory/api/plugins/execute/cleanup?params=timeUnit=month;timeInterval=1;repos=libs-release-local;dryRun=true;paceTimeMS=2000;disablePropertiesSupport=true;keepArtifactsRegex=~%2F.%2A-%5B%5C.%5Cd%2B%5D%2A%5B-%2B%5D%5B%5C.%5Cd%2B%5D%2A%5C..%2A%2F"
```

Admin users and users inside the `cleaners` group can execute the plugin.

There is also ability to control the running script. The following operations can occur

Operation
---------

The plugin have 4 control options:
The plugin has 4 control options:

- `stop`: When detected, the loop deleting artifacts is exited and the script ends. Example:

Expand Down
19 changes: 9 additions & 10 deletions cleanup/artifactCleanup/artifactCleanup.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ executions {
cleanup(groups: [pluginGroup]) { params ->
def timeUnit = params['timeUnit'] ? params['timeUnit'][0] as String : DEFAULT_TIME_UNIT
def timeInterval = params['timeInterval'] ? params['timeInterval'][0] as int : DEFAULT_TIME_INTERVAL
def repos = params['repos'] as String[]
def repos = params['repos'] ? params['repos'] as String[] : ["__none__"] as String[]
def dryRun = params['dryRun'] ? new Boolean(params['dryRun'][0]) : false
def disablePropertiesSupport = params['disablePropertiesSupport'] ? new Boolean(params['disablePropertiesSupport'][0]) : false
def paceTimeMS = params['paceTimeMS'] ? params['paceTimeMS'][0] as int : 0
def keepArtifacts = params['keepArtifacts'] ? new Boolean(params['keepArtifacts'][0]) : false
def keepArtifactsRegex = params['keepArtifactsRegex'] ? params['keepArtifactsRegex'] as Pattern : regex
def keepArtifactsRegex = params['keepArtifactsRegex'] ? Pattern.compile(params['keepArtifactsRegex'][0]) : regex

// Enable fallback support for deprecated month parameter
if ( params['months'] && !params['timeInterval'] ) {
Expand Down Expand Up @@ -123,8 +123,8 @@ if ( deprecatedConfigFile.exists() ) {
def keepArtifactsRegex = policySettings[ 7 ] ? policySettings[ 7 ] as Pattern : regex

jobs {
"scheduledCleanup_$count"(cron: cron) {
log.info "Policy settings for scheduled run at($cron): repo list($repos), timeUnit(month), timeInterval($months), paceTimeMS($paceTimeMS), dryrun($dryRun), disablePropertiesSupport($disablePropertiesSupport), keepArtifacts($keepArtifacts), keepArtifactsRegex($keepArtifactsRegex)"
"artifactCleanupScheduled_$count"(cron: cron) {
log.info "Policy settings for scheduled run at($cron): repo list($repos), timeUnit(month), timeInterval($months), paceTimeMS($paceTimeMS), dryRun($dryRun), disablePropertiesSupport($disablePropertiesSupport), keepArtifacts($keepArtifacts), keepArtifactsRegex($keepArtifactsRegex)"
artifactCleanup( "month", months, repos, log, paceTimeMS, dryRun, disablePropertiesSupport, keepArtifacts, keepArtifactsRegex )
}
}
Expand All @@ -139,7 +139,6 @@ if ( configFile.exists() ) {

def config = new JsonSlurper().parse(configFile.toURL())
log.info "Schedule job policy list: $config.policies"
log.info "Schedule regex: $keepArtifactsRegex"

def count=1
config.policies.each{ policySettings ->
Expand All @@ -151,11 +150,11 @@ if ( configFile.exists() ) {
def dryRun = policySettings.containsKey("dryRun") ? new Boolean(policySettings.dryRun) : false
def disablePropertiesSupport = policySettings.containsKey("disablePropertiesSupport") ? new Boolean(policySettings.disablePropertiesSupport) : false
def keepArtifacts = policySettings.containsKey("keepArtifacts") ? new Boolean(policySettings.keepArtifacts) : false
def keepArtifactsRegex = policySettings.containsKey("keepArtifactsRegex") ? policySettings.keepArtifactsRegex as Pattern : regex
def keepArtifactsRegex = (policySettings.containsKey("keepArtifactsRegex") && policySettings.keepArtifactsRegex) ? Pattern.compile(policySettings.keepArtifactsRegex) : regex

jobs {
"scheduledCleanup_$count"(cron: cron) {
log.info "Policy settings for scheduled run at($cron): repo list($repos), timeUnit($timeUnit), timeInterval($timeInterval), paceTimeMS($paceTimeMS) dryrun($dryRun) disablePropertiesSupport($disablePropertiesSupport), keepArtifacts($keepArtifacts), keepArtifactsRegex($keepArtifactsRegex)"
"artifactCleanupScheduled_$count"(cron: cron) {
log.info "Policy settings for scheduled run at($cron): repo list($repos), timeUnit($timeUnit), timeInterval($timeInterval), paceTimeMS($paceTimeMS) dryRun($dryRun) disablePropertiesSupport($disablePropertiesSupport), keepArtifacts($keepArtifacts), keepArtifactsRegex($keepArtifactsRegex)"
artifactCleanup( timeUnit, timeInterval, repos, log, paceTimeMS, dryRun, disablePropertiesSupport, keepArtifacts, keepArtifactsRegex )
}
}
Expand Down Expand Up @@ -265,8 +264,8 @@ private def artifactCleanup(String timeUnit, int timeInterval, String[] repos, l
if (cntNoDeletePermissions > 0) {
log.info "$cntNoDeletePermissions artifacts could not be deleted due to lack of permissions ($bytesFoundWithNoDeletePermission bytes)"
}
log.info "and $cntNoDeleteRegexPermissions artifacts is protected by regex $keepArtifactsRegex ($bytesFoundWithRegexProtection bytes)"
log.info "After this cleanup, $cntRemovedArtifacts artifacts ($bytesRemoved bytes) was removed from $repos repositories"
log.info "and $cntNoDeleteRegexPermissions artifacts protected by regex $keepArtifactsRegex ($bytesFoundWithRegexProtection bytes)"
log.info "After this cleanup, $cntRemovedArtifacts artifacts ($bytesRemoved bytes) got removed from $repos repositories"
}
}

Expand Down
2 changes: 1 addition & 1 deletion cleanup/artifactCleanup/artifactCleanup.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"paceTimeMS": 500,
"disablePropertiesSupport": true,
"keepArtifacts": true,
"keepArtifactsRegex": "~/.\\_-\\d+\\.\\d+\\.\\d+\\-\\d{2}\\.\\d\\.\\*/"
"keepArtifactsRegex": "~/.*-[.\\d+]+-\\d{2}\\.[1-4][\\.\\w+]*/"
}]
}

0 comments on commit 97ff199

Please sign in to comment.