1
|
require 'apipie-bindings'
|
2
|
require 'hammer_cli'
|
3
|
require 'json'
|
4
|
require 'open-uri'
|
5
|
require 'csv'
|
6
|
require 'hammer_cli_csv/csv'
|
7
|
|
8
|
|
9
|
module HammerCLICsv
|
10
|
class BaseCommand < HammerCLI::Apipie::Command
|
11
|
include ::HammerCLICsv::Utils::Config
|
12
|
|
13
|
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
|
option %w(--export), :flag, 'Export current data instead of importing'
|
17
|
option %w(--file), 'FILE_NAME', 'CSV file (default to /dev/stdout with --export, otherwise required)'
|
18
|
option %w(--prefix), 'PREFIX', 'Prefix for all name columns',
|
19
|
:hidden => true
|
20
|
option %w(--organization), 'ORGANIZATION', _('Only process organization matching this name')
|
21
|
option %w(--continue-on-error), :flag, _('Continue processing even if individual resource error')
|
22
|
|
23
|
option %w(--csv-file), 'FILE_NAME', 'Option --csv-file is deprecated. Use --file',
|
24
|
:deprecated => "Use --file", :hidden => true,
|
25
|
:attribute_name => :option_file
|
26
|
option %w(--csv-export), :flag, 'Option --csv-export is deprecated. Use --export',
|
27
|
:deprecated => "Use --export", :hidden => true,
|
28
|
:attribute_name => :option_export
|
29
|
|
30
|
NAME = 'Name'
|
31
|
COUNT = 'Count'
|
32
|
|
33
|
def self.supported?
|
34
|
false
|
35
|
end
|
36
|
|
37
|
def supported?
|
38
|
self.class.supported?
|
39
|
end
|
40
|
|
41
|
def help
|
42
|
print_message _('**** This command is unsupported and is provided as tech preview. ****') unless supported?
|
43
|
super
|
44
|
end
|
45
|
|
46
|
def execute
|
47
|
@api = api_connection
|
48
|
@server_status = check_server_status(@server, @username, @password)
|
49
|
|
50
|
if option_export?
|
51
|
if option_file
|
52
|
CSV.open(option_file, 'wb', {:force_quotes => false}) do |csv|
|
53
|
export csv
|
54
|
end
|
55
|
else
|
56
|
CSV do |csv|
|
57
|
export csv
|
58
|
end
|
59
|
end
|
60
|
else
|
61
|
import
|
62
|
end
|
63
|
HammerCLI::EX_OK
|
64
|
end
|
65
|
|
66
|
def check_server_status(server, username, password)
|
67
|
url = "#{server}/api/status"
|
68
|
uri = URI(url)
|
69
|
nethttp = Net::HTTP.new(uri.host, uri.port)
|
70
|
nethttp.use_ssl = uri.scheme == 'https'
|
71
|
nethttp.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
72
|
server_status = nethttp.start do |http|
|
73
|
request = Net::HTTP::Get.new uri.request_uri
|
74
|
request.basic_auth(username, password)
|
75
|
response = http.request(request)
|
76
|
JSON.parse(response.body)
|
77
|
end
|
78
|
|
79
|
url = "#{server}/api/v2/plugins"
|
80
|
uri = URI(url)
|
81
|
nethttp = Net::HTTP.new(uri.host, uri.port)
|
82
|
nethttp.use_ssl = uri.scheme == 'https'
|
83
|
nethttp.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
84
|
server_status['plugins'] = nethttp.start do |http|
|
85
|
request = Net::HTTP::Get.new uri.request_uri
|
86
|
request.basic_auth(username, password)
|
87
|
response = http.request(request)
|
88
|
JSON.parse(response.body)['results']
|
89
|
end
|
90
|
|
91
|
server_status
|
92
|
end
|
93
|
|
94
|
def namify(name_format, number = 0)
|
95
|
return '' unless name_format
|
96
|
if name_format.index('%')
|
97
|
name = name_format % number
|
98
|
else
|
99
|
name = name_format
|
100
|
end
|
101
|
name = "#{option_prefix}#{name}" if option_prefix
|
102
|
name
|
103
|
end
|
104
|
|
105
|
def labelize(name)
|
106
|
name.gsub(/[^a-z0-9\-_]/i, '_')
|
107
|
end
|
108
|
|
109
|
def thread_import(return_headers = false, filename=nil, name_column=nil)
|
110
|
filename ||= option_file || '/dev/stdin'
|
111
|
csv = []
|
112
|
CSV.new(open(filename), {
|
113
|
:skip_blanks => true,
|
114
|
:headers => :first_row,
|
115
|
:return_headers => return_headers
|
116
|
}).each do |line|
|
117
|
csv << line
|
118
|
end
|
119
|
lines_per_thread = csv.length / option_threads.to_i + 1
|
120
|
splits = []
|
121
|
|
122
|
option_threads.to_i.times do |current_thread|
|
123
|
start_index = ((current_thread) * lines_per_thread).to_i
|
124
|
finish_index = ((current_thread + 1) * lines_per_thread).to_i
|
125
|
finish_index = csv.length if finish_index > csv.length
|
126
|
if start_index <= finish_index
|
127
|
lines = csv[start_index...finish_index].clone
|
128
|
splits << Thread.new do
|
129
|
lines.each do |line|
|
130
|
next if !line[name_column || NAME].nil? && line[name_column || NAME][0] == '#'
|
131
|
begin
|
132
|
yield line
|
133
|
rescue RuntimeError => e
|
134
|
message = "#{e}\n#{line}"
|
135
|
option_continue_on_error? ? $stderr.puts(_("Error: %{message}") % {:message => message}) : raise(message)
|
136
|
end
|
137
|
end
|
138
|
end
|
139
|
end
|
140
|
end
|
141
|
|
142
|
splits.each do |thread|
|
143
|
thread.join
|
144
|
end
|
145
|
end
|
146
|
|
147
|
def hammer_context
|
148
|
{
|
149
|
:interactive => false,
|
150
|
:username => 'admin',
|
151
|
:password => 'changeme'
|
152
|
}
|
153
|
end
|
154
|
|
155
|
def hammer(context = nil)
|
156
|
HammerCLI::MainCommand.new('', context || hammer_context)
|
157
|
end
|
158
|
|
159
|
def foreman_organization(options = {})
|
160
|
@organizations ||= {}
|
161
|
|
162
|
if options[:name]
|
163
|
return nil if options[:name].nil? || options[:name].empty?
|
164
|
options[:id] = @organizations[options[:name]]
|
165
|
if !options[:id]
|
166
|
organization = @api.resource(:organizations).call(:index, {
|
167
|
:per_page => 999999,
|
168
|
'search' => "name=\"#{options[:name]}\""
|
169
|
})['results']
|
170
|
raise _("Organization '%{name}' not found") % {:name => options[:name]} if !organization || organization.empty?
|
171
|
options[:id] = organization[0]['id']
|
172
|
@organizations[options[:name]] = options[:id]
|
173
|
end
|
174
|
result = options[:id]
|
175
|
else
|
176
|
return nil if options[:id].nil?
|
177
|
options[:name] = @organizations.key(options[:id])
|
178
|
if !options[:name]
|
179
|
organization = @api.resource(:organizations).call(:show, {'id' => options[:id]})
|
180
|
raise _("Organization 'id=%{id}' not found") % {:id => options[:id]} if !organization || organization.empty?
|
181
|
options[:name] = organization['name']
|
182
|
@organizations[options[:name]] = options[:id]
|
183
|
end
|
184
|
result = options[:name]
|
185
|
end
|
186
|
|
187
|
result
|
188
|
end
|
189
|
|
190
|
def foreman_location(options = {})
|
191
|
@locations ||= {}
|
192
|
|
193
|
if options[:name]
|
194
|
return nil if options[:name].nil? || options[:name].empty?
|
195
|
options[:id] = @locations[options[:name]]
|
196
|
if !options[:id]
|
197
|
location = @api.resource(:locations).call(:index, {
|
198
|
:per_page => 999999,
|
199
|
'search' => "name=\"#{options[:name]}\""
|
200
|
})['results']
|
201
|
raise _("Location '%{name}' not found") % {:name => options[:name]} if !location || location.empty?
|
202
|
options[:id] = location[0]['id']
|
203
|
@locations[options[:name]] = options[:id]
|
204
|
end
|
205
|
result = options[:id]
|
206
|
else
|
207
|
return nil if options[:id].nil?
|
208
|
options[:name] = @locations.key(options[:id])
|
209
|
if !options[:name]
|
210
|
location = @api.resource(:locations).call(:show, {'id' => options[:id]})
|
211
|
raise _("Location 'id=%{id}' not found") % {:id => options[:id]} if !location || location.empty?
|
212
|
options[:name] = location['name']
|
213
|
@locations[options[:name]] = options[:id]
|
214
|
end
|
215
|
result = options[:name]
|
216
|
end
|
217
|
|
218
|
result
|
219
|
end
|
220
|
|
221
|
def foreman_role(options = {})
|
222
|
@roles ||= {}
|
223
|
|
224
|
if options[:name]
|
225
|
return nil if options[:name].nil? || options[:name].empty?
|
226
|
options[:id] = @roles[options[:name]]
|
227
|
if !options[:id]
|
228
|
role = @api.resource(:roles).call(:index, {
|
229
|
:per_page => 999999,
|
230
|
'search' => "name=\"#{options[:name]}\""
|
231
|
})['results']
|
232
|
raise _("Role '%{name}' not found") % {:name => options[:name]} if !role || role.empty?
|
233
|
options[:id] = role[0]['id']
|
234
|
@roles[options[:name]] = options[:id]
|
235
|
end
|
236
|
result = options[:id]
|
237
|
else
|
238
|
return nil if options[:id].nil?
|
239
|
options[:name] = @roles.key(options[:id])
|
240
|
if !options[:name]
|
241
|
role = @api.resource(:roles).call(:show, {'id' => options[:id]})
|
242
|
raise _("Role 'id=%{id}' not found") % {:id => options[:id]} if !role || role.empty?
|
243
|
options[:name] = role['name']
|
244
|
@roles[options[:name]] = options[:id]
|
245
|
end
|
246
|
result = options[:name]
|
247
|
end
|
248
|
|
249
|
result
|
250
|
end
|
251
|
|
252
|
def foreman_permission(options = {})
|
253
|
@permissions ||= {}
|
254
|
|
255
|
if options[:name]
|
256
|
return nil if options[:name].nil? || options[:name].empty?
|
257
|
options[:id] = @permissions[options[:name]]
|
258
|
if !options[:id]
|
259
|
permission = @api.resource(:permissions).call(:index, {
|
260
|
:per_page => 999999,
|
261
|
'name' => options[:name]
|
262
|
})['results']
|
263
|
raise _("Permission '%{name}' not found") % {:name => options[:name]} if !permission || permission.empty?
|
264
|
options[:id] = permission[0]['id']
|
265
|
@permissions[options[:name]] = options[:id]
|
266
|
end
|
267
|
result = options[:id]
|
268
|
else
|
269
|
return nil if options[:id].nil?
|
270
|
options[:name] = @permissions.key(options[:id])
|
271
|
if !options[:name]
|
272
|
permission = @api.resource(:permissions).call(:show, {'id' => options[:id]})
|
273
|
raise _("Permission 'id=%{id}' not found") % {:id => options[:id]} if !permission || permission.empty?
|
274
|
options[:name] = permission['name']
|
275
|
@permissions[options[:name]] = options[:id]
|
276
|
end
|
277
|
result = options[:name]
|
278
|
end
|
279
|
|
280
|
result
|
281
|
end
|
282
|
|
283
|
def foreman_filter(role, resource, search)
|
284
|
search = nil if search && search.empty?
|
285
|
filters = @api.resource(:filters).call(:index, {
|
286
|
:per_page => 999999,
|
287
|
'search' => "role=\"#{role}\""
|
288
|
})['results']
|
289
|
filters.each do |filter|
|
290
|
resource_type = (filter['resource_type'] || '').split(':')[-1]
|
291
|
return filter['id'] if resource_type == resource && filter['search'] == search
|
292
|
end
|
293
|
|
294
|
nil
|
295
|
end
|
296
|
|
297
|
def foreman_environment(options = {})
|
298
|
@environments ||= {}
|
299
|
|
300
|
if options[:name]
|
301
|
return nil if options[:name].nil? || options[:name].empty?
|
302
|
options[:id] = @environments[options[:name]]
|
303
|
if !options[:id]
|
304
|
environment = @api.resource(:environments).call(:index, {
|
305
|
:per_page => 999999,
|
306
|
'search' => "name=\"#{ options[:name] }\""
|
307
|
})['results']
|
308
|
raise _("Puppet environment '%{name}' not found") % {:name => options[:name]} if !environment || environment.empty?
|
309
|
options[:id] = environment[0]['id']
|
310
|
@environments[options[:name]] = options[:id]
|
311
|
end
|
312
|
result = options[:id]
|
313
|
else
|
314
|
return nil if options[:id].nil?
|
315
|
options[:name] = @environments.key(options[:id])
|
316
|
if !options[:name]
|
317
|
environment = @api.resource(:environments).call(:show, {'id' => options[:id]})
|
318
|
raise _("Puppet environment 'id=%{id}' not found") % {:id => options[:id]} if !environment || environment.empty?
|
319
|
options[:name] = environment['name']
|
320
|
@environments[options[:name]] = options[:id]
|
321
|
end
|
322
|
result = options[:name]
|
323
|
end
|
324
|
|
325
|
result
|
326
|
end
|
327
|
|
328
|
def foreman_template_kind(options = {})
|
329
|
@template_kinds ||= {}
|
330
|
|
331
|
if options[:name]
|
332
|
return nil if options[:name].nil? || options[:name].empty?
|
333
|
options[:id] = @template_kinds[options[:name]]
|
334
|
if !options[:id]
|
335
|
template_kind = @api.resource(:template_kinds).call(:index, {
|
336
|
:per_page => 999999,
|
337
|
'search' => "name=\"#{options[:name]}\""
|
338
|
})['results']
|
339
|
raise _("Template kind '%{name}' not found") % {:name => options[:name]} if !template_kind || template_kind.empty?
|
340
|
options[:id] = template_kind[0]['id']
|
341
|
@template_kinds[options[:name]] = options[:id]
|
342
|
end
|
343
|
result = options[:id]
|
344
|
else
|
345
|
return nil if options[:id].nil?
|
346
|
options[:name] = @template_kinds.key(options[:id])
|
347
|
if !options[:name]
|
348
|
template_kind = @api.resource(:template_kinds).call(:show, {'id' => options[:id]})
|
349
|
raise _("Template kind 'id=%{id}' not found") % {:id => options[:id]} if !template_kind || template_kind.empty?
|
350
|
options[:name] = template_kind['name']
|
351
|
@template_kinds[options[:name]] = options[:id]
|
352
|
end
|
353
|
result = options[:name]
|
354
|
end
|
355
|
|
356
|
result
|
357
|
end
|
358
|
|
359
|
def foreman_operatingsystem(options = {})
|
360
|
@operatingsystems ||= {}
|
361
|
|
362
|
if options[:name]
|
363
|
return nil if options[:name].nil? || options[:name].empty?
|
364
|
options[:id] = @operatingsystems[options[:name]]
|
365
|
if !options[:id]
|
366
|
(osname, major, minor) = split_os_name(options[:name])
|
367
|
search = "name=\"#{osname}\" and major=\"#{major}\" and minor=\"#{minor}\""
|
368
|
operatingsystems = @api.resource(:operatingsystems).call(:index, {
|
369
|
:per_page => 999999,
|
370
|
'search' => search
|
371
|
})['results']
|
372
|
operatingsystem = operatingsystems[0]
|
373
|
raise _("Operating system '%{name}' not found") % {:name => options[:name]} if !operatingsystem || operatingsystem.empty?
|
374
|
options[:id] = operatingsystem['id']
|
375
|
@operatingsystems[options[:name]] = options[:id]
|
376
|
end
|
377
|
result = options[:id]
|
378
|
else
|
379
|
return nil if options[:id].nil?
|
380
|
options[:name] = @operatingsystems.key(options[:id])
|
381
|
if !options[:name]
|
382
|
operatingsystem = @api.resource(:operatingsystems).call(:show, {'id' => options[:id]})
|
383
|
raise _("Operating system 'id=%{id}' not found") % {:id => options[:id]} if !operatingsystem || operatingsystem.empty?
|
384
|
options[:name] = build_os_name(operatingsystem['name'],
|
385
|
operatingsystem['major'],
|
386
|
operatingsystem['minor'])
|
387
|
@operatingsystems[options[:name]] = options[:id]
|
388
|
end
|
389
|
result = options[:name]
|
390
|
end
|
391
|
|
392
|
result
|
393
|
end
|
394
|
|
395
|
def foreman_architecture(options = {})
|
396
|
@architectures ||= {}
|
397
|
|
398
|
if options[:name]
|
399
|
return nil if options[:name].nil? || options[:name].empty?
|
400
|
options[:id] = @architectures[options[:name]]
|
401
|
if !options[:id]
|
402
|
architecture = @api.resource(:architectures).call(:index, {
|
403
|
:per_page => 999999,
|
404
|
'search' => "name=\"#{options[:name]}\""
|
405
|
})['results']
|
406
|
raise _("Architecture '%{name}' not found") % {:name => options[:name]} if !architecture || architecture.empty?
|
407
|
options[:id] = architecture[0]['id']
|
408
|
@architectures[options[:name]] = options[:id]
|
409
|
end
|
410
|
result = options[:id]
|
411
|
else
|
412
|
return nil if options[:id].nil?
|
413
|
options[:name] = @architectures.key(options[:id])
|
414
|
if !options[:name]
|
415
|
architecture = @api.resource(:architectures).call(:show, {'id' => options[:id]})
|
416
|
raise _("Architecture 'id=%{id}' not found") % {:id => options[:id]} if !architecture || architecture.empty?
|
417
|
options[:name] = architecture['name']
|
418
|
@architectures[options[:name]] = options[:id]
|
419
|
end
|
420
|
result = options[:name]
|
421
|
end
|
422
|
|
423
|
result
|
424
|
end
|
425
|
|
426
|
def foreman_domain(options = {})
|
427
|
@domains ||= {}
|
428
|
|
429
|
if options[:name]
|
430
|
return nil if options[:name].nil? || options[:name].empty?
|
431
|
options[:id] = @domains[options[:name]]
|
432
|
if !options[:id]
|
433
|
domain = @api.resource(:domains).call(:index, {
|
434
|
:per_page => 999999,
|
435
|
'search' => "name=\"#{options[:name]}\""
|
436
|
})['results']
|
437
|
raise _("Domain '%{name}' not found") % {:name => options[:name]} if !domain || domain.empty?
|
438
|
options[:id] = domain[0]['id']
|
439
|
@domains[options[:name]] = options[:id]
|
440
|
end
|
441
|
result = options[:id]
|
442
|
else
|
443
|
return nil if options[:id].nil?
|
444
|
options[:name] = @domains.key(options[:id])
|
445
|
if !options[:name]
|
446
|
domain = @api.resource(:domains).call(:show, {'id' => options[:id]})
|
447
|
raise _("Domain 'id=%{id}' not found") % {:id => options[:id]} if !domain || domain.empty?
|
448
|
options[:name] = domain['name']
|
449
|
@domains[options[:name]] = options[:id]
|
450
|
end
|
451
|
result = options[:name]
|
452
|
end
|
453
|
|
454
|
result
|
455
|
end
|
456
|
|
457
|
def foreman_partitiontable(options = {})
|
458
|
@ptables ||= {}
|
459
|
|
460
|
if options[:name]
|
461
|
return nil if options[:name].nil? || options[:name].empty?
|
462
|
options[:id] = @ptables[options[:name]]
|
463
|
if !options[:id]
|
464
|
ptable = @api.resource(:ptables).call(:index, {
|
465
|
:per_page => 999999,
|
466
|
'search' => "name=\"#{options[:name]}\""
|
467
|
})['results']
|
468
|
raise _("Partition table '%{name}' not found") % {:name => options[:name]} if !ptable || ptable.empty?
|
469
|
options[:id] = ptable[0]['id']
|
470
|
@ptables[options[:name]] = options[:id]
|
471
|
end
|
472
|
result = options[:id]
|
473
|
elsif options[:id]
|
474
|
return nil if options[:id].nil?
|
475
|
options[:name] = @ptables.key(options[:id])
|
476
|
if !options[:name]
|
477
|
ptable = @api.resource(:ptables).call(:show, {'id' => options[:id]})
|
478
|
options[:name] = ptable['name']
|
479
|
@ptables[options[:name]] = options[:id]
|
480
|
end
|
481
|
result = options[:name]
|
482
|
elsif !options[:name] && !options[:id]
|
483
|
result = ''
|
484
|
end
|
485
|
|
486
|
result
|
487
|
end
|
488
|
|
489
|
def foreman_medium(options = {})
|
490
|
@media ||= {}
|
491
|
|
492
|
if options[:name]
|
493
|
return nil if options[:name].nil? || options[:name].empty?
|
494
|
options[:id] = @media[options[:name]]
|
495
|
if !options[:id]
|
496
|
ptable = @api.resource(:media).call(:index, {
|
497
|
:per_page => 999999,
|
498
|
'search' => "name=\"#{options[:name]}\""
|
499
|
})['results']
|
500
|
raise _("Partition table '%{name}' not found") % {:name => options[:name]} if !ptable || ptable.empty?
|
501
|
options[:id] = ptable[0]['id']
|
502
|
@media[options[:name]] = options[:id]
|
503
|
end
|
504
|
result = options[:id]
|
505
|
elsif options[:id]
|
506
|
return nil if options[:id].nil?
|
507
|
options[:name] = @media.key(options[:id])
|
508
|
if !options[:name]
|
509
|
ptable = @api.resource(:media).call(:show, {'id' => options[:id]})
|
510
|
options[:name] = ptable['name']
|
511
|
@media[options[:name]] = options[:id]
|
512
|
end
|
513
|
result = options[:name]
|
514
|
elsif !options[:name] && !options[:id]
|
515
|
result = ''
|
516
|
end
|
517
|
|
518
|
result
|
519
|
end
|
520
|
|
521
|
def foreman_host(options = {})
|
522
|
@query_hosts ||= {}
|
523
|
|
524
|
if options[:name]
|
525
|
return nil if options[:name].nil? || options[:name].empty?
|
526
|
options[:id] = @query_hosts[options[:name]]
|
527
|
if !options[:id]
|
528
|
host = @api.resource(:hosts).call(:index, {
|
529
|
:per_page => 999999,
|
530
|
'search' => "name=\"#{options[:name]}\""
|
531
|
})['results']
|
532
|
raise _("Host '%{name}' not found") % {:name => options[:name]} if !host || host.empty?
|
533
|
options[:id] = host[0]['id']
|
534
|
@query_hosts[options[:name]] = options[:id]
|
535
|
end
|
536
|
result = options[:id]
|
537
|
else
|
538
|
return nil if options[:id].nil?
|
539
|
options[:name] = @query_hosts.key(options[:id])
|
540
|
if !options[:name]
|
541
|
host = @api.resource(:hosts).call(:show, {'id' => options[:id]})
|
542
|
raise _("Host 'id=%{id}' not found") % {:id => options[:id]} if !host || host.empty?
|
543
|
options[:name] = host['name']
|
544
|
@query_hosts[options[:name]] = options[:id]
|
545
|
end
|
546
|
result = options[:name]
|
547
|
end
|
548
|
|
549
|
result
|
550
|
end
|
551
|
|
552
|
def foreman_hostgroup(options = {})
|
553
|
@query_hostgroups ||= {}
|
554
|
|
555
|
if options[:name]
|
556
|
return nil if options[:name].nil? || options[:name].empty?
|
557
|
options[:id] = @query_hostgroups[options[:name]]
|
558
|
if !options[:id]
|
559
|
hostgroup = @api.resource(:hostgroups).call(:index, {
|
560
|
:per_page => 999999,
|
561
|
'search' => "name=\"#{options[:name]}\""
|
562
|
})['results']
|
563
|
raise _("Host Group '%{name}' not found") % {:name => options[:name]} if !hostgroup || hostgroup.empty?
|
564
|
options[:id] = hostgroup[0]['id']
|
565
|
@query_hostgroups[options[:name]] = options[:id]
|
566
|
end
|
567
|
result = options[:id]
|
568
|
else
|
569
|
return nil if options[:id].nil?
|
570
|
options[:name] = @query_hostgroups.key(options[:id])
|
571
|
if !options[:name]
|
572
|
hostgroup = @api.resource(:hostgroups).call(:show, {'id' => options[:id]})
|
573
|
raise _("Host Group 'id=%{id}' not found") % {:id => options[:id]} if !hostgroup || hostgroup.empty?
|
574
|
options[:name] = hostgroup['name']
|
575
|
@query_hostgroups[options[:name]] = options[:id]
|
576
|
end
|
577
|
result = options[:name]
|
578
|
end
|
579
|
|
580
|
result
|
581
|
end
|
582
|
|
583
|
def foreman_provisioning_template(options = {})
|
584
|
@query_config_templates ||= {}
|
585
|
|
586
|
if options[:name]
|
587
|
return nil if options[:name].nil? || options[:name].empty?
|
588
|
options[:id] = @query_config_templates[options[:name]]
|
589
|
if !options[:id]
|
590
|
config_template = @api.resource(:config_templates).call(:index, {
|
591
|
:per_page => 999999,
|
592
|
'search' => "name=\"#{options[:name]}\""
|
593
|
})['results']
|
594
|
raise _("Provisioning template '%{name}' not found") % {:name => options[:name]} if !config_template || config_template.empty?
|
595
|
options[:id] = config_template[0]['id']
|
596
|
@query_config_templates[options[:name]] = options[:id]
|
597
|
end
|
598
|
result = options[:id]
|
599
|
else
|
600
|
return nil if options[:id].nil?
|
601
|
options[:name] = @query_config_templates.key(options[:id])
|
602
|
if !options[:name]
|
603
|
config_template = @api.resource(:config_templates).call(:show, {'id' => options[:id]})
|
604
|
raise _("Provisioning template 'id=%{id}' not found") % {:id => options[:id]} if !config_template || config_template.empty?
|
605
|
options[:name] = config_template['name']
|
606
|
@query_config_templates[options[:name]] = options[:id]
|
607
|
end
|
608
|
result = options[:name]
|
609
|
end
|
610
|
|
611
|
result
|
612
|
end
|
613
|
|
614
|
def foreman_smart_proxy(options = {})
|
615
|
@query_smart_proxies ||= {}
|
616
|
|
617
|
if options[:name]
|
618
|
return nil if options[:name].nil? || options[:name].empty?
|
619
|
options[:id] = @query_smart_proxies[options[:name]]
|
620
|
if !options[:id]
|
621
|
smart_proxy = @api.resource(:smart_proxies).call(:index, {
|
622
|
:per_page => 999999,
|
623
|
'search' => "name=\"#{options[:name]}\""
|
624
|
})['results']
|
625
|
raise _("Smart Proxy '%{name}' not found") % {:name => options[:name]} if !smart_proxy || smart_proxy.empty?
|
626
|
options[:id] = smart_proxy[0]['id']
|
627
|
@query_smart_proxies[options[:name]] = options[:id]
|
628
|
end
|
629
|
result = options[:id]
|
630
|
else
|
631
|
return nil if options[:id].nil?
|
632
|
options[:name] = @query_smart_proxies.key(options[:id])
|
633
|
if !options[:name]
|
634
|
smart_proxy = @api.resource(:smart_proxies).call(:show, {'id' => options[:id]})
|
635
|
raise _("Smart Proxy 'id=%{id}' not found") % {:id => options[:id]} if !smart_proxy || smart_proxy.empty?
|
636
|
options[:name] = smart_proxy['name']
|
637
|
@query_smart_proxies[options[:name]] = options[:id]
|
638
|
end
|
639
|
result = options[:name]
|
640
|
end
|
641
|
|
642
|
result
|
643
|
end
|
644
|
|
645
|
def lifecycle_environment(organization, options = {})
|
646
|
@lifecycle_environments ||= {}
|
647
|
@lifecycle_environments[organization] ||= {
|
648
|
}
|
649
|
|
650
|
if options[:name]
|
651
|
return nil if options[:name].nil? || options[:name].empty?
|
652
|
options[:id] = @lifecycle_environments[organization][options[:name]]
|
653
|
if !options[:id]
|
654
|
@api.resource(:lifecycle_environments).call(:index, {
|
655
|
:per_page => 999999,
|
656
|
'organization_id' => foreman_organization(:name => organization)
|
657
|
})['results'].each do |environment|
|
658
|
@lifecycle_environments[organization][environment['name']] = environment['id']
|
659
|
end
|
660
|
options[:id] = @lifecycle_environments[organization][options[:name]]
|
661
|
raise _("Lifecycle environment '%{name}' not found") % {:name => options[:name]} if !options[:id]
|
662
|
end
|
663
|
result = options[:id]
|
664
|
else
|
665
|
return nil if options[:id].nil?
|
666
|
options[:name] = @lifecycle_environments.key(options[:id])
|
667
|
if !options[:name]
|
668
|
environment = @api.resource(:lifecycle_environments).call(:show, {'id' => options[:id]})
|
669
|
raise _("Lifecycle environment '%{name}' not found") % {:name => options[:name]} if !environment || environment.empty?
|
670
|
options[:name] = environment['name']
|
671
|
@lifecycle_environments[options[:name]] = options[:id]
|
672
|
end
|
673
|
result = options[:name]
|
674
|
end
|
675
|
|
676
|
result
|
677
|
end
|
678
|
|
679
|
def katello_contentview(organization, options = {})
|
680
|
@contentviews ||= {}
|
681
|
@contentviews[organization] ||= {}
|
682
|
|
683
|
if options[:name]
|
684
|
return nil if options[:name].nil? || options[:name].empty?
|
685
|
options[:id] = @contentviews[organization][options[:name]]
|
686
|
if !options[:id]
|
687
|
@api.resource(:content_views).call(:index, {
|
688
|
:per_page => 999999,
|
689
|
'organization_id' => foreman_organization(:name => organization)
|
690
|
})['results'].each do |contentview|
|
691
|
@contentviews[organization][contentview['name']] = contentview['id']
|
692
|
end
|
693
|
options[:id] = @contentviews[organization][options[:name]]
|
694
|
raise _("Content view '%{name}' not found") % {:name => options[:name]} if !options[:id]
|
695
|
end
|
696
|
result = options[:id]
|
697
|
else
|
698
|
return nil if options[:id].nil?
|
699
|
options[:name] = @contentviews.key(options[:id])
|
700
|
if !options[:name]
|
701
|
contentview = @api.resource(:content_views).call(:show, {'id' => options[:id]})
|
702
|
raise _("Puppet contentview 'id=%{id}' not found") % {:id => options[:id]} if !contentview || contentview.empty?
|
703
|
options[:name] = contentview['name']
|
704
|
@contentviews[options[:name]] = options[:id]
|
705
|
end
|
706
|
result = options[:name]
|
707
|
end
|
708
|
|
709
|
result
|
710
|
end
|
711
|
|
712
|
def katello_contentviewversion(organization, name, version='latest')
|
713
|
@contentviewversions ||= {}
|
714
|
@contentviewversions[organization] ||= {}
|
715
|
versionname = "#{version}|#{name}"
|
716
|
|
717
|
return nil if name.nil? || name.empty?
|
718
|
id = @contentviewversions[organization][versionname]
|
719
|
if !id
|
720
|
contentview_id = katello_contentview(organization, :name => name)
|
721
|
contentviewversions = @api.resource(:content_view_versions).call(:index, {
|
722
|
:per_page => 999999,
|
723
|
'content_view_id' => contentview_id
|
724
|
})['results'].sort { |a, b| a['created_at'] <=> b['created_at'] }
|
725
|
if version == 'latest'
|
726
|
@contentviewversions[organization][versionname] = contentviewversions[-1]['id']
|
727
|
else
|
728
|
contentviewversions.each do |contentviewversion|
|
729
|
if contentviewversion['version'] == version.to_f
|
730
|
@contentviewversions[organization][versionname] = contentviewversion['id']
|
731
|
end
|
732
|
end
|
733
|
end
|
734
|
id = @contentviewversions[organization][versionname]
|
735
|
raise _("Content view version '%{name}' with version '%{version}' not found") % {:name => name, :version => version} if !id
|
736
|
end
|
737
|
|
738
|
id
|
739
|
end
|
740
|
|
741
|
def katello_repository(organization, options = {})
|
742
|
@repositories ||= {}
|
743
|
@repositories[organization] ||= {}
|
744
|
|
745
|
if options[:name]
|
746
|
return nil if options[:name].nil? || options[:name].empty?
|
747
|
options[:id] = @repositories[organization][options[:name]]
|
748
|
if !options[:id]
|
749
|
@api.resource(:repositories).call(:index, {
|
750
|
:per_page => 999999,
|
751
|
'organization_id' => foreman_organization(:name => organization)
|
752
|
})['results'].each do |repository|
|
753
|
@repositories[organization][repository['name']] = repository['id']
|
754
|
end
|
755
|
options[:id] = @repositories[organization][options[:name]]
|
756
|
raise _("Repository '%{name}' not found") % {:name => options[:name]} if !options[:id]
|
757
|
end
|
758
|
result = options[:id]
|
759
|
else
|
760
|
return nil if options[:id].nil?
|
761
|
options[:name] = @repositories.key(options[:id])
|
762
|
if !options[:name]
|
763
|
repository = @api.resource(:repositories).call(:show, {'id' => options[:id]})
|
764
|
raise _("Puppet repository 'id=%{id}' not found") % {:id => options[:id]} if !repository || repository.empty?
|
765
|
options[:name] = repository['name']
|
766
|
@repositoriesr[options[:name]] = options[:id]
|
767
|
end
|
768
|
result = options[:name]
|
769
|
end
|
770
|
|
771
|
result
|
772
|
end
|
773
|
|
774
|
def katello_hostcollection(organization, options = {})
|
775
|
@hostcollections ||= {}
|
776
|
@hostcollections[organization] ||= {}
|
777
|
|
778
|
if options[:name]
|
779
|
return nil if options[:name].nil? || options[:name].empty?
|
780
|
options[:id] = @hostcollections[organization][options[:name]]
|
781
|
if !options[:id]
|
782
|
@api.resource(:host_collections).call(:index,
|
783
|
{
|
784
|
:per_page => 999999,
|
785
|
'organization_id' => foreman_organization(:name => organization),
|
786
|
'search' => search_string('host-collections',options[:name])
|
787
|
})['results'].each do |hostcollection|
|
788
|
@hostcollections[organization][hostcollection['name']] = hostcollection['id'] if hostcollection
|
789
|
end
|
790
|
options[:id] = @hostcollections[organization][options[:name]]
|
791
|
raise _("Host collection '%{name}' not found") % {:name => options[:name]} if !options[:id]
|
792
|
end
|
793
|
result = options[:id]
|
794
|
else
|
795
|
return nil if options[:id].nil?
|
796
|
options[:name] = @hostcollections.key(options[:id])
|
797
|
if !options[:name]
|
798
|
hostcollection = @api.resource(:host_collections).call(:show, {'id' => options[:id]})
|
799
|
raise _("Host collection 'id=%{id}' not found") % {:id => options[:id]} if !hostcollection || hostcollection.empty?
|
800
|
options[:name] = hostcollection['name']
|
801
|
@hostcollections[options[:name]] = options[:id]
|
802
|
end
|
803
|
result = options[:name]
|
804
|
end
|
805
|
|
806
|
result
|
807
|
end
|
808
|
|
809
|
def katello_product(organization, options = {})
|
810
|
@products ||= {}
|
811
|
@products[organization] ||= {}
|
812
|
|
813
|
if options[:name]
|
814
|
return nil if options[:name].nil? || options[:name].empty?
|
815
|
options[:id] = @products[organization][options[:name]]
|
816
|
if !options[:id]
|
817
|
@api.resource(:products).call(:index,
|
818
|
{
|
819
|
:per_page => 999999,
|
820
|
'organization_id' => foreman_organization(:name => organization),
|
821
|
'search' => search_string('host-collections',options[:name])
|
822
|
})['results'].each do |product|
|
823
|
@products[organization][product['name']] = product['id'] if product
|
824
|
end
|
825
|
options[:id] = @products[organization][options[:name]]
|
826
|
raise _("Host collection '%{name}' not found") % {:name => options[:name]} if !options[:id]
|
827
|
end
|
828
|
result = options[:id]
|
829
|
else
|
830
|
return nil if options[:id].nil?
|
831
|
options[:name] = @products.key(options[:id])
|
832
|
if !options[:name]
|
833
|
product = @api.resource(:host_collections).call(:show, {'id' => options[:id]})
|
834
|
raise _("Host collection 'id=%{id}' not found") % {:id => options[:id]} if !product || product.empty?
|
835
|
options[:name] = product['name']
|
836
|
@products[options[:name]] = options[:id]
|
837
|
end
|
838
|
result = options[:name]
|
839
|
end
|
840
|
|
841
|
result
|
842
|
end
|
843
|
|
844
|
def foreman_container(options = {})
|
845
|
@containers ||= {}
|
846
|
|
847
|
if options[:name]
|
848
|
return nil if options[:name].nil? || options[:name].empty?
|
849
|
options[:id] = @containers[options[:name]]
|
850
|
if !options[:id]
|
851
|
container = @api.resource(:containers).call(:index, {
|
852
|
:per_page => 999999,
|
853
|
'search' => "name=\"#{options[:name]}\""
|
854
|
})['results']
|
855
|
raise _("Container '%{name}' not found") % {:name => options[:name]} if !container || container.empty?
|
856
|
options[:id] = container[0]['id']
|
857
|
@containers[options[:name]] = options[:id]
|
858
|
end
|
859
|
result = options[:id]
|
860
|
else
|
861
|
return nil if options[:id].nil?
|
862
|
options[:name] = @containers.key(options[:id])
|
863
|
if !options[:name]
|
864
|
container = @api.resource(:containers).call(:show, {'id' => options[:id]})
|
865
|
raise _("Container 'id=%{id}' not found") % {:id => options[:id]} if !container || container.empty?
|
866
|
options[:name] = container['name']
|
867
|
@containers[options[:name]] = options[:id]
|
868
|
end
|
869
|
result = options[:name]
|
870
|
end
|
871
|
|
872
|
result
|
873
|
end
|
874
|
|
875
|
|
876
|
def build_os_name(name, major, minor)
|
877
|
name += " #{major}" if major && major != ''
|
878
|
name += ".#{minor}" if minor && minor != ''
|
879
|
name
|
880
|
end
|
881
|
|
882
|
|
883
|
|
884
|
def split_os_name(name)
|
885
|
tokens = name.split(' ')
|
886
|
is_number = Float(tokens[-1]) rescue false
|
887
|
if is_number
|
888
|
(major, minor) = tokens[-1].split('.').flatten
|
889
|
name = tokens[0...-1].join(' ')
|
890
|
else
|
891
|
name = tokens.join(' ')
|
892
|
end
|
893
|
[name, major || '', minor || '']
|
894
|
end
|
895
|
|
896
|
def export_column(object, name, field=nil)
|
897
|
return '' unless object[name]
|
898
|
values = CSV.generate do |column|
|
899
|
column << object[name].collect do |fields|
|
900
|
field.nil? ? yield(fields) : fields[field]
|
901
|
end
|
902
|
end
|
903
|
values.delete!("\n")
|
904
|
end
|
905
|
|
906
|
def collect_column(column)
|
907
|
return [] if column.nil? || column.empty?
|
908
|
CSV.parse_line(column, {:skip_blanks => true}).collect do |value|
|
909
|
yield value
|
910
|
end
|
911
|
end
|
912
|
|
913
|
def pluralize(name)
|
914
|
case name
|
915
|
when /smart_proxy/
|
916
|
'smart_proxies'
|
917
|
else
|
918
|
"#{name}s"
|
919
|
end
|
920
|
end
|
921
|
|
922
|
def associate_organizations(id, organizations, name)
|
923
|
return if organizations.nil?
|
924
|
|
925
|
associations ||= {}
|
926
|
CSV.parse_line(organizations).each do |organization|
|
927
|
organization_id = foreman_organization(:name => organization)
|
928
|
if associations[organization].nil?
|
929
|
associations[organization] = @api.resource(:organizations).call(:show, {'id' => organization_id})[pluralize(name)].collect do |reference_object|
|
930
|
reference_object['id']
|
931
|
end
|
932
|
end
|
933
|
associations[organization] += [id] if !associations[organization].include? id
|
934
|
@api.resource(:organizations).call(:update, {
|
935
|
'id' => organization_id,
|
936
|
'organization' => {
|
937
|
"#{name}_ids" => associations[organization]
|
938
|
}
|
939
|
})
|
940
|
end if organizations && !organizations.empty?
|
941
|
end
|
942
|
|
943
|
def associate_locations(id, locations, name)
|
944
|
return if locations.nil?
|
945
|
|
946
|
associations ||= {}
|
947
|
CSV.parse_line(locations).each do |location|
|
948
|
location_id = foreman_location(:name => location)
|
949
|
if associations[location].nil?
|
950
|
associations[location] = @api.resource(:locations).call(:show, {'id' => location_id})[pluralize(name)].collect do |reference_object|
|
951
|
reference_object['id']
|
952
|
end
|
953
|
end
|
954
|
associations[location] += [id] if !associations[location].include? id
|
955
|
|
956
|
@api.resource(:locations).call(:update, {
|
957
|
'id' => location_id,
|
958
|
'location' => {
|
959
|
"#{name}_ids" => associations[location]
|
960
|
}
|
961
|
})
|
962
|
end if locations && !locations.empty?
|
963
|
end
|
964
|
|
965
|
def apipie_check_param(resource, action, name)
|
966
|
method = @api.resource(pluralize(resource).to_sym).apidoc[:methods].detect do |api_method|
|
967
|
api_method[:name] == action.to_s
|
968
|
end
|
969
|
return false unless method
|
970
|
|
971
|
found = method[:params].detect do |param|
|
972
|
param[:full_name] == name
|
973
|
end
|
974
|
if !found
|
975
|
nested = method[:params].detect do |param|
|
976
|
param[:name] == resource.to_s
|
977
|
end
|
978
|
if nested
|
979
|
found = nested[:params].detect do |param|
|
980
|
param[:full_name] == name
|
981
|
end
|
982
|
end
|
983
|
end
|
984
|
found
|
985
|
end
|
986
|
|
987
|
def count(value)
|
988
|
return 1 if value.nil? || value.empty?
|
989
|
value.to_i
|
990
|
end
|
991
|
|
992
|
private
|
993
|
|
994
|
def search_string(resource, name)
|
995
|
operator = case resource
|
996
|
when "gpg-key", "sync-plan", "lifecycle-environment", "host-collections"
|
997
|
@server_status['version'] && @server_status['version'].match(/\A1\.6/) ? ':' : '='
|
998
|
else
|
999
|
':'
|
1000
|
end
|
1001
|
"name#{operator}\"#{name}\""
|
1002
|
end
|
1003
|
end
|
1004
|
end
|