|
<?php |
|
/** |
|
* |
|
* Find all the biggest 📦 Dockers with most tags in Artifactory |
|
* @author: Ido Green |
|
* @date: Aug 2019 |
|
* @see: https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API#ArtifactoryRESTAPI-aqlArtifactoryQueryLanguage(AQL) |
|
*/ |
|
|
|
// Make sure to change these values |
|
$outputFile = "allBiggestDockers.csv"; |
|
|
|
// Please change: user/pass and the url to your artifactory |
|
$userPass = "-uUSER_NAME:YOUR_PASSWORD"; |
|
$artifactoryUrl = "YOUR_ARTIFACTORY_URL"; |
|
|
|
echo "🏃🏼♀️ Starting..."; |
|
// |
|
// You also needs this file: aql-2.txt as it contains the query for the largest docker images sorting by size |
|
// Something like this query should work: |
|
// items.find( |
|
// { |
|
// "type":"file", |
|
// "created_by":"jenkins", |
|
// "size":{"$gt":"1000"} |
|
// }) |
|
// .sort({"$desc":["size","name"]}) |
|
// .limit(50) |
|
// |
|
|
|
// Search for the biggest files that our jenkins built. You can change it and tune the size/type of the files. |
|
$curlCmd = "curl {$userPass} -X POST http://{$artifactoryUrl}/artifactory/api/search/aql -d @aql-2.txt -H \"content-type: text/plain\""; |
|
|
|
$allDockerStats = array(); |
|
exec($curlCmd, $allDockerStats, $retStatsVal); |
|
$allDockerStr = implode(" ", $allDockerStats); |
|
$allDockerJson = json_decode($allDockerStr); |
|
|
|
$outputStr = "repo, path, name, size, type, tags\n"; |
|
echo $outputStr; |
|
echo "==================================\n"; |
|
|
|
// Let's go over all the big dockers and see how many tags we have per image |
|
foreach ($allDockerJson->results as $key => $value) { |
|
$mbSize = round($value->size / 1024); |
|
$outputStr .= "$key, {$value->repo} , {$value->path} , {$value->name} , {$mbSize} mb , {$value->type} , "; |
|
$appName = preg_replace('/[0-9]+/', '', $value->path); |
|
$appName = str_replace("/", "", $appName); |
|
|
|
// Get all the tags per images |
|
$curlCmdForTags = "curl {$userPass} http://{$artifactoryUrl}/artifactory/api/docker/{$value->repo}/v2/{$appName}/tags/list"; |
|
exec($curlCmdForTags, $allTags, $retVal); |
|
$tagString = $allTags[2]; |
|
$tagString = str_replace('"tags" : ', "", $tagString); |
|
$tagsArray = explode(',' , $tagString); |
|
|
|
// Add that numbner of tags to our image's details |
|
$outputStr .= count($tagsArray) . "\n"; |
|
} |
|
|
|
// write the results to a CSV file so it will be easy to share it with G-sheet/Excel |
|
file_put_contents($outputFile, $outputStr); |
|
echo "😎 Done. \nPlease check $outputFile and see the results.\n"; |