Class: HammerCLICsv::CsvCommand::ContentHostsCommand
- Inherits:
-
BaseCommand
- Object
- HammerCLI::Apipie::Command
- BaseCommand
- HammerCLICsv::CsvCommand::ContentHostsCommand
show all
- Includes:
- HammerCLIForemanTasks::Helper
- Defined in:
- lib/hammer_cli_csv/content_hosts.rb
Constant Summary
collapse
- ORGANIZATION =
'Organization'
- ENVIRONMENT =
'Environment'
- CONTENTVIEW =
'Content View'
- HOSTCOLLECTIONS =
'Host Collections'
- VIRTUAL =
'Virtual'
- HOST =
'Host'
- OPERATINGSYSTEM =
'OS'
- ARCHITECTURE =
'Arch'
- SOCKETS =
'Sockets'
- RAM =
'RAM'
- CORES =
'Cores'
- SLA =
'SLA'
- PRODUCTS =
'Products'
- SUBSCRIPTIONS =
'Subscriptions'
Constants inherited
from BaseCommand
BaseCommand::COUNT, BaseCommand::NAME
Instance Method Summary
collapse
Methods inherited from BaseCommand
#apipie_check_param, #associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #count, #execute, #export_column, #foreman_architecture, #foreman_container, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_host, #foreman_hostgroup, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_provisioning_template, #foreman_role, #foreman_smart_proxy, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import
Instance Method Details
#create_content_hosts_from_csv(line) ⇒ Object
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
|
# File 'lib/hammer_cli_csv/content_hosts.rb', line 179
def create_content_hosts_from_csv(line)
return if option_organization && line[ORGANIZATION] != option_organization
if !@existing[line[ORGANIZATION]]
@existing[line[ORGANIZATION]] = true
total = @api.resource(:hosts).call(:index, {
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
'per_page' => 1
})['total'].to_i
(total / 20 + 2).to_i.times do |page|
@api.resource(:hosts).call(:index, {
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
'page' => page + 1,
'per_page' => 20
})['results'].each do |host|
@existing[host['name']] = {
:host => host['id'],
:subscription => host['subscription']['id'],
:content => host['content']['id']
}
end
end
end
count(line[COUNT]).times do |number|
name = namify(line[NAME], number)
if !@existing.include? name
print(_("Creating content host '%{name}'...") % {:name => name}) if option_verbose?
host_id = @api.resource(:host_subscriptions).call(:register, {
'name' => name,
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
'lifecycle_environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
'facts' => facts(name, line),
'installed_products' => products(line),
'service_level' => line[SLA],
'type' => 'system'
})['id']
@existing[name] = host_id
else
print(_("Updating content host '%{name}'...") % {:name => name}) if option_verbose?
host_id = @api.resource(:hosts).call(:update, {
'id' => @existing[name][:host],
'host' => {
'name' => name,
'content_facet_attributes' => {
'id' => @existing[name][:content],
'lifecycle_environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW])
},
'subscription_facet_attributes' => {
'id' => @existing[name][:subscription],
'facts' => facts(name, line),
'service_level' => line[SLA]
}
}
})['host_id']
end
if line[VIRTUAL] == 'Yes' && line[HOST]
raise "Content host '#{line[HOST]}' not found" if !@existing[line[HOST]]
@hypervisor_guests[@existing[line[HOST]]] ||= []
@hypervisor_guests[@existing[line[HOST]]] << @existing[name]
end
update_host_facts(host_id, line)
update_host_collections(host_id, line)
update_subscriptions(host_id, line)
puts _('done') if option_verbose?
end
rescue RuntimeError => e
raise "#{e}\n #{line}"
end
|
#export ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/hammer_cli_csv/content_hosts.rb', line 24
def export
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
csv << [NAME, ORGANIZATION, ENVIRONMENT, CONTENTVIEW, HOSTCOLLECTIONS, VIRTUAL, HOST,
OPERATINGSYSTEM, ARCHITECTURE, SOCKETS, RAM, CORES, SLA, PRODUCTS, SUBSCRIPTIONS]
if @server_status['release'] == 'Headpin'
export_sam csv
else
export_foretello csv
end
end
end
|
#export_foretello(csv) ⇒ Object
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
|
# File 'lib/hammer_cli_csv/content_hosts.rb', line 89
def export_foretello(csv)
@api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
next if option_organization && organization['name'] != option_organization
@api.resource(:systems).call(:index, {
'per_page' => 999999,
'organization_id' => foreman_organization(:name => organization['name'])
})['results'].each do |host|
host = @api.resource(:systems).call(:show, {
'id' => host['uuid'],
'fields' => 'full'
})
name = host['name']
organization_name = organization['name']
environment = host['environment']['label']
contentview = host['content_view']['name']
hostcollections = CSV.generate do |column|
column << host['hostCollections'].collect do |hostcollection|
hostcollection['name']
end
end
hostcollections.delete!("\n")
virtual = host['facts']['virt.is_guest'] == 'true' ? 'Yes' : 'No'
hypervisor_host = host['virtual_host'].nil? ? nil : host['virtual_host']['name']
operatingsystem = "#{host['facts']['distribution.name']} " if host['facts']['distribution.name']
operatingsystem += host['facts']['distribution.version'] if host['facts']['distribution.version']
architecture = host['facts']['uname.machine']
sockets = host['facts']['cpu.cpu_socket(s)']
ram = host['facts']['memory.memtotal']
cores = host['facts']['cpu.core(s)_per_socket'] || 1
sla = ''
products = CSV.generate do |column|
column << host['installedProducts'].collect do |product|
"#{product['productId']}|#{product['productName']}"
end
end
products.delete!("\n")
subscriptions = CSV.generate do |column|
column << @api.resource(:subscriptions).call(:index, {
'organization_id' => organization['id'],
'system_id' => host['uuid']
})['results'].collect do |subscription|
"#{subscription['consumed']}|#{subscription['product_id']}|#{subscription['product_name']}"
end
end
subscriptions.delete!("\n")
csv << [name, organization_name, environment, contentview, hostcollections, virtual, hypervisor_host,
operatingsystem, architecture, sockets, ram, cores, sla, products, subscriptions]
end
end
end
|
#export_sam(csv) ⇒ Object
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
|
# File 'lib/hammer_cli_csv/content_hosts.rb', line 36
def export_sam(csv)
guests_hypervisor = {}
host_ids = []
@headpin.get(:organizations).each do |organization|
next if option_organization && organization['name'] != option_organization
host_ids = @headpin.get("organizations/#{organization['label']}/systems").collect do |host|
host['guests'].each { |guest| guests_hypervisor[guest['uuid']] = host['name'] }
host['uuid']
end
end
host_ids.each do |host_id|
host = @headpin.get("systems/#{host_id}")
host_subscriptions = @headpin.get("systems/#{host_id}/subscriptions")['entitlements']
name = host['name']
organization_name = host['owner']['displayName']
environment = host['environment']['name']
contentview = host['content_view']['name']
hostcollections = nil
virtual = host['facts']['virt.is_guest'] == 'true' ? 'Yes' : 'No'
hypervisor = guests_hypervisor[host['uuid']]
if host['facts']['distribution.name']
operatingsystem = "#{host['facts']['distribution.name']} "
operatingsystem += host['facts']['distribution.version'] if host['facts']['distribution.version']
operatingsystem.strip!
end
architecture = host['facts']['uname.machine']
sockets = host['facts']['cpu.cpu_socket(s)']
ram = host['facts']['memory.memtotal']
cores = host['facts']['cpu.core(s)_per_socket'] || 1
sla = host['serviceLevel']
products = CSV.generate do |column|
column << host['installedProducts'].collect do |product|
"#{product['productId']}|#{product['productName']}"
end
end
products.delete!("\n")
subscriptions = CSV.generate do |column|
column << host_subscriptions.collect do |subscription|
"#{subscription['quantity']}|#{subscription['productId']}|#{subscription['poolName']}"
end
end
subscriptions.delete!("\n")
csv << [name, organization_name, environment, contentview, hostcollections, virtual, hypervisor,
operatingsystem, architecture, sockets, ram, cores, sla, products, subscriptions]
end
end
|
#import ⇒ Object
142
143
144
145
146
147
148
149
|
# File 'lib/hammer_cli_csv/content_hosts.rb', line 142
def import
remote = @server_status['plugins'].detect { |plugin| plugin['name'] == 'foreman_csv' }
if remote.nil?
import_locally
else
import_remotely
end
end
|
#import_locally ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/hammer_cli_csv/content_hosts.rb', line 157
def import_locally
@existing = {}
@hypervisor_guests = {}
thread_import do |line|
create_content_hosts_from_csv(line)
end
if !@hypervisor_guests.empty?
print(_('Updating hypervisor and guest associations...')) if option_verbose?
@hypervisor_guests.each do |host_id, guest_ids|
@api.resource(:hosts).call(:update, {
'id' => host_id,
'host' => {
'guest_ids' => guest_ids
}
})
end
puts _('done') if option_verbose?
end
end
|
#import_remotely ⇒ Object
151
152
153
154
155
|
# File 'lib/hammer_cli_csv/content_hosts.rb', line 151
def import_remotely
params = {'content' => ::File.new(::File.expand_path(option_file), 'rb')}
= {:content_type => 'multipart/form-data', :multipart => true}
task_progress(@api.resource(:csv).call(:import_content_hosts, params, ))
end
|