Skip to content

Commit

Permalink
Merge pull request ManageIQ#18902 from agrare/bz_1721612_purge_archiv…
Browse files Browse the repository at this point in the history
…ed_datastores

Add a tool to purge archived storages

(cherry picked from commit fb347be)
  • Loading branch information
gtanzillo authored and agrare committed Jun 25, 2019
1 parent 58f5bbc commit dee7083
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tools/purge_archived_storages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env ruby
require File.expand_path('../config/environment', __dir__)
require "trollop"

opts = Trollop.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 "**** This is a dry run and will not modify the database"
puts " To actually purge the storages pass --no-dry-run"
else
puts "**** This will modify your database ****"
puts " Press Enter to Continue: "
STDIN.getc
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 dee7083

Please sign in to comment.