-
Notifications
You must be signed in to change notification settings - Fork 47
/
rhconsulting_widgets.rake
69 lines (53 loc) · 2.04 KB
/
rhconsulting_widgets.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
# Author: Brant Evans <[email protected]>
require_relative 'rhconsulting_illegal_chars'
require_relative 'rhconsulting_options'
class MiqWidgetsImportExport
class ParsedNonDialogYamlError < StandardError; end
def export(export_dir, options = {})
raise "Must supply export dir" if export_dir.blank?
# Export the Policies
export_widgets(export_dir, options)
end
def import(import_dir)
raise "Must supply import dir" if import_dir.blank?
# Import the Policy Profiles
import_widgets(import_dir)
end
private
def export_widgets(export_dir, options)
custom_widgets = MiqWidget.where(:read_only => "false")
custom_widgets.each { |widget|
# Set the filename and replace spaces and characters that are not allowed in filenames
fname = MiqIllegalChars.replace("#{widget.id}_#{widget.name}.yaml", options)
File.write("#{export_dir}/#{fname}", widget.export_to_array.to_yaml)
}
end
def import_widgets(import_dir)
MiqWidget.transaction do
Dir.glob("#{import_dir}/*yaml") do |filename|
widgets = YAML.load_file(filename)
widgets.each do |widget|
MiqWidget.import_from_hash(widget['MiqWidget'], {:userid=>'admin', :overwrite=>true, :save=>true})
end
end
end
end
end
namespace :rhconsulting do
namespace :miq_widgets do
desc 'Usage information'
task :usage => [:environment] do
puts 'Export - Usage: rake \'rhconsulting:miq_widgets:export[/path/to/dir/with/widgets]\''
puts 'Import - Usage: rake \'rhconsulting:miq_widgets:import[/path/to/dir/with/widgets]\''
end
desc 'Exports all widgets to individual YAML files'
task :export, [:filedir] => [:environment] do |_, arguments|
options = RhconsultingOptions.parse_options(arguments.extras)
MiqWidgetsImportExport.new.export(arguments[:filedir], options)
end
desc 'Imports all policies from individual YAML files'
task :import, [:filedir] => [:environment] do |_, arguments|
MiqWidgetsImportExport.new.import(arguments[:filedir])
end
end
end