foreman_expire_hosts 9.0.1 → 9.1.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.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/concerns/foreman_expire_hosts/api/v2/hosts_controller_extensions.rb +1 -1
  3. data/app/controllers/concerns/foreman_expire_hosts/host_controller_extensions.rb +18 -17
  4. data/app/helpers/foreman_expire_hosts/hosts_helper.rb +6 -4
  5. data/app/lib/expire_hosts_notifications.rb +2 -2
  6. data/app/mailers/expire_hosts_mailer.rb +7 -7
  7. data/app/models/concerns/foreman_expire_hosts/host_ext.rb +15 -14
  8. data/app/overrides/add_expired_on_field_to_host_form.rb +4 -4
  9. data/app/overrides/add_expired_on_field_to_host_show.rb +4 -4
  10. data/app/overrides/deleted_expired_host_comment_in_audits.rb +8 -8
  11. data/app/services/foreman_expire_hosts/action/base.rb +6 -6
  12. data/app/services/foreman_expire_hosts/action/stop_expired_hosts.rb +1 -1
  13. data/app/services/foreman_expire_hosts/notification/base.rb +3 -5
  14. data/app/services/foreman_expire_hosts/safe_destroy.rb +2 -6
  15. data/app/services/foreman_expire_hosts/ui_notifications/hosts/base.rb +2 -2
  16. data/app/services/foreman_expire_hosts/ui_notifications/hosts/expiry_warning.rb +1 -1
  17. data/config/routes.rb +4 -2
  18. data/db/migrate/20220923181700_fix_settings_category_to_dsl.rb +2 -1
  19. data/db/seeds.d/80_expire_hosts_ui_notification.rb +7 -7
  20. data/lib/foreman_expire_hosts/engine.rb +40 -37
  21. data/lib/foreman_expire_hosts/version.rb +1 -1
  22. data/lib/tasks/expired_hosts.rake +2 -4
  23. data/test/functional/api/v2/hosts_controller_test.rb +1 -1
  24. data/test/functional/concerns/hosts_controller_extensions_test.rb +24 -24
  25. data/test/lib/expire_hosts_notifications_test.rb +13 -13
  26. data/test/unit/concerns/host_extensions_test.rb +13 -11
  27. data/test/unit/expiry_edit_authorizer_test.rb +8 -8
  28. data/test/unit/host_status/expiration_status_test.rb +1 -1
  29. data/test/unit/safe_destroy_test.rb +2 -2
  30. metadata +3 -4
  31. data/extra/foreman_expire_hosts.cron +0 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 839f503c394a7967c2a50c93f13197c83b4b9a7a304fbff46721f9416936c8f7
4
- data.tar.gz: 916041f719b68174a64b3337cfcecbbfd2c0552ae60a5c235cff3701c977331f
3
+ metadata.gz: 704970044c55500da0bcca9ef42066764812aa84b64725c76a432ddea9f21b41
4
+ data.tar.gz: 9825c6fac28b05e41c8697eaa9b5c328774c3f551c5b629ccd199fe2c2ca0ccc
5
5
  SHA512:
6
- metadata.gz: cc344d2888bdc7df2471cbdaba3b9236c40e74ae24b2f96cfa38d49188d574beff34ec25d1028b60607083159d99463c2a7916b3dbc212a5f40b962ccf3ff75b
7
- data.tar.gz: ff4a9af4cba0aaee10361997aa5b714240e83da4d1bb6b17bc2a421867615d1562821c6e6508a0212bdf2abb4673c4f430598375cd0252d74ef97170b0c5031a
6
+ metadata.gz: fec4331e4a2fc0d952474e47d1de508716245e41c87c63c0b5ed4d9375f4888ccc9673adbfbd8742d0ffbf700512df64aa86505c400854f26cfb78aee38e21ee
7
+ data.tar.gz: dbbc883d8537ac7d40509f35f9c8877174f73f230e4bbe3eeb2fde0786ebdcf7e41926bca755558ec384446496d9e6673421fe78eed3101ab63cf5e7c987e260
@@ -8,7 +8,7 @@ module ForemanExpireHosts
8
8
 
9
9
  update_api(:create, :update) do
10
10
  param :host, Hash do
11
- param :expired_on, String, :desc => 'Expiry date'
11
+ param :expired_on, String, desc: 'Expiry date'
12
12
  end
13
13
  end
14
14
  end
@@ -4,30 +4,31 @@ module ForemanExpireHosts
4
4
  module HostControllerExtensions
5
5
  def self.prepended(base)
6
6
  base.class_eval do
7
- before_action :validate_multiple_expiration, :only => :update_multiple_expiration
8
- before_action :find_multiple_with_expire_hosts, :only => [:select_multiple_expiration, :update_multiple_expiration]
7
+ before_action :validate_multiple_expiration, only: :update_multiple_expiration
8
+ before_action :find_multiple_with_expire_hosts,
9
+ only: %i[select_multiple_expiration update_multiple_expiration]
9
10
  alias_method :find_multiple_with_expire_hosts, :find_multiple
10
11
  end
11
12
  end
12
13
 
13
- def select_multiple_expiration; end
14
+ def select_multiple_expiration
15
+ end
14
16
 
15
17
  def update_multiple_expiration
16
18
  failed_hosts = {}
17
19
 
18
20
  @hosts.each do |host|
19
- begin
20
- host.expired_on = expiration_date
21
- host.save!
22
- rescue StandardError => e
23
- failed_hosts[host.name] = e
24
- message = if expiration_date.present?
25
- _('Failed to set expiration date for %{host} to %{expiration_date}.') % { :host => host, :expiration_date => l(expiration_date) }
26
- else
27
- _('Failed to clear expiration date for %s.') % host
28
- end
29
- Foreman::Logging.exception(message, e)
30
- end
21
+ host.expired_on = expiration_date
22
+ host.save!
23
+ rescue StandardError => e
24
+ failed_hosts[host.name] = e
25
+ message = if expiration_date.present?
26
+ format(_('Failed to set expiration date for %{host} to %{expiration_date}.'), host: host,
27
+ expiration_date: l(expiration_date))
28
+ else
29
+ _('Failed to clear expiration date for %s.') % host
30
+ end
31
+ Foreman::Logging.exception(message, e)
31
32
  end
