-
Notifications
You must be signed in to change notification settings - Fork 48
/
rhconsulting_alerts.rake
132 lines (108 loc) · 4.02 KB
/
rhconsulting_alerts.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
130
131
132
# Author: Brant Evans <bevans@redhat.com>
require_relative 'rhconsulting_illegal_chars'
require_relative 'rhconsulting_options'
class MiqAlertsImportExport
class ParsedNonDialogYamlError < StandardError; end
def export(export_dir, options = {})
raise "Must supply export dir" if export_dir.blank?
# Export the Alerts
export_alerts(export_dir, options)
end
def export_sets(export_dir, options = {})
raise "Must supply export dir" if export_dir.blank?
# Export the Alert Sets
export_alert_sets(export_dir, options)
end
def import(import_name)
raise "Must supply filename or directory" if import_name.blank?
if File.file?(import_name)
alerts = YAML.load_file(import_name)
import_alerts(alerts)
elsif File.directory?(import_name)
Dir.glob("#{import_name}/*.yaml") do |fname|
alerts = YAML.load_file(fname)
import_alerts(alerts)
end
else
raise "Argument is not a filename or directory"
end
end
def import_sets(import_name)
raise "Must supply filename or directory" if import_name.blank?
if File.file?(import_name)
alertsets = YAML.load_file(import_name)
import_alert_sets(alertsets)
elsif File.directory?(import_name)
Dir.glob("#{import_name}/*.yaml") do |fname|
alertsets = YAML.load_file(fname)
import_alert_sets(alertsets)
end
else
raise "Argument is not a filename or directory"
end
end
private
def export_alerts(export_dir, options)
MiqAlert.order(:id).all.each do |a|
# Replace characters in the description that are not allowed in filenames
fname = MiqIllegalChars.replace(a.description, options)
File.write("#{export_dir}/#{fname}.yaml", a.export_to_yaml)
end
end
def export_alert_sets(export_dir, options)
MiqAlertSet.order(:id).all.each do |a|
puts("Exporting Alert Set: #{a.description}")
# Replace characters in the description that are not allowed in filenames
fname = MiqIllegalChars.replace(a.description, options)
File.write("#{export_dir}/#{fname}.yaml", a.export_to_yaml)
end
end
def import_alerts(alerts)
MiqAlert.transaction do
alerts.each do |alert|
MiqAlert.import_from_hash(alert['MiqAlert'], {:preview => false})
end
end
end
def import_alert_sets(alertsets)
MiqAlertSet.transaction do
alertsets.each do |alertset|
MiqAlertSet.import_from_hash(alertset['MiqAlertSet'], {:preview => false})
end
end
end
end
namespace :rhconsulting do
namespace :miq_alerts do
desc 'Usage information'
task :usage => [:environment] do
puts 'Export - Usage: rake \'rhconsulting:miq_alerts:export[/path/to/dir/with/alerts]\''
puts 'Import - Usage: rake \'rhconsulting:miq_alerts:import[/path/to/dir/with/alerts]\''
end
desc 'Exports all alerts to individual YAML files'
task :export, [:filedir] => [:environment] do |_, arguments|
options = RhconsultingOptions.parse_options(arguments.extras)
MiqAlertsImportExport.new.export(arguments[:filedir], options)
end
desc 'Imports all alerts from individual YAML files'
task :import, [:filedir] => [:environment] do |_, arguments|
MiqAlertsImportExport.new.import(arguments[:filedir])
end
end
namespace :miq_alertsets do
desc 'Usage information'
task :usage => [:environment] do
puts 'Export - Usage: rake \'rhconsulting:miq_alertsets:export[/path/to/dir/with/alertsets]\''
puts 'Import - Usage: rake \'rhconsulting:miq_alertsets:import[/path/to/dir/with/alertsets]\''
end
desc 'Exports all alerts to individual YAML files'
task :export, [:filedir] => [:environment] do |_, arguments|
options = RhconsultingOptions.parse_options(arguments.extras)
MiqAlertsImportExport.new.export_sets(arguments[:filedir], options)
end
desc 'Imports all alerts from individual YAML files'
task :import, [:filedir] => [:environment] do |_, arguments|
MiqAlertsImportExport.new.import_sets(arguments[:filedir])
end
end
end