-
Notifications
You must be signed in to change notification settings - Fork 2
/
purge_orphaned.rb
38 lines (32 loc) · 1.04 KB
/
purge_orphaned.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
# Delete any records older than this:
ORPHANED_CUTTOFF = Time.now.utc - 1.month
# If true, do not delete anything; only report:
REPORT_ONLY = true
old_logger = $log
$log = VMDBLogger.new(STDOUT)
$log.level = Logger::INFO
query = Vm.where("updated_on < ? or updated_on IS NULL", ORPHANED_CUTTOFF)
orphaned = 0
$log.info "Searching for Orphaned VMs older than #{ORPHANED_CUTTOFF} UTC."
$log.info "Expecting to prune #{query.all_orphaned.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.all_orphaned.find_in_batches do |vms|
vms.each do |vm|
begin
orphaned += 1
unless REPORT_ONLY
$log.info "Deleting Orphaned 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 Orphaned VMs. #{REPORT_ONLY ? 'Found' : 'Purged'} #{orphaned} Orphaned VMs."
$log.close
$log = old_logger