hammer_cli_csv 0.0.6 → 1.0.0
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/lib/hammer_cli_csv/activation_keys.rb +57 -65
- data/lib/hammer_cli_csv/base.rb +62 -58
- data/lib/hammer_cli_csv/compute_profiles.rb +13 -15
- data/lib/hammer_cli_csv/compute_resources.rb +13 -15
- data/lib/hammer_cli_csv/content_hosts.rb +105 -86
- data/lib/hammer_cli_csv/content_view_filters.rb +73 -31
- data/lib/hammer_cli_csv/content_views.rb +109 -26
- data/lib/hammer_cli_csv/export.rb +64 -18
- data/lib/hammer_cli_csv/host_collections.rb +35 -33
- data/lib/hammer_cli_csv/hosts.rb +20 -20
- data/lib/hammer_cli_csv/i18n.rb +24 -0
- data/lib/hammer_cli_csv/lifecycle_environments.rb +28 -34
- data/lib/hammer_cli_csv/operating_systems.rb +17 -35
- data/lib/hammer_cli_csv/organizations.rb +13 -7
- data/lib/hammer_cli_csv/products.rb +46 -55
- data/lib/hammer_cli_csv/provisioning_templates.rb +41 -49
- data/lib/hammer_cli_csv/puppet_reports.rb +43 -52
- data/lib/hammer_cli_csv/reports.rb +23 -27
- data/lib/hammer_cli_csv/roles.rb +25 -25
- data/lib/hammer_cli_csv/smart_proxies.rb +13 -15
- data/lib/hammer_cli_csv/subnets.rb +16 -18
- data/lib/hammer_cli_csv/subscriptions.rb +35 -34
- data/lib/hammer_cli_csv/version.rb +1 -1
- data/test/content_hosts_test.rb +61 -0
- data/test/csv_test_helper.rb +22 -3
- data/test/data/activation-keys.csv +118 -118
- data/test/helpers/command.rb +4 -4
- data/test/helpers/resource_disabled.rb +3 -3
- data/test/hosts_test.rb +1 -1
- data/test/roles_test.rb +2 -2
- data/test/setup_test.rb +14 -14
- metadata +19 -4
- data/test/systems_test.rb +0 -71
@@ -18,30 +18,58 @@ module HammerCLICsv
|
|
18
18
|
|
19
19
|
option %w(-v --verbose), :flag, 'be verbose'
|
20
20
|
option %w(--threads), 'THREAD_COUNT', 'Number of threads to hammer with', :default => 1
|
21
|
-
option %w(--server), 'SERVER', 'Server URL'
|
22
|
-
option %w(-u --username), 'USERNAME', 'Username to access server'
|
23
|
-
option %w(-p --password), 'PASSWORD', 'Password to access server'
|
24
21
|
option '--dir', 'DIRECTORY', 'directory to import from'
|
25
22
|
|
26
|
-
RESOURCES = %w(
|
27
|
-
|
28
|
-
|
29
|
-
|
23
|
+
RESOURCES = %w(
|
24
|
+
organizations locations puppet_environments operating_systems
|
25
|
+
domains architectures partition_tables lifecycle_environments host_collections
|
26
|
+
provisioning_templates
|
27
|
+
subscriptions activation_keys hosts content_hosts reports roles users
|
28
|
+
)
|
30
29
|
RESOURCES.each do |resource|
|
31
30
|
dashed = resource.sub('_', '-')
|
32
31
|
option "--#{dashed}", 'FILE', "csv file for #{dashed}"
|
33
32
|
end
|
34
33
|
|
35
34
|
def execute
|
36
|
-
@
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
35
|
+
@server = HammerCLI::Settings.settings[:_params][:host] ||
|
36
|
+
HammerCLI::Settings.get(:csv, :host) ||
|
37
|
+
HammerCLI::Settings.get(:katello, :host) ||
|
38
|
+
HammerCLI::Settings.get(:foreman, :host)
|
39
|
+
@username = HammerCLI::Settings.settings[:_params][:username] ||
|
40
|
+
HammerCLI::Settings.get(:csv, :username) ||
|
41
|
+
HammerCLI::Settings.get(:katello, :username) ||
|
42
|
+
HammerCLI::Settings.get(:foreman, :username)
|
43
|
+
@password = HammerCLI::Settings.settings[:_params][:password] ||
|
44
|
+
HammerCLI::Settings.get(:csv, :password) ||
|
45
|
+
HammerCLI::Settings.get(:katello, :password) ||
|
46
|
+
HammerCLI::Settings.get(:foreman, :password)
|
47
|
+
|
48
|
+
@server_status = check_server_status(@server, @username, @password)
|
49
|
+
|
50
|
+
if @server_status['release'] == 'Headpin'
|
51
|
+
@headpin = HeadpinApi.new({
|
52
|
+
:server => @server,
|
53
|
+
:username => @username,
|
54
|
+
:password => @password
|
55
|
+
})
|
56
|
+
skipped_resources = %w( locations puppet_environments operating_systems
|
57
|
+
domains architectures partition_tables lifecycle_environments
|
58
|
+
provisioning_templates
|
59
|
+
hosts reports )
|
60
|
+
skipped_resources += %w( subscriptions content_hosts roles users ) # TODO: not implemented yet
|
61
|
+
else
|
62
|
+
@api = ApipieBindings::API.new({
|
63
|
+
:uri => @server,
|
64
|
+
:username => @username,
|
65
|
+
:password => @password,
|
66
|
+
:api_version => 2
|
67
|
+
})
|
68
|
+
skipped_resources = []
|
69
|
+
end
|
42
70
|
|
43
71
|
# Swing the hammers
|
44
|
-
RESOURCES.each do |resource|
|
72
|
+
(RESOURCES - skipped_resources).each do |resource|
|
45
73
|
hammer_resource(resource)
|
46
74
|
end
|
47
75
|
|
@@ -51,8 +79,8 @@ module HammerCLICsv
|
|
51
79
|
def hammer(context = nil)
|
52
80
|
context ||= {
|
53
81
|
:interactive => false,
|
54
|
-
:username =>
|
55
|
-
:password =>
|
82
|
+
:username => @username,
|
83
|
+
:password => @password
|
56
84
|
}
|
57
85
|
|
58
86
|
HammerCLI::MainCommand.new('', context)
|
@@ -61,12 +89,30 @@ module HammerCLICsv
|
|
61
89
|
def hammer_resource(resource)
|
62
90
|
return if !self.send("option_#{resource}") && !option_dir
|
63
91
|
options_file = self.send("option_#{resource}") || "#{option_dir}/#{resource.sub('_', '-')}.csv"
|
64
|
-
args =
|
92
|
+
args = []
|
93
|
+
args += %W( --server #{@server} ) if @server
|
94
|
+
args += %W( csv #{resource.sub('_', '-')} --csv-export --csv-file #{options_file} )
|
65
95
|
args << '-v' if option_verbose?
|
66
96
|
args += %W( --threads #{option_threads} )
|
67
|
-
|
97
|
+
puts "Exporting '#{args.join(' ')}'" if option_verbose?
|
68
98
|
hammer.run(args)
|
69
99
|
end
|
100
|
+
|
101
|
+
def check_server_status(server, username, password)
|
102
|
+
url = "#{server}/api/status"
|
103
|
+
uri = URI(url)
|
104
|
+
nethttp = Net::HTTP.new(uri.host, uri.port)
|
105
|
+
nethttp.use_ssl = uri.scheme == 'https'
|
106
|
+
nethttp.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
107
|
+
server_status = nethttp.start do |http|
|
108
|
+
request = Net::HTTP::Get.new uri.request_uri
|
109
|
+
request.basic_auth(username, password)
|
110
|
+
response = http.request(request)
|
111
|
+
JSON.parse(response.body)
|
112
|
+
end
|
113
|
+
|
114
|
+
server_status
|
115
|
+
end
|
70
116
|
end
|
71
117
|
end
|
72
118
|
end
|
@@ -39,20 +39,25 @@ module HammerCLICsv
|
|
39
39
|
DESCRIPTION = 'Description'
|
40
40
|
|
41
41
|
def export
|
42
|
-
CSV.open(option_csv_file, 'wb') do |csv|
|
42
|
+
CSV.open(option_csv_file || '/dev/stdout', 'wb') do |csv|
|
43
43
|
csv << [NAME, COUNT, ORGANIZATION, LIMIT, DESCRIPTION]
|
44
|
-
@
|
45
|
-
.
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
44
|
+
if @server_status['release'] == 'Headpin'
|
45
|
+
@headpin.get(:organizations).each do |organization|
|
46
|
+
@headpin.get("organizations/#{organization['label']}/system_groups").each do |systemgroup|
|
47
|
+
csv << [systemgroup['name'], 1, organization['name'],
|
48
|
+
systemgroup['max_systems'].to_i < 0 ? 'Unlimited' : systemgroup['max_systems'],
|
49
|
+
systemgroup['description']]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
else
|
53
|
+
@api.resource(:organizations).call(:index, {'per_page' => 999999})['results'].each do |organization|
|
54
|
+
@api.resource(:host_collections).call(:index, {
|
55
|
+
'organization_id' => organization['id']
|
56
|
+
})['results'].each do |hostcollection|
|
57
|
+
csv << [hostcollection['name'], 1, organization['id'],
|
58
|
+
hostcollection['max_systems'].to_i < 0 ? 'Unlimited' : hostcollection['max_systems'],
|
59
|
+
hostcollection['description']]
|
60
|
+
end
|
56
61
|
end
|
57
62
|
end
|
58
63
|
end
|
@@ -69,11 +74,10 @@ module HammerCLICsv
|
|
69
74
|
def create_hostcollections_from_csv(line)
|
70
75
|
if !@existing[line[ORGANIZATION]]
|
71
76
|
@existing[line[ORGANIZATION]] = {}
|
72
|
-
@api.resource(:host_collections)
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
})['results'].each do |hostcollection|
|
77
|
+
@api.resource(:host_collections).call(:index, {
|
78
|
+
'per_page' => 999999,
|
79
|
+
'organization_id' => foreman_organization(:name => line[ORGANIZATION])
|
80
|
+
})['results'].each do |hostcollection|
|
77
81
|
@existing[line[ORGANIZATION]][hostcollection['name']] = hostcollection['id']
|
78
82
|
end
|
79
83
|
end
|
@@ -82,23 +86,21 @@ module HammerCLICsv
|
|
82
86
|
name = namify(line[NAME], number)
|
83
87
|
if !@existing[line[ORGANIZATION]].include? name
|
84
88
|
print "Creating system group '#{name}'..." if option_verbose?
|
85
|
-
@api.resource(:host_collections)
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
})
|
89
|
+
@api.resource(:host_collections).call(:create, {
|
90
|
+
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
91
|
+
'name' => name,
|
92
|
+
'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT],
|
93
|
+
'description' => line[DESCRIPTION]
|
94
|
+
})
|
92
95
|
else
|
93
96
|
print "Updating system group '#{name}'..." if option_verbose?
|
94
|
-
@api.resource(:host_collections)
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
})
|
97
|
+
@api.resource(:host_collections).call(:update, {
|
98
|
+
'organization_id' => line[ORGANIZATION],
|
99
|
+
'id' => @existing[line[ORGANIZATION]][name],
|
100
|
+
'name' => name,
|
101
|
+
'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT],
|
102
|
+
'description' => line[DESCRIPTION]
|
103
|
+
})
|
102
104
|
end
|
103
105
|
print "done\n" if option_verbose?
|
104
106
|
end
|
data/lib/hammer_cli_csv/hosts.rb
CHANGED
@@ -84,29 +84,29 @@ module HammerCLICsv
|
|
84
84
|
if !@existing.include? name
|
85
85
|
print "Creating host '#{name}'..." if option_verbose?
|
86
86
|
@api.resource(:hosts).call(:create, {
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
87
|
+
'name' => name,
|
88
|
+
'root_pass' => 'changeme',
|
89
|
+
'mac' => namify(line[MACADDRESS], number),
|
90
|
+
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
91
|
+
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
|
92
|
+
'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
|
93
|
+
'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
|
94
|
+
'domain_id' => foreman_domain(:name => line[DOMAIN]),
|
95
|
+
'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
|
96
|
+
})
|
97
97
|
else
|
98
98
|
print "Updating host '#{name}'..." if option_verbose?
|
99
99
|
@api.resource(:hosts).call(:update, {
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
100
|
+
'id' => @existing[name],
|
101
|
+
'name' => name,
|
102
|
+
'mac' => namify(line[MACADDRESS], number),
|
103
|
+
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
104
|
+
'environment_id' => foreman_environment(:name => line[ENVIRONMENT]),
|
105
|
+
'operatingsystem_id' => foreman_operatingsystem(:name => line[OPERATINGSYSTEM]),
|
106
|
+
'architecture_id' => foreman_architecture(:name => line[ARCHITECTURE]),
|
107
|
+
'domain_id' => foreman_domain(:name => line[DOMAIN]),
|
108
|
+
'ptable_id' => foreman_partitiontable(:name => line[PARTITIONTABLE])
|
109
|
+
})
|
110
110
|
end
|
111
111
|
print "done\n" if option_verbose?
|
112
112
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'hammer_cli/i18n'
|
2
|
+
|
3
|
+
module HammerCLICsv
|
4
|
+
module I18n
|
5
|
+
|
6
|
+
class LocaleDomain < HammerCLI::I18n::LocaleDomain
|
7
|
+
|
8
|
+
def translated_files
|
9
|
+
Dir.glob(File.join(File.dirname(__FILE__), '../**/*.rb'))
|
10
|
+
end
|
11
|
+
|
12
|
+
def locale_dir
|
13
|
+
File.join(File.dirname(__FILE__), '../../locale')
|
14
|
+
end
|
15
|
+
|
16
|
+
def domain_name
|
17
|
+
'hammer_cli_csv'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
HammerCLI::I18n.add_domain(HammerCLICsv::I18n::LocaleDomain.new)
|
@@ -38,15 +38,13 @@ module HammerCLICsv
|
|
38
38
|
def export
|
39
39
|
CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
|
40
40
|
csv << [NAME, COUNT, ORGANIZATION, PRIORENVIRONMENT, DESCRIPTION]
|
41
|
-
@api.resource(:organizations)
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
'organization_id' => organization['id']
|
49
|
-
})['results'].each do |environment|
|
41
|
+
@api.resource(:organizations).call(:index, {
|
42
|
+
'per_page' => 999999
|
43
|
+
})['results'].each do |organization|
|
44
|
+
@api.resource(:lifecycle_environments).call(:index, {
|
45
|
+
'per_page' => 999999,
|
46
|
+
'organization_id' => organization['id']
|
47
|
+
})['results'].each do |environment|
|
50
48
|
if environment['name'] != 'Library'
|
51
49
|
name = environment['name']
|
52
50
|
count = 1
|
@@ -61,15 +59,13 @@ module HammerCLICsv
|
|
61
59
|
|
62
60
|
def import
|
63
61
|
@existing = {}
|
64
|
-
@api.resource(:organizations)
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
'organization_id' => foreman_organization(:name => organization['name'])
|
72
|
-
})['results'].each do |environment|
|
62
|
+
@api.resource(:organizations).call(:index, {
|
63
|
+
'per_page' => 999999
|
64
|
+
})['results'].each do |organization|
|
65
|
+
@api.resource(:lifecycle_environments).call(:index, {
|
66
|
+
'per_page' => 999999,
|
67
|
+
'organization_id' => foreman_organization(:name => organization['name'])
|
68
|
+
})['results'].each do |environment|
|
73
69
|
@existing[organization['name']] ||= {}
|
74
70
|
@existing[organization['name']][environment['name']] = environment['id'] if environment
|
75
71
|
end
|
@@ -87,24 +83,22 @@ module HammerCLICsv
|
|
87
83
|
raise "Organization '#{line[ORGANIZATION]}' does not exist" if !@existing.include? line[ORGANIZATION]
|
88
84
|
if !@existing[line[ORGANIZATION]].include? name
|
89
85
|
print "Creating environment '#{name}'..." if option_verbose?
|
90
|
-
@api.resource(:lifecycle_environments)
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
})
|
86
|
+
@api.resource(:lifecycle_environments).call(:create, {
|
87
|
+
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
88
|
+
'name' => name,
|
89
|
+
'prior' => lifecycle_environment(line[ORGANIZATION], :name => prior),
|
90
|
+
'description' => line[DESCRIPTION]
|
91
|
+
})
|
97
92
|
else
|
98
93
|
print "Updating environment '#{name}'..." if option_verbose?
|
99
|
-
@api.resource(:lifecycle_environments)
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
})
|
94
|
+
@api.resource(:lifecycle_environments).call(:update, {
|
95
|
+
'id' => @existing[line[ORGANIZATION]][name],
|
96
|
+
'name' => name,
|
97
|
+
'new_name' => name,
|
98
|
+
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
99
|
+
'prior' => prior,
|
100
|
+
'description' => line[DESCRIPTION]
|
101
|
+
})
|
108
102
|
end
|
109
103
|
print "done\n" if option_verbose?
|
110
104
|
end
|
@@ -9,25 +9,6 @@
|
|
9
9
|
# have received a copy of GPLv2 along with this software; if not, see
|
10
10
|
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
|
11
11
|
|
12
|
-
#
|
13
|
-
# -= Operating Systems CSV =-
|
14
|
-
#
|
15
|
-
# Columns
|
16
|
-
# Name
|
17
|
-
# - Operating 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
|
-
# Major
|
23
|
-
# Minor
|
24
|
-
# Family
|
25
|
-
#
|
26
|
-
|
27
|
-
require 'hammer_cli'
|
28
|
-
require 'json'
|
29
|
-
require 'csv'
|
30
|
-
|
31
12
|
module HammerCLICsv
|
32
13
|
class CsvCommand
|
33
14
|
class OperatingSystemsCommand < BaseCommand
|
@@ -42,8 +23,9 @@ module HammerCLICsv
|
|
42
23
|
@api.resource(:operatingsystems).call(:index, {:per_page => 999999})['results'].each do |operatingsystem|
|
43
24
|
name = build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])
|
44
25
|
count = 1
|
26
|
+
description = operatingsystem['description']
|
45
27
|
family = operatingsystem['family']
|
46
|
-
csv << [name, count, family]
|
28
|
+
csv << [name, count, description, family]
|
47
29
|
end
|
48
30
|
end
|
49
31
|
end
|
@@ -66,24 +48,24 @@ module HammerCLICsv
|
|
66
48
|
if !@existing.include? name
|
67
49
|
print "Creating operating system '#{name}'..." if option_verbose?
|
68
50
|
@api.resource(:operatingsystems).call(:create, {
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
51
|
+
'operatingsystem' => {
|
52
|
+
'name' => osname,
|
53
|
+
'major' => major,
|
54
|
+
'minor' => minor,
|
55
|
+
'family' => line[FAMILY]
|
56
|
+
}
|
57
|
+
})
|
76
58
|
else
|
77
59
|
print "Updating operating system '#{name}'..." if option_verbose?
|
78
60
|
@api.resource(:operatingsystems).call(:update, {
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
61
|
+
'id' => @existing[name],
|
62
|
+
'operatingsystem' => {
|
63
|
+
'name' => osname,
|
64
|
+
'major' => major,
|
65
|
+
'minor' => minor,
|
66
|
+
'family' => line[FAMILY]
|
67
|
+
}
|
68
|
+
})
|
87
69
|
end
|
88
70
|
print "done\n" if option_verbose?
|
89
71
|
end
|
@@ -74,16 +74,22 @@ module HammerCLICsv
|
|
74
74
|
if !@existing.include? name
|
75
75
|
print "Creating organization '#{name}'... " if option_verbose?
|
76
76
|
@api.resource(:organizations).call(:create, {
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
77
|
+
'name' => name,
|
78
|
+
'organization' => {
|
79
|
+
'name' => name,
|
80
|
+
'label' => label,
|
81
|
+
'description' => line[DESCRIPTION]
|
82
|
+
}
|
83
|
+
})
|
81
84
|
else
|
82
85
|
print "Updating organization '#{name}'... " if option_verbose?
|
83
86
|
@api.resource(:organizations).call(:update, {
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
+
'id' => foreman_organization(:name => name),
|
88
|
+
'organization' => {
|
89
|
+
'id' => foreman_organization(:name => name),
|
90
|
+
'description' => line[DESCRIPTION]
|
91
|
+
}
|
92
|
+
})
|
87
93
|
end
|
88
94
|
print "done\n" if option_verbose?
|
89
95
|
end
|