foreman_virt_who_configure 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/foreman_virt_who_configure/config_copy_and_paste.js +8 -0
  3. data/app/assets/javascripts/foreman_virt_who_configure/config_edit.js +23 -8
  4. data/app/controllers/foreman_virt_who_configure/api/v2/configs_controller.rb +120 -0
  5. data/app/controllers/foreman_virt_who_configure/configs_controller.rb +17 -2
  6. data/app/helpers/foreman_virt_who_configure/compatibility_helper.rb +9 -0
  7. data/app/helpers/foreman_virt_who_configure/configs_helper.rb +4 -10
  8. data/app/models/foreman_virt_who_configure/auth_source_hidden_with_authentication.rb +10 -1
  9. data/app/models/foreman_virt_who_configure/config.rb +24 -29
  10. data/app/models/foreman_virt_who_configure/output_generator.rb +99 -17
  11. data/app/services/sso/basic_with_hidden.rb +1 -1
  12. data/app/views/dashboard/_foreman_virt_who_configs_status_widget.html.erb +7 -8
  13. data/app/views/foreman_virt_who_configure/api/v2/configs/base.json.rabl +3 -0
  14. data/app/views/foreman_virt_who_configure/api/v2/configs/create.json.rabl +3 -0
  15. data/app/views/foreman_virt_who_configure/api/v2/configs/deploy_script.json.rabl +3 -0
  16. data/app/views/foreman_virt_who_configure/api/v2/configs/index.json.rabl +3 -0
  17. data/app/views/foreman_virt_who_configure/api/v2/configs/main.json.rabl +8 -0
  18. data/app/views/foreman_virt_who_configure/api/v2/configs/show.json.rabl +3 -0
  19. data/app/views/foreman_virt_who_configure/api/v2/configs/update.json.rabl +3 -0
  20. data/app/views/foreman_virt_who_configure/configs/show.html.erb +10 -4
  21. data/app/views/foreman_virt_who_configure/configs/steps/_connection_form.erb +13 -13
  22. data/app/views/foreman_virt_who_configure/configs/steps/_general_information_form.erb +4 -4
  23. data/app/views/foreman_virt_who_configure/configs/steps/_schedule_form.erb +1 -2
  24. data/config/routes.rb +19 -0
  25. data/lib/foreman_virt_who_configure/engine.rb +18 -4
  26. data/lib/foreman_virt_who_configure/version.rb +1 -1
  27. data/locale/action_names.rb +1 -0
  28. data/locale/en/LC_MESSAGES/foreman_virt_who_configure.mo +0 -0
  29. data/locale/en/foreman_virt_who_configure.edit.po +733 -0
  30. data/locale/en/foreman_virt_who_configure.po +262 -2
  31. data/locale/en/foreman_virt_who_configure.po.time_stamp +0 -0
  32. data/locale/foreman_virt_who_configure.pot +441 -8
  33. data/test/functional/api/v2/configs_controller_test.rb +178 -0
  34. data/test/unit/output_generator_test.rb +29 -0
  35. metadata +19 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc18a0a6855645558a541c028fdbc976a1e82a28
4
- data.tar.gz: c665d9f4302ec737940875c5c668af1eeaf84588
3
+ metadata.gz: fcd1a62a927233569d45344186f1797f8f83f858
4
+ data.tar.gz: d85400e71ed4d0beb6de66f47a793cf0083cb35e
5
5
  SHA512:
