forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
purge_archived_vms.rb
executable file
·38 lines (30 loc) · 1.15 KB
/
purge_archived_vms.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env ruby
require File.expand_path('../config/environment', __dir__)
# Delete any records older than this:
ARCHIVE_CUTOFF = Time.now.utc - 1.month
# If true, do not delete anything; only report:
REPORT_ONLY = ActiveModel::Type::Boolean.new.cast(ENV.fetch("REPORT_ONLY", true))
$log = Vmdb::Loggers.create_logger($stdout)
$log.level = Logger::INFO
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.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.")
end
query.archived.find_in_batches do |vms|
vms.each do |vm|
archived += 1
unless REPORT_ONLY
$log.info("Deleting archived VM '#{vm.name}' (id #{vm.id})")
vm.destroy
end
rescue => err
$log.log_backtrace(err)
end
end
$log.info("Completed purging archived VMs. #{REPORT_ONLY ? 'Found' : 'Purged'} #{archived} archived VMs.")
$log.info("To cleanup archived VMs re-run with REPORT_ONLY=false #{$PROGRAM_NAME}")