Project

General

Profile

« Previous | Next » 

Revision 9d9e4494

Added by Thomas McKay over 10 years ago

cv - provider, products, repo creation

View differences:

hammer-it
bundle exec hammer -v csv:hosts --verbose --csv-file test/data/hosts.csv
bundle exec hammer -v csv:puppetfacts --verbose --csv-file test/data/puppetfacts.csv
bundle exec hammer -v csv:products --verbose --csv-file test/data/products.csv
bundle exec hammer -v csv:systemgroups --verbose --csv-file test/data/systemgroups.csv
bundle exec hammer -v csv:systems --verbose --csv-file test/data/systems.csv
lib/hammer_cli_csv.rb
require 'hammer_cli_csv/users'
require 'hammer_cli_csv/puppet_facts'
require 'hammer_cli_csv/content_views'
require 'hammer_cli_csv/subscriptions'
require 'hammer_cli_csv/systems'
require 'hammer_cli_csv/system_groups'
require 'hammer_cli_csv/products'
end
lib/hammer_cli_csv/base.rb
NAME = 'Name'
COUNT = 'Count'
HEADERS = {'Accept' => 'version=2,application/json'}
option ["-v", "--verbose"], :flag, "be verbose"
option ['--threads'], 'THREAD_COUNT', 'Number of threads to hammer with', :default => 1
option ['--csv-export'], :flag, 'Export current data instead of importing'
......
@k_systemgroup_api ||= KatelloApi::Resources::SystemGroup.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
@k_environment_api ||= KatelloApi::Resources::Environment.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
@k_contentview_api ||= KatelloApi::Resources::ContentView.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
@k_provider_api ||= KatelloApi::Resources::Provider.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
@k_product_api ||= KatelloApi::Resources::Product.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
@k_repository_api ||= KatelloApi::Resources::Repository.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
@k_contentviewdefinition_api ||= KatelloApi::Resources::ContentViewDefinition.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
@k_subscription_api ||= KatelloApi::Resources::Subscription.new(@init_options.merge({:base_url => "#{@init_options[:base_url]}"}))
@f_architecture_api ||= ForemanApi::Resources::Architecture.new(@init_options)
@f_domain_api ||= ForemanApi::Resources::Domain.new(@init_options)
......
end
end
def labelize(name)
name.gsub(/[^a-z0-9\-_]/i, "_")
end
def thread_import(return_headers=false)
csv = []
CSV.foreach(csv_file, {:skip_blanks => true, :headers => :first_row, :return_headers => return_headers}) do |line|
......
return nil if options[:name].nil? || options[:name].empty?
options[:id] = @organizations[options[:name]]
if !options[:id]
organization = @f_organization_api.index({'search' => "name=\"#{options[:name]}\""}, HEADERS)[0]['results']
organization = @f_organization_api.index({'search' => "name=\"#{options[:name]}\""})[0]['results']
raise RuntimeError.new("Organization '#{options[:name]}' not found") if !organization || organization.empty?
options[:id] = organization[0]['id']
@organizations[options[:name]] = options[:id]
......
return nil if options[:id].nil?
options[:name] = @organizations.key(options[:id])
if !options[:name]
organization = @f_organization_api.show({'id' => options[:id]}, HEADERS)[0]
organization = @f_organization_api.show({'id' => options[:id]})[0]
raise RuntimeError.new("Organization 'id=#{options[:id]}' not found") if !organization || organization.empty?
options[:name] = organization['name']
@organizations[options[:name]] = options[:id]
......
return nil if options[:name].nil? || options[:name].empty?
options[:id] = @environments[options[:name]]
if !options[:id]
environment = @f_environment_api.index({'search' => "name=\"#{options[:name]}\""}, HEADERS)[0]['results']
environment = @f_environment_api.index({'search' => "name=\"#{options[:name]}\""})[0]['results']
raise RuntimeError.new("Puppet environment '#{options[:name]}' not found") if !environment || environment.empty?
options[:id] = environment[0]['id']
@environments[options[:name]] = options[:id]
......
return nil if options[:id].nil?
options[:name] = @environments.key(options[:id])
if !options[:name]
environment = @f_environment_api.show({'id' => options[:id]}, HEADERS)[0]
environment = @f_environment_api.show({'id' => options[:id]})[0]
raise RuntimeError.new("Puppet environment '#{options[:name]}' not found") if !environment || environment.empty?
options[:name] = environment['name']
@environments[options[:name]] = options[:id]
......
if !options[:id]
(osname, major, minor) = split_os_name(options[:name])
search = "name=\"#{osname}\" and major=\"#{major}\" and minor=\"#{minor}\""
operatingsystems = @f_operatingsystem_api.index({'search' => search}, HEADERS)[0]['results']
operatingsystems = @f_operatingsystem_api.index({'search' => search})[0]['results']
operatingsystem = operatingsystems[0]
raise RuntimeError.new("Operating system '#{options[:name]}' not found") if !operatingsystem || operatingsystem.empty?
options[:id] = operatingsystem['id']
......
return nil if options[:id].nil?
options[:name] = @operatingsystems.key(options[:id])
if !options[:name]
operatingsystem = @f_operatingsystem_api.show({'id' => options[:id]}, HEADERS)[0]
operatingsystem = @f_operatingsystem_api.show({'id' => options[:id]})[0]
raise RuntimeError.new("Operating system 'id=#{options[:id]}' not found") if !operatingsystem || operatingsystem.empty?
options[:name] = build_os_name(operatingsystem['name'],
operatingsystem['major'],
......
return nil if options[:name].nil? || options[:name].empty?
options[:id] = @architectures[options[:name]]
if !options[:id]
architecture = @f_architecture_api.index({'search' => "name=\"#{options[:name]}\""}, HEADERS)[0]['results']
architecture = @f_architecture_api.index({'search' => "name=\"#{options[:name]}\""})[0]['results']
raise RuntimeError.new("Architecture '#{options[:name]}' not found") if !architecture || architecture.empty?
options[:id] = architecture[0]['id']
@architectures[options[:name]] = options[:id]
......
return nil if options[:id].nil?
options[:name] = @architectures.key(options[:id])
if !options[:name]
architecture = @f_architecture_api.show({'id' => options[:id]}, HEADERS)[0]
architecture = @f_architecture_api.show({'id' => options[:id]})[0]
raise RuntimeError.new("Architecture 'id=#{options[:id]}' not found") if !architecture || architecture.empty?
options[:name] = architecture['name']
@architectures[options[:name]] = options[:id]
......
return nil if options[:name].nil? || options[:name].empty?
options[:id] = @domains[options[:name]]
if !options[:id]
domain = @f_domain_api.index({'search' => "name=\"#{options[:name]}\""}, HEADERS)[0]['results']
domain = @f_domain_api.index({'search' => "name=\"#{options[:name]}\""})[0]['results']
raise RuntimeError.new("Domain '#{options[:name]}' not found") if !domain || domain.empty?
options[:id] = domain[0]['id']
@domains[options[:name]] = options[:id]
......
return nil if options[:id].nil?
options[:name] = @domains.key(options[:id])
if !options[:name]
domain = @f_domain_api.show({'id' => options[:id]}, HEADERS)[0]
domain = @f_domain_api.show({'id' => options[:id]})[0]
raise RuntimeError.new("Domain 'id=#{options[:id]}' not found") if !domain || domain.empty?
options[:name] = domain['name']
@domains[options[:name]] = options[:id]
......
return nil if options[:name].nil? || options[:name].empty?
options[:id] = @ptables[options[:name]]
if !options[:id]
ptable = @f_partitiontable_api.index({'search' => "name=\"#{options[:name]}\""}, HEADERS)[0]['results']
ptable = @f_partitiontable_api.index({'search' => "name=\"#{options[:name]}\""})[0]['results']
raise RuntimeError.new("Partition table '#{options[:name]}' not found") if !ptable || ptable.empty?
options[:id] = ptable[0]['id']
@ptables[options[:name]] = options[:id]
......
return nil if options[:id].nil?
options[:name] = @ptables.key(options[:id])
if !options[:name]
ptable = @f_partitiontable_api.show({'id' => options[:id]}, HEADERS)[0]
ptable = @f_partitiontable_api.show({'id' => options[:id]})[0]
options[:name] = ptable['name']
@ptables[options[:name]] = options[:id]
end
......
return nil if options[:name].nil? || options[:name].empty?
options[:id] = @environments[organization][options[:name]]
if !options[:id]
@k_environment_api.index({'organization_id' => organization}, HEADERS)[0].each do |environment|
@k_environment_api.index({'organization_id' => organization})[0].each do |environment|
@environments[organization][environment['environment']['name']] = environment['environment']['id']
end
options[:id] = @environments[organization][options[:name]]
......
return nil if options[:id].nil?
options[:name] = @environments.key(options[:id])
if !options[:name]
environment = @k_environment_api.show({'id' => options[:id]}, HEADERS)[0]
environment = @k_environment_api.show({'id' => options[:id]})[0]
raise RuntimeError.new("Puppet environment '#{options[:name]}' not found") if !environment || environment.empty?
options[:name] = environment['name']
@environments[options[:name]] = options[:id]
......
return nil if options[:name].nil? || options[:name].empty?
options[:id] = @contentviews[organization][options[:name]]
if !options[:id]
puts @k_contentview_api.index({'organization_id' => organization, 'environment_id' => 2, 'label' => 'Default_Organization_View'}, HEADERS)[0]
@k_contentview_api.index({'organization_id' => organization, 'label' => options[:name]}, HEADERS)[0].each do |contentview|
puts "CONTENTVIEWS=#{@k_contentview_api.index({'organization_id' => organization, 'environment_id' => 2, 'label' => 'Default_Organization_View'})[0]}"
@k_contentview_api.index({
'organization_id' => organization,
'label' => options[:name]
})[0].each do |contentview|
puts contentview
@contentviews[organization][contentview['contentview']['name']] = contentview['contentview']['id']
end
......
return nil if options[:id].nil?
options[:name] = @contentviews.key(options[:id])
if !options[:name]
contentview = @k_contentview_api.show({'id' => options[:id]}, HEADERS)[0]
contentview = @k_contentview_api.show({'id' => options[:id]})[0]
raise RuntimeError.new("Puppet contentview '#{options[:name]}' not found") if !contentview || contentview.empty?
options[:name] = contentview['name']
@contentviews[options[:name]] = options[:id]
......
result
end
def katello_subscription(organization, options={})
@subscriptions ||= {}
@subscriptions[organization] ||= {}
if options[:name]
return nil if options[:name].nil? || options[:name].empty?
options[:id] = @subscriptions[organization][options[:name]]
if !options[:id]
puts @k_subscription_api.index({
'organization_id' => organization,
'search' => "product_id:#{options[:name]}"
})[0]
@k_subscription_api.index({
'organization_id' => organization,
'search' => "'product_id:#{name}'"
})[0]['results'][0].each do |subscription|
puts subscription
@subscriptions[organization][subscription['subscription']['name']] = subscription['subscription']['id']
end
options[:id] = @subscriptions[organization][options[:name]]
raise RuntimeError.new("Puppet subscription '#{options[:name]}' not found") if !options[:id]
end
result = options[:id]
else
return nil if options[:id].nil?
options[:name] = @subscriptions.key(options[:id])
if !options[:name]
subscription = @k_subscription_api.show({'id' => options[:id]})[0]
raise RuntimeError.new("Puppet subscription '#{options[:name]}' not found") if !subscription || subscription.empty?
options[:name] = subscription['name']
@subscriptions[options[:name]] = options[:id]
end
result = options[:name]
end
result
end
def build_os_name(name, major, minor)
name += " #{major}" if major && major != ""
name += ".#{minor}" if minor && minor != ""
lib/hammer_cli_csv/content_views.rb
# Copyright (c) 2013 Red Hat
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
# -= Systems CSV =-
#
# Columns
# Name
# - System name
# - May contain '%d' which will be replaced with current iteration number of Count
# - eg. "os%d" -> "os1"
# Count
# - Number of times to iterate on this line of the CSV file
require 'hammer_cli'
require 'katello_api'
require 'foreman_api'
require 'json'
require 'csv'
require 'uri'
module HammerCLICsv
class ContentViewsCommand < BaseCommand
ORGANIZATION = 'Organization'
DESCRIPTION = 'Description'
PRODUCT = 'Product'
REPOSITORY = 'Repository'
def export
# TODO
end
def import
@existing_contentviews = {}
thread_import do |line|
create_contentviews_from_csv(line)
end
end
def create_contentviews_from_csv(line)
if !@existing_contentviews[line[ORGANIZATION]]
@existing_contentviews[line[ORGANIZATION]] ||= {}
@k_contentviewdefinition_api.index({'organization_id' => line[ORGANIZATION], 'page_size' => 999999, 'paged' => true})[0]['results'].each do |contentview|
@existing_contentviews[line[ORGANIZATION]][contentview['name']] = contentview['id'] if contentview
end
end
line[COUNT].to_i.times do |number|
name = namify(line[NAME], number)
contentview_id = @existing_contentviews[line[ORGANIZATION]][name]
if !contentview_id
print "Creating content view '#{name}'..." if verbose?
contentview_id = @k_contentviewdefinition_api.create({
'organization_id' => line[ORGANIZATION],
'name' => name,
'label' => labelize(name),
'description' => line[DESCRIPTION],
'composite' => false # TODO: add column?
})[0]['id']
@existing_contentviews[line[ORGANIZATION]][name] = contentview_id
else
print "Updating content view '#{name}'..." if verbose?
@k_contentviewdefinition_api.create({
'description' => line[DESCRIPTION],
})
end
if line[REPOSITORY]
puts "UPDATING REPOSITORY"
elsif line[PRODUCT]
puts "UPDATING PRODUCT"
end
print "done\n" if verbose?
=begin
# Only creating repositories, not updating
repository_name = namify(line[REPOSITORY], number)
if !@existing_repositories[line[ORGANIZATION] + name][labelize(repository_name)]
print "Creating repository '#{repository_name}' in contentview '#{name}'..." if verbose?
@k_repository_api.create({
'name' => repository_name,
'label' => labelize(repository_name),
'contentview_id' => contentview_id,
'url' => line[REPOSITORY_URL],
'content_type' => line[REPOSITORY_TYPE]
})
print "done\n" if verbose?
end
=end
end
rescue RuntimeError => e
raise RuntimeError.new("#{e}\n #{line}")
end
end
HammerCLI::MainCommand.subcommand("csv:contentviews", "import/export content views", HammerCLICsv::ContentViewsCommand)
end
lib/hammer_cli_csv/products.rb
# Copyright (c) 2013 Red Hat
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
# -= Systems CSV =-
#
# Columns
# Name
# - System name
# - May contain '%d' which will be replaced with current iteration number of Count
# - eg. "os%d" -> "os1"
# Count
# - Number of times to iterate on this line of the CSV file
require 'hammer_cli'
require 'katello_api'
require 'foreman_api'
require 'json'
require 'csv'
require 'uri'
module HammerCLICsv
class ProductsCommand < BaseCommand
ORGANIZATION = 'Organization'
PROVIDER = 'Provider'
REPOSITORY = 'Repository'
REPOSITORY_TYPE = 'Repository Type'
REPOSITORY_URL = 'Repository Url'
def export
# TODO
end
def import
@existing_products = {}
@existing_providers = {}
@existing_repositories = {}
thread_import do |line|
create_products_from_csv(line)
end
end
def create_products_from_csv(line)
if !@existing_providers[line[ORGANIZATION]]
@k_provider_api.index({'organization_id' => line[ORGANIZATION], 'page_size' => 999999, 'paged' => true})[0]['results'].each do |provider|
@existing_providers[line[ORGANIZATION]] ||= {}
@existing_providers[line[ORGANIZATION]][provider['name']] = provider['id'] if provider
end
@k_product_api.index({'organization_id' => line[ORGANIZATION], 'page_size' => 999999, 'paged' => true})[0]['results'].each do |product|
@existing_products[line[ORGANIZATION]] ||= {}
@existing_products[line[ORGANIZATION]][product['name']] = product['id'] if product
if product
@k_repository_api.index({
'organization_id' => line[ORGANIZATION],
'product_id' => product['id'],
'enabled' => true,
'library' => true,
'page_size' => 999999, 'paged' => true
})[0]['results'].each do |repository|
@existing_repositories[line[ORGANIZATION]+product['name']] ||= {}
@existing_repositories[line[ORGANIZATION]+product['name']][repository['label']] = repository['id']
end
end
end
end
# Only creating providers, not updating
if !@existing_providers[line[ORGANIZATION]][line[PROVIDER]]
print "Creating provider '#{line[PROVIDER]}'..." if verbose?
id = @k_provider_api.create({
'name' => line[PROVIDER],
'organization_id' => line[ORGANIZATION]
})[0]['id']
@existing_providers[line[ORGANIZATION]][line[PROVIDER]] = id
print "done\n" if verbose?
end
provider_id = @existing_providers[line[ORGANIZATION]][line[PROVIDER]]
# Only creating products, not updating
line[COUNT].to_i.times do |number|
name = namify(line[NAME], number)
product_id = @existing_products[line[ORGANIZATION]][name]
if !product_id
print "Creating product '#{name}'..." if verbose?
product_id = @k_product_api.create({
'name' => name,
'provider_id' => provider_id
})[0]['id']
@existing_products[line[ORGANIZATION]][name] = product_id
print "done\n" if verbose?
end
@existing_repositories[line[ORGANIZATION] + name] ||= {}
# Only creating repositories, not updating
repository_name = namify(line[REPOSITORY], number)
if !@existing_repositories[line[ORGANIZATION] + name][labelize(repository_name)]
print "Creating repository '#{repository_name}' in product '#{name}'..." if verbose?
@k_repository_api.create({
'name' => repository_name,
'label' => labelize(repository_name),
'product_id' => product_id,
'url' => line[REPOSITORY_URL],
'content_type' => line[REPOSITORY_TYPE]
})
print "done\n" if verbose?
end
end
rescue RuntimeError => e
raise RuntimeError.new("#{e}\n #{line}")
end
end
HammerCLI::MainCommand.subcommand("csv:products", "import/export products and repositories", HammerCLICsv::ProductsCommand)
end
lib/hammer_cli_csv/subscriptions.rb
# Copyright (c) 2013 Red Hat
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
# -= Systems CSV =-
#
# Columns
# Name
# - System name
# - May contain '%d' which will be replaced with current iteration number of Count
# - eg. "os%d" -> "os1"
# Count
# - Number of times to iterate on this line of the CSV file
require 'hammer_cli'
require 'katello_api'
require 'foreman_api'
require 'json'
require 'csv'
require 'uri'
module HammerCLICsv
class SubscriptionsCommand < BaseCommand
ORGANIZATION = 'Organization'
MANIFEST = 'Manifest File'
def export
# TODO
end
def import
thread_import do |line|
create_subscriptions_from_csv(line)
end
end
def create_subscriptions_from_csv(line)
puts "TODO: import #{line[MANIFEST]} into organization #{line[ORGANIZATION]}"
rescue RuntimeError => e
raise RuntimeError.new("#{e}\n #{line}")
end
end
HammerCLI::MainCommand.subcommand("csv:subscriptions", "import subscriptions", HammerCLICsv::SubscriptionsCommand)
end
lib/hammer_cli_csv/systems.rb
def import
@existing = {}
#puts "ENVIRONMENT #{katello_environment('ACME_Corporation', :name => 'Library')}"
#puts "CONTENTVIEW #{katello_contentview('ACME_Corporation', :name => 'Default_Organization_View')}"
thread_import do |line|
create_systems_from_csv(line)
end
end
def create_systems_from_csv(line)
@k_system_api.index({'organization_id' => line[ORGANIZATION], 'page_size' => 999999, 'paged' => true}, HEADERS)[0]['results'].each do |system|
@existing[line[ORGANIZATION]] ||= {}
@existing[line[ORGANIZATION]][system['name']] = system['id'] if system
end
facts = {}
facts['cpu.core(s)_per_socket'] = line[CORES]
facts['cpu.cpu_socket(s)'] = line[SOCKETS]
facts['memory.memtotal'] = line[RAM]
facts['uname.machine'] = line[ARCHITECTURE]
if line[OPERATINGSYSTEM].index(' ')
(facts['distribution.name'], facts['distribution.version']) = line[OPERATINGSYSTEM].split(' ')
else
(facts['distribution.name'], facts['distribution.version']) = ['RHEL', line[OPERATINGSYSTEM]]
if !@existing[line[ORGANIZATION]]
@existing[line[ORGANIZATION]] = {}
@k_system_api.index({'organization_id' => line[ORGANIZATION], 'page_size' => 999999, 'paged' => true})[0]['results'].each do |system|
@existing[line[ORGANIZATION]][system['name']] = system['uuid'] if system
end
end
line[COUNT].to_i.times do |number|
name = namify(line[NAME], number)
if !@existing.include? name
subscriptions(line).each do |subscription|
puts subscription
katello_subscription(line[ORGANIZATION], :name => subscription[:number])
end
if !@existing[line[ORGANIZATION]].include? name
print "Creating system '#{name}'..." if verbose?
@k_system_api.create({
'name' => name,
'organization_id' => 'ACME_Corporation', #foreman_organization(:name => line[ORGANIZATION]),
'environment_id' => 1, #katello_environment(:name => line[ENVIRONMENT]),
'content_view_id' => 1, #katello_contentview(:name => line[CONTENTVIEW]),
'facts' => facts,
'cp_type' => 'system'
}, HEADERS)
print "done\n" if verbose?
'organization_id' => line[ORGANIZATION],
'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
'content_view_id' => 2, # TODO: katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
'facts' => facts(line),
'installedProducts' => products(line),
'type' => 'system'
})
else
print "Updating host '#{name}'..." if verbose?
=begin
@k_system_api.update({
'id' => @existing[name],
'host' => {
'id' => @existing[line[ORGANIZATION]][name],
'name' => name,
'mac' => namify(line[MACADDRESS], number),
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
'domain_id' => foreman_domain(:name => line[DOMAIN]),
'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
}
}, HEADERS)
=end
print "done\n" if verbose?
'organization_id' => line[ORGANIZATION],
'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
'content_view_id' => 2, # TODO: katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
'facts' => facts(line),
'installedProducts' => products(line)
})
end
print "done\n" if verbose?
end
rescue RuntimeError => e
raise RuntimeError.new("#{e}\n #{line}")
end
private
def facts(line)
facts = {}
facts['cpu.core(s)_per_socket'] = line[CORES]
facts['cpu.cpu_socket(s)'] = line[SOCKETS]
facts['memory.memtotal'] = line[RAM]
facts['uname.machine'] = line[ARCHITECTURE]
if line[OPERATINGSYSTEM].index(' ')
(facts['distribution.name'], facts['distribution.version']) = line[OPERATINGSYSTEM].split(' ')
else
(facts['distribution.name'], facts['distribution.version']) = ['RHEL', line[OPERATINGSYSTEM]]
end
facts
end
def products(line)
products = CSV.parse_line(line[PRODUCTS]).collect do |product_details|
product = {}
(product[:product_id], product[:productName]) = product_details.split('|')
product
end
products
end
def subscriptions(line)
subscriptions = CSV.parse_line(line[SUBSCRIPTIONS]).collect do |subscription_details|
subscription = {}
(subscription[:number], subscription[:name]) = subscription_details.split('|')
subscription
end
subscriptions
end
end
HammerCLI::MainCommand.subcommand("csv:systems", "import/export systems", HammerCLICsv::SystemsCommand)
test/data/contentviews.csv
Name,Count,Organization,Description,Product,Repository
Katello,1,megacorp,"Katello - The Sysadmin's Fortress",katello-1.4,katello-1.4
Katello,1,megacorp,"Katello - The Sysadmin's Fortress",katello-1.4-client,
test/data/products.csv
Name,Count,Organization,Provider,Repository,Repository Type,Repository Url
Point of Sale,1,megacorp,MegaCorp,katello-1.4-client,yum,http://fedorapeople.org/groups/katello/releases/yum/1.4-client/RHEL/6Server/x86_64/
katello-1.4,1,megacorp,MegaCorp,katello-1.4,yum,http://fedorapeople.org/groups/katello/releases/yum/1.4/RHEL/6Server/x86_64/
test/data/subscriptions.csv
Name,Count,Organization,Manifest File
MegaCorp,1,megacorp,~/Downloads/megacorp.zip
test/data/systems.csv
Name,Count,Organization,Environment,Groups,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions
one%d,1,megacorp,Library,Default_Organization_View,Yes,four1,RHEL 6.4,x86_64,1,4,1,Standard,"69|Red Hat Enterprise Linux Server,79|Red Hat Enterprise Linux Server",RH0103708|Red Hat Enterprise Linux Server Premium (8 sockets) (Up to 4 guests)
Name,Count,Organization,Environment,Content View,Groups,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions
host%d,1,megacorp,Library,Default_Organization_View,,No,,RHEL 6.4,x86_64,1,4,1,Standard,"69|Red Hat Enterprise Linux Server,79|Red Hat Enterprise Linux Server",RH0103708|Red Hat Enterprise Linux Server Premium (8 sockets) (Up to 4 guests)
guest%d,1,megacorp,Library,Default_Organization_View,,Yes,host1,RHEL 6.4,x86_64,1,4,1,Standard,"69|Red Hat Enterprise Linux Server,79|Red Hat Enterprise Linux Server",RH0103708|Red Hat Enterprise Linux Server Premium (8 sockets) (Up to 4 guests)

Also available in: Unified diff