1
|
require 'open-uri'
|
2
|
|
3
|
module HammerCLICsv
|
4
|
class CsvCommand
|
5
|
class ImportCommand < HammerCLI::Apipie::Command
|
6
|
include ::HammerCLICsv::Utils::Config
|
7
|
|
8
|
command_name 'import'
|
9
|
desc 'import by directory'
|
10
|
|
11
|
def self.supported?
|
12
|
true
|
13
|
end
|
14
|
|
15
|
option %w(-v --verbose), :flag, _('be verbose')
|
16
|
option %w(--threads), 'THREAD_COUNT', _('Number of threads to hammer with'),
|
17
|
:default => 1, :hidden => true
|
18
|
option '--dir', 'DIRECTORY', _('directory to import from')
|
19
|
option %w(--organization), 'ORGANIZATION', _('Only process organization matching this name')
|
20
|
option %w(--prefix), 'PREFIX', _('Prefix for all name columns'),
|
21
|
:hidden => true
|
22
|
|
23
|
RESOURCES = %w(
|
24
|
settings organizations locations puppet_environments operating_systems
|
25
|
domains architectures partition_tables lifecycle_environments host_collections
|
26
|
provisioning_templates
|
27
|
subscriptions products content_views content_view_filters activation_keys
|
28
|
hosts content_hosts smart_proxies compute_resources reports roles users
|
29
|
)
|
30
|
SUPPORTED_RESOURCES = %w(
|
31
|
settings
|
32
|
)
|
33
|
RESOURCES.each do |resource|
|
34
|
dashed = resource.gsub('_', '-')
|
35
|
option "--#{dashed}", 'FILE', "csv file for #{dashed}",
|
36
|
:hidden => !SUPPORTED_RESOURCES.include?(resource)
|
37
|
end
|
38
|
|
39
|
def execute
|
40
|
@api = api_connection
|
41
|
|
42
|
resources_specified = RESOURCES.collect do |resource|
|
43
|
resource if self.send("option_#{resource}") || ARGV.include?('--' + resource.gsub('_', '-'))
|
44
|
end
|
45
|
resources_specified.compact!
|
46
|
RESOURCES.each do |resource|
|
47
|
if resources_specified.include?(resource) || (resources_specified == [] && option_dir)
|
48
|
hammer_resource(resource)
|
49
|
end
|
50
|
end
|
51
|
|
52
|
HammerCLI::EX_OK
|
53
|
end
|
54
|
|
55
|
def hammer(context = nil)
|
56
|
context ||= {
|
57
|
:interactive => false,
|
58
|
:username => @username,
|
59
|
:password => @password
|
60
|
}
|
61
|
|
62
|
HammerCLI::MainCommand.new('', context)
|
63
|
end
|
64
|
|
65
|
def hammer_resource(resource)
|
66
|
return if !self.send("option_#{resource}") && !option_dir
|
67
|
options_file = option_dir ? "#{option_dir}/#{resource.gsub('_', '-')}.csv" : self.send("option_#{resource}")
|
68
|
unless options_file_exists? options_file
|
69
|
if option_dir
|
70
|
return unless SUPPORTED_RESOURCES.include?(resource)
|
71
|
puts _("Skipping %{resource} because '%{options_file}' does not exist") %
|
72
|
{:resource => resource, :options_file => options_file} if option_verbose?
|
73
|
return
|
74
|
end
|
75
|
raise "File for #{resource} '#{options_file}' does not exist"
|
76
|
end
|
77
|
puts _("Importing %{resource} from '%{options_file}'") %
|
78
|
{:resource => resource, :options_file => options_file} if option_verbose?
|
79
|
|
80
|
args = %W( csv #{resource.gsub('_', '-')} --file #{options_file} )
|
81
|
args << '-v' if option_verbose?
|
82
|
args += %W( --organization #{option_organization} ) if option_organization
|
83
|
args += %W( --prefix #{option_prefix} ) if option_prefix
|
84
|
args += %W( --threads #{option_threads} ) if option_threads
|
85
|
hammer.run(args)
|
86
|
end
|
87
|
|
88
|
private
|
89
|
|
90
|
def options_file_exists?(options_file)
|
91
|
f = open(options_file)
|
92
|
f.close
|
93
|
true
|
94
|
rescue
|
95
|
false
|
96
|
end
|
97
|
|
98
|
def get_option(name)
|
99
|
HammerCLI::Settings.settings[:_params][name] ||
|
100
|
HammerCLI::Settings.get(:csv, name) ||
|
101
|
HammerCLI::Settings.get(:katello, name) ||
|
102
|
HammerCLI::Settings.get(:foreman, name)
|
103
|
end
|
104
|
end
|
105
|
end
|
106
|
end
|