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(--server), 'SERVER', 'Server URL'
|
28
|
option %w(-u --username), 'USERNAME', 'Username to access server'
|
29
|
option %w(-p --password), 'PASSWORD', 'Password to access server'
|
30
|
option '--dir', 'DIRECTORY', 'directory to import from'
|
31
|
option '--roles', 'FILE', 'source to import roles'
|
32
|
option '--users', 'FILE', 'source to import users'
|
33
|
option '--hosts', 'FILE', 'source to import hosts'
|
34
|
option '--organizations', 'FILE', 'source to import organizations'
|
35
|
option '--locations', 'FILE', 'source to import locations'
|
36
|
option '--puppet-environments', 'FILE', 'source to puppet environments'
|
37
|
option '--operating-systems', 'FILE', 'source to operating systems'
|
38
|
option '--architectures', 'FILE', 'source to architectures'
|
39
|
option '--domains', 'FILE', 'source to domains'
|
40
|
option '--architectures', 'FILE', 'source to architectures'
|
41
|
option '--partition-tables', 'FILE', 'source to partition-tables'
|
42
|
|
43
|
def ctx
|
44
|
{
|
45
|
:interactive => false,
|
46
|
:username => 'admin',
|
47
|
:password => 'changeme'
|
48
|
}
|
49
|
end
|
50
|
|
51
|
def hammer(context = nil)
|
52
|
HammerCLI::MainCommand.new('', context || ctx)
|
53
|
end
|
54
|
|
55
|
def execute
|
56
|
@api = ApipieBindings::API.new({
|
57
|
:uri => option_server || HammerCLI::Settings.get(:csv, :host),
|
58
|
:username => option_username || HammerCLI::Settings.get(:csv, :username),
|
59
|
:password => option_password || HammerCLI::Settings.get(:csv, :password),
|
60
|
:api_version => 2
|
61
|
})
|
62
|
|
63
|
|
64
|
%w( organizations locations roles users puppet_environments operating_systems
|
65
|
hosts domains architectures partition_tables ).each do |resource|
|
66
|
swing(resource)
|
67
|
end
|
68
|
|
69
|
HammerCLI::EX_OK
|
70
|
end
|
71
|
|
72
|
def swing(resource)
|
73
|
return if !self.send("option_#{resource}") && !option_dir
|
74
|
options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.sub('_', '-')}.csv"
|
75
|
raise "File for #{resource} '#{options_file}' does not exist" unless File.exists? options_file
|
76
|
args = %W{ csv #{resource.sub('_', '-')} --csv-file #{options_file} }
|
77
|
args << '-v' if option_verbose?
|
78
|
hammer.run(args)
|
79
|
end
|
80
|
end
|
81
|
end
|
82
|
end
|