hammer_cli_csv 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +15 -0
  2. data/lib/hammer_cli_csv.rb +48 -0
  3. data/lib/hammer_cli_csv/activation_keys.rb +170 -0
  4. data/lib/hammer_cli_csv/architectures.rb +95 -0
  5. data/lib/hammer_cli_csv/base.rb +706 -0
  6. data/lib/hammer_cli_csv/compute_profiles.rb +94 -0
  7. data/lib/hammer_cli_csv/compute_resources.rb +92 -0
  8. data/lib/hammer_cli_csv/content_hosts.rb +357 -0
  9. data/lib/hammer_cli_csv/content_view_filters.rb +158 -0
  10. data/lib/hammer_cli_csv/content_views.rb +86 -0
  11. data/lib/hammer_cli_csv/csv.rb +23 -0
  12. data/lib/hammer_cli_csv/domains.rb +103 -0
  13. data/lib/hammer_cli_csv/exception_handler.rb +53 -0
  14. data/lib/hammer_cli_csv/host_collections.rb +108 -0
  15. data/lib/hammer_cli_csv/hosts.rb +118 -0
  16. data/lib/hammer_cli_csv/import.rb +82 -0
  17. data/lib/hammer_cli_csv/installation_medias.rb +88 -0
  18. data/lib/hammer_cli_csv/lifecycle_environments.rb +116 -0
  19. data/lib/hammer_cli_csv/locations.rb +84 -0
  20. data/lib/hammer_cli_csv/operating_systems.rb +95 -0
  21. data/lib/hammer_cli_csv/organizations.rb +107 -0
  22. data/lib/hammer_cli_csv/partition_tables.rb +98 -0
  23. data/lib/hammer_cli_csv/products.rb +179 -0
  24. data/lib/hammer_cli_csv/provisioning_templates.rb +96 -0
  25. data/lib/hammer_cli_csv/puppet_environments.rb +105 -0
  26. data/lib/hammer_cli_csv/puppet_facts.rb +99 -0
  27. data/lib/hammer_cli_csv/puppet_reports.rb +244 -0
  28. data/lib/hammer_cli_csv/reports.rb +91 -0
  29. data/lib/hammer_cli_csv/roles.rb +115 -0
  30. data/lib/hammer_cli_csv/smart_proxies.rb +88 -0
  31. data/lib/hammer_cli_csv/subnets.rb +121 -0
  32. data/lib/hammer_cli_csv/subscriptions.rb +135 -0
  33. data/lib/hammer_cli_csv/users.rb +133 -0
  34. data/lib/hammer_cli_csv/version.rb +16 -0
  35. data/test/activation_keys_test.rb +17 -0
  36. data/test/apipie_resource_mock.rb +77 -0
  37. data/test/config.template.yml +17 -0
  38. data/test/csv_test_helper.rb +65 -0
  39. data/test/data/activation-keys.csv +119 -0
  40. data/test/data/architectures.csv +5 -0
  41. data/test/data/content-hosts.csv +4 -0
  42. data/test/data/content-view-filters.csv +2 -0
  43. data/test/data/content-views.csv +6 -0
  44. data/test/data/domains.csv +8 -0
  45. data/test/data/host-collections.csv +16 -0
  46. data/test/data/hosts.csv +12 -0
  47. data/test/data/installation-medias.csv +7 -0
  48. data/test/data/lifecycle-environments.csv +6 -0
  49. data/test/data/locations.csv +10 -0
  50. data/test/data/operating-systems.csv +16 -0
  51. data/test/data/organizations.csv +5 -0
  52. data/test/data/partition-tables.csv +187 -0
  53. data/test/data/products.csv +19 -0
  54. data/test/data/puppet-environments.csv +6 -0
  55. data/test/data/puppet-facts.csv +9 -0
  56. data/test/data/reports.csv +4 -0
  57. data/test/data/roles.csv +159 -0
  58. data/test/data/smart-proxies.csv +2 -0
  59. data/test/data/subscriptions.csv +19 -0
  60. data/test/data/users.csv +119 -0
  61. data/test/helpers/command.rb +67 -0
  62. data/test/helpers/resource_disabled.rb +69 -0
  63. data/test/hosts_test.rb +47 -0
  64. data/test/organizations_test.rb +74 -0
  65. data/test/roles_test.rb +39 -0
  66. data/test/setup_test.rb +164 -0
  67. data/test/systems_test.rb +71 -0
  68. data/test/users_test.rb +35 -0
  69. metadata +174 -0
