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