Created
June 19, 2013 09:41
-
-
Save greggigon/5813049 to your computer and use it in GitHub Desktop.
This simple Artifactory plugin does a job of Calculating repository size by recursively iterating over Repository items and adding sizes of the artifacts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{"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} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a great one!
Care to contribute it to https://github.com/JFrogDev/artifactory-user-plugins?