-
Notifications
You must be signed in to change notification settings - Fork 47
/
rhconsulting_scanitems.rake
129 lines (107 loc) · 3.52 KB
/
rhconsulting_scanitems.rake
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Author: A Liu Ly <[email protected]>
require_relative 'rhconsulting_illegal_chars'
require_relative 'rhconsulting_options'
class MiqScanItemsImportExport
class ParsedNonDialogYamlError < StandardError; end
def export(export_dir, options = {})
raise "Must supply export dir" if export_dir.blank?
# Export the Scan Items
export_scan_items(export_dir, options)
end
def import(import_dir)
raise "Must supply import dir" if import_dir.blank?
# Import the Scan Items
import_scan_items(import_dir)
end
private
def export_scan_items(export_dir, options)
ScanItemSet.all.each do |p|
# Skip read only entries
if p.read_only
next
end
# Also skip any entries that are using file defaults...
uses_files = false
p.members.each do |m|
if m.filename
uses_files = true
end
end
if uses_files
next
end
puts("Exporting Scan Profile: #{p.name} (#{p.description})")
# Replace invalid filename characters
pname = MiqIllegalChars.replace(p.name, options)
fname = "ScanProfile_#{pname}.yaml"
# Clean-up data
profile = ScanItem.get_profile(p.name).first.dup
%w(id created_on updated_on).each { |k| profile.delete(k) }
profile['definition'].each do |dd|
%w(id created_on updated_on description).each { |k| dd.delete(k) }
end
s = profile.to_yaml
s.gsub!(/\n\s*guid:\s+\S+\n/,"\n")
File.write("#{export_dir}/#{fname}", s)
end
end
def import_scan_items(import_dir)
Dir.glob("#{import_dir}/ScanProfile_*yaml") do |filename|
puts("Importing Scan Profile: #{File.basename(filename, '.yaml').gsub(/^ScanProfile_/, '')} ....")
hash = YAML.load_file(filename)
items = hash["definition"]
hash.delete("definition")
profile = ScanItemSet.find_by(:name => hash["name"]);
if profile.nil?
if hash["guid"].nil?
hash["guid"] = SecureRandom.uuid
end
profile = ScanItemSet.new(hash)
else
profile.attributes = hash
end
profile.save!
# Delete existing members
profile.members.each do |one|
if one.filename
# OK, this was defined through yaml file... just skip it
next
#profile.delete(one)
else
one.destroy
end
end
items.each do |i|
if i['filename']
# OK, this rules refers to a file, just use it...
next
else
if i['guid'].nil?
i['guid'] = SecureRandom.uuid
end
#puts i.inspect
si = ScanItem.create(i)
end
profile.add_member(si)
end
end
end
end
namespace :rhconsulting do
namespace :miq_scanprofiles do
desc 'Usage information'
task :usage => [:environment] do
puts 'Export - Usage: rake \'rhconsulting:miq_scanprofiles:export[/path/to/dir/with/scan/items]\''
puts 'Import - Usage: rake \'rhconsulting:miq_scanprofiles:import[/path/to/dir/with/scan/items]\''
end
desc 'Exports all scan profiles to individual YAML files'
task :export, [:filedir] => [:environment] do |_, arguments|
options = RhconsultingOptions.parse_options(arguments.extras)
MiqScanItemsImportExport.new.export(arguments[:filedir], options)
end
desc 'Imports all scan profiles from individual YAML files'
task :import, [:filedir] => [:environment] do |_, arguments|
MiqScanItemsImportExport.new.import(arguments[:filedir])
end
end
end