Last active
August 29, 2015 14:05
-
-
Save cfitz/4fefcfa659ef18bfbe6c to your computer and use it in GitHub Desktop.
Delete ASPACE repository
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
#!/usr/bin/env ruby | |
require 'json' | |
require 'rest_client' | |
require "highline/import" | |
# point this to your AS backend | |
BACKEND_URL = "http://localhost:8089" | |
TOKEN = { "X-ArchivesSpace-Session" => JSON.parse( RestClient.post "#{BACKEND_URL}/users/admin/login", { password: 'admin'} )["session"] } | |
def delete_resource(resource) | |
puts "- deleting #{resource["uri"]}" | |
RestClient.delete("#{BACKEND_URL}/#{resource["uri"]}", TOKEN) | |
end | |
def delete_repo(repo) | |
%w{ accessions archival_objects classification_terms classifications digital_object_components digital_objects events resources }.each do |klass| | |
# iterate thru list of resources in the repo | |
puts "#{BACKEND_URL}/#{repo}/#{klass}?all_ids=true" | |
puts "#" * 30 | |
puts "deleting #{klass}" | |
JSON(RestClient.get( "#{BACKEND_URL}/#{repo}/#{klass}?all_ids=true", TOKEN )).each do |resource_id| | |
# get the resource | |
resource = JSON(RestClient.get( "#{BACKEND_URL}/#{repo}/#{klass}/#{resource_id}", TOKEN ) ) | |
if resource.has_key?("children") | |
resource["children"].each { |child| delete_resource(child) } | |
end | |
begin | |
delete_resource(resource) | |
rescue RestClient::Conflict | |
puts "#{resource["uri"]} cannot be delete because of a parent. you >>MIGHT<< need to rerun the script again if the repository won't delete. sorry " | |
next | |
end | |
end | |
end | |
puts "Attempting to delete #{repo}" | |
puts "\n\n" | |
RestClient.delete("#{BACKEND_URL}/#{repo}", TOKEN) | |
end | |
# get a list of repos | |
repos = JSON.parse( RestClient.get "#{BACKEND_URL}/repositories" ).collect { |repo| repo["uri"] } | |
puts "#" * 100 | |
puts "#" | |
puts "#\t Repositories available to delete: " | |
puts "#" | |
puts "#\t #{ repos.join(' , ')}" | |
puts "#\n#\n#\n" | |
puts "#" * 100 | |
repo = ask "Select repository to delete ( you have to enter it exactly .. ) : " | |
repo.chomp! | |
exit if repo.nil? | |
if repos.include?(repo) | |
puts "\n\n\n" | |
puts "!" * 100 | |
puts "!\t DANGER " | |
puts "!" | |
puts "!\t YOU ARE ABOUT TO DELETE #{repo}" | |
puts "!" | |
puts "!" * 100 | |
puts "\n\n" | |
input = ask "PLEASE RE-ENTER THE REPOSITORY NAME TO VERIFY THIS IS WHAT YOU WANT TO DO : " | |
unless input.chomp == repo | |
puts "#{ input.chomp} not found in available repos" | |
exit | |
end | |
delete_repo(repo) | |
else | |
puts "#{repo} not found in available repos." | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment