Simple Artifactory plugin for total size of Artefacts in local repositories

I’ve done a little plugin I hope might be useful for others who would like to know what is the total size of artefacts stored by Artifactory in local repository.

The functionality is quite simple:

  1. You can obtain a size of a repository by sending a POST request to url: /artifactory/api/plugins/execute/getRepositorySize?params=repoName=repository-name
  2. You can obtain a report in a form of a JSON Array for all local repositories by sending a POST request to url: /artifactory/api/plugins/execute/getAllRepositorySizes

Plugin code and a sample JSON response for all repository sizes below.

Just want to add that JFrog did a brilliant work with their very simple and elegant Artifactory API.


import org.artifactory.repo.RepoPathFactory
def getDirectorySizeRecursively(def repoPath){
def children = repositories.getChildren(repoPath)
if (children.size() == 0) return 0
def total = 0
children.each { item ->
if (item.folder) {
total += getDirectorySizeRecursively(item.repoPath)
} else {
total += item.size
}
}
total
}
executions {
/**
* This method expects parameter repoName to be passed. It will than calculate the TOTAL size of repository and return the value.
* If the repo name is invalid or not provided it will fail.
* Sample invokation:
* http://localhost:8081/artifactory/api/plugins/execute/getRepositorySize?params=repoName=ext-release-local
*/
getRepositorySize() { params ->
if (params.size() > 0 && params['repoName'] && params['repoName'].size() > 0){
def repoName = params['repoName'][0]
if (repositories.localRepositories.contains(repoName)){
message = getDirectorySizeRecursively(RepoPathFactory.create(repoName, '/'))
status = 200
} else {
message = "Repository $repoName doesn't exists"
status = 404
}
} else {
message = 'Missing repository name parameter repoName={name}'
status = 400
}
}
/**
* Prepare report on all repositories
* Sample invokation:
* http://localhost:8081/artifactory/api/plugins/execute/getAllRepositorySizes
*/
getAllRepositorySizes() { params ->
def repositoryReport = "[\n"
def totalNumberOfRepositories = repositories.localRepositories.size()
repositories.localRepositories.eachWithIndex { repoName, index ->
def repoPath = RepoPathFactory.create(repoName, '/')
def size = getDirectorySizeRecursively(repoPath)
repositoryReport+="{\"repoName\":\"$repoName\", \"sizeInBytes\": $size}"
if (index < totalNumberOfRepositories -1) {
repositoryReport+=",\n"
}
}
repositoryReport+="\n]"
message = repositoryReport
status = 200
}
}


[
{"repoName":"libs-release-local", "sizeInBytes": 122233},
{"repoName":"libs-snapshot-local", "sizeInBytes": 0},
{"repoName":"plugins-release-local", "sizeInBytes": 0},
{"repoName":"plugins-snapshot-local", "sizeInBytes": 0},
{"repoName":"ext-release-local", "sizeInBytes": 122233},
{"repoName":"ext-snapshot-local", "sizeInBytes": 0}
]

To install plugin just copy the RepositorySize.groovy file to ARTIFACTORY_HOME/etc/plugins folder.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s