32
33
 
33
34
  if failed_hosts.empty?
@@ -38,8 +39,8 @@ module ForemanExpireHosts
38
39
  end
39
40
  else
40
41
  error n_('The expiration date could not be set for host: %s.',
41
- 'The expiration date could not be set for hosts: %s.',
42
- failed_hosts.count) % failed_hosts.map { |h, err| "#{h} (#{err})" }.to_sentence
42
+ 'The expiration date could not be set for hosts: %s.',
43
+ failed_hosts.count) % failed_hosts.map { |h, err| "#{h} (#{err})" }.to_sentence
43
44
  end
44
45
  redirect_back_or_to hosts_path
45
46
  end
@@ -4,10 +4,10 @@ module ForemanExpireHosts
4
4
  module HostsHelper
5
5
  def expire_hosts_host_multiple_actions
6
6
  actions = []
7
- if authorized_for(:controller => :hosts, :action => :select_multiple_expiration)
7
+ if authorized_for(controller: :hosts, action: :select_multiple_expiration)
8
8
  actions << {
9
9
  action: [_('Change Expiration'), select_multiple_expiration_hosts_path],
10
- priority: 200
10
+ priority: 200,
11
11
  }
12
12
  end
13
13
  actions
@@ -19,11 +19,13 @@ module ForemanExpireHosts
19
19
  if host.expired_past_grace_period?
20
20
  message = _('This host has expired %s ago and needs to be deleted manually.') % time_ago_in_words(host.expired_on)
21
21
  elsif host.expired?
22
- message = _('This host has expired %{time_ago} ago and will be deleted on %{delete_date}.') % { :delete_date => l(host.expiration_grace_period_end_date), :time_ago => time_ago_in_words(host.expired_on) }
22
+ message = format(_('This host has expired %{time_ago} ago and will be deleted on %{delete_date}.'),
23
+ delete_date: l(host.expiration_grace_period_end_date), time_ago: time_ago_in_words(host.expired_on))
23
24
  elsif host.expires_today?
24
25
  message = _('This host will expire today.')
25
26
  elsif host.pending_expiration?
26
- message = _('This host will expire in %{distance_of_time} (on %{expire_date}).') % { :expire_date => l(host.expired_on), :distance_of_time => future_time_in_words(host.expired_on) }
27
+ message = format(_('This host will expire in %{distance_of_time} (on %{expire_date}).'),
28
+ expire_date: l(host.expired_on), distance_of_time: future_time_in_words(host.expired_on))
27
29
  end
28
30
  message
29
31
  end
@@ -19,8 +19,8 @@ module ExpireHostsNotifications
19
19
  notifiable_hosts = Host.with_expire_date(expiry_date).preload(:owner)
20
20
 
21
21
  ForemanExpireHosts::Notification::ExpiryWarning.new(
22
- :hosts => notifiable_hosts,
23
- :expiry_date => expiry_date
22
+ hosts: notifiable_hosts,
23
+ expiry_date: expiry_date
24
24
  ).deliver
25
25
  end
26
26
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ExpireHostsMailer < ApplicationMailer
4
- default :content_type => 'text/html', :from => Setting[:email_reply_address] || 'noreply@your-foreman.com'
4
+ default content_type: 'text/html', from: Setting[:email_reply_address] || 'noreply@your-foreman.com'
5
5
 
6
6
  def deleted_hosts_notification(recipient, hosts)
