From f373528954ae7695d32778368cb253ba7a35d4c6 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Thu, 20 Jun 2019 15:21:16 -0400 Subject: [PATCH 1/2] Add a tool to purge archived storages 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. Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1721612 --- tools/purge_archived_storages.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 tools/purge_archived_storages.rb diff --git a/tools/purge_archived_storages.rb b/tools/purge_archived_storages.rb new file mode 100755 index 00000000000..5111e8e1e23 --- /dev/null +++ b/tools/purge_archived_storages.rb @@ -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) From 8e440be4a0084b9ca4cdb8e44070657ba1334110 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Thu, 20 Jun 2019 15:39:31 -0400 Subject: [PATCH 2/2] Add an explicit "Press Enter to Continue" --- tools/purge_archived_storages.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tools/purge_archived_storages.rb b/tools/purge_archived_storages.rb index 5111e8e1e23..0b0303ee4b5 100755 --- a/tools/purge_archived_storages.rb +++ b/tools/purge_archived_storages.rb @@ -7,9 +7,12 @@ 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" + 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