hammer-cli-csv / lib / hammer_cli_csv / products.rb @ f8ecc788
1 |
# Copyright 2013-2014 Red Hat, Inc.
|
---|---|
2 |
#
|
3 |
# This software is licensed to you under the GNU General Public
|
4 |
# License as published by the Free Software Foundation; either version
|
5 |
# 2 of the License (GPLv2) or (at your option) any later version.
|
6 |
# There is NO WARRANTY for this software, express or implied,
|
7 |
# including the implied warranties of MERCHANTABILITY,
|
8 |
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
|
9 |
# have received a copy of GPLv2 along with this software; if not, see
|
10 |
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
|
11 |
|
12 |
#
|
13 |
# -= Systems CSV =-
|
14 |
#
|
15 |
# Columns
|
16 |
# Name
|
17 |
# - System name
|
18 |
# - May contain '%d' which will be replaced with current iteration number of Count
|
19 |
# - eg. "os%d" -> "os1"
|
20 |
# Count
|
21 |
# - Number of times to iterate on this line of the CSV file
|
22 |
|
23 |
require 'hammer_cli'
|
24 |
require 'json'
|
25 |
require 'csv'
|
26 |
require 'uri'
|
27 |
|
28 |
module HammerCLICsv |
29 |
class ProductsCommand < BaseCommand |
30 |
LABEL = 'Label' |
31 |
ORGANIZATION = 'Organization' |
32 |
REPOSITORY = 'Repository' |
33 |
REPOSITORY_TYPE = 'Repository Type' |
34 |
REPOSITORY_URL = 'Repository Url' |
35 |
DESCRIPTION = 'Description' |
36 |
|
37 |
def export |
38 |
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv| |
39 |
csv << [NAME, COUNT, LABEL, ORGANIZATION, REPOSITORY, REPOSITORY_TYPE, REPOSITORY_URL] |
40 |
@api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization| |
41 |
@api.resource(:products).call(:index, { |
42 |
'per_page' => 999999, |
43 |
'enabled' => true, |
44 |
'organization_id' => katello_organization(:name => organization['name']) |
45 |
})['results'].each do |product| |
46 |
# product = @api.resource(:products).call(:show, {
|
47 |
# 'id' => product['id'],
|
48 |
# 'fields' => 'full'
|
49 |
# })
|
50 |
product['library_repositories'].each do |repository| |
51 |
if repository['sync_state'] != 'not_synced' |
52 |
repository_type = "#{repository['product_type'] == 'custom' ? 'Custom' : 'Red Hat'} #{repository['content_type'].capitalize}"
|
53 |
csv << [namify(product['name'], 1), 1, product['label'], organization['name'], repository['name'], |
54 |
repository_type, repository['feed']]
|
55 |
#puts " HTTPS: #{repository['unprotected'] ? 'No' : 'Yes'}"
|
56 |
end
|
57 |
end
|
58 |
|
59 |
end
|
60 |
end
|
61 |
end
|
62 |
end
|
63 |
|
64 |
def import |
65 |
@existing_products = {}
|
66 |
@existing_repositories = {}
|
67 |
|
68 |
thread_import do |line|
|
69 |
create_products_from_csv(line) |
70 |
end
|
71 |
end
|
72 |
|
73 |
def create_products_from_csv(line) |
74 |
if !@existing_products[line[ORGANIZATION]] |
75 |
@existing_products[line[ORGANIZATION]] = {} |
76 |
@api.resource(:products).call(:index, { |
77 |
'organization_id' => katello_organization(:name => line[ORGANIZATION]), |
78 |
'page_size' => 999999, |
79 |
'paged' => true |
80 |
})['results'].each do |product| |
81 |
@existing_products[line[ORGANIZATION]][product['name']] = product['id'] if product |
82 |
|
83 |
if product
|
84 |
@api.resource(:repositories).call(:index, { |
85 |
'organization_id' => katello_organization(:name => line[ORGANIZATION]), |
86 |
'product_id' => product['id'], |
87 |
'enabled' => true, |
88 |
'library' => true, |
89 |
'page_size' => 999999, 'paged' => true |
90 |
})['results'].each do |repository| |
91 |
@existing_repositories[line[ORGANIZATION] + product['name']] ||= {} |
92 |
@existing_repositories[line[ORGANIZATION] + product['name']][repository['label']] = repository['id'] |
93 |
end
|
94 |
end
|
95 |
end
|
96 |
end
|
97 |
|
98 |
# Only creating products, not updating
|
99 |
line[COUNT].to_i.times do |number| |
100 |
name = namify(line[NAME], number)
|
101 |
puts @existing_products
|
102 |
product_id = @existing_products[line[ORGANIZATION]][name] |
103 |
if !product_id
|
104 |
print "Creating product '#{name}'..." if option_verbose? |
105 |
if line[REPOSITORY_TYPE] =~ /Red Hat/ |
106 |
raise "Red Hat product '#{name}' does not exist in '#{line[ORGANIZATION]}'"
|
107 |
end
|
108 |
|
109 |
product_id = @api.resource(:products).call(:create, { |
110 |
'organization_id' => katello_organization(:name => line[ORGANIZATION]), |
111 |
'name' => name
|
112 |
})['id']
|
113 |
@existing_products[line[ORGANIZATION]][name] = product_id |
114 |
else
|
115 |
# Nothing to update for products
|
116 |
print "Updating product '#{name}'..." if option_verbose? |
117 |
end
|
118 |
@existing_repositories[line[ORGANIZATION] + name] = {} |
119 |
print "done\n" if option_verbose? |
120 |
|
121 |
repository_name = namify(line[REPOSITORY], number)
|
122 |
|
123 |
# Hash the existing repositories for the product
|
124 |
if !@existing_repositories[line[ORGANIZATION] + name][repository_name] |
125 |
@api.resource(:repositories).call(:index, { |
126 |
'organization_id' => katello_organization(:name => line[ORGANIZATION]), |
127 |
'library' => true, |
128 |
'all' => false, |
129 |
'product_id' => product_id
|
130 |
})['results'].each do |repository| |
131 |
@existing_repositories[line[ORGANIZATION] + name][repository['name']] = repository |
132 |
end
|
133 |
end
|
134 |
|
135 |
if !@existing_repositories[line[ORGANIZATION] + name][repository_name] |
136 |
print "Creating repository '#{repository_name}' in product '#{name}'..." if option_verbose? |
137 |
if line[REPOSITORY_TYPE] =~ /Red Hat/ |
138 |
# TMP
|
139 |
puts "TMP"
|
140 |
@api.resource(:repositories).call(:index, { |
141 |
'organization_id' => katello_organization(:name => line[ORGANIZATION]), |
142 |
'library' => true, |
143 |
'all' => true, |
144 |
'product_id' => product_id
|
145 |
})['results'].each do |repository| |
146 |
puts repository if repository['name'] == repository_name |
147 |
end
|
148 |
puts "END TMP"
|
149 |
#repository_id = redhat_create_repository(product_id, repository_name)
|
150 |
|
151 |
else
|
152 |
repository_id = @api.resource(:repositories).call(:create, { |
153 |
'organization_id' => katello_organization(:name => line[ORGANIZATION]), |
154 |
'name' => repository_name,
|
155 |
'label' => labelize(repository_name),
|
156 |
'product_id' => product_id,
|
157 |
'url' => line[REPOSITORY_URL], |
158 |
'content_type' => content_type(line[REPOSITORY_TYPE]) |
159 |
})['id']
|
160 |
end
|
161 |
@existing_repositories[line[ORGANIZATION] + name][line[LABEL]] = repository_id |
162 |
|
163 |
puts "TODO: skipping sync"
|
164 |
# task_id = @api.resource(:repositories).call(:sync, {
|
165 |
# 'organization_id' => katello_organization(:name => line[ORGANIZATION]),
|
166 |
# 'id' => repository_id
|
167 |
# })['id']
|
168 |
# TODO: wait for sync task
|
169 |
print "done\n" if option_verbose? |
170 |
end
|
171 |
end
|
172 |
|
173 |
rescue RuntimeError => e |
174 |
raise "#{e}\n #{line}"
|
175 |
end
|
176 |
|
177 |
private |
178 |
|
179 |
def redhat_create_repository(product_id, repository_name) |
180 |
@api.resource(:repository_sets).call(:index, { |
181 |
'product_id' => product_id
|
182 |
})['results'].each do |repository| |
183 |
puts repository |
184 |
puts @api.resource(:repository_sets).call(:show, { |
185 |
'product_id' => product_id,
|
186 |
'id' => repository['id']}) |
187 |
return
|
188 |
if repository['name'] == repository_name |
189 |
puts repository |
190 |
@api.resource(:repository_sets).call(:enable, { |
191 |
'product_id' => product_id,
|
192 |
'repository_id' => repository['id'] |
193 |
}) |
194 |
return
|
195 |
end
|
196 |
raise "Repository '#{repository_name}' does not exist"
|
197 |
end
|
198 |
end
|
199 |
|
200 |
def content_type(repository_type) |
201 |
case repository_type
|
202 |
when /Yum/ |
203 |
'yum'
|
204 |
when /Puppet/ |
205 |
'puppet'
|
206 |
else
|
207 |
raise "Unrecognized repository type '#{repository_type}'"
|
208 |
end
|
209 |
end
|
210 |
end
|
211 |
|
212 |
HammerCLICsv::CsvCommand.subcommand('products', |
213 |
'import or export products',
|
214 |
HammerCLICsv::ProductsCommand) |
215 |
end
|