hammer_cli_csv 1.0.1 → 1.0.2
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 +8 -8
- data/config/cli_config.yml +2 -0
- data/lib/hammer_cli_csv.rb +4 -11
- data/lib/hammer_cli_csv/activation_keys.rb +7 -21
- data/lib/hammer_cli_csv/architectures.rb +6 -37
- data/lib/hammer_cli_csv/base.rb +177 -21
- data/lib/hammer_cli_csv/compute_profiles.rb +4 -21
- data/lib/hammer_cli_csv/compute_resources.rb +4 -16
- data/lib/hammer_cli_csv/containers.rb +65 -0
- data/lib/hammer_cli_csv/content_hosts.rb +22 -26
- data/lib/hammer_cli_csv/content_view_filters.rb +90 -60
- data/lib/hammer_cli_csv/content_views.rb +4 -17
- data/lib/hammer_cli_csv/csv.rb +0 -12
- data/lib/hammer_cli_csv/domains.rb +74 -38
- data/lib/hammer_cli_csv/exception_handler.rb +0 -11
- data/lib/hammer_cli_csv/export.rb +14 -20
- data/lib/hammer_cli_csv/headpin_api.rb +0 -11
- data/lib/hammer_cli_csv/host_collections.rb +5 -23
- data/lib/hammer_cli_csv/host_groups.rb +115 -0
- data/lib/hammer_cli_csv/hosts.rb +32 -56
- data/lib/hammer_cli_csv/import.rb +46 -24
- data/lib/hammer_cli_csv/installation_medias.rb +4 -15
- data/lib/hammer_cli_csv/job_templates.rb +142 -0
- data/lib/hammer_cli_csv/lifecycle_environments.rb +5 -38
- data/lib/hammer_cli_csv/locations.rb +4 -33
- data/lib/hammer_cli_csv/operating_systems.rb +17 -33
- data/lib/hammer_cli_csv/organizations.rb +13 -41
- data/lib/hammer_cli_csv/partition_tables.rb +66 -54
- data/lib/hammer_cli_csv/products.rb +12 -24
- data/lib/hammer_cli_csv/provisioning_templates.rb +4 -18
- data/lib/hammer_cli_csv/puppet_environments.rb +36 -59
- data/lib/hammer_cli_csv/puppet_facts.rb +36 -58
- data/lib/hammer_cli_csv/puppet_reports.rb +4 -38
- data/lib/hammer_cli_csv/reports.rb +4 -15
- data/lib/hammer_cli_csv/roles.rb +4 -15
- data/lib/hammer_cli_csv/settings.rb +49 -0
- data/lib/hammer_cli_csv/smart_proxies.rb +9 -24
- data/lib/hammer_cli_csv/splice.rb +0 -12
- data/lib/hammer_cli_csv/subnets.rb +10 -21
- data/lib/hammer_cli_csv/subscriptions.rb +21 -24
- data/lib/hammer_cli_csv/sync_plans.rb +4 -19
- data/lib/hammer_cli_csv/users.rb +4 -15
- data/lib/hammer_cli_csv/version.rb +1 -12
- data/test/content_hosts_test.rb +1 -1
- data/test/content_views_test.rb +52 -0
- data/test/data/content-hosts.csv +1 -1
- data/test/data/content-view-filters.csv +1 -1
- data/test/data/content-views.csv +5 -5
- data/test/data/hosts.csv +2 -2
- data/test/data/operating-systems.csv +16 -16
- data/test/data/products.csv +1 -1
- data/test/data/settings.csv +5 -0
- data/test/import_test.rb +79 -0
- metadata +14 -18
@@ -1,14 +1,3 @@
|
|
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
1
|
require 'hammer_cli_foreman'
|
13
2
|
require 'hammer_cli_foreman_tasks'
|
14
3
|
|
@@ -20,8 +9,6 @@ module HammerCLICsv
|
|
20
9
|
command_name 'content-views'
|
21
10
|
desc 'import or export content-views'
|
22
11
|
|
23
|
-
option %w(--organization), 'ORGANIZATION', 'Only process organization matching this name'
|
24
|
-
|
25
12
|
LABEL = 'Label'
|
26
13
|
ORGANIZATION = 'Organization'
|
27
14
|
DESCRIPTION = 'Description'
|
@@ -30,8 +17,8 @@ module HammerCLICsv
|
|
30
17
|
ENVIRONMENTS = "Lifecycle Environments"
|
31
18
|
|
32
19
|
def export
|
33
|
-
CSV.open(
|
34
|
-
csv << [NAME,
|
20
|
+
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
|
21
|
+
csv << [NAME, LABEL, ORGANIZATION, COMPOSITE, REPOSITORIES, ENVIRONMENTS]
|
35
22
|
@api.resource(:organizations).call(:index, {
|
36
23
|
:per_page => 999999
|
37
24
|
})['results'].each do |organization|
|
@@ -61,7 +48,7 @@ module HammerCLICsv
|
|
61
48
|
composite_contentviews << [name, 1, label, orgname, composite, contentviews, environments]
|
62
49
|
else
|
63
50
|
repositories = export_column(contentview, 'repositories', 'name')
|
64
|
-
csv << [name,
|
51
|
+
csv << [name, label, orgname, composite, repositories, environments]
|
65
52
|
end
|
66
53
|
end
|
67
54
|
composite_contentviews.each do |contentview|
|
@@ -106,7 +93,7 @@ module HammerCLICsv
|
|
106
93
|
end
|
107
94
|
end
|
108
95
|
|
109
|
-
line[COUNT].
|
96
|
+
count(line[COUNT]).times do |number|
|
110
97
|
name = namify(line[NAME], number)
|
111
98
|
|
112
99
|
contentview_id = @existing_contentviews[line[ORGANIZATION]][name]
|
data/lib/hammer_cli_csv/csv.rb
CHANGED
@@ -1,15 +1,3 @@
|
|
1
|
-
|
2
|
-
# Copyright 2013-2014 Red Hat, Inc.
|
3
|
-
#
|
4
|
-
# This software is licensed to you under the GNU General Public
|
5
|
-
# License as published by the Free Software Foundation; either version
|
6
|
-
# 2 of the License (GPLv2) or (at your option) any later version.
|
7
|
-
# There is NO WARRANTY for this software, express or implied,
|
8
|
-
# including the implied warranties of MERCHANTABILITY,
|
9
|
-
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
|
10
|
-
# have received a copy of GPLv2 along with this software; if not, see
|
11
|
-
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
|
12
|
-
|
13
1
|
require 'hammer_cli'
|
14
2
|
require 'hammer_cli/exit_codes'
|
15
3
|
|
@@ -1,48 +1,33 @@
|
|
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
|
-
# -= Domains CSV =-
|
14
|
-
#
|
15
|
-
# Columns
|
16
|
-
# Name
|
17
|
-
# - Domain 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
1
|
module HammerCLICsv
|
29
2
|
class CsvCommand
|
30
3
|
class DomainsCommand < BaseCommand
|
31
4
|
command_name 'domains'
|
32
5
|
desc 'import or export domains'
|
33
6
|
|
34
|
-
FULLNAME = 'Full Name'
|
35
7
|
ORGANIZATIONS = 'Organizations'
|
8
|
+
LOCATIONS = 'Locations'
|
9
|
+
DESCRIPTION = 'Description'
|
10
|
+
SMART_PROXY = 'DNS Smart Proxy'
|
11
|
+
PARAMETERS = 'Parameters'
|
12
|
+
|
13
|
+
SEPARATOR = ' = '
|
36
14
|
|
37
15
|
def export
|
38
|
-
CSV.open(
|
39
|
-
csv << [NAME,
|
40
|
-
|
41
|
-
|
16
|
+
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
17
|
+
csv << [NAME, ORGANIZATIONS, LOCATIONS, DESCRIPTION, SMART_PROXY, PARAMETERS]
|
18
|
+
search_options = {:per_page => 999999}
|
19
|
+
search_options['search'] = "organization=\"#{option_organization}\"" if option_organization
|
20
|
+
@api.resource(:domains).call(:index, search_options)['results'].each do |domain|
|
21
|
+
domain = @api.resource(:domains).call(:show, {'id' => domain['id']})
|
22
|
+
raise "Domain 'id=#{domain['id']}' not found" if !domain || domain.empty?
|
23
|
+
|
42
24
|
name = domain['name']
|
43
|
-
|
44
|
-
|
45
|
-
|
25
|
+
organizations = option_organization ? option_organization : export_column(domain, 'organizations', 'name')
|
26
|
+
locations = export_column(domain, 'locations', 'name')
|
27
|
+
description = domain['fullname']
|
28
|
+
capsule = foreman_smart_proxy(:id => domain['dns_id'])
|
29
|
+
parameters = export_parameters(domain['parameters'])
|
30
|
+
csv << [name, organizations, locations, description, capsule, parameters]
|
46
31
|
end
|
47
32
|
end
|
48
33
|
end
|
@@ -59,18 +44,28 @@ module HammerCLICsv
|
|
59
44
|
end
|
60
45
|
|
61
46
|
def create_domains_from_csv(line)
|
62
|
-
line[
|
47
|
+
dns_id = foreman_smart_proxy(:name => line[SMART_PROXY])
|
48
|
+
count(line[COUNT]).times do |number|
|
63
49
|
name = namify(line[NAME], number)
|
50
|
+
description = namify(line[DESCRIPTION], number)
|
64
51
|
if !@existing.include? name
|
65
52
|
print "Creating domain '#{name}'..." if option_verbose?
|
66
53
|
domain_id = @api.resource(:domains).call(:create, {
|
67
|
-
'
|
54
|
+
'domain' => {
|
55
|
+
'name' => name,
|
56
|
+
'fullname' => description,
|
57
|
+
'dns_id' => dns_id
|
58
|
+
}
|
68
59
|
})['id']
|
69
60
|
else
|
70
61
|
print "Updating domain '#{name}'..." if option_verbose?
|
71
62
|
domain_id = @api.resource(:domains).call(:update, {
|
72
63
|
'id' => @existing[name],
|
73
|
-
'
|
64
|
+
'domain' => {
|
65
|
+
'name' => name,
|
66
|
+
'fullname' => description,
|
67
|
+
'dns_id' => dns_id
|
68
|
+
}
|
74
69
|
})['id']
|
75
70
|
end
|
76
71
|
|
@@ -93,11 +88,52 @@ module HammerCLICsv
|
|
93
88
|
})
|
94
89
|
end
|
95
90
|
|
91
|
+
import_parameters(domain_id, line[PARAMETERS])
|
92
|
+
|
96
93
|
print "done\n" if option_verbose?
|
97
94
|
end
|
98
95
|
rescue RuntimeError => e
|
99
96
|
raise "#{e}\n #{line}"
|
100
97
|
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def export_parameters(parameters)
|
102
|
+
return '' if parameters.nil? || parameters.empty?
|
103
|
+
|
104
|
+
values = CSV.generate do |column|
|
105
|
+
column << parameters.collect do |parameter|
|
106
|
+
"#{parameter['name']}#{SEPARATOR}#{parameter['value']}"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
values.delete!("\n")
|
110
|
+
end
|
111
|
+
|
112
|
+
def import_parameters(domain_id, parameters)
|
113
|
+
collect_column(parameters) do |parameter|
|
114
|
+
(parameter_name, parameter_value) = parameter.split(SEPARATOR)
|
115
|
+
|
116
|
+
results = @api.resource(:parameters).call(:index, { :domain_id => domain_id, :search => "name=\"#{parameter_name}\"" })['results']
|
117
|
+
if results.empty?
|
118
|
+
@api.resource(:parameters).call(:create, {
|
119
|
+
'domain_id' => domain_id,
|
120
|
+
'parameter' => {
|
121
|
+
'name' => parameter_name,
|
122
|
+
'value' => parameter_value
|
123
|
+
}
|
124
|
+
})
|
125
|
+
else
|
126
|
+
@api.resource(:parameters).call(:create, {
|
127
|
+
'id' => results[0]['id'],
|
128
|
+
'domain_id' => domain_id,
|
129
|
+
'parameter' => {
|
130
|
+
'name' => parameter_name,
|
131
|
+
'value' => parameter_value
|
132
|
+
}
|
133
|
+
})
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
101
137
|
end
|
102
138
|
end
|
103
139
|
end
|
@@ -1,14 +1,3 @@
|
|
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
1
|
require 'hammer_cli/exception_handler'
|
13
2
|
|
14
3
|
module HammerCLICsv
|
@@ -1,24 +1,14 @@
|
|
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
1
|
module HammerCLICsv
|
14
2
|
class CsvCommand
|
15
3
|
class ExportCommand < HammerCLI::Apipie::Command
|
16
4
|
command_name 'export'
|
17
5
|
desc 'export into directory'
|
18
6
|
|
19
|
-
option %w(-v --verbose), :flag, 'be verbose'
|
20
|
-
option %w(--threads), 'THREAD_COUNT', 'Number of threads to hammer with',
|
21
|
-
|
7
|
+
option %w(-v --verbose), :flag, _('be verbose')
|
8
|
+
option %w(--threads), 'THREAD_COUNT', _('Number of threads to hammer with'),
|
9
|
+
:default => 1, :hidden => true
|
10
|
+
option '--dir', 'DIRECTORY', _('directory to import from')
|
11
|
+
option %w(--organization), 'ORGANIZATION', _('Only process organization matching this name')
|
22
12
|
|
23
13
|
RESOURCES = %w(
|
24
14
|
organizations locations puppet_environments operating_systems
|
@@ -32,15 +22,18 @@ module HammerCLICsv
|
|
32
22
|
end
|
33
23
|
|
34
24
|
def execute
|
35
|
-
@server = HammerCLI::Settings.settings[:_params]
|
25
|
+
@server = (HammerCLI::Settings.settings[:_params] &&
|
26
|
+
HammerCLI::Settings.settings[:_params][:host]) ||
|
36
27
|
HammerCLI::Settings.get(:csv, :host) ||
|
37
28
|
HammerCLI::Settings.get(:katello, :host) ||
|
38
29
|
HammerCLI::Settings.get(:foreman, :host)
|
39
|
-
@username = HammerCLI::Settings.settings[:_params]
|
30
|
+
@username = (HammerCLI::Settings.settings[:_params] &&
|
31
|
+
HammerCLI::Settings.settings[:_params][:username]) ||
|
40
32
|
HammerCLI::Settings.get(:csv, :username) ||
|
41
33
|
HammerCLI::Settings.get(:katello, :username) ||
|
42
34
|
HammerCLI::Settings.get(:foreman, :username)
|
43
|
-
@password = HammerCLI::Settings.settings[:_params]
|
35
|
+
@password = (HammerCLI::Settings.settings[:_params] &&
|
36
|
+
HammerCLI::Settings.settings[:_params][:password]) ||
|
44
37
|
HammerCLI::Settings.get(:csv, :password) ||
|
45
38
|
HammerCLI::Settings.get(:katello, :password) ||
|
46
39
|
HammerCLI::Settings.get(:foreman, :password)
|
@@ -91,9 +84,10 @@ module HammerCLICsv
|
|
91
84
|
options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.sub('_', '-')}.csv"
|
92
85
|
args = []
|
93
86
|
args += %W( --server #{@server} ) if @server
|
94
|
-
args += %W( csv #{resource.sub('_', '-')} --
|
87
|
+
args += %W( csv #{resource.sub('_', '-')} --export --file #{options_file} )
|
95
88
|
args << '-v' if option_verbose?
|
96
|
-
args += %W( --
|
89
|
+
args += %W( --organization #{option_organization} ) if option_organization
|
90
|
+
args += %W( --threads #{option_threads} ) if option_threads
|
97
91
|
puts "Exporting '#{args.join(' ')}'" if option_verbose?
|
98
92
|
hammer.run(args)
|
99
93
|
end
|
@@ -1,14 +1,3 @@
|
|
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
1
|
require 'csv'
|
13
2
|
|
14
3
|
module HammerCLICsv
|
@@ -1,39 +1,21 @@
|
|
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
|
-
require 'hammer_cli'
|
14
|
-
require 'json'
|
15
|
-
require 'csv'
|
16
|
-
|
17
1
|
module HammerCLICsv
|
18
2
|
class CsvCommand
|
19
3
|
class HostCollectionsCommand < BaseCommand
|
20
4
|
command_name 'host-collections'
|
21
5
|
desc 'import or export host collections'
|
22
6
|
|
23
|
-
option %w(--organization), 'ORGANIZATION', 'Only process organization matching this name'
|
24
|
-
|
25
7
|
ORGANIZATION = 'Organization'
|
26
8
|
LIMIT = 'Limit'
|
27
9
|
DESCRIPTION = 'Description'
|
28
10
|
|
29
11
|
def export
|
30
|
-
CSV.open(
|
31
|
-
csv << [NAME,
|
12
|
+
CSV.open(option_file || '/dev/stdout', 'wb') do |csv|
|
13
|
+
csv << [NAME, ORGANIZATION, LIMIT, DESCRIPTION]
|
32
14
|
if @server_status['release'] == 'Headpin'
|
33
15
|
@headpin.get(:organizations).each do |organization|
|
34
16
|
next if option_organization && organization['name'] != option_organization
|
35
17
|
@headpin.get("organizations/#{organization['label']}/system_groups").each do |systemgroup|
|
36
|
-
csv << [systemgroup['name'],
|
18
|
+
csv << [systemgroup['name'], organization['name'],
|
37
19
|
systemgroup['max_systems'].to_i < 0 ? 'Unlimited' : systemgroup['max_systems'],
|
38
20
|
systemgroup['description']]
|
39
21
|
end
|
@@ -45,7 +27,7 @@ module HammerCLICsv
|
|
45
27
|
'organization_id' => organization['id']
|
46
28
|
})['results'].each do |hostcollection|
|
47
29
|
limit = hostcollection['unlimited_content_hosts'] ? 'Unlimited' : hostcollection['max_content_hosts']
|
48
|
-
csv << [hostcollection['name'],
|
30
|
+
csv << [hostcollection['name'], organization['name'],
|
49
31
|
limit,
|
50
32
|
hostcollection['description']]
|
51
33
|
end
|
@@ -75,7 +57,7 @@ module HammerCLICsv
|
|
75
57
|
end
|
76
58
|
end
|
77
59
|
|
78
|
-
line[COUNT].
|
60
|
+
count(line[COUNT]).times do |number|
|
79
61
|
name = namify(line[NAME], number)
|
80
62
|
params = {
|
81
63
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
@@ -0,0 +1,115 @@
|
|
1
|
+
module HammerCLICsv
|
2
|
+
class CsvCommand
|
3
|
+
class HostGroupsCommand < BaseCommand
|
4
|
+
command_name 'host-groups'
|
5
|
+
desc 'import or export host-groups'
|
6
|
+
|
7
|
+
PARENT = 'Parent Host Group'
|
8
|
+
ORGANIZATIONS = 'Organizations'
|
9
|
+
LOCATIONS = 'Locations'
|
10
|
+
SUBNET = 'Subnet'
|
11
|
+
DOMAIN = 'Domain'
|
12
|
+
OPERATING_SYSTEM = 'Operating System'
|
13
|
+
ENVIRONMENT = 'Puppet Environment'
|
14
|
+
COMPUTE_PROFILE = 'Compute Profile'
|
15
|
+
PARTITION_TABLE = 'Partition Table'
|
16
|
+
MEDIUM = 'Medium'
|
17
|
+
ARCHITECTURE = 'Architecture'
|
18
|
+
REALM = 'Realm'
|
19
|
+
PUPPET_PROXY = 'Puppet Proxy'
|
20
|
+
PUPPET_CA_PROXY = 'Puppet CA Proxy'
|
21
|
+
CONTENT_SOURCE = 'Content Source'
|
22
|
+
|
23
|
+
def export
|
24
|
+
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
25
|
+
csv << [NAME, PARENT, ORGANIZATIONS, LOCATIONS, SUBNET, DOMAIN, OPERATING_SYSTEM,
|
26
|
+
ENVIRONMENT, COMPUTE_PROFILE, PARTITION_TABLE, MEDIUM, ARCHITECTURE, REALM,
|
27
|
+
PUPPET_PROXY, PUPPET_CA_PROXY, CONTENT_SOURCE]
|
28
|
+
search_options = {:per_page => 999999}
|
29
|
+
search_options['search'] = "organization=\"#{option_organization}\"" if option_organization
|
30
|
+
@api.resource(:hostgroups).call(:index, search_options)['results'].each do |hostgroup|
|
31
|
+
hostgroup = @api.resource(:hostgroups).call(:show, {'id' => hostgroup['id']})
|
32
|
+
raise "Host Group 'id=#{hostgroup['id']}' not found" if !hostgroup || hostgroup.empty?
|
33
|
+
|
34
|
+
name = hostgroup['name']
|
35
|
+
organizations = export_column(hostgroup, 'organizations', 'name')
|
36
|
+
locations = export_column(hostgroup, 'locations', 'name')
|
37
|
+
subnet = hostgroup['subnet_name']
|
38
|
+
operating_system = hostgroup['operatingsystem_name']
|
39
|
+
domain = hostgroup['domain_name']
|
40
|
+
puppet_environment = hostgroup['environment_name']
|
41
|
+
compute_profile = hostgroup['compute_profile_name']
|
42
|
+
partition_table = hostgroup['ptable_name']
|
43
|
+
medium = hostgroup['medium_name']
|
44
|
+
architecture = hostgroup['architecture_name']
|
45
|
+
realm = hostgroup['realm_name']
|
46
|
+
puppet_proxy = hostgroup['puppet_proxy_id'] ? foreman_host(:id => hostgroup['puppet_proxy_id']) : nil
|
47
|
+
puppet_ca_proxy = hostgroup['puppet_ca_proxy_id'] ? foreman_host(:id => hostgroup['puppet_ca_proxy_id']) : nil
|
48
|
+
# TODO: http://projects.theforeman.org/issues/7597
|
49
|
+
# content_source = hostgroup['content_source_id'] ? foreman_host(:id => hostgroup['content_source_id']) : nil
|
50
|
+
content_source = nil
|
51
|
+
parent = hostgroup['ancestry'] ? foreman_hostgroup(:id => hostgroup['ancestry']) : nil
|
52
|
+
|
53
|
+
csv << [name, parent, organizations, locations, subnet, domain, operating_system,
|
54
|
+
puppet_environment, compute_profile, partition_table, medium, architecture,
|
55
|
+
realm, puppet_proxy, puppet_ca_proxy, content_source]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def import
|
61
|
+
@existing = {}
|
62
|
+
@api.resource(:hostgroups).call(:index, {:per_page => 999999})['results'].each do |host|
|
63
|
+
@existing[host['name']] = host['id'] if host
|
64
|
+
end
|
65
|
+
|
66
|
+
thread_import do |line|
|
67
|
+
create_hosts_from_csv(line)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def create_hosts_from_csv(line)
|
72
|
+
return if option_organization && !CSV.parse_line(line[ORGANIZATIONS], {:skip_blanks => true}).include?(option_organization)
|
73
|
+
|
74
|
+
count(line[COUNT]).times do |number|
|
75
|
+
name = namify(line[NAME], number)
|
76
|
+
if !@existing.include? name
|
77
|
+
print "Creating host group '#{name}'..." if option_verbose?
|
78
|
+
# @api.resource(:hosts).call(:create, {
|
79
|
+
# 'host' => {
|
80
|
+
# 'name' => name,
|
81
|
+
# 'root_pass' => 'changeme',
|
82
|
+
# 'mac' => namify(line[MACADDRESS], number),
|
83
|
+
# 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
84
|
+
# 'location_id' => foreman_location(:name => line[LOCATION]),
|
85
|
+
# 'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
|
86
|
+
# 'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
|
87
|
+
# 'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
|
88
|
+
# 'domain_id' => foreman_domain(:name => line[DOMAIN]),
|
89
|
+
# 'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
|
90
|
+
# }
|
91
|
+
# })
|
92
|
+
else
|
93
|
+
print "Updating host '#{name}'..." if option_verbose?
|
94
|
+
# @api.resource(:hosts).call(:update, {
|
95
|
+
# 'id' => @existing[name],
|
96
|
+
# 'host' => {
|
97
|
+
# 'name' => name,
|
98
|
+
# 'mac' => namify(line[MACADDRESS], number),
|
99
|
+
# 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
100
|
+
# 'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
|
101
|
+
# 'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
|
102
|
+
# 'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
|
103
|
+
# 'domain_id' => foreman_domain(:name => line[DOMAIN]),
|
104
|
+
# 'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
|
105
|
+
# }
|
106
|
+
# })
|
107
|
+
end
|
108
|
+
print "done\n" if option_verbose?
|
109
|
+
end
|
110
|
+
rescue RuntimeError => e
|
111
|
+
raise "#{e}\n #{line}"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|