hammer_cli_csv 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/lib/hammer_cli_csv.rb +48 -0
- data/lib/hammer_cli_csv/activation_keys.rb +170 -0
- data/lib/hammer_cli_csv/architectures.rb +95 -0
- data/lib/hammer_cli_csv/base.rb +706 -0
- data/lib/hammer_cli_csv/compute_profiles.rb +94 -0
- data/lib/hammer_cli_csv/compute_resources.rb +92 -0
- data/lib/hammer_cli_csv/content_hosts.rb +357 -0
- data/lib/hammer_cli_csv/content_view_filters.rb +158 -0
- data/lib/hammer_cli_csv/content_views.rb +86 -0
- data/lib/hammer_cli_csv/csv.rb +23 -0
- data/lib/hammer_cli_csv/domains.rb +103 -0
- data/lib/hammer_cli_csv/exception_handler.rb +53 -0
- data/lib/hammer_cli_csv/host_collections.rb +108 -0
- data/lib/hammer_cli_csv/hosts.rb +118 -0
- data/lib/hammer_cli_csv/import.rb +82 -0
- data/lib/hammer_cli_csv/installation_medias.rb +88 -0
- data/lib/hammer_cli_csv/lifecycle_environments.rb +116 -0
- data/lib/hammer_cli_csv/locations.rb +84 -0
- data/lib/hammer_cli_csv/operating_systems.rb +95 -0
- data/lib/hammer_cli_csv/organizations.rb +107 -0
- data/lib/hammer_cli_csv/partition_tables.rb +98 -0
- data/lib/hammer_cli_csv/products.rb +179 -0
- data/lib/hammer_cli_csv/provisioning_templates.rb +96 -0
- data/lib/hammer_cli_csv/puppet_environments.rb +105 -0
- data/lib/hammer_cli_csv/puppet_facts.rb +99 -0
- data/lib/hammer_cli_csv/puppet_reports.rb +244 -0
- data/lib/hammer_cli_csv/reports.rb +91 -0
- data/lib/hammer_cli_csv/roles.rb +115 -0
- data/lib/hammer_cli_csv/smart_proxies.rb +88 -0
- data/lib/hammer_cli_csv/subnets.rb +121 -0
- data/lib/hammer_cli_csv/subscriptions.rb +135 -0
- data/lib/hammer_cli_csv/users.rb +133 -0
- data/lib/hammer_cli_csv/version.rb +16 -0
- data/test/activation_keys_test.rb +17 -0
- data/test/apipie_resource_mock.rb +77 -0
- data/test/config.template.yml +17 -0
- data/test/csv_test_helper.rb +65 -0
- data/test/data/activation-keys.csv +119 -0
- data/test/data/architectures.csv +5 -0
- data/test/data/content-hosts.csv +4 -0
- data/test/data/content-view-filters.csv +2 -0
- data/test/data/content-views.csv +6 -0
- data/test/data/domains.csv +8 -0
- data/test/data/host-collections.csv +16 -0
- data/test/data/hosts.csv +12 -0
- data/test/data/installation-medias.csv +7 -0
- data/test/data/lifecycle-environments.csv +6 -0
- data/test/data/locations.csv +10 -0
- data/test/data/operating-systems.csv +16 -0
- data/test/data/organizations.csv +5 -0
- data/test/data/partition-tables.csv +187 -0
- data/test/data/products.csv +19 -0
- data/test/data/puppet-environments.csv +6 -0
- data/test/data/puppet-facts.csv +9 -0
- data/test/data/reports.csv +4 -0
- data/test/data/roles.csv +159 -0
- data/test/data/smart-proxies.csv +2 -0
- data/test/data/subscriptions.csv +19 -0
- data/test/data/users.csv +119 -0
- data/test/helpers/command.rb +67 -0
- data/test/helpers/resource_disabled.rb +69 -0
- data/test/hosts_test.rb +47 -0
- data/test/organizations_test.rb +74 -0
- data/test/roles_test.rb +39 -0
- data/test/setup_test.rb +164 -0
- data/test/systems_test.rb +71 -0
- data/test/users_test.rb +35 -0
- metadata +174 -0
@@ -0,0 +1,96 @@
|
|
1
|
+
# Copyright 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
|
+
module HammerCLICsv
|
13
|
+
class CsvCommand
|
14
|
+
class ProvisioningTemplatesCommand < BaseCommand
|
15
|
+
command_name 'provisioning-templates'
|
16
|
+
desc 'import or export provisioning templates'
|
17
|
+
|
18
|
+
ORGANIZATIONS = 'Organizations'
|
19
|
+
LOCATIONS = 'Locations'
|
20
|
+
KIND = 'Kind'
|
21
|
+
SOURCE = 'Source'
|
22
|
+
|
23
|
+
def export
|
24
|
+
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
25
|
+
csv << [NAME, COUNT, ORGANIZATIONS, LOCATIONS, KIND, SOURCE]
|
26
|
+
@api.resource(:config_templates)
|
27
|
+
.call(:index, {
|
28
|
+
:per_page => 999999
|
29
|
+
})['results'].each do |template_id|
|
30
|
+
template = @api.resource(:config_templates).call(:show, {:id => template_id['id']})
|
31
|
+
name = template['name']
|
32
|
+
count = 1
|
33
|
+
kind = template['snippet'] ? 'snippet' : template['template_kind_name']
|
34
|
+
organizations = export_column(template, 'organizations', 'name')
|
35
|
+
locations = export_column(template, 'locations', 'name')
|
36
|
+
unless name == 'Boot disk iPXE - generic host' || name == 'Boot disk iPXE - host'
|
37
|
+
csv << [name, count, organizations, locations, kind, template['template']]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def import
|
44
|
+
@existing = {}
|
45
|
+
@api.resource(:config_templates)
|
46
|
+
.call(:index, {
|
47
|
+
:per_page => 999999
|
48
|
+
})['results'].each do |template|
|
49
|
+
@existing[template['name']] = template['id'] if template
|
50
|
+
end
|
51
|
+
|
52
|
+
thread_import do |line|
|
53
|
+
create_templates_from_csv(line)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def create_templates_from_csv(line)
|
58
|
+
organizations = collect_column(line[ORGANIZATIONS]) do |organization|
|
59
|
+
foreman_organization(:name => organization)
|
60
|
+
end
|
61
|
+
locations = collect_column(line[LOCATIONS]) do |location|
|
62
|
+
foreman_location(:name => location)
|
63
|
+
end
|
64
|
+
|
65
|
+
line[COUNT].to_i.times do |number|
|
66
|
+
name = namify(line[NAME], number)
|
67
|
+
if !@existing.include? name
|
68
|
+
print "Creating provisioning template '#{name}'..." if option_verbose?
|
69
|
+
id = @api.resource(:config_templates)
|
70
|
+
.call(:create, {
|
71
|
+
'name' => name,
|
72
|
+
'snippet' => line[KIND] == 'snippet',
|
73
|
+
'template_kind_id' => line[KIND] == 'snippet' ? nil : foreman_template_kind(:name => line[KIND]),
|
74
|
+
'organizations' => organizations,
|
75
|
+
'locations' => locations
|
76
|
+
})['id']
|
77
|
+
else
|
78
|
+
print "Updating provisioning template '#{name}'..." if option_verbose?
|
79
|
+
id = @api.resource(:config_template)
|
80
|
+
.call(:update, {
|
81
|
+
'id' => @existing[name],
|
82
|
+
'name' => name,
|
83
|
+
'organizations' => organizations,
|
84
|
+
'locations' => locations
|
85
|
+
})['id']
|
86
|
+
end
|
87
|
+
@existing[name] = id
|
88
|
+
|
89
|
+
print "done\n" if option_verbose?
|
90
|
+
end
|
91
|
+
rescue RuntimeError => e
|
92
|
+
raise "#{e}\n #{line[NAME]}"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,105 @@
|
|
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
|
+
# -= Environments CSV =-
|
14
|
+
#
|
15
|
+
# Columns
|
16
|
+
# Name
|
17
|
+
# - Environment 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
|
+
|
24
|
+
require 'hammer_cli'
|
25
|
+
require 'json'
|
26
|
+
require 'csv'
|
27
|
+
|
28
|
+
module HammerCLICsv
|
29
|
+
class CsvCommand
|
30
|
+
class PuppetEnvironmentsCommand < BaseCommand
|
31
|
+
command_name 'puppet-environments'
|
32
|
+
desc 'import or export puppet environments'
|
33
|
+
|
34
|
+
ORGANIZATIONS = 'Organizations'
|
35
|
+
|
36
|
+
def export
|
37
|
+
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
38
|
+
csv << [NAME, COUNT, ORGANIZATIONS]
|
39
|
+
@api.resource(:environments).call(:index, {:per_page => 999999})['results'].each do |environment|
|
40
|
+
name = environment['name']
|
41
|
+
count = 1
|
42
|
+
csv << [name, count]
|
43
|
+
raise 'TODO: organizations'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def import
|
49
|
+
@existing = {}
|
50
|
+
@api.resource(:environments).call(:index, {:per_page => 999999})['results'].each do |environment|
|
51
|
+
@existing[environment['name']] = environment['id'] if environment
|
52
|
+
end
|
53
|
+
|
54
|
+
thread_import do |line|
|
55
|
+
create_environments_from_csv(line)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def create_environments_from_csv(line)
|
60
|
+
line[COUNT].to_i.times do |number|
|
61
|
+
name = namify(line[NAME], number)
|
62
|
+
if !@existing.include? name
|
63
|
+
print "Creating environment '#{name}'..." if option_verbose?
|
64
|
+
id = @api.resource(:environments).call(:create, {
|
65
|
+
'environment' => {
|
66
|
+
'name' => name
|
67
|
+
}
|
68
|
+
})['id']
|
69
|
+
else
|
70
|
+
print "Updating environment '#{name}'..." if option_verbose?
|
71
|
+
id = @api.resource(:environments).call(:update, {
|
72
|
+
'id' => @existing[name],
|
73
|
+
'environment' => {
|
74
|
+
'name' => name
|
75
|
+
}
|
76
|
+
})['environment']['id']
|
77
|
+
end
|
78
|
+
|
79
|
+
# Update associated resources
|
80
|
+
# TODO: Bug #4738: organization json does not include puppet environments
|
81
|
+
# http://projects.theforeman.org/issues/4738#change-15319
|
82
|
+
# Update below to match style of domains
|
83
|
+
organization_ids = CSV.parse_line(line[ORGANIZATIONS]).collect do |organization|
|
84
|
+
foreman_organization(:name => organization)
|
85
|
+
end
|
86
|
+
organization_ids += @api.resource(:environments).call(:show, {'id' => id})['organizations'].collect do |organization|
|
87
|
+
organization['id']
|
88
|
+
end
|
89
|
+
organization_ids.uniq!
|
90
|
+
|
91
|
+
@api.resource(:environments).call(:update, {
|
92
|
+
'id' => id,
|
93
|
+
'environment' => {
|
94
|
+
'organization_ids' => organization_ids
|
95
|
+
}
|
96
|
+
})
|
97
|
+
|
98
|
+
print "done\n" if option_verbose?
|
99
|
+
end
|
100
|
+
rescue RuntimeError => e
|
101
|
+
raise "#{e}\n #{line}"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,99 @@
|
|
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
|
+
# -= Puppet Facts CSV =-
|
14
|
+
#
|
15
|
+
# Columns
|
16
|
+
# Name
|
17
|
+
# - Host 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
|
+
# <Fact Key names>
|
23
|
+
# - May contain '%d' which will be replaced with current iteration number of Count
|
24
|
+
#
|
25
|
+
|
26
|
+
require 'hammer_cli'
|
27
|
+
require 'json'
|
28
|
+
require 'csv'
|
29
|
+
|
30
|
+
module HammerCLICsv
|
31
|
+
class CsvCommand
|
32
|
+
class PuppetFactsCommand < BaseCommand
|
33
|
+
command_name 'puppet-facts'
|
34
|
+
desc 'import or export puppet facts'
|
35
|
+
|
36
|
+
def export
|
37
|
+
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
38
|
+
headers = [NAME, COUNT]
|
39
|
+
# Extracted facts are always based upon the first host found, otherwise this would be an intensive
|
40
|
+
# method to gather all the possible column names
|
41
|
+
any_host = @api.resource(:hosts).call(:index, {:per_page => 1})['results'][0]
|
42
|
+
headers += @api.resource(:puppetfactss).call(:index, {
|
43
|
+
'host_id' => any_host['name'],
|
44
|
+
'per_page' => 999999
|
45
|
+
})['results'][any_host['name']].keys
|
46
|
+
csv << headers
|
47
|
+
|
48
|
+
@api.resource(:hosts).call(:index, {:per_page => 999999})['results'].each do |host|
|
49
|
+
line = [host['name'], 1]
|
50
|
+
facts = @api.resource(:puppetfactss).call(:index, {'host_id' => host['name'], 'per_page' => 999999})[host['name']]
|
51
|
+
facts ||= {}
|
52
|
+
headers[2..-1].each do |fact_name|
|
53
|
+
line << facts[fact_name] || ''
|
54
|
+
end
|
55
|
+
csv << line
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def import
|
61
|
+
@headers = nil
|
62
|
+
|
63
|
+
thread_import(true) do |line|
|
64
|
+
create_puppetfacts_from_csv(line)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def create_puppetfacts_from_csv(line)
|
69
|
+
if @headers.nil?
|
70
|
+
@headers = line
|
71
|
+
return
|
72
|
+
end
|
73
|
+
|
74
|
+
line[COUNT].to_i.times do |number|
|
75
|
+
name = namify(line[NAME], number)
|
76
|
+
print "Updating puppetfacts '#{name}'..." if option_verbose?
|
77
|
+
facts = line.to_hash
|
78
|
+
facts.delete(NAME)
|
79
|
+
facts.delete(COUNT)
|
80
|
+
|
81
|
+
# Namify the values if the host name was namified
|
82
|
+
if name != line[NAME]
|
83
|
+
facts.each do |fact, value|
|
84
|
+
facts[fact] = namify(value, number) unless value.nil? || value.empty?
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
@api.resource(:hosts).call(:facts, {
|
89
|
+
'name' => name,
|
90
|
+
'facts' => facts
|
91
|
+
})
|
92
|
+
print "done\n" if option_verbose?
|
93
|
+
end
|
94
|
+
rescue RuntimeError => e
|
95
|
+
raise "#{e}\n #{line}"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,244 @@
|
|
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
|
+
# MAC Address
|
23
|
+
# - MAC address
|
24
|
+
# - May contain '%d' which will be replaced with current iteration number of Count
|
25
|
+
# - eg. "FF:FF:FF:FF:FF:%02x" -> "FF:FF:FF:FF:FF:0A"
|
26
|
+
# - Warning: be sure to keep count below 255 or MAC hex will exceed limit
|
27
|
+
#
|
28
|
+
|
29
|
+
require 'hammer_cli'
|
30
|
+
require 'json'
|
31
|
+
require 'csv'
|
32
|
+
require 'uri'
|
33
|
+
|
34
|
+
module HammerCLICsv
|
35
|
+
class CsvCommand
|
36
|
+
class PuppetReportsCommand < BaseCommand
|
37
|
+
command_name 'puppet-reports'
|
38
|
+
desc 'import or export puppet reports'
|
39
|
+
|
40
|
+
ORGANIZATION = 'Organization'
|
41
|
+
ENVIRONMENT = 'Environment'
|
42
|
+
CONTENTVIEW = 'Content View'
|
43
|
+
SYSTEMGROUPS = 'System Groups'
|
44
|
+
VIRTUAL = 'Virtual'
|
45
|
+
HOST = 'Host'
|
46
|
+
OPERATINGSYSTEM = 'OS'
|
47
|
+
ARCHITECTURE = 'Arch'
|
48
|
+
SOCKETS = 'Sockets'
|
49
|
+
RAM = 'RAM'
|
50
|
+
CORES = 'Cores'
|
51
|
+
SLA = 'SLA'
|
52
|
+
PRODUCTS = 'Products'
|
53
|
+
SUBSCRIPTIONS = 'Subscriptions'
|
54
|
+
|
55
|
+
def export
|
56
|
+
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
|
57
|
+
csv << [NAME, COUNT, ORGANIZATION, ENVIRONMENT, CONTENTVIEW, SYSTEMGROUPS, VIRTUAL, HOST,
|
58
|
+
OPERATINGSYSTEM, ARCHITECTURE, SOCKETS, RAM, CORES, SLA, PRODUCTS, SUBSCRIPTIONS]
|
59
|
+
@api.resource(:organizations)
|
60
|
+
.call(:index, {
|
61
|
+
:per_page => 999999
|
62
|
+
})['results'].each do |organization|
|
63
|
+
@api.resource(:systems)
|
64
|
+
.call(:index, {
|
65
|
+
'per_page' => 999999,
|
66
|
+
'organization_id' => organization['id']
|
67
|
+
})['results'].each do |system|
|
68
|
+
system = @api.resource(:systems)
|
69
|
+
.call(:show, {
|
70
|
+
'id' => system['uuid'],
|
71
|
+
'fields' => 'full'
|
72
|
+
})
|
73
|
+
|
74
|
+
name = system['name']
|
75
|
+
count = 1
|
76
|
+
organization_label = organization['label']
|
77
|
+
environment = system['environment']['label']
|
78
|
+
contentview = system['content_view']['name']
|
79
|
+
hostcollections = CSV.generate do |column|
|
80
|
+
column << system['systemGroups'].collect do |hostcollection|
|
81
|
+
hostcollection['name']
|
82
|
+
end
|
83
|
+
end
|
84
|
+
hostcollections.delete!("\n")
|
85
|
+
virtual = system['facts']['virt.is_guest'] == 'true' ? 'Yes' : 'No'
|
86
|
+
host = system['host']
|
87
|
+
operatingsystem = "#{system['facts']['distribution.name']} " if system['facts']['distribution.name']
|
88
|
+
operatingsystem += system['facts']['distribution.version'] if system['facts']['distribution.version']
|
89
|
+
architecture = system['facts']['uname.machine']
|
90
|
+
sockets = system['facts']['cpu.cpu_socket(s)']
|
91
|
+
ram = system['facts']['memory.memtotal']
|
92
|
+
cores = system['facts']['cpu.core(s)_per_socket']
|
93
|
+
sla = ''
|
94
|
+
products = CSV.generate do |column|
|
95
|
+
column << system['installedProducts'].collect do |product|
|
96
|
+
"#{product['productId']}|#{product['productName']}"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
products.delete!("\n")
|
100
|
+
subscriptions = CSV.generate do |column|
|
101
|
+
column << @api.resource(:subscriptions)
|
102
|
+
.call(:index, {
|
103
|
+
'system_id' => system['uuid']
|
104
|
+
})['results'].collect do |subscription|
|
105
|
+
"#{subscription['product_id']}|#{subscription['product_name']}"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
subscriptions.delete!("\n")
|
109
|
+
csv << [name, count, organization_label, environment, contentview, hostcollections, virtual, host,
|
110
|
+
operatingsystem, architecture, sockets, ram, cores, sla, products, subscriptions]
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def import
|
117
|
+
@existing = {}
|
118
|
+
@host_guests = {}
|
119
|
+
|
120
|
+
thread_import do |line|
|
121
|
+
create_systems_from_csv(line)
|
122
|
+
end
|
123
|
+
|
124
|
+
print 'Updating host and guest associations...' if option_verbose?
|
125
|
+
@host_guests.each do |host_id, guest_ids|
|
126
|
+
@api.resource(:systems)
|
127
|
+
.call(:update, {
|
128
|
+
'id' => host_id,
|
129
|
+
'guest_ids' => guest_ids
|
130
|
+
})
|
131
|
+
end
|
132
|
+
puts 'done' if option_verbose?
|
133
|
+
end
|
134
|
+
|
135
|
+
def create_systems_from_csv(line)
|
136
|
+
if !@existing[line[ORGANIZATION]]
|
137
|
+
@existing[line[ORGANIZATION]] = {}
|
138
|
+
@api.resource(:systems)
|
139
|
+
.call(:index, {
|
140
|
+
'organization_id' => line[ORGANIZATION],
|
141
|
+
'per_page' => 999999
|
142
|
+
})['results'].each do |system|
|
143
|
+
@existing[line[ORGANIZATION]][system['name']] = system['uuid'] if system
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
line[COUNT].to_i.times do |number|
|
148
|
+
name = namify(line[NAME], number)
|
149
|
+
|
150
|
+
# TODO: w/ @daviddavis p-r
|
151
|
+
#subscriptions(line).each do |subscription|
|
152
|
+
# katello_subscription(line[ORGANIZATION], :name => subscription[:number])
|
153
|
+
#end
|
154
|
+
|
155
|
+
if !@existing[line[ORGANIZATION]].include? name
|
156
|
+
print "Creating system '#{name}'..." if option_verbose?
|
157
|
+
system_id = @api.resource(:systems)
|
158
|
+
.call(:create, {
|
159
|
+
'name' => name,
|
160
|
+
'organization_id' => line[ORGANIZATION],
|
161
|
+
'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
|
162
|
+
'content_view_id' => lifecycle_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
|
163
|
+
'facts' => facts(line),
|
164
|
+
'installed_products' => products(line),
|
165
|
+
'type' => 'system'
|
166
|
+
})['uuid']
|
167
|
+
@existing[line[ORGANIZATION]][name] = system_id
|
168
|
+
else
|
169
|
+
print "Updating system '#{name}'..." if option_verbose?
|
170
|
+
puts line
|
171
|
+
system_id = @api.resource(:systems)
|
172
|
+
.call(:update, {
|
173
|
+
'id' => @existing[line[ORGANIZATION]][name],
|
174
|
+
'name' => name,
|
175
|
+
'environment_id' => katello_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
|
176
|
+
'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
|
177
|
+
'facts' => facts(line),
|
178
|
+
'installed_products' => products(line)
|
179
|
+
})['uuid']
|
180
|
+
end
|
181
|
+
|
182
|
+
if line[VIRTUAL] == 'Yes' && line[HOST]
|
183
|
+
raise "Host system '#{line[HOST]}' not found" if !@existing[line[ORGANIZATION]][line[HOST]]
|
184
|
+
@host_guests[@existing[line[ORGANIZATION]][line[HOST]]] ||= []
|
185
|
+
@host_guests[@existing[line[ORGANIZATION]][line[HOST]]] << system_id
|
186
|
+
end
|
187
|
+
|
188
|
+
set_host_collections(system_id, line)
|
189
|
+
|
190
|
+
puts 'done' if option_verbose?
|
191
|
+
end
|
192
|
+
rescue RuntimeError => e
|
193
|
+
raise "#{e}\n #{line}"
|
194
|
+
end
|
195
|
+
|
196
|
+
private
|
197
|
+
|
198
|
+
def facts(line)
|
199
|
+
facts = {}
|
200
|
+
facts['cpu.core(s)_per_socket'] = line[CORES]
|
201
|
+
facts['cpu.cpu_socket(s)'] = line[SOCKETS]
|
202
|
+
facts['memory.memtotal'] = line[RAM]
|
203
|
+
facts['uname.machine'] = line[ARCHITECTURE]
|
204
|
+
if line[OPERATINGSYSTEM].index(' ')
|
205
|
+
(facts['distribution.name'], facts['distribution.version']) = line[OPERATINGSYSTEM].split(' ')
|
206
|
+
else
|
207
|
+
(facts['distribution.name'], facts['distribution.version']) = ['RHEL', line[OPERATINGSYSTEM]]
|
208
|
+
end
|
209
|
+
facts['virt.is_guest'] = line[VIRTUAL] == 'Yes' ? true : false
|
210
|
+
facts
|
211
|
+
end
|
212
|
+
|
213
|
+
def set_host_collections(system_id, line)
|
214
|
+
CSV.parse_line(line[SYSTEMGROUPS]).each do |hostcollection_name|
|
215
|
+
@api.resource(:hostcollections)
|
216
|
+
.call(:add_systems, {
|
217
|
+
'id' => katello_hostcollection(line[ORGANIZATION], :name => hostcollection_name),
|
218
|
+
'system_ids' => [system_id]
|
219
|
+
})
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
def products(line)
|
224
|
+
products = CSV.parse_line(line[PRODUCTS]).collect do |product_details|
|
225
|
+
product = {}
|
226
|
+
# TODO: these get passed straight through to candlepin; probably would be better to process in server
|
227
|
+
# to allow underscore product_id here
|
228
|
+
(product['productId'], product['productName']) = product_details.split('|')
|
229
|
+
product
|
230
|
+
end
|
231
|
+
products
|
232
|
+
end
|
233
|
+
|
234
|
+
def subscriptions(line)
|
235
|
+
subscriptions = CSV.parse_line(line[SUBSCRIPTIONS]).collect do |subscription_details|
|
236
|
+
subscription = {}
|
237
|
+
(subscription[:number], subscription[:name]) = subscription_details.split('|')
|
238
|
+
subscription
|
239
|
+
end
|
240
|
+
subscriptions
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|