7
7
  build_mail(
@@ -54,16 +54,16 @@ class ExpireHostsMailer < ApplicationMailer
54
54
  user = user_for_recipient(recipient)
55
55
  @hosts = hosts
56
56
  @authorized_for_expiry_date_change = ForemanExpireHosts::ExpiryEditAuthorizer.new(
57
- :user => user,
58
- :hosts => hosts
57
+ user: user,
58
+ hosts: hosts
59
59
  ).authorized?
60
60
  set_locale_for(user) do
61
61
  mail(
62
- :to => recipient_mail(recipient),
63
- :subject => _(subject),
64
- :importance => 'High'
62
+ to: recipient_mail(recipient),
63
+ subject: _(subject),
64
+ importance: 'High'
65
65
  ) do |format|
66
- format.html { render :layout => 'application_mailer' }
66
+ format.html { render layout: 'application_mailer' }
67
67
  end
68
68
  end
69
69
  end
@@ -7,32 +7,33 @@ module ForemanExpireHosts
7
7
  included do
8
8
  after_validation :validate_expired_on
9
9
 
10
- validates :expired_on, :presence => true, :if => -> { Setting[:is_host_expiry_date_mandatory] }
10
+ validates :expired_on, presence: true, if: -> { Setting[:is_host_expiry_date_mandatory] }
11
11
 
12
- has_one :expiration_status_object, :class_name => 'HostStatus::ExpirationStatus', :foreign_key => 'host_id', :inverse_of => :host
12
+ has_one :expiration_status_object, class_name: 'HostStatus::ExpirationStatus', foreign_key: 'host_id',
13
+ inverse_of: :host
13
14
 
14
15
  before_validation :refresh_expiration_status
15
16
 
16
- scope :expiring, -> { where('expired_on IS NOT NULL') }
17
- scope :with_expire_date, ->(date) { expiring.where('expired_on = ?', date) }
17
+ scope :expiring, -> { where.not(expired_on: nil) }
18
+ scope :with_expire_date, ->(date) { expiring.where(expired_on: date) }
18
19
  scope :expired, -> { expiring.where('expired_on <= ?', Date.today) }
19
20
  scope :expiring_today, -> { expiring.with_expire_date(Date.today) }
20
- scope :expired_past_grace_period, -> { expiring.where('expired_on <= ?', Date.today - Setting[:days_to_delete_after_host_expiration].to_i) }
21
+ scope :expired_past_grace_period, lambda {
22
+ expiring.where('expired_on <= ?', Date.today - Setting[:days_to_delete_after_host_expiration].to_i)
23
+ }
21
24
 
22
- scoped_search :on => :expired_on, :complete_value => true, :rename => :expires, :only_explicit => true
25
+ scoped_search on: :expired_on, complete_value: true, rename: :expires, only_explicit: true
23
26
  end
24
27
 
25
28
  def validate_expired_on
26
- if self.expires?
29
+ if expires?
27
30
  begin
28
31
  errors.add(:expired_on, _('must be in the future')) unless expired_on.to_s.to_date > Date.today
29
32
  rescue StandardError
30
33
  errors.add(:expired_on, _('is invalid'))
31
34
  end
32
35
  end
33
- if self.changed.include?('expired_on')
34
- errors.add(:expired_on, _('no permission to edit')) unless can_modify_expiry_date?
35
- end
36
+ errors.add(:expired_on, _('no permission to edit')) if changed.include?('expired_on') && !can_modify_expiry_date?
36
37
  true
37
38
  end
38
39
 
@@ -76,17 +77,17 @@ module ForemanExpireHosts
76
77
  return true if defined?(Rails::Console)
77
78
  return true unless User.current
78
79
  return true if Authorizer.new(User.current).can?(:edit_host_expiry, self)
79
- return true if self.owner_type.nil? || self.owner.nil?
80
+ return true if owner_type.nil? || owner.nil?
80
81
 
81
82
  Setting[:can_owner_modify_host_expiry_date] &&
82
- ((self.owner_type == 'User' && self.owner == User.current) ||
83
- (self.owner_type == 'Usergroup' && self.owner.all_users.include?(User.current)))
83
+ ((owner_type == 'User' && owner == User.current) ||
84
+ (owner_type == 'Usergroup' && owner.all_users.include?(User.current)))
84
85
  end
85
86
 
86
87
  private
87
88
 
88
89
  def refresh_expiration_status
89
- self.get_status(HostStatus::ExpirationStatus).refresh
90
+ get_status(HostStatus::ExpirationStatus).refresh
90
91
  end
91
92
  end
92
93
  end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Deface::Override.new(
4
- :virtual_path => 'hosts/_form',
5
- :name => 'host_form_expired_on_field',
6
- :insert_after => 'div#model_name',
7
- :partial => 'hosts/expired_on_field'
4
+ virtual_path: 'hosts/_form',
5
+ name: 'host_form_expired_on_field',
6
+ insert_after: 'div#model_name',
7
+ partial: 'hosts/expired_on_field'
8
8
  )
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Deface::Override.new(
4
- :virtual_path => 'hosts/show',
5
- :name => 'host_expiry_warning_in_show',
6
- :insert_before => '#host-show',
7
- :partial => 'hosts/expired_message'
4
+ virtual_path: 'hosts/show',
5
+ name: 'host_expiry_warning_in_show',
6
+ insert_before: '#host-show',
7
+ partial: 'hosts/expired_message'
8
8
  )
@@ -1,15 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Deface::Override.new(
4
- :virtual_path => 'audits/_list',
5
- :name => 'deleted_expired_host_audit_comment_in_list',
6
- :insert_bottom => 'div.row div.audit-content',
7
- :text => "\n <%= destroyed_expired_host_audit_comment_in_list(audit) %>"
4
+ virtual_path: 'audits/_list',
5
+ name: 'deleted_expired_host_audit_comment_in_list',
6
+ insert_bottom: 'div.row div.audit-content',
7
+ text: "\n <%= destroyed_expired_host_audit_comment_in_list(audit) %>"
8
8
  )
9
9
 
10
10
  Deface::Override.new(
11
- :virtual_path => 'audits/show',
12
- :name => 'deleted_expired_host_audit_comment_in_show',
13
- :insert_bottom => 'div#tab1 table',
14
- :text => "\n <%= destroyed_expired_host_audit_comment_in_show(@audit) %>"
11
+ virtual_path: 'audits/show',
12
+ name: 'deleted_expired_host_audit_comment_in_show',
13
+ insert_bottom: 'div#tab1 table',
14
+ text: "\n <%= destroyed_expired_host_audit_comment_in_show(@audit) %>"
15
15
  )
@@ -24,10 +24,10 @@ module ForemanExpireHosts
24
24
 
25
25
  if result
26
26
  logger.info "Action #{self.class.name} for host #{host.name} was successful."
27
- self.successful_hosts << host
27
+ successful_hosts << host
28
28
  else
29
29
  logger.info "Action #{self.class.name} for host #{host.name} failed."
30
- self.failed_hosts << host
30
+ failed_hosts << host
31
31
  end
32
32
  end
33
33
  end
@@ -47,18 +47,18 @@ module ForemanExpireHosts
47
47
 
48
48
  def success_notification_options
49
49
  {
50
- :hosts => successful_hosts
50
+ hosts: successful_hosts,
51
51
  }
52
52
  end
53
53
 
54
54
  def failure_notification_options
55
55
  {
56
- :hosts => failed_hosts,
57
- :to => User.anonymous_admin
56
+ hosts: failed_hosts,
57
+ to: User.anonymous_admin,
58
58
  }
59
59
  end
60
60
 
61
- delegate :logger, :to => :ForemanExpireHosts
61
+ delegate :logger, to: :ForemanExpireHosts
62
62
  end
63
63
  end
64
64
  end
@@ -29,7 +29,7 @@ module ForemanExpireHosts
29
29
 
30
30
  def success_notification_options
31
31
  super.merge(
32
- :delete_date => (Date.today + days_to_delete_after_expired)
32
+ delete_date: (Date.today + days_to_delete_after_expired)
33
33
  )
34
34
  end
35
35
 
@@ -32,10 +32,8 @@ module ForemanExpireHosts
32
32
 
33
33
  build_mail_notification(recipient, hosts).deliver_now
34
34
  rescue SocketError, Net::SMTPError => e
35
- message = _('Failed to deliver %{notification_name} for Hosts %{hosts}') % {
36
- :notification_name => humanized_name,
37
- :hosts => hosts.map(&:name).to_sentence
38
- }
35
+ message = format(_('Failed to deliver %{notification_name} for Hosts %{hosts}'),
36
+ notification_name: humanized_name, hosts: hosts.map(&:name).to_sentence)
39
37
  Foreman::Logging.exception(message, e)
40
38
  end
41
39
 
@@ -46,7 +44,7 @@ module ForemanExpireHosts
46
44
  end
47
45
  end
48
46
 
49
- delegate :logger, :to => :ForemanExpireHosts
47
+ delegate :logger, to: :ForemanExpireHosts
50
48
 
51
49
  def humanized_name
52
50
  _('Notification')
@@ -20,12 +20,8 @@ module ForemanExpireHosts
20
20
  subject.destroy!
21
21
  end
22
22
  rescue ActiveRecord::RecordNotDestroyed => e
23
- message = _('Failed to delete %{class_name} %{subject}: %{message} - Errors: %{errors}') % {
24
- :class_name => subject.class.name,
25
- :subject => subject,
26
- :message => e.message,
27
- :errors => e.record.errors.full_messages.to_sentence
28
- }
23
+ message = format(_('Failed to delete %{class_name} %{subject}: %{message} - Errors: %{errors}'),
24
+ class_name: subject.class.name, subject: subject, message: e.message, errors: e.record.errors.full_messages.to_sentence)
29
25
  Foreman::Logging.exception(message, e)
30
26
  false
31
27
  end
@@ -37,13 +37,13 @@ module ForemanExpireHosts
37
37
  def message_variables
38
38
  {
39
39
  subject: subject,
40
- initator: initiator
40
+ initator: initiator,
41
41
  }
42
42
  end
43
43
 
44
44
  def update_notification
45
45
  find_notification
46
- .update(expired_at: blueprint.expired_at, :message => parsed_message)
46
+ .update(expired_at: blueprint.expired_at, message: parsed_message)
47
47
  end
48
48
 
49
49
  def redeliver!
@@ -22,7 +22,7 @@ module ForemanExpireHosts
22
22
  end
23
23
 
24
24
  def message_variables
25
- super.merge(:relative_expiry_time => time_ago_in_words(subject.expired_on))
25
+ super.merge(relative_expiry_time: time_ago_in_words(subject.expired_on))
26
26
  end
27
27
  end
28
28
  end
data/config/routes.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Foreman::Application.routes.draw do
4
- post 'expired_hosts/select_multiple_expiration' => 'hosts#select_multiple_expiration', as: 'select_multiple_expiration_hosts'
5
- post 'expired_hosts/update_multiple_expiration' => 'hosts#update_multiple_expiration', as: 'update_multiple_expiration_hosts'
4
+ post 'expired_hosts/select_multiple_expiration' => 'hosts#select_multiple_expiration',
5
+ :as => 'select_multiple_expiration_hosts'
6
+ post 'expired_hosts/update_multiple_expiration' => 'hosts#update_multiple_expiration',
7
+ :as => 'update_multiple_expiration_hosts'
6
8
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  class FixSettingsCategoryToDsl < ActiveRecord::Migration[6.0]
4
4
  def up
5
- Setting.where(category: 'Setting::ExpireHost').update_all(category: 'Setting') if column_exists?(:settings, :category)
5
+ Setting.where(category: 'Setting::ExpireHost').update_all(category: 'Setting') if column_exists?(:settings,
6
+ :category)
6
7
  end
7
8
  end
@@ -11,9 +11,9 @@
11
11
  links:
12
12
  [
13
13
  path_method: :host_path,
14
- title: _('Details')
15
- ]
16
- }
14
+ title: _('Details'),
15
+ ],
16
+ },
17
17
  },