@@ -0,0 +1,95 @@
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
+ # -= 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
+ module HammerCLICsv
32
+ class CsvCommand
33
+ class OperatingSystemsCommand < BaseCommand
34
+ command_name 'operating-systems'
35
+ desc 'import or export operating systems'
36
+
37
+ FAMILY = 'Family'
38
+
39
+ def export
40
+ CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
41
+ csv << [NAME, COUNT, FAMILY]
42
+ @api.resource(:operatingsystems).call(:index, {:per_page => 999999})['results'].each do |operatingsystem|
43
+ name = build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])
44
+ count = 1
45
+ family = operatingsystem['family']
46
+ csv << [name, count, family]
47
+ end
48
+ end
49
+ end
50
+
51
+ def import
52
+ @existing = {}
53
+ @api.resource(:operatingsystems).call(:index, {:per_page => 999999})['results'].each do |operatingsystem|
54
+ @existing[build_os_name(operatingsystem['name'], operatingsystem['major'], operatingsystem['minor'])] = operatingsystem['id'] if operatingsystem
55
+ end
56
+
57
+ thread_import do |line|
58
+ create_operatingsystems_from_csv(line)
59
+ end
60
+ end
61
+
62
+ def create_operatingsystems_from_csv(line)
63
+ line[COUNT].to_i.times do |number|
64
+ name = namify(line[NAME], number)
65
+ (osname, major, minor) = split_os_name(name)
66
+ if !@existing.include? name
67
+ print "Creating operating system '#{name}'..." if option_verbose?
68
+ @api.resource(:operatingsystems).call(:create, {
69
+ 'operatingsystem' => {
70
+ 'name' => osname,
71
+ 'major' => major,
72
+ 'minor' => minor,
73
+ 'family' => line[FAMILY]
74
+ }
75
+ })
76
+ else
77
+ print "Updating operating system '#{name}'..." if option_verbose?
78
+ @api.resource(:operatingsystems).call(:update, {
79
+ 'id' => @existing[name],
80
+ 'operatingsystem' => {
81
+ 'name' => osname,
82
+ 'major' => major,
83
+ 'minor' => minor,
84
+ 'family' => line[FAMILY]
85
+ }
86
+ })
87
+ end
88
+ print "done\n" if option_verbose?
89
+ end
90
+ rescue RuntimeError => e
91
+ raise "#{e}\n #{line}"
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,107 @@
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
+ # -= Organizations CSV =-
14
+ #
15
+ # Columns
16
+ # Name
17
+ # - Name of the organization.
18
+ # - May contain '%d' which will be replaced with current iteration number of Count
19
+ # - eg. "organization%d" -> "organization1"
20
+ # Count
21
+ # - Number of times to iterate on this line of the CSV file
22
+ # Org Label
23
+ # - Label of the organization.
24
+ # - May contain '%d' which will be replaced with current iteration number of Count
25
+ # - eg. "organization%d" -> "organization1"
26
+ # Description
27
+ #
28
+
29
+ require 'hammer_cli'
30
+ #require 'net/http'
31
+ require 'json'
32
+ require 'csv'
33
+
34
+ module HammerCLICsv
35
+ class CsvCommand
36
+ class OrganizationsCommand < BaseCommand
37
+ command_name 'organizations'
38
+ desc 'import or export organizations'
39
+
40
+ option %w(--sam), :flag, 'export from SAM-1.3 or SAM-1.4'
41
+
42
+ ORGANIZATION = 'Organization'
43
+ DESCRIPTION = 'Description'
44
+
45
+ def export
46
+ CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
47
+ csv << [NAME, COUNT, ORGANIZATION, DESCRIPTION]
48
+ if option_sam?
49
+ server = option_server || HammerCLI::Settings.get(:csv, :host)
50
+ username = option_username || HammerCLI::Settings.get(:csv, :username)
51
+ password = option_password || HammerCLI::Settings.get(:csv, :password)
52
+ url = "#{server}/api/organizations"
53
+ uri = URI(url)
54
+ Net::HTTP.start(uri.host, uri.port,
55
+ :use_ssl => uri.scheme == 'https',
56
+ :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
57
+ request = Net::HTTP::Get.new uri.request_uri
58
+ request.basic_auth(username, password)
59
+ response = http.request(request)
60
+
61
+ JSON.parse(response.body).each do |organization|
62
+ csv << [organization['name'], 1, organization['label'], organization['description']]
63
+ end
64
+ end
65
+ else
66
+ @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
67
+ csv << [organization['name'], 1, organization['label'], organization['description']]
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ def import
74
+ @existing = {}
75
+ @api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
76
+ @existing[organization['name']] = organization['id'] if organization
77
+ end
78
+
79
+ thread_import do |line|
80
+ create_organizations_from_csv(line)
81
+ end
82
+ end
83
+
84
+ def create_organizations_from_csv(line)
85
+ line[COUNT].to_i.times do |number|
86
+ name = namify(line[NAME], number)
87
+ label = namify(line[ORGANIZATION], number)
88
+ if !@existing.include? name
89
+ print "Creating organization '#{name}'... " if option_verbose?
90
+ @api.resource(:organizations).call(:create, {
91
+ 'name' => name,
92
+ 'label' => label,
93
+ 'description' => line[DESCRIPTION]
94
+ })
95
+ else
96
+ print "Updating organization '#{name}'... " if option_verbose?
97
+ @api.resource(:organizations).call(:update, {
98
+ 'id' => label,
99
+ 'description' => line[DESCRIPTION]
100
+ })
101
+ end
102
+ print "done\n" if option_verbose?
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,98 @@
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
+ # -= Partition Tables CSV =-
14
+ #
15
+ # Columns
16
+ # Name
17
+ # - Partition table 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 PartitionTablesCommand < BaseCommand
31
+ command_name 'partition-tables'
32
+ desc 'import or export partition tables'
33
+
34
+ OSFAMILY = 'OS Family'
35
+ OPERATINGSYSTEMS = 'Operating Systems'
36
+ LAYOUT = 'Layout'
37
+
38
+ def export
39
+ CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => true}) do |csv|
40
+ csv << [NAME, COUNT, OSFAMILY, OPERATINGSYSTEMS, LAYOUT]
41
+ @api.resource(:ptables).call(:index, {:per_page => 999999})['results'].each do |ptable|
42
+ ptable = @api.resource(:ptables).call(:show, {'id' => ptable['id']})
43
+ name = ptable['name']
44
+ count = 1
45
+ osfamily = ptable['os_family']
46
+ layout = ptable['layout']
47
+ csv << [name, count, osfamily, layout]
48
+ end
49
+ end
50
+ end
51
+
52
+ def import
53
+ @existing = {}
54
+ @api.resource(:ptables).call(:index, {:per_page => 999999})['results'].each do |ptable|
55
+ @existing[ptable['name']] = ptable['id'] if ptable
56
+ end
57
+
58
+ thread_import do |line|
59
+ create_ptables_from_csv(line)
60
+ end
61
+ end
62
+
63
+ def create_ptables_from_csv(line)
64
+ line[COUNT].to_i.times do |number|
65
+ name = namify(line[NAME], number)
66
+ operatingsystem_ids = CSV.parse_line(line[OPERATINGSYSTEMS]).collect do |operatingsystem_name|
67
+ foreman_operatingsystem(:name => operatingsystem_name)
68
+ end if line[OPERATINGSYSTEMS]
69
+ if !@existing.include? name
70
+ print "Creating ptable '#{name}'... " if option_verbose?
71
+ @api.resource(:ptables).call(:create, {
72
+ 'ptable' => {
73
+ 'name' => name,
74
+ 'os_family' => line[OSFAMILY],
75
+ 'operatingsystem_ids' => operatingsystem_ids,
76
+ 'layout' => line[LAYOUT]
77
+ }
78
+ })
79
+ else
80
+ print "Updating ptable '#{name}'..." if option_verbose?
81
+ @api.resource(:ptables).call(:update, {
82
+ 'id' => @existing[name],
83
+ 'ptable' => {
84
+ 'name' => name,
85
+ 'os_family' => line[OSFAMILY],
86
+ 'operatingsystem_ids' => operatingsystem_ids,
87
+ 'layout' => line[LAYOUT]
88
+ }
89
+ })
90
+ end
91
+ print "done\n" if option_verbose?
92
+ end
93
+ rescue RuntimeError => e
94
+ raise "#{e}\n #{line}"
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,179 @@
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
+ module HammerCLICsv
13
+ class CsvCommand
14
+ class ProductsCommand < BaseCommand
15
+ command_name 'products'
16
+ desc 'import or export products'
17
+
18
+ LABEL = 'Label'
19
+ ORGANIZATION = 'Organization'
20
+ REPOSITORY = 'Repository'
21
+ REPOSITORY_TYPE = 'Repository Type'
22
+ REPOSITORY_URL = 'Repository Url'
23
+ DESCRIPTION = 'Description'
24
+
25
+ def export
26
+ CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
27
+ csv << [NAME, COUNT, LABEL, ORGANIZATION, REPOSITORY, REPOSITORY_TYPE, REPOSITORY_URL]
28
+ @api.resource(:organizations)
29
+ .call(:index, {
30
+ :per_page => 999999
31
+ })['results'].each do |organization|
32
+ @api.resource(:products)
33
+ .call(:index, {
34
+ 'per_page' => 999999,
35
+ 'enabled' => true,
36
+ 'organization_id' => foreman_organization(:name => organization['name'])
37
+ })['results'].each do |product|
38
+ product['repositories'].each do |repository|
39
+ repository_type = repository['product_type'] == 'custom' ? 'Custom' : 'Red Hat'
40
+ repository_type += " #{repository['content_type'].capitalize}"
41
+ csv << [product['name'], 1, product['label'], organization['name'],
42
+ repository['name'], repository_type, repository['url']]
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ def import
50
+ @existing_products = {}
51
+ @existing_repositories = {}
52
+
53
+ thread_import do |line|
54
+ create_products_from_csv(line)
55
+ end
56
+ end
57
+
58
+ def create_products_from_csv(line)
59
+ if !@existing_products[line[ORGANIZATION]]
60
+ @existing_products[line[ORGANIZATION]] = {}
61
+ @api.resource(:products)
62
+ .call(:index, {
63
+ 'per_page' => 999999,
64
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
65
+ 'enabled' => true
66
+ })['results'].each do |product|
67
+ @existing_products[line[ORGANIZATION]][product['name']] = product['id']
68
+
69
+ @api.resource(:repositories)
70
+ .call(:index, {
71
+ 'page_size' => 999999, 'paged' => true,
72
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
73
+ 'product_id' => product['id'],
74
+ 'enabled' => true,
75
+ 'library' => true
76
+ })['results'].each do |repository|
77
+ @existing_repositories[line[ORGANIZATION] + product['name']] ||= {}
78
+ @existing_repositories[line[ORGANIZATION] + product['name']][repository['label']] = repository['id']
79
+ end
80
+ end
81
+ end
82
+
83
+ line[COUNT].to_i.times do |number|
84
+ name = namify(line[NAME], number)
85
+ product_id = @existing_products[line[ORGANIZATION]][name]
86
+ if product_id.nil?
87
+ print "Creating product '#{name}'..." if option_verbose?
88
+ if line[REPOSITORY_TYPE] =~ /Red Hat/
89
+ raise "Red Hat product '#{name}' does not exist in '#{line[ORGANIZATION]}'"
90
+ end
91
+
92
+ product_id = @api.resource(:products)
93
+ .call(:create, {
94
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
95
+ 'name' => name
96
+ })['id']
97
+ @existing_products[line[ORGANIZATION]][name] = product_id
98
+ else
99
+ # Nothing to update for products
100
+ print "Updating product '#{name}'..." if option_verbose?
101
+ end
102
+ @existing_repositories[line[ORGANIZATION] + name] = {}
103
+ print "done\n" if option_verbose?
104
+
105
+ repository_name = namify(line[REPOSITORY], number)
106
+
107
+ if !@existing_repositories[line[ORGANIZATION] + name][repository_name]
108
+ @api.resource(:repositories)
109
+ .call(:index, {
110
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
111
+ 'library' => true,
112
+ 'all' => false,
113
+ 'product_id' => product_id
114
+ })['results'].each do |repository|
115
+ @existing_repositories[line[ORGANIZATION] + name][repository['name']] = repository
116
+ end
117
+ end
118
+
119
+ repository = @existing_repositories[line[ORGANIZATION] + name][repository_name]
120
+ if !repository
121
+ raise "Red Hat product '#{name}' does not have repository '#{repository_name}'" if line[REPOSITORY_TYPE] =~ /Red Hat/
122
+
123
+ print "Creating repository '#{repository_name}' in product '#{name}'..." if option_verbose?
124
+ repository = @api.resource(:repositories)
125
+ .call(:create, {
126
+ 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
127
+ 'name' => repository_name,
128
+ 'label' => labelize(repository_name),
129
+ 'product_id' => product_id,
130
+ 'url' => line[REPOSITORY_URL],
131
+ 'content_type' => content_type(line[REPOSITORY_TYPE])
132
+ })
133
+ @existing_repositories[line[ORGANIZATION] + name][line[LABEL]] = repository
134
+ puts 'done' if option_verbose?
135
+ end
136
+
137
+ print "Sync'ing repository '#{repository_name}' in product '#{name}'..." if option_verbose?
138
+ if repository['sync_state'] == 'finished'
139
+ puts 'already done' if option_verbose?
140
+ else
141
+ if line[REPOSITORY_TYPE] =~ /Red Hat/
142
+ print 'skipping Red Hat repo sync... so slow!... '
143
+ else
144
+ sync_repository(line, repository)
145
+ end
146
+ print "done\n" if option_verbose?
147
+ end
148
+ end
149
+
150
+ rescue RuntimeError => e
151
+ raise "#{e}\n #{line}"
152
+ end
153
+
154
+ private
155
+
156
+ def content_type(repository_type)
157
+ case repository_type
158
+ when /yum/i
159
+ 'yum'
160
+ when /puppet/i
161
+ 'puppet'
162
+ else
163
+ raise "Unrecognized repository type '#{repository_type}'"
164
+ end
165
+ end
166
+
167
+ def sync_repository(line, repository)
168
+ # TODO: --server needs to come from config/settings
169
+ args = %W{ repository synchronize
170
+ --id #{ repository['id'] }
171
+ --organization-id #{ foreman_organization(:name => line[ORGANIZATION]) } }
172
+ hammer.run(args)
173
+
174
+ rescue RuntimeError => e
175
+ raise "#{e}\n #{line}"
176
+ end
177
+ end
178
+ end
179
+ end