foreman_expire_hosts 6.0.1 → 6.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/Gemfile +2 -0
  4. data/app/controllers/concerns/foreman_expire_hosts/api/v2/hosts_controller_extensions.rb +2 -0
  5. data/app/controllers/concerns/foreman_expire_hosts/host_controller_extensions.rb +5 -3
  6. data/app/helpers/concerns/foreman_expire_hosts/audits_helper_extensions.rb +2 -0
  7. data/app/helpers/concerns/foreman_expire_hosts/hosts_helper_extensions.rb +2 -0
  8. data/app/helpers/expire_hosts_mailer_helper.rb +2 -0
  9. data/app/mailers/expire_hosts_mailer.rb +2 -0
  10. data/app/models/concerns/foreman_expire_hosts/host_ext.rb +3 -1
  11. data/app/models/host_status/expiration_status.rb +2 -0
  12. data/app/models/setting/expire_hosts.rb +2 -0
  13. data/app/overrides/add_expired_on_field_to_host_form.rb +2 -0
  14. data/app/overrides/add_expired_on_field_to_host_show.rb +2 -0
  15. data/app/overrides/add_js_to_host_index.rb +2 -0
  16. data/app/overrides/deleted_expired_host_comment_in_audits.rb +2 -0
  17. data/app/services/foreman_expire_hosts/action/base.rb +2 -0
  18. data/app/services/foreman_expire_hosts/action/delete_expired_hosts.rb +2 -0
  19. data/app/services/foreman_expire_hosts/action/stop_expired_hosts.rb +2 -0
  20. data/app/services/foreman_expire_hosts/expiry_edit_authorizer.rb +2 -0
  21. data/app/services/foreman_expire_hosts/notification/base.rb +4 -2
  22. data/app/services/foreman_expire_hosts/notification/deleted_hosts.rb +2 -0
  23. data/app/services/foreman_expire_hosts/notification/expiry_warning.rb +2 -0
  24. data/app/services/foreman_expire_hosts/notification/failed_deleted_hosts.rb +2 -0
  25. data/app/services/foreman_expire_hosts/notification/failed_stopped_hosts.rb +2 -0
  26. data/app/services/foreman_expire_hosts/notification/stopped_hosts.rb +2 -0
  27. data/app/services/foreman_expire_hosts/safe_destroy.rb +6 -4
  28. data/app/services/foreman_expire_hosts/ui_notifications/hosts/base.rb +2 -0
  29. data/app/services/foreman_expire_hosts/ui_notifications/hosts/expiry_warning.rb +2 -0
  30. data/app/services/foreman_expire_hosts/ui_notifications/hosts/stopped_host.rb +2 -0
  31. data/app/views/api/v2/hosts/expiration.json.rabl +2 -0
  32. data/config/routes.rb +2 -0
  33. data/db/migrate/20150427101516_add_expiry_on_to_hosts.rb +2 -0
  34. data/db/seeds.d/80_expire_hosts_ui_notification.rb +2 -0
  35. data/foreman_expire_hosts.gemspec +3 -1
  36. data/lib/expire_hosts_notifications.rb +2 -0
  37. data/lib/foreman_expire_hosts.rb +2 -0
  38. data/lib/foreman_expire_hosts/engine.rb +8 -3
  39. data/lib/foreman_expire_hosts/version.rb +3 -1
  40. data/lib/tasks/expired_hosts.rake +2 -0
  41. data/test/factories/foreman_expire_hosts_factories.rb +2 -0
  42. data/test/functional/api/v2/hosts_controller_test.rb +2 -0
  43. data/test/functional/concerns/hosts_controller_extensions_test.rb +2 -0
  44. data/test/helpers/hosts_helper_test.rb +2 -0
  45. data/test/lib/expire_hosts_notifications_test.rb +19 -0
  46. data/test/test_plugin_helper.rb +2 -0
  47. data/test/unit/concerns/host_extensions_test.rb +3 -1
  48. data/test/unit/expire_hosts_mailer_test.rb +6 -0
  49. data/test/unit/expiry_edit_authorizer_test.rb +2 -0
  50. data/test/unit/host_status/expiration_status_test.rb +2 -0
  51. data/test/unit/safe_destroy_test.rb +2 -0
  52. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66c086d3dc04916321804be89e8d5f027691b5aa5dc10b0cef317b7ac5d4ee9e