18
18
  {
19
19
  group: _('Hosts'),
@@ -25,8 +25,8 @@
25
25
  links:
26
26
  [
27
27
  path_method: :host_path,
28
- title: _('Details')
29
- ]
30
- }
31
- }
28
+ title: _('Details'),
29
+ ],
30
+ },
31
+ },
32
32
  ].each { |blueprint| UINotifications::Seed.new(blueprint).configure }
@@ -13,10 +13,12 @@ module ForemanExpireHosts
13
13
  end
14
14
  end
15
15
 
16
- initializer 'foreman_expire_hosts.register_plugin', :before => :finisher_hook do |app|
16
+ initializer 'foreman_expire_hosts.register_plugin', before: :finisher_hook do |app|
17
17
  app.reloader.to_prepare do
18
+ require 'foreman/cron'
19
+
18
20
  Foreman::Plugin.register :foreman_expire_hosts do
19
- requires_foreman '>= 3.13.0'
21
+ requires_foreman '>= 3.18.0'
20
22
  register_custom_status HostStatus::ExpirationStatus
21
23
 
22
24
  # strong parameters
@@ -24,14 +26,14 @@ module ForemanExpireHosts
24
26
 
25
27
  security_block :foreman_expire_hosts do
26
28
  permission :edit_host_expiry,
27
- {},
28
- :resource_type => 'Host'
29
+ {},
30
+ resource_type: 'Host'
29
31
  end
30
32
 
31
33
  # Extend built in permissions
32
34
  Foreman::AccessControl.permission(:edit_hosts).actions.concat [
33
35
  'hosts/select_multiple_expiration',
34
- 'hosts/update_multiple_expiration'
36
+ 'hosts/update_multiple_expiration',
35
37
  ]
36
38
 
37
39
  Setting::BLANK_ATTRS << 'host_expiry_email_recipients'
@@ -39,35 +41,35 @@ module ForemanExpireHosts
39
41
  settings do
40
42
  category(:expire_hosts, N_('Expire Hosts')) do
