forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ManageIQ#18902 from agrare/bz_1721612_purge_archiv…
…ed_datastores Add a tool to purge archived storages (cherry picked from commit fb347be)
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
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
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) |