6
- metadata.gz: 94a21f9543e5519eadfda90acdddc83f5d035e14eca3c1952d5228e79eb608a6658c7852166fee9a3b320bdc56fded20291f8e036abe86bc2415f647e0136f32
7
- data.tar.gz: 4727236938d4b4e145fb6452698985173f6292d476ea123362b83a3b55674e9eaefa942d7c2896acaf09255846f852ee98d87d68b946584b56551dfaa93f1464
6
+ metadata.gz: 4bc9ac6611d4c47f0323afdf087759cba98545e4d101a8b61ad974e2c97a8cc6a7ef05627bbf5c842261a03edb2a7063ab434ae1e12ad745ecd2ddef36be8ee3
7
+ data.tar.gz: bfb46ba995b7574ebd567e5f6017835bb27d688a85f24f9de7c368f2ff37cc64ffb44e80b79d215a85e67dfffecc2991fc2fe32a9d34714c80bec1bd2878d18f
@@ -0,0 +1,8 @@
1
+ function virt_who_copy_configuration_to_clipboard(text) {
2
+ var aux = document.createElement("textarea");
3
+ $(aux).html(text);
4
+ document.body.appendChild(aux);
5
+ aux.select();
6
+ document.execCommand("copy");
7
+ document.body.removeChild(aux);
8
+ }
@@ -16,16 +16,31 @@ function virt_who_update_listing_mode() {
16
16
  }
17
17
  }
18
18
 
