1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
12
|
require 'hammer_cli'
|
13
|
require 'hammer_cli_csv'
|
14
|
require 'hammer_cli_foreman'
|
15
|
require 'hammer_cli_katello'
|
16
|
require 'json'
|
17
|
require 'csv'
|
18
|
require 'uri'
|
19
|
|
20
|
module HammerCLICsv
|
21
|
class CsvCommand
|
22
|
class ImportCommand < HammerCLI::Apipie::Command
|
23
|
command_name 'import'
|
24
|
desc 'import by directory'
|
25
|
|
26
|
option %w(-v --verbose), :flag, 'be verbose'
|
27
|
option %w(--threads), 'THREAD_COUNT', 'Number of threads to hammer with', :default => 1
|
28
|
option %w(--server), 'SERVER', 'Server URL'
|
29
|
option %w(-u --username), 'USERNAME', 'Username to access server'
|
30
|
option %w(-p --password), 'PASSWORD', 'Password to access server'
|
31
|
option '--dir', 'DIRECTORY', 'directory to import from'
|
32
|
|
33
|
RESOURCES = %w( organizations locations roles users puppet_environments operating_systems
|
34
|
domains architectures partition_tables lifecycle_environments host_collections
|
35
|
subscriptions activation_keys hosts content_hosts reports )
|
36
|
RESOURCES.each do |resource|
|
37
|
dashed = resource.sub('_', '-')
|
38
|
option "--#{dashed}", 'FILE', "csv file for #{dashed}"
|
39
|
end
|
40
|
|
41
|
def ctx
|
42
|
{
|
43
|
:interactive => false,
|
44
|
:username => 'admin',
|
45
|
:password => 'changeme'
|
46
|
}
|
47
|
end
|
48
|
|
49
|
def hammer(context = nil)
|
50
|
HammerCLI::MainCommand.new('', context || ctx)
|
51
|
end
|
52
|
|
53
|
def execute
|
54
|
@api = ApipieBindings::API.new({
|
55
|
:uri => option_server || HammerCLI::Settings.get(:csv, :host),
|
56
|
:username => option_username || HammerCLI::Settings.get(:csv, :username),
|
57
|
:password => option_password || HammerCLI::Settings.get(:csv, :password),
|
58
|
:api_version => 2
|
59
|
})
|
60
|
|
61
|
|
62
|
RESOURCES.each do |resource|
|
63
|
hammer_resource(resource)
|
64
|
end
|
65
|
|
66
|
HammerCLI::EX_OK
|
67
|
end
|
68
|
|
69
|
def hammer_resource(resource)
|
70
|
return if !self.send("option_#{resource}") && !option_dir
|
71
|
options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.sub('_', '-')}.csv"
|
72
|
if !File.exists? options_file
|
73
|
return if option_dir
|
74
|
raise "File for #{resource} '#{options_file}' does not exist"
|
75
|
end
|
76
|
|
77
|
args = %W( csv #{resource.sub('_', '-')} --csv-file #{options_file} )
|
78
|
args << '-v' if option_verbose?
|
79
|
args += %W( --threads #{option_threads} )
|
80
|
hammer.run(args)
|
81
|
end
|
82
|
end
|
83
|
end
|
84
|
end
|