Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kamal2222ahmed/c3d65dfac3f2bc26183e7f5e4caa51cd to your computer and use it in GitHub Desktop.
Save kamal2222ahmed/c3d65dfac3f2bc26183e7f5e4caa51cd to your computer and use it in GitHub Desktop.
Clean up nexus artifacts with API
import groovyx.net.http.*;
import static groovyx.net.http.ContentType.*;
import static groovyx.net.http.Method.*;
class NexusArtifactCleanup {
/**
* Settings in which to run script.
*/
def settings = [
baseUrl: 'http://localhost:8083/nexus',
repositoryId: 'snapshots',
pattern: '.*SNAPSHOT',
age: 0,
nexusUsername: 'admin',
nexusPassword: 'admin'
];
/**
* MAIN Method.
*/
def static main( def args ) {
NexusArtifactCleanup cleanup = new NexusArtifactCleanup();
cleanup.cleanupArtifacts();
}
/**
* Controller method. Has 3 steps.
* 1) Find Artifacts to delete.
* 2) Delete Artifacts.
* 3) Rebuild Nexus Repository Metadata.
*/
def cleanupArtifacts() {
def nexusRepositoriesServiceUrl = settings.baseUrl + '/service/local/repositories/' + settings.repositoryId + '/content/';
def nexusMetadataServiceUrl = settings.baseUrl + '/service/local/metadata/repositories/' + settings.repositoryId + '/content/';
// Find all repository items that match the regular expression pattern defined in settings.
def urls = findArtifacts(nexusRepositoriesServiceUrl, settings.pattern, settings.age);
// The number of artifacts to be deleted.
def size = urls.size();
// Delete each artifact via the Nexus Rest API.
urls.each {
println "DELETING... " + it;
deleteContent(it, settings.nexusUsername, settings.nexusPassword);
}
// Rebuild Nexus repository metadata, if we have deleted artifacts.
if (size > 0) {
println "REBUILDING REPOSITORY METADATA... ";
rebuildRepoMetadata(nexusMetadataServiceUrl, settings.nexusUsername, settings.nexusPassword);
}
}
/**
* Finds artifacts that match the inputted regex pattern, and meet the age requirement.
*/
def findArtifacts ( String url, String pattern, int age ) {
def artifactUrls = [];
def xml = fetchContent(url);
xml.data.'content-item'.each {
def text = it.text.text();
def resourceURI = it.resourceURI.text();
def isLeaf = it.leaf.text();
if (text ==~ pattern) {
def lastModifiedDate = new Date().parse('yyyy-MM-dd HH:mm:ss.SSS z', it.lastModified.text());
if ((new Date() - age) > lastModifiedDate) {
artifactUrls << resourceURI;
}
}
if (isLeaf == 'false') {
artifactUrls += findArtifacts(resourceURI, pattern, age);
}
}
return artifactUrls;
}
/**
* Queries Nexus Repository.
*/
def fetchContent ( String url ) {
RESTClient rc = new RESTClient(url);
def response = rc.get(contentType: XML);
return response.data;
}
/**
* Deletes Nexus Artifact.
*/
def deleteContent (String url, String username, String password ) {
RESTClient rc = new RESTClient(url);
rc.auth.basic(username, password);
def response = rc.delete(contentType: ANY);
}
/**
* Rebuilds Nexus Metadata.
*/
def rebuildRepoMetadata( String url, String username, String password ) {
RESTClient rc = new RESTClient(url);
rc.auth.basic(username, password);
def response = rc.delete(contentType: ANY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment