Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Clean up unused (long time no download) artifacts from Artifactory repository. See: http://aleung.github.com/blog/2013/03/22/clean-aged-artifacts-from-artifactory/

View clean_aged_artifacts.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#!/bin/env ruby
 
# --- Configuration ---------------------------------
 
# Remove artifacts which were created before $age_days ago and haven't been downloaded in recent $age_days.
$age_days = 730
 
# The repository to be cleaned.
$repo = 'repository-key'
 
$user = 'user'
$password = 'password'
 
$host = 'mavenrepo.mycompany.com'
$artifactory_root_path = '/repo'
 
# --- Configuration end ------------------------------
 
 
require 'rubygems'
require 'net/http'
require 'json'
 
def handle_usused_result(result)
JSON.parse(result)["results"].each { |artifact|
delete_artifact(artifact["uri"])
}
end
 
def delete_artifact(artifact_uri)
puts "Delete artifact #{artifact_uri}"
%r|.*/#{$repo}/(.*)/.*| =~ artifact_uri
url = "#{$artifactory_root_path}/#{$repo}/#{$1}"
 
http = Net::HTTP.new($host)
request = Net::HTTP::Delete.new(url)
request.basic_auth($user, $password)
response = http.request(request)
puts "#{response.code} #{response.body}\n"
end
 
def retrieve_unused_artifacts()
since = (Time.now - $age_days * 3600*24).to_i * 1000
url = "#{$artifactory_root_path}/api/search/usage?notUsedSince=#{since}&createdBefore=#{since}&repos=#{$repo}"
puts url
 
http = Net::HTTP.new($host)
http.read_timeout = 500
request = Net::HTTP::Get.new(url)
request.basic_auth($user, $password)
response = http.request(request)
 
if response.code != "200"
puts "#{response.code} #{response.body}"
exit!
end
 
save_file(response.body)
return response.body
end
 
def save_file(result)
File.open('unused_artifactorys.txt', 'w') { |f| f.puts(result) }
end
 
def load_file()
File.open('unused_artifactorys.txt') { |f| f.gets }
end
 
handle_usused_result(retrieve_unused_artifacts())
 
# handle_usused_result(load_file())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.