1 |
c2496839
|
Tom McKay
|
module HammerCLICsv
|
2 |
|
|
class CsvCommand
|
3 |
|
|
class ExportCommand < HammerCLI::Apipie::Command
|
4 |
f5d21adb
|
Tom McKay
|
include ::HammerCLICsv::Utils::Config
|
5 |
|
|
|
6 |
c2496839
|
Tom McKay
|
command_name 'export'
|
7 |
|
|
desc 'export into directory'
|
8 |
|
|
|
9 |
b2e88b7c
|
Tom McKay
|
def self.supported?
|
10 |
|
|
true
|
11 |
|
|
end
|
12 |
|
|
|
13 |
02387fb5
|
Tom McKay
|
option %w(-v --verbose), :flag, _('be verbose')
|
14 |
|
|
option %w(--threads), 'THREAD_COUNT', _('Number of threads to hammer with'),
|
15 |
|
|
:default => 1, :hidden => true
|
16 |
b2e88b7c
|
Tom McKay
|
option '--dir', 'DIRECTORY', _('directory to export to')
|
17 |
02387fb5
|
Tom McKay
|
option %w(--organization), 'ORGANIZATION', _('Only process organization matching this name')
|
18 |
c2496839
|
Tom McKay
|
|
19 |
a90dd3fc
|
Tom McKay
|
RESOURCES = %w(
|
20 |
b2e88b7c
|
Tom McKay
|
settings organizations locations puppet_environments operating_systems
|
21 |
a90dd3fc
|
Tom McKay
|
domains architectures partition_tables lifecycle_environments host_collections
|
22 |
|
|
provisioning_templates
|
23 |
|
|
subscriptions activation_keys hosts content_hosts reports roles users
|
24 |
|
|
)
|
25 |
b2e88b7c
|
Tom McKay
|
SUPPORTED_RESOURCES = %w(
|
26 |
|
|
settings
|
27 |
|
|
)
|
28 |
c2496839
|
Tom McKay
|
RESOURCES.each do |resource|
|
29 |
|
|
dashed = resource.sub('_', '-')
|
30 |
b2e88b7c
|
Tom McKay
|
option "--#{dashed}", 'FILE', "csv file for #{dashed}",
|
31 |
|
|
:hidden => !SUPPORTED_RESOURCES.include?(resource)
|
32 |
c2496839
|
Tom McKay
|
end
|
33 |
|
|
|
34 |
|
|
def execute
|
35 |
f5d21adb
|
Tom McKay
|
@api = api_connection
|
36 |
|
|
skipped_resources = (RESOURCES - SUPPORTED_RESOURCES)
|
37 |
c2496839
|
Tom McKay
|
|
38 |
a90dd3fc
|
Tom McKay
|
(RESOURCES - skipped_resources).each do |resource|
|
39 |
c2496839
|
Tom McKay
|
hammer_resource(resource)
|
40 |
|
|
end
|
41 |
|
|
|
42 |
|
|
HammerCLI::EX_OK
|
43 |
|
|
end
|
44 |
|
|
|
45 |
|
|
def hammer(context = nil)
|
46 |
|
|
context ||= {
|
47 |
|
|
:interactive => false,
|
48 |
a90dd3fc
|
Tom McKay
|
:username => @username,
|
49 |
|
|
:password => @password
|
50 |
c2496839
|
Tom McKay
|
}
|
51 |
|
|
|
52 |
|
|
HammerCLI::MainCommand.new('', context)
|
53 |
|
|
end
|
54 |
|
|
|
55 |
|
|
def hammer_resource(resource)
|
56 |
|
|
return if !self.send("option_#{resource}") && !option_dir
|
57 |
|
|
options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.sub('_', '-')}.csv"
|
58 |
a90dd3fc
|
Tom McKay
|
args = []
|
59 |
|
|
args += %W( --server #{@server} ) if @server
|
60 |
02387fb5
|
Tom McKay
|
args += %W( csv #{resource.sub('_', '-')} --export --file #{options_file} )
|
61 |
c2496839
|
Tom McKay
|
args << '-v' if option_verbose?
|
62 |
02387fb5
|
Tom McKay
|
args += %W( --organization #{option_organization} ) if option_organization
|
63 |
|
|
args += %W( --threads #{option_threads} ) if option_threads
|
64 |
1909f2e6
|
Tom McKay
|
puts "Exporting '#{args.join(' ')}'" if option_verbose?
|
65 |
c2496839
|
Tom McKay
|
hammer.run(args)
|
66 |
|
|
end
|
67 |
a90dd3fc
|
Tom McKay
|
|
68 |
|
|
def check_server_status(server, username, password)
|
69 |
|
|
url = "#{server}/api/status"
|
70 |
|
|
uri = URI(url)
|
71 |
|
|
nethttp = Net::HTTP.new(uri.host, uri.port)
|
72 |
|
|
nethttp.use_ssl = uri.scheme == 'https'
|
73 |
|
|
nethttp.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
74 |
|
|
server_status = nethttp.start do |http|
|
75 |
|
|
request = Net::HTTP::Get.new uri.request_uri
|
76 |
|
|
request.basic_auth(username, password)
|
77 |
|
|
response = http.request(request)
|
78 |
|
|
JSON.parse(response.body)
|
79 |
|
|
end
|
80 |
|
|
|
81 |
|
|
server_status
|
82 |
|
|
end
|
83 |
c2496839
|
Tom McKay
|
end
|
84 |
|
|
end
|
85 |
|
|
end |