1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
12
|
|
13
|
|
14
|
|
15
|
|
16
|
|
17
|
|
18
|
|
19
|
|
20
|
|
21
|
|
22
|
|
23
|
require 'hammer_cli'
|
24
|
require 'json'
|
25
|
require 'csv'
|
26
|
require 'uri'
|
27
|
|
28
|
module HammerCLICsv
|
29
|
class CsvCommand
|
30
|
class SubscriptionsCommand < BaseCommand
|
31
|
command_name 'subscriptions'
|
32
|
desc 'import or export subscriptions'
|
33
|
|
34
|
ORGANIZATION = 'Organization'
|
35
|
MANIFEST = 'Manifest File'
|
36
|
CONTENT_SET = 'Content Set'
|
37
|
ARCH = 'Arch'
|
38
|
RELEASE = 'Release'
|
39
|
|
40
|
def export
|
41
|
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
|
42
|
csv << [NAME, COUNT, ORGANIZATION, MANIFEST, CONTENT_SET, ARCH, RELEASE]
|
43
|
@api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
|
44
|
@api.resource(:products).call(:index, {
|
45
|
'per_page' => 999999,
|
46
|
'organization_id' => foreman_organization(:name => organization['name']),
|
47
|
'enabled' => true
|
48
|
})['results'].each do |product|
|
49
|
if product['provider']['name'] == 'Red Hat'
|
50
|
product['product_content'].each do |product_content|
|
51
|
if product_content['enabled']
|
52
|
puts product_content
|
53
|
content_set = product_content['content']['name']
|
54
|
release = '?????'
|
55
|
arches = product_content['content']['arches']
|
56
|
if arches.nil?
|
57
|
csv << [product['name'], 1, organization['name'], nil, content_set, nil, release]
|
58
|
else
|
59
|
arches.split(',').each do |arch|
|
60
|
csv << [product['name'], 1, organization['name'], nil, content_set, arch, release]
|
61
|
end
|
62
|
end
|
63
|
end
|
64
|
end
|
65
|
end
|
66
|
end
|
67
|
end
|
68
|
end
|
69
|
end
|
70
|
|
71
|
def import
|
72
|
thread_import do |line|
|
73
|
if line[MANIFEST] && !line[MANIFEST].empty?
|
74
|
import_manifest_from_csv(line)
|
75
|
else
|
76
|
enable_products_from_csv(line)
|
77
|
end
|
78
|
end
|
79
|
end
|
80
|
|
81
|
def enable_products_from_csv(line)
|
82
|
results = @api.resource(:products).call(:index, {
|
83
|
'per_page' => 999999,
|
84
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
85
|
'name' => line[NAME]
|
86
|
})['results']
|
87
|
raise "No match for product '#{line[NAME]}'" if results.length == 0
|
88
|
raise "Multiple matches for product '#{line[NAME]}'" if results.length != 1
|
89
|
product = results[0]
|
90
|
|
91
|
results = @api.resource(:repository_sets).call(:index, {
|
92
|
'per_page' => 999999,
|
93
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
94
|
'product_id' => product['id'],
|
95
|
'name' => line[CONTENT_SET]
|
96
|
})['results']
|
97
|
raise "No match for content set '#{line[CONTENT_SET]}'" if results.length == 0
|
98
|
raise "Multiple matches for content set '#{line[CONTENT_SET]}'" if results.length != 1
|
99
|
repository_set = results[0]
|
100
|
|
101
|
repository = repository_set['repositories'].find do |repo|
|
102
|
repo['name'].end_with?("#{line[ARCH]} #{line[RELEASE]}")
|
103
|
end
|
104
|
|
105
|
if repository.nil?
|
106
|
print "Enabling repository #{line[CONTENT_SET]} #{line[ARCH]} #{line[RELEASE]}..." if option_verbose?
|
107
|
product_content = product['product_content'].find do |content|
|
108
|
content['content']['name'] == line[CONTENT_SET]
|
109
|
end
|
110
|
raise "No match for content set '#{line[CONTENT_SET]}'" if !product_content
|
111
|
|
112
|
@api.resource(:repository_sets).call(:enable, {
|
113
|
'id' => product_content['content']['id'],
|
114
|
'product_id' => product['id'],
|
115
|
'basearch' => line[ARCH],
|
116
|
'releasever' => line[RELEASE]
|
117
|
})
|
118
|
puts 'done' if option_verbose?
|
119
|
else
|
120
|
puts "Repository #{repository['name']} already enabled" if option_verbose?
|
121
|
end
|
122
|
end
|
123
|
|
124
|
def import_manifest_from_csv(line)
|
125
|
|
126
|
args = %W{ subscription upload --file #{ line[MANIFEST] }
|
127
|
--organization-id #{ foreman_organization(:name => line[ORGANIZATION]) } }
|
128
|
hammer.run(args)
|
129
|
|
130
|
rescue RuntimeError => e
|
131
|
raise "#{e}\n #{line}"
|
132
|
end
|
133
|
end
|
134
|
end
|
135
|
end
|