19
- $(document).ready(function () {
20
- $('#foreman_virt_who_configure_config_hypervisor_type').change(function () {
21
- selected_type = $(this).val();
22
- $.each(['hypervisor_server', 'hypervisor_username'], function(index, value) {
23
- var element = $('#foreman_virt_who_configure_config_' + value)
24
- var help = element.data("help")[selected_type];
25
- element.parent().siblings('span.help-block.help-inline').children('a[rel=popover]').attr('data-content', help);
26
- });
19
+ function virt_who_update_hypervisor_fields() {
20
+ selected_type = $('#foreman_virt_who_configure_config_hypervisor_type').val();
21
+ var element = $('#foreman_virt_who_configure_config_hypervisor_password');
22
+ element.closest('.form-group').toggle(selected_type != 'libvirt');
23
+ }
24
+
25
+ function virt_who_update_credentials_help() {
26
+ selected_type = $(this).val();
27
+ $.each(['hypervisor_server', 'hypervisor_username'], function(index, value) {
28
+ var element = $('#foreman_virt_who_configure_config_' + value);
29
+ var help = element.data("help")[selected_type];
30
+ var inline_help_popover = element.parents('.form-group').find('.help-inline a[rel=popover]');
31
+ if (inline_help_popover[0]) {
32
+ // pre Foreman 1.15
33
+ inline_help_popover.attr('data-content', help);
34
+ } else {
35
+ element.parents('.form-group').find('label a[rel=popover]').attr('data-content', help);
36
+ }
27
37
  });
38
+ }
28
39
 
40
+ $(document).ready(function () {
29
41
  virt_who_update_listing_mode();
42
+ virt_who_update_hypervisor_fields();
30
43
  $('#foreman_virt_who_configure_config_listing_mode').change(virt_who_update_listing_mode);
44
+ $('#foreman_virt_who_configure_config_hypervisor_type').change(virt_who_update_credentials_help);
45
+ $('#foreman_virt_who_configure_config_hypervisor_type').change(virt_who_update_hypervisor_fields);
31
46
  });
@@ -0,0 +1,120 @@
1
+ module ForemanVirtWhoConfigure
2
+ module Api
3
+ module V2
4
+ class ConfigsController < ::Api::V2::BaseController
5
+ include ::Api::Version2
6
+ include ForemanVirtWhoConfigure::Concerns::ConfigParameters if Foreman::Version.new.short >= '1.13'
7
+ resource_description do
8
+ api_base_url "/foreman_virt_who_configure/api"
9
+ end
10
+
11
+ before_action :find_resource, :only => %w{show deploy_script update destroy}
12
+
13
+ api :GET, '/configs', N_("List of virt-who configurations")
14
+ api :GET, "/organizations/:organization_id/configs", N_("List of virt-who configurations per organization")
15
+ param_group :search_and_pagination, ::Api::V2::BaseController
16
+
17
+ def index
18
+ @configs = resource_scope_for_index
19
+ end
20
+
21
+ api :GET, "/configs/:id/", N_("Show a virt-who configuration")
22
+ param :id, :identifier, :required => true
23
+
24
+ def show
25
+ end
26
+
27
+ api :GET, "/configs/:id/deploy_script/", N_("Renders a deploy script for the specified virt-who configuration")
28
+ param :id, :identifier, :required => true
29
+ def deploy_script
30
+ respond_to do |format|
31
+ format.text { render :text => @config.virt_who_bash_script }
32
+ format.sh { render :text => @config.virt_who_bash_script }
33
+ format.json
34
+ end
35
+ end
36
+
37
+ def_param_group :config do
38
+ param :foreman_virt_who_configure_config, Hash, :required => true, :action_aware => true do
39
+ param :name, String, :desc => N_("Configuration name"), :required => true
40
+ param :interval, Config::AVAILABLE_INTERVALS.keys.map(&:to_i), :desc => N_("Configuration interval in minutes"), :required => true
41
+ param :filtering_mode, Config::FILTERING_MODES.keys.map(&:to_i), :desc => N_("Hypervisor filtering mode, %{unlimited} means no filtering, %{whitelist} means whitelist, %{blacklist} means blacklist") % { :unlimited => Config::UNLIMITED, :whitelist => Config::WHITELIST, :blacklist => Config::BLACKLIST }, :required => true
42
+ param :whitelist, String, :desc => N_("Hypervisor whitelist, applicable only when filtering mode is set to 1. Wildcards and regular expressions are supported, multiple records must be separated by comma."), :required => false
43
+ param :blacklist, String, :desc => N_("Hypervisor blacklist, applicable only when filtering mode is set to 2. Wildcards and regular expressions are supported, multiple records must be separated by comma."), :required => false
44
+ param :hypervisor_id, Config::HYPERVISOR_IDS, :desc => N_("Specifies how the hypervisor will be identified."), :required => true
45
+ param :hypervisor_type, Config::HYPERVISOR_TYPES.keys, :desc => N_("Hypervisor type"), :required => true
46
+ param :hypervisor_server, String, :desc => N_("Fully qualified host name or IP address of the hypervisor"), :required => true
47
+ param :hypervisor_username, String, :desc => N_("Account name by which virt-who is to connect to the hypervisor."), :required => true
48
+ param :hypervisor_password, String, :desc => N_("Hypervisor password, required for all hypervisor types except for libvirt"), :required => false
49
+ param :satellite_url, String, :desc => N_("Satellite server FQDN"), :required => true
50
+ param :debug, :bool, :desc => N_("Enable debugging output")
51
+ param :proxy, String, :desc => N_('HTTP proxy that should be used for communication between the server on which virt-who is running and the hypervisors and virtualization managers.'), :required => false
52
+ param :no_proxy, String, :desc => N_("Ignore proxy. A comma-separated list of hostnames or domains or ip addresses to ignore proxy settings for. Optionally this may be set to * to bypass proxy settings for all hostnames domains or ip addresses.")
53
+ param :organization_id, Integer, :required => true, :desc => N_("Organization of the virt-who configuration") if ::SETTINGS[:organizations_enabled]
54
+ end
55
+ end
56
+
57
+ api :POST, '/configs', N_("Create a virt-who configuration")
58
+ param_group :config, :as => :create
59
+
60
+ def create
61
+ @config = Config.new(config_params)
62
+ process_response @config.save
63
+ end
64
+
65
+ api :PUT, '/configs/:id', N_("Update a virt-who configuration")
66
+ param :id, :number, :desc => N_("Configuration numeric identifier"), :required => true
67
+ param_group :config
68
+
69
+ def update
70
+ process_response @config.update_attributes(config_params)
71
+ end
72
+
73
+ api :DELETE, '/configs/:id', N_("Delete a virt-who configuration")
74
+ param :id, :number, :desc => N_("Configuration numeric identifier"), :required => true
75
+
76
+ def destroy
77
+ process_response @config.destroy
78
+ end
79
+
80
+ def apply_organization_filter(scope)
81
+ scope.for_organization(Organization.current)
82
+ end
83
+
84
+ def resource_scope(*args)
85
+ apply_organization_filter(super)
86
+ end
87
+
88
+ def resource_class
89
+ ForemanVirtWhoConfigure::Config
90
+ end
91
+
92
+ protected
93
+
94
+ def action_permission
95
+ case params[:action]
96
+ when 'deploy_script'
97
+ 'view'
98
+ else
99
+ super
100
+ end
101
+ end
102
+
103
+ private
104
+
105
+ # compatibility layer for 1.11 - pre strong params patch
106
+ def config_params
107
+ if defined?(super)
108
+ super
109
+ else
110
+ params[:foreman_virt_who_configure_config]
111
+ end
112
+ end
113
+
114
+ def allowed_nested_id
115
+ %w(organization_id)
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -2,7 +2,7 @@ module ForemanVirtWhoConfigure
2
2
  class ConfigsController < ::ForemanVirtWhoConfigure::ApplicationController
3
3
  include Foreman::Controller::AutoCompleteSearch
4
4
  include ForemanVirtWhoConfigure::Concerns::ConfigParameters if Foreman::Version.new.short >= '1.13'
5
- before_action :find_resource, :only => [:edit, :update, :destroy, :show]
5
+ before_action :find_resource, :only => [:edit, :update, :destroy, :show, :deploy_script]
6
6
 
7
7
  # in 1.11 we can't use welcome from application controller since it does not work with namespaces
8
8
  def welcome
@@ -30,7 +30,7 @@ module ForemanVirtWhoConfigure
30
30
  end
31
31
  @config.hypervisor_type ||= Config::HYPERVISOR_DEFAULT_TYPE
32
32
  @config.organization ||= Organization.current
33
- @config.satellite_url ||= URI.parse(Setting.foreman_url).host
33
+ @config.satellite_url ||= URI.parse(Setting[:foreman_url]).host
34
34
  end
35
35
 
36
36
  def create
@@ -64,6 +64,10 @@ module ForemanVirtWhoConfigure
64
64
  end
65
65
  end
66
66
 
67
+ def deploy_script
68
+ send_data @config.virt_who_bash_script, :filename => "deploy_virt_who_config_#{@config.id}.sh", :type => 'text/x-shellscript', :disposition => :attachment
69
+ end
70
+
67
71
  def auto_complete_controller_name
68
72
  'foreman_virt_who_configure_configs'
69
73
  end
@@ -90,5 +94,16 @@ module ForemanVirtWhoConfigure
90
94
  def resource_scope(*args)
91
95
  apply_organization_filter(super)
92
96
  end
97
+
98
+ # equivalent to which was added after 1.11
99
+ # define_action_permission ['deploy_script'], :view
100
+ def action_permission
101
+ case params[:action]
102
+ when 'deploy_script'
103
+ :view
104
+ else
105
+ super
106
+ end
107
+ end
93
108
  end
94
109
  end
@@ -18,5 +18,14 @@ module ForemanVirtWhoConfigure
18
18
  help_path
19
19
  end
20
20
  end
21
+
22
+ def inline_help_popover(message)
23
+ version = Foreman::Version.new
24
+ if version.major.to_i >= 1 && version.minor.to_i >= 15
25
+ { :label_help => message.html_safe }
26
+ else
27
+ { :help_inline => popover('', message).html_safe }
28
+ end
29
+ end
21
30
  end
22
31
  end
@@ -18,7 +18,7 @@ module ForemanVirtWhoConfigure
18
18
  # 'vdsm' => '',
19
19
  'hyperv' => _('Account name by which virt-who is to connect to the hypervisor. By default this is <code>Administrator</code>. To use an alternate account, create a user account and assign that account to the following groups (Windows 2012 Server): Hyper-V Administrators and Remote Management Users.'),
20
20
  'xen' => _('Account name by which virt-who is to connect to the hypervisor.'),
21
- 'libvirt' => _('Account name by which virt-who is to connect to the hypervisor.')
21
+ 'libvirt' => _('Account name by which virt-who is to connect to the hypervisor. Virt-who does not support password based authentication, you must manually setup SSH key, see <a href="https://access.redhat.com/solutions/1515983">Red Hat Knowledgebase solution How to configure virt-who for a KVM host</a> for more information.')
22
22
  }
23
23
  end
24
24
 
@@ -26,9 +26,7 @@ module ForemanVirtWhoConfigure
26
26
  message = case config.status
27
27
  when :unknown
28
28
  _('No Report Yet')
29
- when :ok
30
- l(config.last_report_at, :format => :long)
31
- when :error
29
+ when :ok, :out_of_date
32
30
  l(config.last_report_at, :format => :long)
33
31
  else
34
32
  _('Unknown configuration status')
@@ -43,10 +41,8 @@ module ForemanVirtWhoConfigure
43
41
 
44
42
  def report_status_class(status)
45
43
  icon_class = case status
46
- when :ok
44
+ when :ok, :out_of_date
47
45
  'pficon-ok'
48
- when :error
49
- 'pficon-error-circle-o'
50
46
  when :unknown
51
47
  'pficon-info'
52
48
  else
@@ -58,12 +54,10 @@ module ForemanVirtWhoConfigure
58
54
 
59
55
  def status_class(status)
60
56
  case status
61
- when :ok
57
+ when :ok, :out_of_date
62
58
  'status-ok'.html_safe
63
59
  when :unknown
64
60
  'status-info'.html_safe
65
- when :error
66
- 'status-error'.html_safe
67
61
  else
68
62
  'status-warn'.html_safe
69
63
  end
@@ -6,8 +6,17 @@ module ForemanVirtWhoConfigure
6
6
  end
7
7
  end
8
8
 
9
+ def auth_method_name
10
+ "HIDDEN with API access"
11
+ end
12
+ alias_method :to_label, :auth_method_name
13
+
9
14
  def authenticate(login, password)
10
- AuthSourceInternal.new.authenticate(login, password)
15
+ # the authentication should only happen if our SSO signals that it's available
16
+ # we need to reset the signal since for non API calls the SSO is not used at all and does not set signal to false
17
+ result = Thread.current[:service_user_login] ? AuthSourceInternal.new.authenticate(login, password) : false
18
+ Thread.current[:service_user_login] = nil
19
+ result
11
20
  end
12
21
  end
13
22
  end
@@ -6,22 +6,22 @@ module ForemanVirtWhoConfigure
6
6
  :satellite_url, :proxy, :no_proxy, :name
7
7
  ]
8
8
  include Authorizable
9
- audited
9
+ audited :except => [ :hypervisor_password, :last_report_at, :out_of_date_at ]
10
10
  validates_lengths_from_database
11
11
 
12
12
  UNLIMITED = 0
13
13
  WHITELIST = 1
14
14
  BLACKLIST = 2
15
15
  FILTERING_MODES = {
16
- UNLIMITED.to_s => _('Unlimited'),
17
- WHITELIST.to_s => _('Whitelist'),
18
- BLACKLIST.to_s => _('Blacklist'),
16
+ UNLIMITED.to_s => N_('Unlimited'),
17
+ WHITELIST.to_s => N_('Whitelist'),
18
+ BLACKLIST.to_s => N_('Blacklist'),
19
19
  }
20
20
 
21
21
  WIZARD_STEPS = {
22
- 'general_information' => _('General information'),
23
- 'schedule' => _('Schedule'),
24
- 'connection' => _('Connection')
22
+ 'general_information' => N_('General information'),
23
+ 'schedule' => N_('Schedule'),
24
+ 'connection' => N_('Connection')
25
25
  }
26
26
 
27
27
  HYPERVISOR_IDS = [ 'hostname', 'uuid', 'hwuuid' ]
@@ -38,18 +38,18 @@ module ForemanVirtWhoConfigure
38
38
  HYPERVISOR_DEFAULT_TYPE = 'esx'
39
39
 
40
40
  AVAILABLE_INTERVALS = {
41
- '60' => _('Every hour'),
42
- '120' => _('Every 2 hours'),
43
- '240' => _('Every 4 hours'),
44
- '480' => _('Every 8 hours'),
45
- '720' => _('Every 12 hours'),
41
+ '60' => N_('Every hour'),
42
+ '120' => N_('Every 2 hours'),
43
+ '240' => N_('Every 4 hours'),
44
+ '480' => N_('Every 8 hours'),
45
+ '720' => N_('Every 12 hours'),
46
46
  }
47
47
 
48
48
  DEFAULT_INTERVAL = 120
49
49
 
50
50
  STATUSES = {
51
51
  'ok' => N_('OK'),
52
- 'error' => N_('Error'),
52
+ 'out_of_date' => N_('No change'),
53
53
  'unknown' => N_('Unknown')
54
54
  }
55
55
 
@@ -57,7 +57,7 @@ module ForemanVirtWhoConfigure
57
57
  {
58
58
  :unknown => N_('The configuration was not deployed yet or the virt-who was unable to report the status'),
59
59
  :ok => N_('The virt-who report arrived within the interval'),
60
- :error => N_('The virt-who report has not arrived within the interval, please check the virt-who reporter status and logs')
60
+ :out_of_date => N_('The virt-who report has not arrived within the interval, which indicates there was no change on hypervisor')
61
61
  }
62
62
  )
63
63
 
@@ -77,16 +77,6 @@ module ForemanVirtWhoConfigure
77
77
  scoped_search :on => :last_report_at, :complete_value => true, :only_explicit => true
78
78
  # TODO add more related objects and attributes
79
79
 
80
- # Foreman 1.11 specifics, can be removed later, otherwise when string does not start with "encrypts" prefix
81
- # we get 500 when we try to create log message that relies on name method
82
- def name
83
- title
84
- end
85
-
86
- def title
87
- compute_resource ? compute_resource.name : hypervisor_server
88
- end
89
-
90
80
  # compatibility layer for 1.11 - pre strong params patch
91
81
  if self.respond_to?(:attr_accessible)
92
82
  attr_accessible(*PERMITTED_PARAMS)
@@ -117,7 +107,7 @@ module ForemanVirtWhoConfigure
117
107
  sanitize_sql_for_conditions([' out_of_date_at >= ? ', DateTime.now.utc.to_s(:db)])
118
108
  when 'unknown'
119
109
  sanitize_sql_for_conditions({:last_report_at => nil})
120
- when 'error'
110
+ when 'out_of_date'
121
111
  sanitize_sql_for_conditions([' out_of_date_at < ? ', DateTime.now.utc.to_s(:db)])
122
112
  end
123
113
  { :conditions => condition }
@@ -169,17 +159,21 @@ module ForemanVirtWhoConfigure
169
159
  end
170
160
 
171
161
  def step_name(step_key)
172
- WIZARD_STEPS[step_key]
162
+ _(WIZARD_STEPS[step_key])
173
163
  end
174
164
 
175
165
  def steps
176
166
  WIZARD_STEPS.keys
177
167
  end
178
168
 
179
- def virt_who_config_file
169
+ def virt_who_bash_script
170
+ virt_who_config_script(:bash_script)
171
+ end
172
+
173
+ def virt_who_config_script(format = nil)
180
174
  generator = OutputGenerator.new(self)
181
175
  if generator.ready_for_virt_who_output?
182
- generator.virt_who_output
176
+ generator.virt_who_output(format)
183
177
  else
184
178
  generator.missing_virt_who_input_messages.join("\n")
185
179
  end
@@ -198,7 +192,8 @@ module ForemanVirtWhoConfigure
198
192
  when !self.out_of_date?
199
193
  :ok
200
194
  else
201
- :error
195
+ # out of date is currently considered ok too, virt-who does not send any report if there's no change on hypervisor
196
+ :out_of_date
202
197
  end
203
198
  end
204
199
 
@@ -1,11 +1,35 @@
1
1
  module ForemanVirtWhoConfigure
2
2
  class OutputGenerator
3
+ class ConfigurationResult
4
+ attr_reader :code, :identifier, :message
5
+ def initialize(code, identifier, message)
6
+ @code, @message, @identifier = code, message, identifier
7
+ end
8
+ end
9
+
10
+ MINIMUM_VIRT_WHO_VERSION = '0.19'
11
+
12
+ CONFIGURATION_RESULTS = [
13
+ ConfigurationResult.new(0, 'success', N_('Success')),
14
+ ConfigurationResult.new(1, 'virt_who_too_old', N_('Newer version of virt-who is required, minimum version is %s') % MINIMUM_VIRT_WHO_VERSION),
15
+ ConfigurationResult.new(2, 'virt_who_config_file_issue', N_('Unable to create virt-who config file')),
16
+ ConfigurationResult.new(4, 'virt_who_sysconfig_file_issue', N_('Unable to create sysconfig file')),
17
+ ConfigurationResult.new(8, 'virt_who_chkconfig_issue', N_('Unable to enable virt-who service using chkconfig')),
18
+ ConfigurationResult.new(16, 'virt_who_service_issue', N_('Unable to start virt-who service, please see virt-who logs for more details')),
19
+ ConfigurationResult.new(32, 'virt_who_installation', N_('Unable to install virt-who package, make sure the host is properly subscribed and has access to satellite-tools repository')),
20
+ ]
21
+
3
22
  attr_reader :config
4
23
 
5
24
  def initialize(config)
6
25
  @config = config
7
26
  end
8
27
 
28
+ def error_code(error_name)
29
+ result = CONFIGURATION_RESULTS.find { |result| result.identifier == error_name.to_s }
30
+ result.try(:code)
31
+ end
32
+
9
33
  def ready_for_virt_who_output?
10
34
  missing_virt_who_input_messages.empty?
11
35
  end
@@ -19,16 +43,50 @@ module ForemanVirtWhoConfigure
19
43
  messages
20
44
  end
21
45
 
22
- def virt_who_output
23
- <<EOS
24
- echo "Installing virt-who.."
25
- yum install -y virt-who
26
- echo "Encrypting password.."
27
- cr_password=`virt-who-password --password "#{cr_password}" 2> /dev/null`
28
- user_password=`virt-who-password --password "#{service_user_password}" 2> /dev/null`
29
-
30
- echo "Creating virt-who configuration.."
31
- cat > #{config_file_path} << EOF
46
+ def virt_who_output(format = nil)
47
+ result = ''
48
+ result += "#!/usr/bin/bash\n" if format == :bash_script
49
+ result += <<EOS
50
+ heading() {
51
+ echo -e "\\n== $1 =="
52
+ }
53
+
54
+ step() {
55
+ step_count=5
56
+ heading "[$1/$step_count] $2"
57
+ }
58
+
59
+ version_lte() {
60
+ [ "$1" = "`echo -e "$1\\n$2" | sort -V | head -n1`" ]
61
+ }
62
+
63
+ version_lt() {
64
+ [ "$1" = "$2" ] && return 1 || version_lte $1 $2
65
+ }
66
+
67
+ verify_minimal_version() {
68
+ minimal_version=#{MINIMUM_VIRT_WHO_VERSION}
69
+ installed_version=`rpm -q --queryformat '%{VERSION}' virt-who`
70
+
71
+ if version_lt $installed_version $minimal_version; then
72
+ echo "virt-who $installed_version does not meet minimum requirements, please make sure this host is properly subscribed and has access to satellite-tools repository, minimal virt-who version is $minimal_version"
73
+ return 1
74
+ else
75
+ return 0
76
+ fi
77
+ }
78
+
79
+ result_code=#{error_code(:success)}
80
+ step 1 "Installing virt-who"
81
+ yum install -y virt-who || result_code=$(($result_code|#{error_code(:virt_who_installation)}))
82
+
83
+ if verify_minimal_version; then
84
+ step 2 "Encrypting password"
85
+ cr_password=`virt-who-password --password "#{cr_password}" 2> /dev/null`
86
+ user_password=`virt-who-password --password "#{service_user_password}" 2> /dev/null`
87
+
88
+ step 3 "Creating virt-who configuration"
89
+ cat > #{config_file_path} << EOF
32
90
  [#{identifier}]
33
91
  type=#{type}
34
92
  hypervisor_id=#{hypervisor_id}
@@ -42,20 +100,40 @@ rhsm_username=#{service_user_username}
42
100
  rhsm_encrypted_password=$user_password
43
101
  rhsm_prefix=/rhsm
44
102
  EOF
103
+ if [ $? -ne 0 ]; then result_code=$(($result_code|#{error_code(:virt_who_config_file_issue)})); fi
45
104
 
46
- echo "Creating sysconfig virt-who configuration.."
47
- cat > /etc/sysconfig/virt-who << EOF
105
+ step 4 "Creating sysconfig virt-who configuration"
106
+ cat > #{sysconfig_file_path} << EOF
48
107
  VIRTWHO_SATELLITE6=1
49
108
  VIRTWHO_DEBUG=#{config.debug? ? 1 : 0}
50
109
  VIRTWHO_INTERVAL=#{config.interval * 60}#{proxy_strings}
51
110
  EOF
111
+ if [ $? -ne 0 ]; then result_code=$(($result_code|#{error_code(:virt_who_sysconfig_file_issue)})); fi
112
+
113
+ step 5 "Enabling and restarting the virt-who service"
114
+ chkconfig virt-who on || result_code=$(($result_code|#{error_code(:virt_who_chkconfig_issue)}))
115
+ service virt-who restart || result_code=$(($result_code|#{error_code(:virt_who_service_issue)}))
116
+ else
117
+ result_code=$(($result_code|#{error_code(:virt_who_too_old)}))
118
+ fi
119
+
120
+ heading "Finished"
121
+ if [ $result_code -ne 0 ]; then
122
+ echo "There were some errors during configuration:"
123
+ #{error_handling}
124
+ else
125
+ echo "Finished successully"
126
+ fi
52
127
 
53
- echo "Enabling and restarting the virt-who service"
54
- chkconfig virt-who on
55
- service virt-who restart
56
-
57
- echo "Done."
58
128
  EOS
129
+ result += "exit $result_code\n" if format == :bash_script
130
+ result
131
+ end
132
+
133
+ def error_handling
134
+ CONFIGURATION_RESULTS.map do |result|
135
+ "[ $(($result_code&#{result.code})) -ge 1 ] && echo '#{result.message}'"
136
+ end.join("\n ")
59
137
  end
60
138
 
61
139
  def filtering
@@ -81,6 +159,10 @@ EOS
81
159
  "/etc/virt-who.d/#{identifier}.conf"
82
160
  end
83
161
 
162
+ def sysconfig_file_path
163
+ '/etc/sysconfig/virt-who'
164
+ end
165
+
84
166
  def identifier
85
167
  "virt-who-config-#{config.id}"
86
168
  end