Skip to content

Commit

Permalink
Add a tool to purge archived storages
Browse files Browse the repository at this point in the history
Storages that aren't connected to any hosts aren't automatically deleted
and thus a tool is needed to clean them up.

This looks for storages which aren't represented in the host_storages
table which means they aren't a part of any EMS and can be removed.
  • Loading branch information
agrare committed Jun 20, 2019
1 parent dbd6f04 commit aa4cf9d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tools/purge_archived_storages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env ruby
require File.expand_path('../config/environment', __dir__)
require "optimist"

opts = Optimist.options do
opt :dry_run, "Just print out what would be done without modifying anything", :type => :boolean, :default => true
end

if opts[:dry_run]
puts "\n"
puts "* This is a dry run and will not modify the database"
puts "* To actually delete archived datastores pass --no-dry-run\n\n"
end

active_storage_ids = HostStorage.pluck(:storage_id).uniq
archived_storages = Storage.where.not(:id => active_storage_ids).pluck(:id, :name)

if archived_storages.empty?
puts "No archived storages found"
else
puts "Deleting the following storages:"
puts archived_storages.map { |id, name| "ID [#{id}] Name [#{name}]" }.join("\n")
end

return if opts[:dry_run]

archived_storage_ids = archived_storages.map(&:first)
Storage.destroy(archived_storage_ids)

0 comments on commit aa4cf9d

Please sign in to comment.