Revision 5da9eed3
Added by Thomas McKay almost 8 years ago
lib/hammer_cli_csv/content_hosts.rb | ||
---|---|---|
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
|
||
export_katello csv
|
||
end
|
||
end
|
||
|
||
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
|
||
|
||
def export_foretello(csv)
|
||
def export_katello(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, {
|
||
@api.resource(:hosts).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'
|
||
host = @api.resource(:hosts).call(:show, {
|
||
'id' => host['id']
|
||
})
|
||
host['facts'] ||= {}
|
||
|
||
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
|
||
if host['content_facet_attributes']
|
||
environment = host['content_facet_attributes']['lifecycle_environment']['name']
|
||
contentview = host['content_facet_attributes']['content_view']['name']
|
||
hostcollections = export_column(host['content_facet_attributes'], 'host_collections', 'name')
|
||
else
|
||
environment = nil
|
||
contentview = nil
|
||
hostcollections = nil
|
||
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
|
||
virtual = host['facts']['virt::is_guest'] == 'true' ? 'Yes' : 'No'
|
||
hypervisor_host = host['subscription_facet_attributes']['virtual_host'].nil? ? nil : host['subscription_facet_attributes']['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")
|
||
products = export_column(host['subscription_facet_attributes'], 'installed_products', 'productName')
|
||
subscriptions = CSV.generate do |column|
|
||
column << @api.resource(:subscriptions).call(:index, {
|
||
column << @api.resource(:host_subscriptions).call(:index, {
|
||
'organization_id' => organization['id'],
|
||
'system_id' => host['uuid']
|
||
'host_id' => host['id']
|
||
})['results'].collect do |subscription|
|
||
"#{subscription['consumed']}|#{subscription['product_id']}|#{subscription['product_name']}"
|
||
"#{subscription['quantity_consumed']}"\
|
||
"|#{subscription['product_id']}"\
|
||
"|#{subscription['product_name']}"\
|
||
"|#{subscription['contract_number']}|#{subscription['account_number']}"
|
||
end
|
||
end
|
||
subscriptions.delete!("\n")
|
||
... | ... | |
@hypervisor_guests = {}
|
||
|
||
thread_import do |line|
|
||
create_content_hosts_from_csv(line)
|
||
create_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
|
||
'id' => host_id,
|
||
'host' => {
|
||
'subscription_facet_attributes' => {
|
||
'hypervisor_guest_uuids' => guest_ids
|
||
}
|
||
}
|
||
})
|
||
end
|
||
puts _('done') if option_verbose?
|
||
end
|
||
end
|
||
|
||
def create_content_hosts_from_csv(line)
|
||
def create_from_csv(line)
|
||
return if option_organization && line[ORGANIZATION] != option_organization
|
||
|
||
if !@existing[line[ORGANIZATION]]
|
||
@existing[line[ORGANIZATION]] = true
|
||
# Fetching all content hosts is too slow and times out due to the complexity of the data
|
||
# rendered in the json.
|
||
# http://projects.theforeman.org/issues/6307
|
||
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
|
||
update_existing(line)
|
||
|
||
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
|
||
params = {
|
||
'name' => name,
|
||
'facts' => facts(name, line),
|
||
'lifecycle_environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
|
||
'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
|
||
'installed_products' => products(line),
|
||
'service_level' => line[SLA]
|
||
}
|
||
host = @api.resource(:host_subscriptions).call(:create, params)
|
||
@existing[name] = host['id']
|
||
else
|
||
# TODO: remove passing facet IDs to update
|
||
# Bug #13849 - updating a host's facet should not require the facet id to be included in facet params
|
||
# http://projects.theforeman.org/issues/13849
|
||
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),
|
||
# TODO: PUT /hosts subscription_facet_attributes missing "installed_products"
|
||
# http://projects.theforeman.org/issues/13854
|
||
#'installed_products' => products(line),
|
||
'service_level' => line[SLA]
|
||
}
|
||
params = {
|
||
'id' => @existing[name],
|
||
'host' => {
|
||
'content_facet_attributes' => {
|
||
'lifecycle_environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
|
||
'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW])
|
||
},
|
||
'subscription_facet_attributes' => {
|
||
'facts' => facts(name, line),
|
||
'installed_products' => products(line),
|
||
'service_level' => line[SLA]
|
||
}
|
||
})['host_id']
|
||
}
|
||
}
|
||
host = @api.resource(:hosts).call(:update, params)
|
||
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]
|
||
@hypervisor_guests[@existing[line[HOST]]] << "#{line[ORGANIZATION]}/#{name}"
|
||
end
|
||
|
||
update_host_facts(host_id, line)
|
||
update_host_collections(host_id, line)
|
||
update_subscriptions(host_id, line)
|
||
update_host_collections(host, line)
|
||
update_subscriptions(host, line)
|
||
|
||
puts _('done') if option_verbose?
|
||
end
|
||
... | ... | |
facts
|
||
end
|
||
|
||
def update_host_facts(host_id, line)
|
||
end
|
||
|
||
def update_host_collections(host_id, line)
|
||
def update_host_collections(host, line)
|
||
return nil if !line[HOSTCOLLECTIONS]
|
||
CSV.parse_line(line[HOSTCOLLECTIONS]).each do |hostcollection_name|
|
||
@api.resource(:host_collections).call(:add_hosts, {
|
||
'id' => katello_hostcollection(line[ORGANIZATION], :name => hostcollection_name),
|
||
'hosts_ids' => [host_id]
|
||
'host_ids' => [host['id']]
|
||
})
|
||
end
|
||
end
|
||
... | ... | |
|
||
def products(line)
|
||
return nil if !line[PRODUCTS]
|
||
products = CSV.parse_line(line[PRODUCTS]).collect do |product_details|
|
||
CSV.parse_line(line[PRODUCTS]).collect do |product_details|
|
||
product = {}
|
||
# TODO: these get passed straight through to candlepin; probably would be better to process in server
|
||
# to allow underscore product_id here
|
||
(product['productId'], product['productName']) = product_details.split('|')
|
||
(product['product_id'], product['product_name']) = product_details.split('|')
|
||
product['arch'] = line[ARCHITECTURE]
|
||
product['version'] = os_name_version(line[OPERATINGSYSTEM])[1]
|
||
product
|
||
end
|
||
products
|
||
end
|
||
|
||
def update_subscriptions(host_id, line)
|
||
def update_subscriptions(host, line)
|
||
existing_subscriptions = @api.resource(:host_subscriptions).call(:index, {
|
||
'host_id' => host_id
|
||
'host_id' => host['id']
|
||
})['results']
|
||
if existing_subscriptions.length != 0
|
||
@api.resource(:host_subscriptions).call(:remove_subscriptions, {
|
||
'host_id' => host_id,
|
||
'host_id' => host['id'],
|
||
'subscriptions' => existing_subscriptions
|
||
})
|
||
end
|
||
... | ... | |
return if line[SUBSCRIPTIONS].nil? || line[SUBSCRIPTIONS].empty?
|
||
|
||
subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |details|
|
||
(amount, sku, name) = details.split('|')
|
||
(amount, sku, name, contract, account) = details.split('|')
|
||
{
|
||
:id => katello_subscription(line[ORGANIZATION], :name => name),
|
||
:id => katello_subscription(line[ORGANIZATION], :name => name, :contract => contract,
|
||
:account => account),
|
||
:quantity => (amount.nil? || amount.empty? || amount == 'Automatic') ? 0 : amount.to_i
|
||
}
|
||
end
|
||
|
||
@api.resource(:host_subscriptions).call(:add_subscriptions, {
|
||
'host_id' => host_id,
|
||
'host_id' => host['id'],
|
||
'subscriptions' => subscriptions
|
||
})
|
||
end
|
||
|
||
def update_existing(line)
|
||
if !@existing[line[ORGANIZATION]]
|
||
@existing[line[ORGANIZATION]] = true
|
||
# Fetching all content hosts can be too slow and times so page
|
||
# http://projects.theforeman.org/issues/6307
|
||
total = @api.resource(:hosts).call(:index, {
|
||
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
||
'per_page' => 1
|
||
})['total'].to_i
|
||
(total / 20 + 1).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|
|
||
if host['subscription_facet_attributes']
|
||
@existing[host['name']] = host['id']
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|
test/content_hosts_test.rb | ||
---|---|---|
hammer.run(%W{csv content-hosts --verbose --file #{file.path}})
|
||
}
|
||
stderr.must_equal ''
|
||
stdout[0..-2].must_equal "Creating content host '#{hostname}'...done\nUpdating hypervisor and guest associations...done"
|
||
file.unlink
|
||
stdout[0..-2].must_equal "Creating content host '#{hostname}'...done"
|
||
|
||
# Update system
|
||
file = Tempfile.new('systems_test')
|
||
file.write("Name,Count,Organization,Environment,Content View,System Groups,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
|
||
file.write("#{hostname},1,Mega Corporation,Library,Default Organization View,Mega Corp HQ,No,,RHEL 6.4,x86_64,1,8,1,,,\n")
|
||
file.rewind
|
||
|
||
stdout,stderr = capture {
|
||
hammer.run(%W{csv content-hosts --verbose --csv-file #{file.path}})
|
||
hammer.run(%W{csv content-hosts --verbose --file #{file.path}})
|
||
}
|
||
stderr.must_equal ''
|
||
stdout[0..-2].must_equal "Updating content host '#{hostname}'...done\nUpdating hypervisor and guest associations...done"
|
||
stdout[0..-2].must_equal "Updating content host '#{hostname}'...done"
|
||
file.unlink
|
||
|
||
stdout,stderr = capture {
|
||
hammer.run(%W{organization list --search megacorp})
|
||
hammer.run(%W{organization list --search label=megacorp})
|
||
}
|
||
stdout.split("\n").length.must_equal 5
|
||
organization_id = stdout.split("\n")[3].split('|')[0].to_i
|
||
|
||
# Verify system
|
||
system = api.resource(:systems).call(:index, {
|
||
# Verify host
|
||
host = api.resource(:hosts).call(:index, {
|
||
'organization_id' => organization_id,
|
||
'search' => hostname
|
||
'search' => "name=#{hostname}"
|
||
})['results']
|
||
system.wont_be_nil
|
||
system.wont_be_empty
|
||
system[0]['name'].must_equal hostname
|
||
host.wont_be_nil
|
||
host.wont_be_empty
|
||
host[0]['name'].must_equal hostname
|
||
|
||
# Clean up
|
||
api.resource(:systems).call(:destroy, {
|
||
'id' => system[0]['id']
|
||
})
|
||
api.resource(:hosts).call(:destroy, {
|
||
'id' => host[0]['id']
|
||
})
|
||
end
|
||
|
||
end
|
test/data/content-hosts.csv | ||
---|---|---|
Name,Count,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions
|
||
host%d,1,Mega Corporation,Library,Default Organization View,"Mega Corp HQ",No,,RHEL 6.4,x86_64,1,4 GB,1,Standard,"69|Red Hat Enterprise Linux Server",
|
||
host%d,1,Mega Corporation,Library,Default Organization View,"Mega Corp HQ",No,,RHEL 6.4,x86_64,1,4 GB,1,Standard,"69|Red Hat Enterprise Linux Server,83|Red Hat Enterprise Linux High Availability (for RHEL Server)",
|
||
guest%d,4,Mega Corporation,Library,Default Organization View,"Mega Corp HQ,Accounting",Yes,host0,RHEL 6.4,x86_64,1,4 GB,1,Standard,"69|Red Hat Enterprise Linux Server",
|
||
dhcp129-%03d.megacorp.com,100,Mega Corporation,Library,Default Organization View,"Mega Corp HQ",No,,RHEL 6.4,x86_64,1,4 GB,1,Standard,"69|Red Hat Enterprise Linux Server",
|
||
xyz%d,4,Mega Corporation,Library,Default Organization View,"Mega Corp HQ,Accounting",Yes,,RHEL 6.4,x86_64,1,4 GB,1,Standard,"69|Red Hat Enterprise Linux Server",
|
||
|
test/data/hosts.csv | ||
---|---|---|
Name,Count,Organization,Location,Puppet Environment,Operating System,Architecture,MAC Address,Domain,Partition Table
|
||
dhcp130-%03d.megacorp.com,255,Mega Corporation,Asia Pacific,Development,RHEL 6.4,x86_64,"02:FE:B5:E0:70:%02x",megacorp.com,RedHat default
|
||
dhcp129-%03d.megacorp.com,255,Mega Corporation,Development,RHEL 6.4,x86_64,"01:FE:B5:E0:70:%02x",megacorp.com,RedHat default
|
||
# dhcp129-1%02d.megacorp.com,255,Mega Corporation,Development,RHEL 6.4,x86_64,"01:FE:B5:E1:70:%02x",megacorp.com,RedHat default
|
||
# dhcp129-2%02d.megacorp.com,255,Mega Corporation,Development,RHEL 6.4,x86_64,"01:FE:B5:E2:70:%02x",megacorp.com,RedHat default
|
||
# dhcp129-3%02d.megacorp.com,255,Mega Corporation,Development,RHEL 6.4,x86_64,"01:FE:B5:E3:70:%02x",megacorp.com,RedHat default
|
||
# dhcp129-4%02d.megacorp.com,255,Mega Corporation,Development,RHEL 6.3,x86_64,"01:54:00:E4:83:%02x",megacorp.com,RedHat default
|
||
# dhcp129-5%02d.megacorp.com,255,Mega Corporation,Development,RHEL 6.3,x86_64,"01:54:00:E5:83:%02x",megacorp.com,RedHat default
|
||
# dhcp129-6%02d.megacorp.com,255,Mega Corporation,Development,RHEL 6.3,x86_64,"01:54:00:E6:83:%02x",megacorp.com,RedHat default
|
||
# dhcp129-7%02d.megacorp.com,255,Mega Corporation,Development,RHEL 6.3,x86_64,"01:54:00:E7:83:%02x",megacorp.com,RedHat default
|
||
# dhcp129-8%02d.megacorp.com,255,Mega Corporation,Development,RHEL 6.3,x86_64,"01:54:00:E8:83:%02x",megacorp.com,RedHat default
|
||
# dhcp129-9%02d.megacorp.com,255,Mega Corporation,Development,RHEL 6.3,x86_64,"01:54:00:E9:83:%02x",megacorp.com,RedHat default
|
Also available in: Unified diff
fixes #14265 - import content hosts with hypervisors and guests