41
43
  setting('is_host_expiry_date_mandatory',
42
- type: :boolean,
43
- description: N_('Make expiry date field mandatory on host creation/update'),
44
- default: false,
45
- full_name: N_('Require host expiry date'))
44
+ type: :boolean,
45
+ description: N_('Make expiry date field mandatory on host creation/update'),
46
+ default: false,
47
+ full_name: N_('Require host expiry date'))
46
48
  setting('can_owner_modify_host_expiry_date',
47
- type: :boolean,
48
- description: N_('Allow host owner to modify host expiry date field. If the field is false then admin only can edit expiry field'),
49
- default: false,
50
- full_name: N_('Host owner can modify host expiry date'))
49
+ type: :boolean,
50
+ description: N_('Allow host owner to modify host expiry date field. If the field is false then admin only can edit expiry field'),
51
+ default: false,
52
+ full_name: N_('Host owner can modify host expiry date'))
51
53
  setting('notify1_days_before_host_expiry',
52
- type: :integer,
53
- description: N_('Send first notification to owner of hosts about his hosts expiring in given days. Must be integer only'),
54
- default: 7,
55
- full_name: N_('First expiry notification'))
54
+ type: :integer,
55
+ description: N_('Send first notification to owner of hosts about his hosts expiring in given days. Must be integer only'),
56
+ default: 7,
57
+ full_name: N_('First expiry notification'))
56
58
  setting('notify2_days_before_host_expiry',
57
- type: :integer,
58
- description: N_('Send second notification to owner of hosts about his hosts expiring in given days. Must be integer only'),
59
- default: 1,
60
- full_name: N_('Second expiry notification'))
59
+ type: :integer,
60
+ description: N_('Send second notification to owner of hosts about his hosts expiring in given days. Must be integer only'),
61
+ default: 1,
62
+ full_name: N_('Second expiry notification'))
61
63
  setting('days_to_delete_after_host_expiration',
62
- type: :integer,
63
- description: N_('Delete expired hosts after given days of hosts expiry date. Must be integer only'),
64
- default: 3,
65
- full_name: N_('Expiry grace period in days'))
64
+ type: :integer,
65
+ description: N_('Delete expired hosts after given days of hosts expiry date. Must be integer only'),
66
+ default: 3,
67
+ full_name: N_('Expiry grace period in days'))
66
68
  setting('host_expiry_email_recipients',
67
- type: :string,
68
- description: N_('All notifications will be delivered to its owner. If any other users/admins need to receive those expiry warning notifications then those emails can be configured comma separated here.'), # rubocop:disable Layout/LineLength
69
- default: nil,
70
- full_name: N_('Expiry e-mail recipients'))
69
+ type: :string,
70
+ description: N_('All notifications will be delivered to its owner. If any other users/admins need to receive those expiry warning notifications then those emails can be configured comma separated here.'),
71
+ default: nil,
72
+ full_name: N_('Expiry e-mail recipients'))
71
73
  end
72
74
  end
73
75
 
@@ -76,19 +78,20 @@ module ForemanExpireHosts
76
78
  describe_host do
77
79
  multiple_actions_provider :expire_hosts_host_multiple_actions
78
80
  end
81
+
82
+ # Register recurring task with Foreman::Cron framework
83
+ Foreman::Cron.register(:daily, 'expired_hosts:deliver_notifications')
79
84
  end
80
85
  end
81
86
  end
82
87
 
83
88
  config.to_prepare do
84
- begin
85
- ::Host::Managed.include ForemanExpireHosts::HostExt
86
- ::HostsController.prepend ForemanExpireHosts::HostControllerExtensions
87
- ::AuditsHelper.include ForemanExpireHosts::AuditsHelperExtensions
88
- ::Api::V2::HostsController.include ForemanExpireHosts::Api::V2::HostsControllerExtensions
89
- rescue StandardError => e
90
- Rails.logger.warn "ForemanExpireHosts: skipping engine hook (#{e})"
91
- end
89
+ ::Host::Managed.include ForemanExpireHosts::HostExt
90
+ ::HostsController.prepend ForemanExpireHosts::HostControllerExtensions
91
+ ::AuditsHelper.include ForemanExpireHosts::AuditsHelperExtensions
92
+ ::Api::V2::HostsController.include ForemanExpireHosts::Api::V2::HostsControllerExtensions
93
+ rescue StandardError => e
94
+ Rails.logger.warn "ForemanExpireHosts: skipping engine hook (#{e})"
92
95
  end
93
96
 
94
97
  rake_tasks do
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ForemanExpireHosts
4
- VERSION = '9.0.1'
4
+ VERSION = '9.1.0'
5
5
  end
@@ -3,7 +3,7 @@
3
3
  # Tasks
4
4
  namespace :expired_hosts do
5
5
  desc 'Delete all expired hosts, send notification email about expiring hosts'
6
- task :deliver_notifications => :environment do
6
+ task deliver_notifications: :environment do
7
7
  User.as_anonymous_admin do
8
8
  ExpireHostsNotifications.delete_expired_hosts
9
9
  ExpireHostsNotifications.stop_expired_hosts
@@ -28,6 +28,4 @@ end
28
28
  Rake::Task[:test].enhance ['test:foreman_expire_hosts']
29
29
 
30
30
  load 'tasks/jenkins.rake'
31
- if Rake::Task.task_defined?(:'jenkins:unit')
32
- Rake::Task['jenkins:unit'].enhance ['test:foreman_expire_hosts']
33
- end
31
+ Rake::Task['jenkins:unit'].enhance ['test:foreman_expire_hosts'] if Rake::Task.task_defined?(:'jenkins:unit')
@@ -10,7 +10,7 @@ class Api::V2::HostsControllerTest < ActionController::TestCase
10
10
  end
11
11
 
12
12
  test 'should show individual record with expiry data' do
13
- get :show, params: { :id => host.to_param }
13
+ get :show, params: { id: host.to_param }
14
14
  assert_response :success
15
15
  show_response = ActiveSupport::JSON.decode(@response.body)
16
16
  assert_not_empty show_response
@@ -13,22 +13,22 @@ class HostsControllerTest < ActionController::TestCase
13
13
  test 'new host with expiration date' do
14
14
  expiration_date = Date.today + 14
15
15
  assert_difference 'Host.count' do
