Last active
December 20, 2015 00:09
-
-
Save calam1/6039411 to your computer and use it in GitHub Desktop.
This script will delete directories from Artifactory that have artifacts that have not been downloaded in "x" amount of days. In this example it is 8 days. Tried to just delete the individual artifacts but needed the pro version to do that. We are just deleting the directories instead since that appears to work in the free version. This is more …
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
#!/bin/env ruby | |
# --- Configuration --------------------------------- | |
# Remove artifacts which were created before $age_days ago and haven't been downloaded in recent $age_days. | |
$age_days = 8 | |
# The repository to be cleaned. | |
$repo = 'lib-release-local' | |
$user = 'admin' | |
$password = 'password' | |
$host = 'localhost:8081' | |
$artifactory_root_path = '/artifactory' | |
$old_url = '' | |
# --- Configuration end ------------------------------ | |
require 'rubygems' | |
require 'net/http' | |
require 'json' | |
require 'uri' | |
def handle_unused_result(result) | |
if result != -1 | |
JSON.parse(result)["results"].each { |artifact| | |
delete_artifact(artifact["uri"]) | |
} | |
end | |
if result == -1 | |
puts "No artifacts found that need to be deleted!!!!!" | |
end | |
end | |
def delete_artifact(artifact_uri) | |
%r|.*/#{$repo}/(.*)/.*| =~ artifact_uri | |
url = "#{$artifactory_root_path}/#{$repo}/#{$1}" | |
if $old_url != url | |
puts "this directory will be deleted " + url; | |
$old_url = url | |
uri = URI.parse("http://localhost:8081") | |
# http = Net::HTTP.new($host) | |
http = Net::HTTP.new(uri.host, uri.port) | |
request = Net::HTTP::Delete.new(url) #it appears that the free version will allow you to delete directories | |
# request = Net::HTTP::Delete.new(artifact_uri) the artifact_uri is the actual artifact and this will only work on the pro version | |
request.basic_auth($user, $password) | |
response = http.request(request) | |
puts "#{response.code} #{response.body}\n" | |
end | |
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 | |
uri = URI.parse("http://localhost:8081") | |
# http = Net::HTTP.new($host) | |
http = Net::HTTP.new(uri.host, uri.port) | |
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! | |
return -1 | |
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_unused_result(retrieve_unused_artifacts()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this can be simplified by using the rest_client gem, which requires ruby 1.9.3(I think, at least 1.9 minimally)