4
- data.tar.gz: 84e16983231ab5651c03c5d56484c8645bd1c6910bd677aa7bcacefb139b06d2
3
+ metadata.gz: fa01a7018582689c460e28a5e27dff4e4372ff655399ee622bf45532ece9abc6
4
+ data.tar.gz: fb08933cd67d1b5b9bf322431e322812a189498407e23b2203322d2bc4072006
5
5
  SHA512:
6
- metadata.gz: b2990701bc9efa238f25f6a6a7def12d87e9403e4b6cf0f41b477c1d5cc16a467f2edcb211fabd724034b3c149d00d654087a42e4a8e323fd403a2030ac81492
7
- data.tar.gz: 60a5e736f9e54223e5c62bcf426514e10aec9569708d3f58b672da60e41667b3537848c8fa6f00c8904c3c36aa87038304cdf44bc7d51e6a087d5c906ff3302d
6
+ metadata.gz: 02c674e4fd329fe84e740fc752ae0db2ac5e1a0619ef3d2ab4fd4001bba6c94a9c03d5854a206a81a638a3bf1ce27210a123c1e70d32da573124e93d73a7cfeb
7
+ data.tar.gz: 0c046cf84b6d6b2b6045b941f714f847d1903703d4c8a1f3988b94ffa3363804bf89faf8ffc6c5d63b3a1369ee56f25d3dedeee97f9ec0d7f297b73f9d92b2ca
data/.rubocop.yml CHANGED
@@ -2,7 +2,7 @@ inherit_from:
2
2
  - .rubocop_todo.yml
3
3
 
4
4
  AllCops:
5
- TargetRubyVersion: 2.2
5
+ TargetRubyVersion: 2.3
6
6
  TargetRailsVersion: 5.1
7
7
  Exclude:
8
8
  - 'Rakefile'
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'http://rubygems.org'
2
4
 
3
5
  gemspec
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module Api
3
5
  module V2
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module HostControllerExtensions
3
5
  def self.prepended(base)
@@ -20,14 +22,14 @@ module ForemanExpireHosts
20
22
  begin
21
23
  host.expired_on = expiration_date
22
24
  host.save!
23
- rescue StandardError => error
24
- failed_hosts[host.name] = error
25
+ rescue StandardError => e
26
+ failed_hosts[host.name] = e
25
27
  message = if expiration_date.present?
26
28
  _('Failed to set expiration date for %{host} to %{expiration_date}.') % { :host => host, :expiration_date => l(expiration_date) }
27
29
  else
28
30
  _('Failed to clear expiration date for %s.') % host
29
31
  end
30
- Foreman::Logging.exception(message, error)
32
+ Foreman::Logging.exception(message, e)
31
33
  end
32
34
  end
33
35
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module AuditsHelperExtensions
3
5
  extend ActiveSupport::Concern
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module HostsHelperExtensions
3
5
  extend ActiveSupport::Concern
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ExpireHostsMailerHelper
2
4
  def relative_date(date, opts = {})
3
5
  return _('N/A') if date.blank?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ExpireHostsMailer < ApplicationMailer
2
4
  default :content_type => 'text/html', :from => Setting[:email_reply_address] || 'noreply@your-foreman.com'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module HostExt
3
5
  extend ActiveSupport::Concern
@@ -13,7 +15,7 @@ module ForemanExpireHosts
13
15
 
14
16
  scope :expiring, -> { where('expired_on IS NOT NULL') }
15
17
  scope :with_expire_date, ->(date) { expiring.where('expired_on = ?', date) }
16
- scope :expired, -> { expiring.where('expired_on < ?', Date.today) }
18
+ scope :expired, -> { expiring.where('expired_on <= ?', Date.today) }
17
19
  scope :expiring_today, -> { expiring.with_expire_date(Date.today) }
18
20
  scope :expired_past_grace_period, -> { expiring.where('expired_on <= ?', Date.today - Setting[:days_to_delete_after_host_expiration].to_i) }
19
21
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HostStatus
2
4
  class ExpirationStatus < HostStatus::Status
