forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fix_disk_sizes.rb
executable file
·64 lines (52 loc) · 1.96 KB
/
fix_disk_sizes.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env ruby
require File.expand_path('../config/environment', __dir__)
def getDinfo(vim)
dinfo = []
vim.virtualMachinesByMor.each do |_k, v|
v['config']['hardware']['device'].each do |dev|
next if dev.xsiType != "VirtualDisk"
dinfo << {
:fileName => dev['backing']['fileName'],
:capacityInKB => dev['capacityInKB'].to_i,
:diskMode => dev['backing']['diskMode'],
:thinProvisioned => dev['backing']['thinProvisioned']
}
end
end
dinfo
end
log_header = "MIQ(#{__FILE__})"
$log.info("#{log_header} Correcting Disk Sizes...")
disks_by_filename = Disk.all.index_by do |d|
d.filename
end
changed_disks = {}
ExtManagementSystem.all.each do |e|
$log.info("#{log_header} Correcting Disk Sizes for disks under ExtManagementSystem name: [#{e.name}], id: [#{e.id}]...")
begin
vim = e.connect
dinfo = getDinfo(vim)
rescue => err
$log.error("#{log_header} Error during Correcting Disk Sizes for disks under ExtManagementSystem name: [#{e.name}], id: [#{e.id}]...Skipping")
$log.log_backtrace(err)
next
ensure
vim.disconnect if vim rescue nil
end
dinfo.each do |di|
d = disks_by_filename[di[:fileName]]
next if d.nil?
data = {
:new => {:size => di[:capacityInKB].kilobytes, :disk_type => di[:thinProvisioned] == 'true' ? 'thin' : 'thick', :mode => di[:diskMode]},
:old => {:size => d.size, :disk_type => d.disk_type, :mode => d.mode}
}
next unless data[:new] != data[:old]
# Only nil out 'size_on_disk' if the provision size does not match
data[:new][:size_on_disk] = nil if data[:new][:size] != data[:old][:size]
changed_disks[d.id] = data
d.update(data[:new])
end
$log.info("#{log_header} Collecting Disk Sizes for disks under ExtManagementSystem name: [#{e.name}], id: [#{e.id}]...Complete")
end
$log.info("#{log_header} Changed disks: #{changed_disks.inspect}")
$log.info("#{log_header} Correcting Disk Sizes...Complete")