forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevm_dump.rb
executable file
·74 lines (61 loc) · 1.7 KB
/
evm_dump.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
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env ruby
require File.expand_path('../config/environment', __dir__)
LOG_DIR = "./"
logfile = File.join(LOG_DIR, "evm_dump.log")
File.delete(logfile) if File.exist?(logfile)
$log = VMDBLogger.new(logfile)
$log.level = VMDBLogger.const_get("DEBUG")
yml_fnames = []
def log(level, msg)
puts "[#{Time.now.utc}] #{level.to_s.upcase}: #{msg}"
$log.send(level, msg)
end
def yml_fname(klass)
File.join(LOG_DIR, "#{klass.name.underscore}.yml")
end
def yml_dump(yml_fname, items)
File.delete(yml_fname) if File.exist?(yml_fname)
File.open(yml_fname, "w") { |fd| fd.write(YAML.dump(items)) }
end
### Main
# verify we are in the vmdb directory
unless File.exist?('app')
log(:error, "Please run this script using 'script/runner miq_queue_dump.rb' from vmdb directory")
exit 1
end
# NOTE: Models that are not needed in dump can be commented directly in list.
MODELS = [
AssignedServerRole,
Job,
MiqEnterprise,
MiqQueue,
MiqServer,
MiqTask,
MiqWorker,
ServerRole,
Zone
]
#
MODELS += ARGV.collect { |model| Object.const_get(model) }
MODELS.each do |klass|
log(:info, "Getting #{klass} objects")
items = klass.all.to_a
if items.length > 0
fname = yml_fname(klass)
yml_fnames << fname
log(:info, "Writing #{items.length} #{klass} objects to #{fname}")
yml_dump(fname, items)
else
log(:info, "Found #{items.length} #{klass} objects")
end
end
if yml_fnames.length > 0
zip_fname = File.join(LOG_DIR, "evm_dump.zip")
File.delete(zip_fname) if File.exist?(zip_fname)
cmdline = "zip #{zip_fname} #{logfile} #{yml_fnames.join(' ')}"
log(:info, "Zipping dump into #{zip_fname}")
system(cmdline)
yml_fnames.each { |fname| File.delete(fname) }
end
log(:info, "Done")
exit 0