16
- post :create, params: { :host => {
17
- :name => 'myotherfullhost',
18
- :mac => 'aabbecddee06',
19
- :ip => '2.3.4.125',
20
- :domain_id => domains(:mydomain).id,
21
- :operatingsystem_id => operatingsystems(:redhat).id,
22
- :architecture_id => architectures(:x86_64).id,
23
- :subnet_id => subnets(:one).id,
24
- :medium_id => media(:one).id,
25
- :pxe_loader => 'Grub2 UEFI',
26
- :realm_id => realms(:myrealm).id,
27
- :disk => 'empty partition',
28
- :root_pass => 'xybxa6JUkz63w',
29
- :location_id => taxonomies(:location1).id,
30
- :organization_id => taxonomies(:organization1).id,
31
- :expired_on => expiration_date
16
+ post :create, params: { host: {
17
+ name: 'myotherfullhost',
18
+ mac: 'aabbecddee06',
19
+ ip: '2.3.4.125',
20
+ domain_id: domains(:mydomain).id,
21
+ operatingsystem_id: operatingsystems(:redhat).id,
22
+ architecture_id: architectures(:x86_64).id,
23
+ subnet_id: subnets(:one).id,
24
+ medium_id: media(:one).id,
25
+ pxe_loader: 'Grub2 UEFI',
26
+ realm_id: realms(:myrealm).id,
27
+ disk: 'empty partition',
28
+ root_pass: 'xybxa6JUkz63w',
29
+ location_id: taxonomies(:location1).id,
30
+ organization_id: taxonomies(:organization1).id,
31
+ expired_on: expiration_date,
32
32
  } }, session: set_session_user
33
33
  end
34
34
  h = Host.search_for('myotherfullhost').first
@@ -42,7 +42,7 @@ class HostsControllerTest < ActionController::TestCase
42
42
 
43
43
  test 'should add expiration date to host' do
44
44
  expiration_date = Date.today + 14
45
- put :update, params: { :id => host.name, :host => { :expired_on => expiration_date } }, session: set_session_user
45
+ put :update, params: { id: host.name, host: { expired_on: expiration_date } }, session: set_session_user
46
46
  h = Host.find(host.id)
47
47
  assert_equal expiration_date, h.expired_on
48
48
  end
@@ -58,7 +58,7 @@ class HostsControllerTest < ActionController::TestCase
58
58
 
59
59
  test 'show a host selection' do
60
60
  host_ids = @hosts.map(&:id)
61
- post :select_multiple_expiration, params: { :host_ids => host_ids }, session: set_session_user, xhr: true
61
+ post :select_multiple_expiration, params: { host_ids: host_ids }, session: set_session_user, xhr: true
62
62
  assert_response :success
63
63
  @hosts.each do |host|
64
64
  assert response.body =~ /#{host.name}/m
@@ -67,10 +67,10 @@ class HostsControllerTest < ActionController::TestCase
67
67
 
68
68
  test 'should set expiration date' do
69
69
  expiration_date = Date.today + 14
70
- params = { :host_ids => @hosts.map(&:id),
71
- :host => { 'expired_on' => expiration_date.strftime('%Y-%m-%d') } }
70
+ params = { host_ids: @hosts.map(&:id),
71
+ host: { 'expired_on' => expiration_date.strftime('%Y-%m-%d') } }
72
72
 
73
- post :update_multiple_expiration, params: params, session: set_session_user.merge(:user => users(:admin).id)
73
+ post :update_multiple_expiration, params: params, session: set_session_user.merge(user: users(:admin).id)
74
74
 
75
75
  assert_empty flash[:error]
76
76
 
@@ -80,10 +80,10 @@ class HostsControllerTest < ActionController::TestCase
80
80
  end
81
81
 
82
82
  test 'should clear the expiration date of multiple hosts' do
83
- params = { :host_ids => @hosts.map(&:id),
84
- :host => { :expired_on => '' } }
83
+ params = { host_ids: @hosts.map(&:id),
84
+ host: { expired_on: '' } }
85
85
 
86
- post :update_multiple_expiration, params: params, session: set_session_user.merge(:user => users(:admin).id)
86
+ post :update_multiple_expiration, params: params, session: set_session_user.merge(user: users(:admin).id)
87
87
 
88
88
  assert_empty flash[:error]
89
89
 
@@ -16,8 +16,8 @@ class ExpireHostsNotificationsTest < ActiveSupport::TestCase
16
16
  context '#delete_expired_hosts' do
17
17
  context 'with single owner' do
18
18
  setup do
19
- FactoryBot.create_list(:host, 2, :expired, :owner => user)
20
- FactoryBot.create_list(:host, 2, :owner => user)
19
+ FactoryBot.create_list(:host, 2, :expired, owner: user)
20
+ FactoryBot.create_list(:host, 2, owner: user)
21
21
  end
22
22
 
23
23
  test 'should delete expired hosts' do
@@ -31,8 +31,8 @@ class ExpireHostsNotificationsTest < ActiveSupport::TestCase
31
31
 
32
32
  context 'with usergroup owner' do
33
33
  setup do
34
- FactoryBot.create_list(:host, 2, :expired, :owner => usergroup)
35
- FactoryBot.create_list(:host, 2, :owner => usergroup)
34
+ FactoryBot.create_list(:host, 2, :expired, owner: usergroup)
35
+ FactoryBot.create_list(:host, 2, owner: usergroup)
36
36
  end
37
37
 
38
38
  test 'should delete expired hosts' do
@@ -65,7 +65,7 @@ class ExpireHostsNotificationsTest < ActiveSupport::TestCase
65
65
  end
66
66
 
67
67
  test 'should deliver notification to additional recipients' do
68
- FactoryBot.create_list(:host, 2, :expired, :owner => user)
68
+ FactoryBot.create_list(:host, 2, :expired, owner: user)
69
69
  ExpireHostsNotifications.delete_expired_hosts
70
70
  assert_equal 2, ActionMailer::Base.deliveries.count
71
71
  assert_includes ActionMailer::Base.deliveries.first.subject, 'Deleted expired hosts'
@@ -83,13 +83,13 @@ class ExpireHostsNotificationsTest < ActiveSupport::TestCase
83
83
 
84
84
  context '#stop_expired_hosts' do
85
85
  let(:power_mock) { mock('power') }
86
- let(:host) { FactoryBot.create(:host, :expired, :on_compute_resource, :owner => user) }
86
+ let(:host) { FactoryBot.create(:host, :expired, :on_compute_resource, owner: user) }
87
87
  let(:blueprint) { NotificationBlueprint.find_by(name: 'expire_hosts_stopped_host') }
88
88
  setup do
89
89
  power_mock.stubs(:ready?).returns(true)
90
90
  host.unstub(:queue_compute)
91
91
  Host.any_instance.stubs(:power).returns(power_mock)
92
- FactoryBot.create_list(:host, 2, :owner => user)
92
+ FactoryBot.create_list(:host, 2, owner: user)
93
93
  end
94
94
 
95
95
  test 'should stop expired hosts' do
@@ -120,7 +120,7 @@ class ExpireHostsNotificationsTest < ActiveSupport::TestCase
120
120
  end
121
121
 
122
122
  context 'with a host expiring today' do
123
- let(:host) { FactoryBot.create(:host, :expires_today, :on_compute_resource, :owner => user) }
123
+ let(:host) { FactoryBot.create(:host, :expires_today, :on_compute_resource, owner: user) }
124
124
  let(:delete_date) { Date.today + Setting[:days_to_delete_after_host_expiration].to_i }
125
125
 
126
126
  it 'should stop the host and show the correct delete date' do
@@ -139,7 +139,7 @@ class ExpireHostsNotificationsTest < ActiveSupport::TestCase
139
139
 
140
140
  context '#deliver_expiry_warning_notification' do
141
141
  let(:blueprint) { NotificationBlueprint.find_by(name: 'expire_hosts_expiry_warning') }
142
- let(:hosts) { FactoryBot.create_list(:host, 2, :expires_in_a_week, :owner => user) }
142
+ let(:hosts) { FactoryBot.create_list(:host, 2, :expires_in_a_week, owner: user) }
143
143
 
144
144
  setup do
145
145
  Setting['notify1_days_before_host_expiry'] = 7
@@ -181,7 +181,7 @@ class ExpireHostsNotificationsTest < ActiveSupport::TestCase
181
181
 
182
182
  test 'should send two notifications for two users' do
183
183
  owner2 = FactoryBot.create(:user, :with_mail)
184
- FactoryBot.create(:host, :expires_in_a_week, :owner => owner2)
184
+ FactoryBot.create(:host, :expires_in_a_week, owner: owner2)
185
185
  ExpireHostsNotifications.deliver_expiry_warning_notification
186
186
  assert_equal 2, ActionMailer::Base.deliveries.count
187
187
  assert_includes ActionMailer::Base.deliveries.first.subject, 'Expiring hosts in foreman'
@@ -190,9 +190,9 @@ class ExpireHostsNotificationsTest < ActiveSupport::TestCase
190
190
 
191
191
  test 'should send three notifications for three users' do
192
192
  user2 = FactoryBot.create(:user, :with_mail)
193
- user3 = FactoryBot.create(:user, :with_mail, :usergroups => [usergroup])
194
- FactoryBot.create(:host, :expires_in_a_week, :owner => user2)
195
- FactoryBot.create(:host, :expires_in_a_week, :owner => usergroup)
193
+ user3 = FactoryBot.create(:user, :with_mail, usergroups: [usergroup])
194
+ FactoryBot.create(:host, :expires_in_a_week, owner: user2)
195
+ FactoryBot.create(:host, :expires_in_a_week, owner: usergroup)
196
196
  ExpireHostsNotifications.deliver_expiry_warning_notification
197
197
  assert_equal 3, ActionMailer::Base.deliveries.count
198
198
  assert_includes ActionMailer::Base.deliveries.first.subject, 'Expiring hosts in foreman'
@@ -3,7 +3,7 @@
3
3
  require 'test_plugin_helper'
4
4
 
5
5
  class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
6
- EXPIRATION_SCOPES = ['expiring', 'expired', 'expiring_today', 'expired_past_grace_period'].freeze
6
+ EXPIRATION_SCOPES = %w[expiring expired expiring_today expired_past_grace_period].freeze
7
7
 
8
8
  setup do
9
9
  User.current = FactoryBot.build(:user, :admin)
@@ -35,7 +35,7 @@ class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
35
35
  context 'changing expiration date for user owned host' do
36
36
  setup do
37
37
  @user = FactoryBot.create(:user)
38
- @host = FactoryBot.create(:host, :expired, :owner => @user)
38
+ @host = FactoryBot.create(:host, :expired, owner: @user)
39
39
  end
40
40
 
41
41
  test 'admin should be able to change expiration date' do
@@ -72,23 +72,25 @@ class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
72
72
 
73
73
  context 'with edit_host_expiry permission' do
74
74
  let(:permission) { Permission.find_by(name: 'edit_host_expiry') }
75
- let(:filter) { FactoryBot.create(:filter, :permissions => [permission]) }
76
- let(:role) { FactoryBot.create(:role, :filters => [filter]) }
77
- let(:user) { FactoryBot.create(:user, :organizations => [host.organization], :locations => [host.location], :roles => [role]) }
75
+ let(:filter) { FactoryBot.create(:filter, permissions: [permission]) }
76
+ let(:role) { FactoryBot.create(:role, filters: [filter]) }
77
+ let(:user) do
78
+ FactoryBot.create(:user, organizations: [host.organization], locations: [host.location], roles: [role])
79
+ end
78
80
 
79
81
  test 'user can change expiry date' do
80
82
  as_user user do
81
- assert_equal true, host.can_modify_expiry_date?
83
+ assert host.can_modify_expiry_date?
82
84
  end
83
85
  end
84
86
  end
85
87
 
86
88
  context 'without edit_host_expiry permission' do
87
- let(:user) { FactoryBot.build(:user, :organizations => [host.organization], :locations => [host.location]) }
89
+ let(:user) { FactoryBot.build(:user, organizations: [host.organization], locations: [host.location]) }
88
90
 
89
91
  test 'user can not change expiry date' do
90
92
  as_user user do
91
- assert_equal false, host.can_modify_expiry_date?
93
+ assert_not host.can_modify_expiry_date?
92
94
  end
93
95
  end
94
96
  end
@@ -150,7 +152,7 @@ class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
150
152
  end
151
153
 
152
154
  test 'should only exist in correct scopes' do
153
- exists_only_in_scopes(@host, ['expiring', 'expired', 'expired_past_grace_period'])
155
+ exists_only_in_scopes(@host, %w[expiring expired expired_past_grace_period])
154
156
  end
155
157
  end
156
158
 
@@ -181,7 +183,7 @@ class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
181
183
  end
182
184
 
183
185
  test 'should only exist in correct scopes' do
184
- exists_only_in_scopes(@host, ['expiring', 'expired', 'expiring_today'])
186
+ exists_only_in_scopes(@host, %w[expiring expired expiring_today])
185
187
  end
186
188
  end
187
189
 
@@ -242,7 +244,7 @@ class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
242
244
  end
243
245
 
244
246
  test 'should only exist in correct scopes' do
245
- exists_only_in_scopes(@host, ['expiring', 'expired'])
247
+ exists_only_in_scopes(@host, %w[expiring expired])
246
248
  end
247
249
  end
248
250
 
@@ -10,7 +10,7 @@ module ForemanExpireHosts
10
10
  context 'with admin user' do
11
11
  let(:user) { FactoryBot.create(:user, :admin) }
12
12
  test 'should be authorized' do
13
- assert_equal true, authorizer.authorized?
13
+ assert authorizer.authorized?
14
14
  end
15
15
  end
16
16
 
@@ -23,25 +23,25 @@ module ForemanExpireHosts
23
23
 
24
24
  context 'with edit_hosts and edit_host_expiry permission' do
25
25
  test 'should be authorized' do
26
- FactoryBot.create(:filter, :role => role, :permissions => [edit_permission, edit_expiry_permission])
27
- assert_equal true, authorizer.authorized?
26
+ FactoryBot.create(:filter, role: role, permissions: [edit_permission, edit_expiry_permission])
27
+ assert authorizer.authorized?
28
28
  end
29
29
  end
30
30
 
31
31
  context 'with edit_hosts and owner permission' do
32
32
  setup do
33
- FactoryBot.create(:filter, :role => role, :permissions => [edit_permission])
33
+ FactoryBot.create(:filter, role: role, permissions: [edit_permission])
34
34
  end
35
- let(:hosts) { FactoryBot.create_list(:host, 2, :owner => user) }
35
+ let(:hosts) { FactoryBot.create_list(:host, 2, owner: user) }
36
36
 
37
37
  test 'should be authorized if setting allows owner' do
38
38
  Setting[:can_owner_modify_host_expiry_date] = true
39
- assert_equal true, authorizer.authorized?
39
+ assert authorizer.authorized?
40
40
  end
41
41
 
42
42
  test 'should not be authorized if setting does not allow owner' do
43
43
  Setting[:can_owner_modify_host_expiry_date] = false
44
- assert_equal false, authorizer.authorized?
44
+ assert_not authorizer.authorized?
45
45
  end
46
46
  end
47
47
  end
@@ -49,7 +49,7 @@ module ForemanExpireHosts
49
49
  context 'with unauthorized user' do
50
50
  let(:user) { FactoryBot.create(:user) }
51
51
  test 'should not be authorized' do
52
- assert_equal false, authorizer.authorized?
52
+ assert_not authorizer.authorized?
53
53
  end
54
54
  end
55
55
  end
@@ -5,7 +5,7 @@ require 'test_plugin_helper'
5
5
  class ExpirationStatusTest < ActiveSupport::TestCase
6
6
  def setup
7
7
  @host = FactoryBot.build(:host)
8
- @status = HostStatus::ExpirationStatus.new(:host => @host)
8
+ @status = HostStatus::ExpirationStatus.new(host: @host)
9
9
  end
10
10
 
11
11
  test 'is valid' do
@@ -19,8 +19,8 @@ module ForemanExpireHosts
19
19
 
20
20
  describe 'model with failing callbacks' do
21
21
  test 'return false on record delete' do
22
- h = HostWithFailingCallbacks.create!(:name => 'test')
23
- assert_equal false, SafeDestroy.new(h).destroy!
22
+ h = HostWithFailingCallbacks.create!(name: 'test')
23
+ assert_not SafeDestroy.new(h).destroy!
24
24
  assert_equal 1, HostWithFailingCallbacks.all.count
25
25
  end
26
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_expire_hosts
3
3
  version: !ruby/object:Gem::Version
4
- version: 9.0.1
4
+ version: 9.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nagarjuna Rachaneni
8
8
  - Timo Goebel
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-06-18 00:00:00.000000000 Z
11
+ date: 1980-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deface
@@ -91,7 +91,6 @@ files:
91
91
  - db/migrate/20220923181700_fix_settings_category_to_dsl.rb
92
92
  - db/migrate/20230112104438_change_expired_on_type.rb
93
93
  - db/seeds.d/80_expire_hosts_ui_notification.rb
94
- - extra/foreman_expire_hosts.cron
95
94
  - lib/foreman_expire_hosts.rb
96
95
  - lib/foreman_expire_hosts/engine.rb
97
96
  - lib/foreman_expire_hosts/version.rb
@@ -125,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
124
  - !ruby/object:Gem::Version
126
125
  version: '0'
127
126
  requirements: []
128
- rubygems_version: 3.6.7
127
+ rubygems_version: 4.0.3
129
128
  specification_version: 4
130
129
  summary: Foreman plugin for limiting host lifetime
131
130
  test_files:
@@ -1,8 +0,0 @@
1
- SHELL=/bin/sh
2
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
3
-
4
- RAILS_ENV=production
5
- FOREMAN_HOME=/usr/share/foreman
6
-
7
- # Send out notifications about expired hosts
8
- 45 7 * * * foreman /usr/sbin/foreman-rake expired_hosts:deliver_notifications >>/var/log/foreman/expired_hosts.log 2>&1