-
Notifications
You must be signed in to change notification settings - Fork 83
/
content_view_version.rb
548 lines (431 loc) · 17.9 KB
/
content_view_version.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
require 'fileutils'
module HammerCLIKatello
class ContentViewVersion < HammerCLIKatello::Command
resource :content_view_versions
command_name 'version'
desc 'View and manage content view versions'
class ListCommand < HammerCLIKatello::ListCommand
include LifecycleEnvironmentNameMapping
output do
field :id, _("ID")
field :name, _("Name")
field :version, _("Version")
field :description, _("Description")
field :environments, _("Lifecycle Environments"), Fields::List
end
def extend_data(version)
version['environments'] = version['environments'].map { |e| e["name"] }
version
end
build_options do |o|
o.expand(:all).including(:organizations)
end
extend_with(HammerCLIKatello::CommandExtensions::LifecycleEnvironment.new)
end
class InfoCommand < HammerCLIKatello::InfoCommand
include LifecycleEnvironmentNameMapping
resource :content_view_versions, :show
output do
field :id, _("ID")
field :name, _("Name")
field :version, _("Version")
field :description, _("Description")
from :content_view do
field :id, _("Content View ID")
field :name, _("Content View Name")
field :label, _("Content View Label")
end
collection :environments, _("Lifecycle Environments") do
field :id, _("ID")
field :name, _("Name")
field :label, _("Label")
end
collection :repositories, _("Repositories") do
field :id, _("ID")
field :name, _("Name")
field :label, _("Label")
end
collection :puppet_modules, _("Puppet Modules") do
field :id, _("ID")
field :name, _("Name")
field :author, _("Author")
field :version, _("Version")
end
end
build_options do |o|
o.expand(:all).including(:environments, :content_views, :organizations)
end
extend_with(HammerCLIKatello::CommandExtensions::LifecycleEnvironment.new)
end
class RepublishRepositoriesCommand < HammerCLIKatello::SingleResourceCommand
include HammerCLIForemanTasks::Async
action :republish_repositories
command_name "republish-repositories"
success_message _("Content View Version is being republished with task %{id}.")
failure_message _("Could not republish the Content View")
build_options do |o|
o.expand(:all).including(:content_views, :organizations)
end
end
class PromoteCommand < HammerCLIKatello::SingleResourceCommand
include HammerCLIForemanTasks::Async
include LifecycleEnvironmentNameMapping
action :promote
command_name "promote"
success_message _("Content view is being promoted with task %{id}.")
failure_message _("Could not promote the content view")
option "--force",
:flag,
_("force content view promotion and bypass lifecycle environment restriction")
option "--to-lifecycle-environment", "TO_ENVIRONMENT",
_("Name of the target environment"), :attribute_name => :option_to_environment_name
option "--to-lifecycle-environment-id", "TO_ENVIRONMENT_ID",
_("Id of the target environment"), :attribute_name => :option_to_environment_id
option "--from-lifecycle-environment", "FROM_ENVIRONMENT_ID",
_("Environment name from where to promote its version from (if version is unknown)"),
:attribute_name => :option_from_environment_name
option "--from-lifecycle-environment-id", "FROM_ENVIRONMENT_ID",
_(["Id of the environment from where to promote its version ",
"from (if version is unknown)"].join),
:attribute_name => :option_from_environment_id
option "--environment-ids", "ENVIRONMENT_IDS", _("Identifiers for Lifecycle Environment"),
format: HammerCLI::Options::Normalizers::List.new,
attribute_name: :option_environment_ids,
deprecated: { '--environment-ids' => _('Use --lifecycle-environment-ids instead') }
def environment_search_options
{
"option_id" => options["option_to_environment_id"],
"option_name" => options["option_to_environment_name"],
"option_organization_id" => options["option_organization_id"],
"option_organization_name" => options["option_organization_name"],
"option_organization_label" => options["option_organization_label"]
}
end
def request_params
params = super
params['environment_id'] = resolver.lifecycle_environment_id(environment_search_options)
params['force'] = option_force?
params
end
build_options do |o|
o.expand(:all).except(:environments).including(:content_views, :organizations)
o.without(:environment_id)
end
extend_with(
HammerCLIKatello::CommandExtensions::LifecycleEnvironments.new(only: :option_sources)
)
end
class DeleteCommand < HammerCLIKatello::DeleteCommand
include HammerCLIForemanTasks::Async
include LifecycleEnvironmentNameMapping
action :destroy
command_name "delete"
success_message _("Content view is being deleted with task %{id}.")
failure_message _("Could not delete the content view")
build_options do |o|
o.expand(:all).including(:environments, :content_views, :organizations)
end
extend_with(HammerCLIKatello::CommandExtensions::LifecycleEnvironment.new)
end
class UpdateCommand < HammerCLIKatello::UpdateCommand
action :update
command_name "update"
success_message _("Content view version updated.")
failure_message _("Could not update the content view version")
build_options do |o|
o.expand(:all).including(:environments, :content_views, :organizations)
end
extend_with(HammerCLIKatello::CommandExtensions::LifecycleEnvironment.new)
end
class IncrementalUpdate < HammerCLIKatello::Command
include HammerCLIForemanTasks::Async
action :incremental_update
command_name "incremental-update"
success_message _("Incremental update is being performed with task %{id}.")
failure_message _("An error occurred incrementally updating the content view")
option('--lifecycle-environment-ids',
'ENVIRONMENT_IDS',
_("list of lifecycle environment IDs to update the content view version in"),
:format => HammerCLI::Options::Normalizers::List.new
)
option('--lifecycle-environments',
'ENVIRONMENTS',
_("list of lifecycle environment names to update the content view version in"),
:format => HammerCLI::Options::Normalizers::List.new,
:attribute_name => :option_lifecycle_environment_names
)
option('--organization',
'ORGANIZATION_NAME',
_("Organization name for resolving lifecycle environment names"),
:attribute_name => :option_organization_name
)
option('--organization-id',
'ORGANIZATION_ID',
_("Organization id for resolving lifecycle environment names")
)
option('--update-all-hosts',
'UPDATE',
_('Update all editable and applicable hosts within the specified Content View and \
Lifecycle Environments'),
:format => HammerCLI::Options::Normalizers::Bool.new
)
option('--host-ids',
'HOST_IDS',
_("IDs of hosts to update"),
:format => HammerCLI::Options::Normalizers::List.new
)
validate_options :before, 'IdResolution' do
organization_options = [:option_organization_id, :option_organization_name]
if option(:option_lifecycle_environment_ids).exist?
any(*organization_options).rejected
option(:option_lifecycle_environment_names).rejected
end
if option(:option_lifecycle_environment_names).exist?
any(*organization_options).required
end
end
def request_params
params = super
params[:content_view_version_environments] = [
{
:content_view_version_id => option_content_view_version_id
}
]
if options.key?(HammerCLI.option_accessor_name(:lifecycle_environment_names)) ||
options.key?(HammerCLI.option_accessor_name(:lifecycle_environment_ids))
params[:content_view_version_environments][0][:environment_ids] =
resolver.environment_ids(options)
end
add_content = {}
params['add_content'].each do |key, value|
add_content[key] = value if options.key?(HammerCLI.option_accessor_name(key))
end
params = request_params_hosts(params)
params['add_content'] = add_content
params.delete('id')
params
end
def request_params_hosts(params)
if params['update_hosts'] && params['update_hosts']['included'] &&
params['update_hosts']['included'].key?('ids')
params['update_hosts'].delete('excluded')
else
params.delete('update_hosts')
end
if option_host_ids
params['update_hosts'] = {'included' => {'ids' => option_host_ids}}
elsif option_update_all_hosts
params['update_hosts'] = { 'included' => {:search => ''}}
end
params
end
build_options do |o|
o.expand(:all).except(:environments)
o.without(:content_view_version_environments, :ids, :update_hosts, :update_hosts_included)
end
end
class ExportDefaultCommand < HammerCLIForeman::Command
include HammerCLIKatello::LocalHelper
include HammerCLIKatello::ApipieHelper
PUBLISHED_REPOS_DIR = "/var/lib/pulp/published/yum/https/repos/".freeze
desc _('Export the library default content view')
command_name "export-default"
option '--export-dir', 'EXPORT_DIR', _("Directory to put content view version export into.")
validate_options do
option(:option_export_dir).required
end
build_options
def execute
export_dir = options['option_export_dir']
Dir.mkdir(export_dir) unless Dir.exist?(export_dir)
result = Kernel.system("rsync -aL #{PUBLISHED_REPOS_DIR} #{export_dir}")
if result == true
output.print_message _("Default content view export is available at #{export_dir}")
HammerCLI::EX_OK
else
output.print_error _("Could not export the default content view at #{export_dir}")
HammerCLI::EX_CANTCREAT
end
end
end
class LegacyExportCommand < HammerCLIKatello::SingleResourceCommand
include HammerCLIForemanTasks::Async
include LifecycleEnvironmentNameMapping
desc _('Export a content view (legacy method)')
action :export
command_name "export-legacy"
success_message _("Content view is being exported in task %{id}.")
failure_message _("Could not export the content view")
build_options do |o|
o.expand(:all).including(:environments, :content_views, :organizations)
end
extend_with(HammerCLIKatello::CommandExtensions::LifecycleEnvironment.new)
end
class ExportCommand < HammerCLIForeman::Command
include HammerCLIKatello::LocalHelper
include HammerCLIKatello::ApipieHelper
include HammerCLIKatello::CVImportExportHelper
PUBLISHED_REPOS_DIR = "/var/lib/pulp/published/yum/https/repos/".freeze
desc _('Export a content view version')
command_name "export"
success_message _("Content view export is available in %{directory}.")
failure_message _("Could not export the content view")
option "--id", "ID", _("Content View Version numeric identifier")
option '--export-dir', 'EXPORT_DIR', _("Directory to put content view version export into.")
validate_options do
option(:option_export_dir).required
option(:option_id).required
end
build_options
def execute
cvv = show(:content_view_versions, 'id' => options['option_id'])
cv = show(:content_views, 'id' => cvv['content_view_id'])
composite = cv["composite"]
export_json_options = { :cvv => cvv }
if composite
components = cv['components']
export_json_options[:component_cvvs] = components.collect do |component|
component['name']
end
export_json_options[:repositories] = []
else
repositories = fetch_exportable_cvv_repositories(cvv)
collect_packages(repositories)
export_json_options[:repositories] = repositories
end
json = export_json(export_json_options)
if repositories&.any? || cv['composite']
create_tar(cv, cvv, repositories, json)
else
msg = <<~MSG
Ensure the content view version '#{cvv['name']}' has at least one repository.
MSG
raise _(msg)
end
return HammerCLI::EX_OK
end
def create_tar(cv, cvv, repositories, json)
export_prefix = "export-#{cv['label']}-#{cvv['major']}.#{cvv['minor']}"
export_file = "#{export_prefix}.json"
export_repos_tar = "#{export_prefix}-repos.tar"
export_tar = "#{export_prefix}.tar"
export_dir = File.expand_path(options['option_export_dir'].to_s)
Dir.mkdir("#{export_dir}/#{export_prefix}")
if repositories&.any?
Dir.chdir(PUBLISHED_REPOS_DIR) do
repo_tar = "#{export_dir}/#{export_prefix}/#{export_repos_tar}"
repo_dirs = []
repositories.each do |repo|
repo_dirs.push(repo['relative_path'])
end
`tar cvfh #{repo_tar} #{repo_dirs.join(" ")}`
end
end
Dir.chdir("#{export_dir}/#{export_prefix}") do
File.open(export_file, 'w') do |file|
file.write(JSON.pretty_generate(json))
end
end
Dir.chdir(export_dir) do
`tar cf #{export_tar} #{export_prefix}`
FileUtils.rm_rf(export_prefix)
end
end
end
class ImportCommand < HammerCLIForeman::Command
include HammerCLIForemanTasks::Async
include HammerCLIKatello::LocalHelper
include HammerCLIKatello::ApipieHelper
include HammerCLIKatello::CVImportExportHelper
attr_accessor :export_tar_dir, :export_tar_file, :export_tar_prefix
desc _('Import a content view version')
command_name "import"
success_message _("Content view imported.")
failure_message _("Could not import the content view")
option "--organization-id", "ORGANIZATION_ID", _("Organization numeric identifier")
option(
'--export-tar', 'EXPORT_TAR',
_("Location of export tar on disk")
)
validate_options do
option(:option_export_tar).required
option(:option_organization_id).required
end
build_options
# rubocop:disable Metrics/AbcSize
def execute
unless File.exist?(options['option_export_tar'])
raise _("Export tar #{options['option_export_tar']} does not exist.")
end
import_tar_params = obtain_export_params(options['option_export_tar'])
untar_export(import_tar_params)
export_json = read_json(import_tar_params)
cv = content_view(export_json['name'], options['option_organization_id'])
major = export_json['major'].to_s
minor = export_json['minor'].to_s
import_checks(export_json, cv, major, minor)
if export_json['composite_components']
composite_version_ids = export_json['composite_components'].map do |component|
find_local_component_id(component)
end
update(:content_views, 'id' => cv['id'], 'component_ids' => composite_version_ids)
publish(cv['id'], export_json['major'], export_json['minor'])
else
sync_repositories(export_json['repositories'], options['option_organization_id'],
import_tar_params)
unless cv['default']
publish(
cv['id'], export_json['major'],
export_json['minor'], repos_units(export_json['repositories'])
)
end
end
return HammerCLI::EX_OK
end
# rubocop:enable Metrics/AbcSize
def sync_repositories(repositories, organization_id, options)
export_tar_dir = options[:dirname]
export_tar_prefix = options[:prefix]
repositories.each do |repo|
library_repos = index(
:repositories,
'organization_id' => organization_id,
'library' => true
)
library_repo = library_repos.select do |candidate_repo|
candidate_repo['label'] == repo['label']
end
library_repo = library_repo.first
if library_repo.nil?
msg = _("Unable to sync repositories, no library repository found for %s")
raise msg % repo['label']
end
synchronize(
library_repo['id'],
"file://#{export_tar_dir}/#{export_tar_prefix}/#{repo['relative_path']}"
)
end
end
def repos_units(repositories)
repositories.collect do |repo|
{
'label' => repo['label'],
'rpm_filenames' => repo['rpm_filenames']
}
end
end
def content_view(name, organization_id)
index(:content_views, 'name' => name, 'organization_id' => organization_id).first
end
def synchronize(id, source_url)
task_progress(call(:sync, :repositories, 'id' => id, 'source_url' => source_url))
end
def publish(id, major, minor, repos_units = nil)
params = {'id' => id, 'major' => major, 'minor' => minor }
params['repos_units'] = repos_units if repos_units
task_progress(call(:publish, :content_views, params))
end
end
autoload_subcommands
end
end