hammer_cli_csv 0.0.6 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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( organizations locations puppet_environments operating_systems
27
- domains architectures partition_tables lifecycle_environments host_collections
28
- provisioning_templates
29
- subscriptions activation_keys hosts content_hosts reports roles users )
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
- @api = ApipieBindings::API.new({
37
- :uri => option_server || HammerCLI::Settings.get(:csv, :host),
38
- :username => option_username || HammerCLI::Settings.get(:csv, :username),
39
- :password => option_password || HammerCLI::Settings.get(:csv, :password),
40
- :api_version => 2
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 => 'admin', # TODO: this needs to come from config/settings
55
- :password => 'changeme' # TODO: this needs to come from config/settings
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 = %W( csv #{resource.sub('_', '-')} --csv-export --csv-file #{options_file} )
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
- args += %W( --server #{option_server} ) if option_server
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
- @api.resource(:organizations)\
45
- .call(:index, {
46
- 'per_page' => 999999
47
- })['results'].each do |organization|
48
- @api.resource(:host_collections)\
49
- .call(:index, {
50
- 'organization_id' => organization['id']
51
- }).each do |hostcollection|
52
- puts hostcollection
53
- csv << [hostcollection['name'], 1, organization['id'],
54
- hostcollection['max_systems'].to_i < 0 ? 'Unlimited' : sytemgroup['max_systems'],
55
- hostcollection['description']]
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
- .call(:index, {
74
- 'per_page' => 999999,
75
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION])
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
- .call(:create, {
87
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
88
- 'name' => name,
89
- 'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT],
90
- 'description' => line[DESCRIPTION]
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
- .call(:update, {
96
- 'organization_id' => line[ORGANIZATION],
97
- 'id' => @existing[line[ORGANIZATION]][name],
98
- 'name' => name,
99
- 'max_systems' => (line[LIMIT] == 'Unlimited') ? -1 : line[LIMIT],
100
- 'description' => line[DESCRIPTION]
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
@@ -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
- '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
- })
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
- '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
- })
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
- .call(:index, {
43
- 'per_page' => 999999
44
- })['results'].each do |organization|
45
- @api.resource(:lifecycle_environments)\
46
- .call(:index, {
47
- 'per_page' => 999999,
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
- .call(:index, {
66
- 'per_page' => 999999
67
- })['results'].each do |organization|
68
- @api.resource(:lifecycle_environments)\
69
- .call(:index, {
70
- 'per_page' => 999999,
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
- .call(:create, {
92
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
93
- 'name' => name,
94
- 'prior' => lifecycle_environment(line[ORGANIZATION], :name => prior),
95
- 'description' => line[DESCRIPTION]
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
- .call(:update, {
101
- 'id' => @existing[line[ORGANIZATION]][name],
102
- 'name' => name,
103
- 'new_name' => name,
104
- 'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
105
- 'prior' => prior,
106
- 'description' => line[DESCRIPTION]
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
- 'operatingsystem' => {
70
- 'name' => osname,
71
- 'major' => major,
72
- 'minor' => minor,
73
- 'family' => line[FAMILY]
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
- 'id' => @existing[name],
80
- 'operatingsystem' => {
81
- 'name' => osname,
82
- 'major' => major,
83
- 'minor' => minor,
84
- 'family' => line[FAMILY]
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
- 'name' => name,
78
- 'label' => label,
79
- 'description' => line[DESCRIPTION]
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
- 'id' => label,
85
- 'description' => line[DESCRIPTION]
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