3
5
  OK = 0
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Setting::ExpireHosts < Setting
2
4
  Setting::BLANK_ATTRS.push('host_expiry_email_recipients')
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Deface::Override.new(
2
4
  :virtual_path => 'hosts/_form',
3
5
  :name => 'host_form_expired_on_field',
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Deface::Override.new(
2
4
  :virtual_path => 'hosts/show',
3
5
  :name => 'host_expiry_waring_in_show',
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Deface::Override.new(
2
4
  :virtual_path => 'hosts/_list',
3
5
  :name => 'host_list_expiration_js',
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Deface::Override.new(
2
4
  :virtual_path => 'audits/_list',
3
5
  :name => 'deleted_expired_host_audit_comment_in_list',
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module Action
3
5
  class Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module Action
3
5
  class DeleteExpiredHosts < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module Action
3
5
  class StopExpiredHosts < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  class ExpiryEditAuthorizer
3
5
  attr_accessor :user, :hosts
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module Notification
3
5
  class Base
@@ -27,12 +29,12 @@ module ForemanExpireHosts
27
29
  return true if hosts.empty?
28
30
 
29
31
  build_mail_notification(recipient, hosts).deliver_now
30
- rescue SocketError, Net::SMTPError => error
32
+ rescue SocketError, Net::SMTPError => e
31
33
  message = _('Failed to deliver %{notification_name} for Hosts %{hosts}') % {
32
34
  :notification_name => humanized_name,
33
35
  :hosts => hosts.map(&:name).to_sentence
34
36
  }
35
- Foreman::Logging.exception(message, error)
37
+ Foreman::Logging.exception(message, e)
36
38
  end
37
39
 
38
40
  def deliver_ui_notifications
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module Notification
3
5
  class DeletedHosts < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module Notification
3
5
  class ExpiryWarning < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module Notification
3
5
  class FailedDeletedHosts < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module Notification
3
5
  class FailedStoppedHosts < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module Notification
3
5
  class StoppedHosts < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  class SafeDestroy
3
5
  # See http://projects.theforeman.org/issues/14702 for reasoning.
@@ -17,14 +19,14 @@ module ForemanExpireHosts
17
19
  else
18
20
  subject.destroy!
19
21
  end
20
- rescue ActiveRecord::RecordNotDestroyed => invalid
22
+ rescue ActiveRecord::RecordNotDestroyed => e
21
23
  message = _('Failed to delete %{class_name} %{subject}: %{message} - Errors: %{errors}') % {
22
24
  :class_name => subject.class.name,
23
25
  :subject => subject,
24
- :message => invalid.message,
25
- :errors => invalid.record.errors.full_messages.to_sentence
26
+ :message => e.message,
27
+ :errors => e.record.errors.full_messages.to_sentence
26
28
  }
27
- Foreman::Logging.exception(message, invalid)
29
+ Foreman::Logging.exception(message, e)
28
30
  false
29
31
  end
30
32
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module UINotifications
3
5
  module Hosts
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module UINotifications
3
5
  module Hosts
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module UINotifications
3
5
  module Hosts
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  attributes :expired_on
data/config/routes.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Foreman::Application.routes.draw do
2
4
  post 'expired_hosts/select_multiple_expiration' => 'hosts#select_multiple_expiration', as: 'select_multiple_expiration_hosts'
3
5
  post 'expired_hosts/update_multiple_expiration' => 'hosts#update_multiple_expiration', as: 'update_multiple_expiration_hosts'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddExpiryOnToHosts < ActiveRecord::Migration[4.2]
2
4
  def change
3
5
  add_column :hosts, :expired_on, :date unless column_exists? :hosts, :expired_on
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  [
2
4
  {
3
5
  group: _('Hosts'),
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path('lib/foreman_expire_hosts/version', __dir__)
2
4
  require 'date'
3
5
 
@@ -22,5 +24,5 @@ Gem::Specification.new do |s|
22
24
  s.add_dependency 'deface'
23
25
 
24
26
  s.add_development_dependency 'rdoc'
25
- s.add_development_dependency 'rubocop', '0.59.2'
27
+ s.add_development_dependency 'rubocop', '0.71.0'
26
28
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ExpireHostsNotifications
2
4
  class << self
3
5
  def delete_expired_hosts
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'foreman_expire_hosts/engine'
2
4
  module ForemanExpireHosts
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'deface'
2
4
  require 'bootstrap-datepicker-rails'
3
5
 
@@ -34,11 +36,14 @@ module ForemanExpireHosts
34
36
  permission :edit_host_expiry,
35
37
  {},
36
38
  :resource_type => 'Host'
37
- permission :edit_hosts,
38
- { :hosts => [:select_multiple_expiration, :update_multiple_expiration] },
39
- :resource_type => 'Host'
40
39
  end
41
40
 
41
+ # Extend built in permissions
42
+ Foreman::AccessControl.permission(:edit_hosts).actions.concat [
43
+ 'hosts/select_multiple_expiration',
44
+ 'hosts/update_multiple_expiration'
45
+ ]
46
+
42
47
  extend_rabl_template 'api/v2/hosts/main', 'api/v2/hosts/expiration'
43
48
  end
44
49
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
- VERSION = '6.0.1'.freeze
4
+ VERSION = '6.0.2'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Tasks
2
4
  namespace :expired_hosts do
3
5
  desc 'Delete all expired hosts, send notification email about expiring hosts'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  FactoryBot.modify do
2
4
  factory :host do
3
5
  trait :without_validation do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_plugin_helper'
2
4
 
3
5
  class Api::V2::HostsControllerTest < ActionController::TestCase
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_plugin_helper'
2
4
 
3
5
  class HostsControllerTest < ActionController::TestCase
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_plugin_helper'
2
4
 
3
5
  class HostsHelperTest < ActionView::TestCase
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_plugin_helper'
2
4
  require 'notifications_test_helper'
3
5
 
@@ -117,6 +119,23 @@ class ExpireHostsNotificationsTest < ActiveSupport::TestCase
117
119
  assert_equal 1, ActionMailer::Base.deliveries.count
118
120
  assert_includes ActionMailer::Base.deliveries.first.subject, 'Failed to stop expired hosts'
119
121
  end
122
+
123
+ context 'with a host expiring today' do
124
+ let(:host) { FactoryBot.create(:host, :expires_today, :on_compute_resource, :owner => user) }
125
+ let(:delete_date) { Date.today + Setting[:days_to_delete_after_host_expiration].to_i }
126
+
127
+ it 'should stop the host and show the correct delete date' do
128
+ power_mock.expects(:stop).returns(true)
129
+ ExpireHostsNotifications.stop_expired_hosts
130
+ assert_equal 1, ActionMailer::Base.deliveries.count
131
+ assert_includes ActionMailer::Base.deliveries.first.subject, 'Stopped expired hosts'
132
+ assert_includes ActionMailer::Base.deliveries.first.body, "These hosts will be destroyed on #{delete_date}."
133
+
134
+ travel_to delete_date do
135
+ assert_includes Host.expired_past_grace_period, host
136
+ end
137
+ end
138
+ end
120
139
  end
121
140
 
122
141
  context '#deliver_expiry_warning_notification' do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This calls the main test_helper in Foreman-core
2
4
  require 'test_helper'
3
5
  require 'database_cleaner'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_plugin_helper'
2
4
 
3
5
  class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
@@ -180,7 +182,7 @@ class ForemanExpireHostsHostExtTest < ActiveSupport::TestCase
180
182
  end
181
183
 
182
184
  test 'should only exist in correct scopes' do
183
- exists_only_in_scopes(@host, ['expiring', 'expiring_today'])
185
+ exists_only_in_scopes(@host, ['expiring', 'expired', 'expiring_today'])
184
186
  end
185
187
  end
186
188
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_plugin_helper'
2
4
 
3
5
  class ExpireHostMailerTest < ActionMailer::TestCase
@@ -35,6 +37,10 @@ class ExpireHostMailerTest < ActionMailer::TestCase
35
37
  assert_includes mail.subject, 'Stopped expired hosts in Foreman'
36
38
  end
37
39
 
40
+ test 'should include a deletion date' do
41
+ assert_includes mail.body, "These hosts will be destroyed on #{Date.today}."
42
+ end
43
+
38
44
  test 'should show mitigation text if authorized' do
39
45
  ForemanExpireHosts::ExpiryEditAuthorizer.any_instance.stubs(:authorized?).returns(true)
40
46
  assert_includes mail.body.to_s, 'Please change their expiry date'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_plugin_helper'
2
4
 
3
5
  module ForemanExpireHosts
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_plugin_helper'
2
4
 
3
5
  class ExpirationStatusTest < ActiveSupport::TestCase
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_plugin_helper'
2
4
 
3
5
  module ForemanExpireHosts
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_expire_hosts
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.1
4
+ version: 6.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nagarjuna Rachaneni
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-07-01 00:00:00.000000000 Z
12
+ date: 2019-08-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bootstrap-datepicker-rails
@@ -59,14 +59,14 @@ dependencies:
59
59
  requirements:
60
60
  - - '='
61
61
  - !ruby/object:Gem::Version
62
- version: 0.59.2
62
+ version: 0.71.0
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - '='
68
68
  - !ruby/object:Gem::Version
69
- version: 0.59.2
69
+ version: 0.71.0
70
70
  description: This Plugin will add new column expired_on to hosts to limit the lifetime
71
71
  of a host.
72
72
  email: