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
data/lib/hammer_cli_csv/hosts.rb
CHANGED
@@ -1,36 +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
|
-
#
|
13
|
-
# -= Hosts 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
|
-
# 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
1
|
module HammerCLICsv
|
35
2
|
class CsvCommand
|
36
3
|
class HostsCommand < BaseCommand
|
@@ -38,6 +5,7 @@ module HammerCLICsv
|
|
38
5
|
desc 'import or export hosts'
|
39
6
|
|
40
7
|
ORGANIZATION = 'Organization'
|
8
|
+
LOCATION = 'Location'
|
41
9
|
ENVIRONMENT = 'Environment'
|
42
10
|
OPERATINGSYSTEM = 'Operating System'
|
43
11
|
ARCHITECTURE = 'Architecture'
|
@@ -46,14 +14,15 @@ module HammerCLICsv
|
|
46
14
|
PARTITIONTABLE = 'Partition Table'
|
47
15
|
|
48
16
|
def export
|
49
|
-
CSV.open(
|
50
|
-
csv << [NAME,
|
51
|
-
|
17
|
+
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
18
|
+
csv << [NAME, ORGANIZATION, LOCATION, ENVIRONMENT, OPERATINGSYSTEM, ARCHITECTURE, MACADDRESS, DOMAIN, PARTITIONTABLE]
|
19
|
+
search_options = {:per_page => 999999}
|
20
|
+
search_options['search'] = "organization=\"#{option_organization}\"" if option_organization
|
21
|
+
@api.resource(:hosts).call(:index, search_options)['results'].each do |host|
|
52
22
|
host = @api.resource(:hosts).call(:show, {'id' => host['id']})
|
53
23
|
raise "Host 'id=#{host['id']}' not found" if !host || host.empty?
|
54
24
|
|
55
25
|
name = host['name']
|
56
|
-
count = 1
|
57
26
|
organization = foreman_organization(:id => host['organization_id'])
|
58
27
|
environment = foreman_environment(:id => host['environment_id'])
|
59
28
|
operatingsystem = foreman_operatingsystem(:id => host['operatingsystem_id'])
|
@@ -62,7 +31,7 @@ module HammerCLICsv
|
|
62
31
|
domain = foreman_domain(:id => host['domain_id'])
|
63
32
|
ptable = foreman_partitiontable(:id => host['ptable_id'])
|
64
33
|
|
65
|
-
csv << [name,
|
34
|
+
csv << [name, organization, environment, operatingsystem, architecture, mac, domain, ptable]
|
66
35
|
end
|
67
36
|
end
|
68
37
|
end
|
@@ -79,33 +48,40 @@ module HammerCLICsv
|
|
79
48
|
end
|
80
49
|
|
81
50
|
def create_hosts_from_csv(line)
|
82
|
-
line[
|
51
|
+
return if option_organization && line[ORGANIZATION] != option_organization
|
52
|
+
|
53
|
+
count(line[COUNT]).times do |number|
|
83
54
|
name = namify(line[NAME], number)
|
84
55
|
if !@existing.include? name
|
85
56
|
print "Creating host '#{name}'..." if option_verbose?
|
86
57
|
@api.resource(:hosts).call(:create, {
|
87
|
-
'
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
58
|
+
'host' => {
|
59
|
+
'name' => name,
|
60
|
+
'root_pass' => 'changeme',
|
61
|
+
'mac' => namify(line[MACADDRESS], number),
|
62
|
+
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
63
|
+
'location_id' => foreman_location(:name => line[LOCATION]),
|
64
|
+
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
|
65
|
+
'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
|
66
|
+
'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
|
67
|
+
'domain_id' => foreman_domain(:name => line[DOMAIN]),
|
68
|
+
'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
|
69
|
+
}
|
96
70
|
})
|
97
71
|
else
|
98
72
|
print "Updating host '#{name}'..." if option_verbose?
|
99
73
|
@api.resource(:hosts).call(:update, {
|
100
74
|
'id' => @existing[name],
|
101
|
-
'
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
75
|
+
'host' => {
|
76
|
+
'name' => name,
|
77
|
+
'mac' => namify(line[MACADDRESS], number),
|
78
|
+
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
79
|
+
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
|
80
|
+
'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
|
81
|
+
'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
|
82
|
+
'domain_id' => foreman_domain(:name => line[DOMAIN]),
|
83
|
+
'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
|
84
|
+
}
|
109
85
|
})
|
110
86
|
end
|
111
87
|
print "done\n" if option_verbose?
|
@@ -1,14 +1,4 @@
|
|
1
|
-
|
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
|
-
|
1
|
+
require 'open-uri'
|
12
2
|
|
13
3
|
module HammerCLICsv
|
14
4
|
class CsvCommand
|
@@ -16,23 +6,41 @@ module HammerCLICsv
|
|
16
6
|
command_name 'import'
|
17
7
|
desc 'import by directory'
|
18
8
|
|
19
|
-
option %w(-v --verbose), :flag, 'be verbose'
|
20
|
-
option %w(--threads), 'THREAD_COUNT', 'Number of threads to hammer with',
|
21
|
-
|
9
|
+
option %w(-v --verbose), :flag, _('be verbose')
|
10
|
+
option %w(--threads), 'THREAD_COUNT', _('Number of threads to hammer with'),
|
11
|
+
:default => 1, :hidden => true
|
12
|
+
option '--dir', 'DIRECTORY', _('directory to import from')
|
13
|
+
option %w(--organization), 'ORGANIZATION', _('Only process organization matching this name')
|
14
|
+
option %w(--prefix), 'PREFIX', _('Prefix for all name columns')
|
22
15
|
|
23
|
-
RESOURCES = %w( organizations locations puppet_environments operating_systems
|
16
|
+
RESOURCES = %w( settings organizations locations puppet_environments operating_systems
|
24
17
|
domains architectures partition_tables lifecycle_environments host_collections
|
25
18
|
provisioning_templates
|
26
19
|
subscriptions products content_views content_view_filters activation_keys
|
27
|
-
hosts content_hosts reports roles users )
|
20
|
+
hosts content_hosts smart_proxies reports roles users )
|
28
21
|
RESOURCES.each do |resource|
|
29
|
-
dashed = resource.
|
22
|
+
dashed = resource.gsub('_', '-')
|
30
23
|
option "--#{dashed}", 'FILE', "csv file for #{dashed}"
|
31
24
|
end
|
32
25
|
|
33
26
|
def execute
|
34
|
-
@
|
35
|
-
|
27
|
+
@server = (HammerCLI::Settings.settings[:_params] &&
|
28
|
+
HammerCLI::Settings.settings[:_params][:host]) ||
|
29
|
+
HammerCLI::Settings.get(:csv, :host) ||
|
30
|
+
HammerCLI::Settings.get(:katello, :host) ||
|
31
|
+
HammerCLI::Settings.get(:foreman, :host)
|
32
|
+
@username = (HammerCLI::Settings.settings[:_params] &&
|
33
|
+
HammerCLI::Settings.settings[:_params][:username]) ||
|
34
|
+
HammerCLI::Settings.get(:csv, :username) ||
|
35
|
+
HammerCLI::Settings.get(:katello, :username) ||
|
36
|
+
HammerCLI::Settings.get(:foreman, :username)
|
37
|
+
@password = (HammerCLI::Settings.settings[:_params] &&
|
38
|
+
HammerCLI::Settings.settings[:_params][:password]) ||
|
39
|
+
HammerCLI::Settings.get(:csv, :password) ||
|
40
|
+
HammerCLI::Settings.get(:katello, :password) ||
|
41
|
+
HammerCLI::Settings.get(:foreman, :password)
|
42
|
+
@api = ApipieBindings::API.new({:uri => @server, :username => @username,
|
43
|
+
:password => @password, :api_version => 2})
|
36
44
|
|
37
45
|
# Swing the hammers
|
38
46
|
RESOURCES.each do |resource|
|
@@ -54,20 +62,34 @@ module HammerCLICsv
|
|
54
62
|
|
55
63
|
def hammer_resource(resource)
|
56
64
|
return if !self.send("option_#{resource}") && !option_dir
|
57
|
-
options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.
|
58
|
-
|
59
|
-
|
65
|
+
options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.gsub('_', '-')}.csv"
|
66
|
+
unless options_file_exists? options_file
|
67
|
+
if option_dir
|
68
|
+
puts _("Skipping #{resource} because '#{options_file}' does not exist") if option_verbose?
|
69
|
+
return
|
70
|
+
end
|
60
71
|
raise "File for #{resource} '#{options_file}' does not exist"
|
61
72
|
end
|
73
|
+
puts _("Importing #{resource} from '#{options_file}'") if option_verbose?
|
62
74
|
|
63
|
-
args = %W( csv #{resource.
|
75
|
+
args = %W( csv #{resource.gsub('_', '-')} --file #{options_file} )
|
64
76
|
args << '-v' if option_verbose?
|
65
|
-
args += %W( --
|
77
|
+
args += %W( --organization #{option_organization} ) if option_organization
|
78
|
+
args += %W( --prefix #{option_prefix} ) if option_prefix
|
79
|
+
args += %W( --threads #{option_threads} ) if option_threads
|
66
80
|
hammer.run(args)
|
67
81
|
end
|
68
82
|
|
69
83
|
private
|
70
84
|
|
85
|
+
def options_file_exists?(options_file)
|
86
|
+
f = open(options_file)
|
87
|
+
f.close
|
88
|
+
true
|
89
|
+
rescue
|
90
|
+
false
|
91
|
+
end
|
92
|
+
|
71
93
|
def get_option(name)
|
72
94
|
HammerCLI::Settings.settings[:_params][name] ||
|
73
95
|
HammerCLI::Settings.get(:csv, name) ||
|
@@ -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
|
module HammerCLICsv
|
13
2
|
class CsvCommand
|
14
3
|
class InstallationMediasCommand < BaseCommand
|
@@ -20,14 +9,14 @@ module HammerCLICsv
|
|
20
9
|
ORGANIZATIONS = 'Organizations'
|
21
10
|
|
22
11
|
def export
|
23
|
-
CSV.open(
|
24
|
-
csv << [NAME,
|
12
|
+
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
13
|
+
csv << [NAME, PATH, OSFAMILY]
|
25
14
|
@api.resource(:media).call(:index, {:per_page => 999999})['results'].each do |installation_media|
|
26
15
|
name = installation_media['name']
|
27
16
|
count = 1
|
28
17
|
path = installation_media['path']
|
29
18
|
os_family = installation_media['os_family']
|
30
|
-
csv << [name,
|
19
|
+
csv << [name, path, os_family]
|
31
20
|
end
|
32
21
|
end
|
33
22
|
end
|
@@ -44,7 +33,7 @@ module HammerCLICsv
|
|
44
33
|
end
|
45
34
|
|
46
35
|
def create_installation_medias_from_csv(line)
|
47
|
-
line[COUNT].
|
36
|
+
count(line[COUNT]).times do |number|
|
48
37
|
name = namify(line[NAME], number)
|
49
38
|
if !@existing.include? name
|
50
39
|
print "Creating installation_media '#{name}'..." if option_verbose?
|
@@ -0,0 +1,142 @@
|
|
1
|
+
module HammerCLICsv
|
2
|
+
class CsvCommand
|
3
|
+
class JobTemplatesCommand < BaseCommand
|
4
|
+
command_name 'job-templates'
|
5
|
+
desc 'import or export job templates'
|
6
|
+
|
7
|
+
ORGANIZATIONS = 'Organizations'
|
8
|
+
LOCATIONS = 'Locations'
|
9
|
+
JOB = 'Job Category'
|
10
|
+
PROVIDER = 'Provider'
|
11
|
+
SNIPPET = 'Snippet'
|
12
|
+
TEMPLATE = 'Template'
|
13
|
+
|
14
|
+
def export
|
15
|
+
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
16
|
+
csv << [NAME, ORGANIZATIONS, LOCATIONS, JOB, PROVIDER, SNIPPET, TEMPLATE]
|
17
|
+
@api.resource(:job_templates).call(:index, {
|
18
|
+
:per_page => 999999
|
19
|
+
})['results'].each do |template_id|
|
20
|
+
template = @api.resource(:job_templates).call(:show, {:id => template_id['id']})
|
21
|
+
next if template['locked']
|
22
|
+
next unless option_organization.nil? || template['organizations'].detect { |org| org['name'] == option_organization }
|
23
|
+
name = template['name']
|
24
|
+
job = template['job_name']
|
25
|
+
snippet = template['snippet'] ? 'Yes' : 'No'
|
26
|
+
provider = template['provider_type']
|
27
|
+
organizations = export_column(template, 'organizations', 'name')
|
28
|
+
locations = export_column(template, 'locations', 'name')
|
29
|
+
csv << [name, organizations, locations, job, provider, snippet, template['template']]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def import
|
35
|
+
@existing = {}
|
36
|
+
@api.resource(:job_templates).call(:index, {
|
37
|
+
:per_page => 999999
|
38
|
+
})['results'].each do |template|
|
39
|
+
@existing[template['name']] = template['id'] if template
|
40
|
+
end
|
41
|
+
|
42
|
+
thread_import do |line|
|
43
|
+
create_templates_from_csv(line)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def create_templates_from_csv(line)
|
48
|
+
organizations = collect_column(line[ORGANIZATIONS]) do |organization|
|
49
|
+
foreman_organization(:name => organization)
|
50
|
+
end
|
51
|
+
if option_organization
|
52
|
+
org_id = foreman_organization(:name => option_organization)
|
53
|
+
return if org_id.nil? || !organizations.include?(org_id)
|
54
|
+
organizations = [org_id]
|
55
|
+
end
|
56
|
+
locations = collect_column(line[LOCATIONS]) do |location|
|
57
|
+
foreman_location(:name => location)
|
58
|
+
end
|
59
|
+
|
60
|
+
count(line[COUNT]).times do |number|
|
61
|
+
name = namify(line[NAME], number)
|
62
|
+
job_name = namify(line[JOB], number)
|
63
|
+
options = {
|
64
|
+
'job_template' => {
|
65
|
+
'name' => name,
|
66
|
+
'job_name' => job_name,
|
67
|
+
'snippet' => line[SNIPPET] == 'Yes' ? true : false,
|
68
|
+
'provider_type' => line[PROVIDER],
|
69
|
+
'organization_ids' => organizations,
|
70
|
+
'location_ids' => locations,
|
71
|
+
'template' => line[TEMPLATE]
|
72
|
+
}
|
73
|
+
}
|
74
|
+
template_id = @existing[name]
|
75
|
+
if !template_id
|
76
|
+
print _("Creating job template '%{name}'...") % {:name => name } if option_verbose?
|
77
|
+
template_id = @api.resource(:job_templates).call(:create, options)['id']
|
78
|
+
@existing[name] = template_id
|
79
|
+
else
|
80
|
+
print _("Updating job template '%{name}'...") % {:name => name} if option_verbose?
|
81
|
+
options['id'] = template_id
|
82
|
+
template_id = @api.resource(:job_templates).call(:update, options)['id']
|
83
|
+
end
|
84
|
+
|
85
|
+
# Update associated resources
|
86
|
+
# @template_organizations ||= {}
|
87
|
+
# organizations.each do |organization_id|
|
88
|
+
# if @template_organizations[organization_id].nil?
|
89
|
+
# @template_organizations[organization_id] = @api.resource(:organizations).call(:show, {
|
90
|
+
# 'id' => organization_id
|
91
|
+
# })['config_templates'].collect do |template|
|
92
|
+
# template['id']
|
93
|
+
# end
|
94
|
+
# end
|
95
|
+
# if !@template_organizations[organization_id].include? template_id
|
96
|
+
# @template_organizations[organization_id] << template_id
|
97
|
+
# @api.resource(:organizations).call(:update, {
|
98
|
+
# 'id' => organization_id,
|
99
|
+
# 'organization' => {
|
100
|
+
# 'config_template_ids' => @template_organizations[organization_id]
|
101
|
+
# }
|
102
|
+
# })
|
103
|
+
# end
|
104
|
+
# end
|
105
|
+
# @template_locations ||= {}
|
106
|
+
# locations.each do |location_id|
|
107
|
+
# if @template_locations[location_id].nil?
|
108
|
+
# @template_locations[location_id] = @api.resource(:locations).call(:show, {
|
109
|
+
# 'id' => location_id
|
110
|
+
# })['config_templates'].collect do |template|
|
111
|
+
# template['id']
|
112
|
+
# end
|
113
|
+
# end
|
114
|
+
# if !@template_locations[location_id].include? template_id
|
115
|
+
# @template_locations[location_id] += [template_id]
|
116
|
+
# @api.resource(:locations).call(:update, {
|
117
|
+
# 'id' => location_id,
|
118
|
+
# 'location' => {
|
119
|
+
# 'config_template_ids' => @template_locations[location_id]
|
120
|
+
# }
|
121
|
+
# })
|
122
|
+
# end
|
123
|
+
# end
|
124
|
+
|
125
|
+
puts _('done') if option_verbose?
|
126
|
+
end
|
127
|
+
rescue RuntimeError => e
|
128
|
+
raise "#{e}\n #{line[NAME]}"
|
129
|
+
end
|
130
|
+
|
131
|
+
def export_associations(template)
|
132
|
+
return '' unless template['template_combinations']
|
133
|
+
values = CSV.generate do |column|
|
134
|
+
column << template['template_combinations'].collect do |combo|
|
135
|
+
"#{combo['hostgroup_name']}|#{combo['environment_name']}"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
values.delete!("\n")
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -1,45 +1,16 @@
|
|
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
1
|
module HammerCLICsv
|
29
2
|
class CsvCommand
|
30
3
|
class LifecycleEnvironmentsCommand < BaseCommand
|
31
4
|
command_name 'lifecycle-environments'
|
32
5
|
desc 'import or export lifecycle environments'
|
33
6
|
|
34
|
-
option %w(--organization), 'ORGANIZATION', 'Only process organization matching this name'
|
35
|
-
|
36
7
|
ORGANIZATION = 'Organization'
|
37
8
|
PRIORENVIRONMENT = 'Prior Environment'
|
38
9
|
DESCRIPTION = 'Description'
|
39
10
|
|
40
11
|
def export
|
41
|
-
CSV.open(
|
42
|
-
csv << [NAME,
|
12
|
+
CSV.open(option_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
13
|
+
csv << [NAME, ORGANIZATION, PRIORENVIRONMENT, DESCRIPTION]
|
43
14
|
@api.resource(:organizations).call(:index, {
|
44
15
|
'per_page' => 999999
|
45
16
|
})['results'].each do |organization|
|
@@ -51,10 +22,9 @@ module HammerCLICsv
|
|
51
22
|
})['results'].sort { |a, b| a['created_at'] <=> b['created_at'] }.each do |environment|
|
52
23
|
if environment['name'] != 'Library'
|
53
24
|
name = environment['name']
|
54
|
-
count = 1
|
55
25
|
prior = environment['prior']['name']
|
56
26
|
description = environment['description']
|
57
|
-
csv << [name,
|
27
|
+
csv << [name, organization['name'], prior, description]
|
58
28
|
end
|
59
29
|
end
|
60
30
|
end
|
@@ -83,7 +53,7 @@ module HammerCLICsv
|
|
83
53
|
def create_environments_from_csv(line)
|
84
54
|
return if option_organization && line[ORGANIZATION] != option_organization
|
85
55
|
|
86
|
-
line[COUNT].
|
56
|
+
count(line[COUNT]).times do |number|
|
87
57
|
name = namify(line[NAME], number)
|
88
58
|
prior = namify(line[PRIORENVIRONMENT], number)
|
89
59
|
raise "Organization '#{line[ORGANIZATION]}' does not exist" if !@existing.include? line[ORGANIZATION]
|
@@ -92,7 +62,7 @@ module HammerCLICsv
|
|
92
62
|
@api.resource(:lifecycle_environments).call(:create, {
|
93
63
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
94
64
|
'name' => name,
|
95
|
-
'prior' => lifecycle_environment(line[ORGANIZATION], :name => prior),
|
65
|
+
#'prior' => lifecycle_environment(line[ORGANIZATION], :name => prior),
|
96
66
|
'prior_id' => lifecycle_environment(line[ORGANIZATION], :name => prior),
|
97
67
|
'description' => line[DESCRIPTION]
|
98
68
|
})
|
@@ -100,9 +70,6 @@ module HammerCLICsv
|
|
100
70
|
print "Updating environment '#{name}'..." if option_verbose?
|
101
71
|
@api.resource(:lifecycle_environments).call(:update, {
|
102
72
|
'id' => @existing[line[ORGANIZATION]][name],
|
103
|
-
'name' => name,
|
104
|
-
'new_name' => name,
|
105
|
-
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
106
73
|
'description' => line[DESCRIPTION]
|
107
74
|
})
|
108
75
|
end
|