Skip to content

Commit

Permalink
Merge pull request #14176 from kbrock/archived_script
Browse files Browse the repository at this point in the history
purge archived only brings back archived records
  • Loading branch information
Fryguy committed Mar 7, 2017
2 parents 425a16f + f2a05b1 commit 745a3d9
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions tools/purge_archived_vms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,32 @@
$log = VMDBLogger.new(STDOUT)
$log.level = Logger::INFO

archived, not_archived = 0, 0
query = Vm.where("updated_on < ? or updated_on IS NULL", ARCHIVE_CUTOFF)
archived = 0

$log.info "Searching for archived VMs older than #{ARCHIVE_CUTOFF} UTC."
$log.info "Expecting to prune #{query.all_archived.count} of the #{query.count} older vms"
if REPORT_ONLY
$log.info "Reporting only; no rows will be deleted."
else
$log.warn "Will delete any matching records." unless REPORT_ONLY
$log.warn "Will delete any matching records."
end

query = Vm.where(:updated_on => nil)
.or(Vm.where("updated_on < ?", ARCHIVE_CUTOFF))
.includes(:ext_management_system, :storage)

query.find_in_batches do |vms|
query.all_archived.find_in_batches do |vms|
vms.each do |vm|
begin
if vm.archived?
archived += 1
unless REPORT_ONLY
$log.info "Deleting archived VM '#{vm.name}' (id #{vm.id})"
vm.destroy
end
else
not_archived += 1
archived += 1
unless REPORT_ONLY
$log.info "Deleting archived VM '#{vm.name}' (id #{vm.id})"
vm.destroy
end
rescue => err
$log.error("#{err} #{err.backtrace.join("\n")}")
end
end
end

$log.info "Completed purging archived VMs. #{REPORT_ONLY ? 'Found' : 'Purged'} #{archived} archived VMs. There were #{not_archived} non-archived VMs."
$log.info "Completed purging archived VMs. #{REPORT_ONLY ? 'Found' : 'Purged'} #{archived} archived VMs."

$log.close
$log = old_logger

0 comments on commit 745a3d9

Please sign in to comment.