foreman_expire_hosts 6.0.0 → 7.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +78 -0
  3. data/.rubocop.yml +18 -13
  4. data/.rubocop_todo.yml +11 -31
  5. data/Gemfile +2 -0
  6. data/README.md +1 -0
  7. data/app/controllers/concerns/foreman_expire_hosts/api/v2/hosts_controller_extensions.rb +2 -0
  8. data/app/controllers/concerns/foreman_expire_hosts/host_controller_extensions.rb +20 -15
  9. data/app/helpers/concerns/foreman_expire_hosts/audits_helper_extensions.rb +4 -0
  10. data/app/helpers/concerns/foreman_expire_hosts/hosts_helper_extensions.rb +5 -22
  11. data/app/helpers/expire_hosts_mailer_helper.rb +3 -0
  12. data/app/helpers/foreman_expire_hosts/hosts_helper.rb +16 -0
  13. data/app/mailers/expire_hosts_mailer.rb +3 -0
  14. data/app/models/concerns/foreman_expire_hosts/host_ext.rb +9 -1
  15. data/app/models/host_status/expiration_status.rb +3 -0
  16. data/app/models/setting/expire_hosts.rb +2 -0
  17. data/app/overrides/add_expired_on_field_to_host_form.rb +4 -2
  18. data/app/overrides/add_expired_on_field_to_host_show.rb +5 -3
  19. data/app/overrides/deleted_expired_host_comment_in_audits.rb +8 -6
  20. data/app/services/foreman_expire_hosts/action/base.rb +9 -2
  21. data/app/services/foreman_expire_hosts/action/delete_expired_hosts.rb +3 -0
  22. data/app/services/foreman_expire_hosts/action/stop_expired_hosts.rb +6 -2
  23. data/app/services/foreman_expire_hosts/expiry_edit_authorizer.rb +2 -0
  24. data/app/services/foreman_expire_hosts/notification/base.rb +12 -3
  25. data/app/services/foreman_expire_hosts/notification/deleted_hosts.rb +2 -0
  26. data/app/services/foreman_expire_hosts/notification/expiry_warning.rb +2 -0
  27. data/app/services/foreman_expire_hosts/notification/failed_deleted_hosts.rb +2 -0
  28. data/app/services/foreman_expire_hosts/notification/failed_stopped_hosts.rb +2 -0
  29. data/app/services/foreman_expire_hosts/notification/stopped_hosts.rb +2 -0
  30. data/app/services/foreman_expire_hosts/safe_destroy.rb +7 -5
  31. data/app/services/foreman_expire_hosts/ui_notifications/hosts/base.rb +4 -1
  32. data/app/services/foreman_expire_hosts/ui_notifications/hosts/expiry_warning.rb +2 -0
  33. data/app/services/foreman_expire_hosts/ui_notifications/hosts/stopped_host.rb +2 -0
  34. data/app/views/api/v2/hosts/expiration.json.rabl +2 -0
  35. data/app/views/hosts/_expired_on_field.html.erb +11 -20
  36. data/app/views/hosts/select_multiple_expiration.html.erb +10 -15
  37. data/config/routes.rb +2 -0
  38. data/db/migrate/20150427101516_add_expiry_on_to_hosts.rb +2 -0
  39. data/db/seeds.d/80_expire_hosts_ui_notification.rb +2 -0
  40. data/extra/foreman_expire_hosts.cron +8 -0
  41. data/foreman_expire_hosts.gemspec +10 -3
  42. data/lib/expire_hosts_notifications.rb +3 -0
  43. data/lib/foreman_expire_hosts.rb +2 -0
  44. data/lib/foreman_expire_hosts/engine.rb +22 -10
  45. data/lib/foreman_expire_hosts/version.rb +3 -1
  46. data/lib/tasks/expired_hosts.rake +3 -1
  47. data/test/factories/foreman_expire_hosts_factories.rb +7 -5
  48. data/test/functional/api/v2/hosts_controller_test.rb +3 -1
  49. data/test/functional/concerns/hosts_controller_extensions_test.rb +3 -1
  50. data/test/helpers/hosts_helper_test.rb +5 -0
  51. data/test/lib/expire_hosts_notifications_test.rb +91 -45
  52. data/test/test_plugin_helper.rb +2 -0
  53. data/test/unit/concerns/host_extensions_test.rb +21 -19
  54. data/test/unit/expire_hosts_mailer_test.rb +6 -0
  55. data/test/unit/expiry_edit_authorizer_test.rb +2 -0
  56. data/test/unit/host_status/expiration_status_test.rb +3 -1
  57. data/test/unit/safe_destroy_test.rb +3 -1
  58. metadata +53 -26
  59. data/app/assets/javascripts/foreman_expire_hosts/application.js +0 -3
  60. data/app/assets/javascripts/foreman_expire_hosts/datepicker_for_host_expired_on_field.js +0 -27
  61. data/app/assets/stylesheets/foreman_expire_hosts/application.scss +0 -4
  62. data/app/overrides/add_js_to_host_index.rb +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8d016e02f798e790c84d0ff29badd0fbfbaf122b941787842dce237f19fcb0d9
4
- data.tar.gz: 361195ee1b65ac38de1bad8e9ff7ecd6172202f52d524c3729bfbff022fe0a85
3
+ metadata.gz: f2f0094dacc9c70e393e1bf88b942e63e03d61ad43f9279dac3bc55dc8796d0f
4
+ data.tar.gz: f9768b9ad368199ed8b8d8965664788797cf59db5c0888cf4f846c324105d889
5
5
  SHA512:
6
- metadata.gz: 55d8ac2d1ddb0b96effb841ce5b897ef3c583b3b5bd619b337015c35a0f4744f7db57e70919ec8c946ad0a2d3a424263d1fad0e01ded37e540ca220d258158c7
7
- data.tar.gz: 94c8f531186c1c55b70297e8c3905f17fb823326e3cf06c94c221aa7daa675deefc9f017161d8b0070d48164b58c8a6f8031eebf951d839990da24beeb656676
6
+ metadata.gz: e2093d7a71c66e85f18b2712e0bd9c39397d1f731e8cdd229a1b7e55805d97d7b751694760b2f4912b169e3110a6a9847bdff8a7d43595b6d7904f35100bd1b9
7
+ data.tar.gz: 6d24cb1a6fe7c1704680a95e06b90ebffeb8636e752e007a5bd8be48d5101fa5b25c169396ed61f7ff2320f77f46083115b78798626d371ed540fcaee632c143
@@ -0,0 +1,78 @@
1
+ name: CI
2
+ on: [push, pull_request]
3
+ env:
4
+ RAILS_ENV: test
5
+ DATABASE_URL: postgresql://postgres:@localhost/test
6
+ DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL: true
7
+ jobs:
8
+ rubocop:
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v2
12
+ - name: Setup Ruby
13
+ uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.3
16
+ - name: Setup
17
+ run: |
18
+ gem install bundler
19
+ bundle install --jobs=3 --retry=3
20
+ - name: Run rubocop
21
+ run: bundle exec rubocop
22
+ test:
23
+ runs-on: ubuntu-latest
24
+ needs: rubocop
25
+ services:
26
+ postgres:
27
+ image: postgres:12.1
28
+ ports: ['5432:5432']
29
+ options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
30
+ strategy:
31
+ fail-fast: false
32
+ matrix:
33
+ foreman-core-branch: [1.24-stable, 2.0-stable, 2.1-stable, develop]
34
+ ruby-version: [2.3, 2.5, 2.6]
35
+ node-version: [10]
36
+ exclude:
37
+ - foreman-core-branch: 2.0-stable
38
+ ruby-version: 2.3
39
+ - foreman-core-branch: 2.1-stable
40
+ ruby-version: 2.3
41
+ - foreman-core-branch: develop
42
+ ruby-version: 2.3
43
+ steps:
44
+ - name: Install dependencies
45
+ run: |
46
+ sudo apt-get update
47
+ sudo apt-get install build-essential libcurl4-openssl-dev libvirt-dev ruby-libvirt zlib1g-dev libpq-dev
48
+ - uses: actions/checkout@v2
49
+ with:
50
+ repository: theforeman/foreman
51
+ ref: ${{ matrix.foreman-core-branch }}
52
+ - uses: actions/checkout@v2
53
+ with:
54
+ path: foreman_expire_hosts
55
+ - name: Setup Ruby
56
+ uses: ruby/setup-ruby@v1
57
+ with:
58
+ ruby-version: ${{ matrix.ruby-version }}
59
+ - name: Setup Node
60
+ uses: actions/setup-node@v1
61
+ with:
62
+ node-version: ${{ matrix.node-version }}
63
+ - name: Setup Plugin
64
+ run: |
65
+ echo "gem 'foreman_expire_hosts', path: './foreman_expire_hosts'" > bundler.d/foreman_expire_hosts.local.rb
66
+ gem install bundler
67
+ bundle config set without journald development console mysql2 sqlite
68
+ bundle install --jobs=3 --retry=3
69
+ bundle exec rake db:create
70
+ bundle exec rake db:migrate
71
+ npm install
72
+ bundle exec rake webpack:compile
73
+ - name: Run plugin tests
74
+ run: bundle exec rake test:foreman_expire_hosts
75
+ - name: Precompile plugin assets
76
+ run: bundle exec rake 'plugin:assets:precompile[foreman_expire_hosts]'
77
+ env:
78
+ RAILS_ENV: production
@@ -1,8 +1,13 @@
1
1
  inherit_from:
2
2
  - .rubocop_todo.yml
3
3
 
4
+ require:
5
+ - rubocop-minitest
6
+ - rubocop-performance
7
+ - rubocop-rails
8
+
4
9
  AllCops:
5
- TargetRubyVersion: 2.2
10
+ TargetRubyVersion: 2.3
6
11
  TargetRailsVersion: 5.1
7
12
  Exclude:
8
13
  - 'Rakefile'
@@ -17,18 +22,12 @@ Rails/Date:
17
22
  Style/Documentation:
18
23
  Enabled: false
19
24
 
20
- Metrics/MethodLength:
21
- Max: 40
22
-
23
25
  Naming/AccessorMethodName:
24
26
  Enabled: false
25
27
 
26
28
  Style/RedundantSelf:
27
29
  Enabled: false
28
30
 
29
- Metrics/ClassLength:
30
- Max: 500
31
-
32
31
  Naming/FileName:
33
32
  Exclude:
34
33
  - 'db/seeds.d/*'
@@ -36,9 +35,6 @@ Naming/FileName:
36
35
  Style/WordArray:
37
36
  Enabled: false
38
37
 
39
- Style/BracesAroundHashParameters:
40
- Enabled: false
41
-
42
38
  Style/RescueModifier:
43
39
  Enabled: false
44
40
 
@@ -48,9 +44,6 @@ Style/ClassAndModuleChildren:
48
44
  Style/EachWithObject:
49
45
  Enabled: false
50
46
 
51
- Metrics/ParameterLists:
52
- Enabled: false
53
-
54
47
  Style/SymbolArray:
55
48
  Enabled: false
56
49
 
@@ -71,3 +64,15 @@ Style/CommentedKeyword:
71
64
 
72
65
  Rails/LexicallyScopedActionFilter:
73
66
  Enabled: false
67
+
68
+ Rails/OutputSafety:
69
+ Enabled: false
70
+
71
+ Rails/SkipsModelValidations:
72
+ Enabled: false
73
+
74
+ Metrics:
75
+ Enabled: false
76
+
77
+ Layout/LineLength:
78
+ Enabled: false
@@ -1,42 +1,22 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2017-08-01 16:36:14 +0200 using RuboCop version 0.49.1.
3
+ # on 2019-12-04 09:30:58 +0100 using RuboCop version 0.75.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 12
10
- Metrics/AbcSize:
11
- Max: 51
9
+ Minitest/RefuteFalse:
10
+ Enabled: false
12
11
 
13
- # Offense count: 4
14
- # Configuration parameters: CountComments, ExcludedMethods.
15
- Metrics/BlockLength:
16
- Max: 35
17
- Exclude:
18
- - 'test/**/*.rb'
12
+ Minitest/AssertTruthy:
13
+ Enabled: false
19
14
 
20
- # Offense count: 3
21
- Metrics/CyclomaticComplexity:
22
- Max: 11
15
+ Style/HashEachMethods:
16
+ Enabled: false
23
17
 
24
- # Offense count: 89
25
- # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
26
- # URISchemes: http, https
27
- Metrics/LineLength:
28
- Max: 362
18
+ Style/HashTransformKeys:
19
+ Enabled: false
29
20
 
30
- # Offense count: 1
31
- # Configuration parameters: CountComments.
32
- Metrics/ModuleLength:
33
- Max: 110
34
-
35
- # Offense count: 3
36
- Metrics/PerceivedComplexity:
37
- Max: 11
38
-
39
- # Offense count: 2
40
- Rails/OutputSafety:
41
- Exclude:
42
- - 'app/helpers/concerns/foreman_expire_hosts/audits_helper_extensions.rb'
21
+ Style/HashTransformValues:
22
+ Enabled: false
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'http://rubygems.org'
2
4
 
3
5
  gemspec
data/README.md CHANGED
@@ -18,6 +18,7 @@ This plugin will send two warning notifications before host expiry (see Settings
18
18
  | >= 1.16 | ~> 4.0 |
19
19
  | >= 1.17 | ~> 5.0 |
20
20
  | >= 1.18 | ~> 6.0 |
21
+ | >= 1.24 | ~> 7.0 |
21
22
 
22
23
  # Screenshots
23
24
  ![Expiry date field in host form](https://raw.githubusercontent.com/ingenico-group/screenshots/master/foreman_host_expiry/expiry-date-field-in-host-form.png)
@@ -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)
@@ -11,23 +13,20 @@ module ForemanExpireHosts
11
13
  def select_multiple_expiration; end
12
14
 
13
15
  def update_multiple_expiration
14
- expiration_date = params[:host][:expired_on]
15
- expiration_date = Date.parse(expiration_date) if expiration_date.present?
16
-
17
16
  failed_hosts = {}
18
17
 
19
18
  @hosts.each do |host|
20
19
  begin
21
20
  host.expired_on = expiration_date
22
21
  host.save!
23
- rescue StandardError => error
24
- failed_hosts[host.name] = error
22
+ rescue StandardError => e
23
+ failed_hosts[host.name] = e
25
24
  message = if expiration_date.present?
26
25
  _('Failed to set expiration date for %{host} to %{expiration_date}.') % { :host => host, :expiration_date => l(expiration_date) }
27
26
  else
28
27
  _('Failed to clear expiration date for %s.') % host
29
28
  end
30
- Foreman::Logging.exception(message, error)
29
+ Foreman::Logging.exception(message, e)
31
30
  end
32
31
  end
33
32
 
@@ -48,15 +47,21 @@ module ForemanExpireHosts
48
47
  private
49
48
 
50
49
  def validate_multiple_expiration
51
- if params[:host].nil? || (expiration_date = params[:host][:expired_on]).nil?
52
- error _('No expiration date selected!')
53
- redirect_to(select_multiple_expiration_hosts_path) && (return false)
54
- end
55
- begin
56
- Date.parse(expiration_date) if expiration_date.present?
57
- rescue ArgumentError
58
- error _('Invalid expiration date!')
59
- redirect_to(select_multiple_expiration_hosts_path) && (return false)
50
+ expiration_date
51
+ rescue ArgumentError
52
+ error _('Invalid expiration date!')
53
+ redirect_to(hosts_path) && (return false)
54
+ end
55
+
56
+ def expiration_date
57
+ @expiration_date ||= begin
58
+ year = params['host']['expired_on(1i)']
59
+ month = params['host']['expired_on(2i)']
60
+ day = params['host']['expired_on(3i)']
61
+
62
+ return if year.empty? && month.empty? && day.empty?
63
+
64
+ Date.parse("#{year}-#{month}-#{day}")
60
65
  end
61
66
  end
62
67
 
@@ -1,14 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module AuditsHelperExtensions
3
5
  extend ActiveSupport::Concern
4
6
 
5
7
  def destroyed_expired_host_audit_comment_in_list(audit)
6
8
  return unless audit.auditable_type.to_s == 'Host' && audit.action == 'destroy' && audit.comment.present?
9
+
7
10
  "<div style='color: #737373;font-size: 14px'>Comment: #{audit.comment}</div>".html_safe
8
11
  end
9
12
 
10
13
  def destroyed_expired_host_audit_comment_in_show(audit)
11
14
  return unless audit.auditable_type.to_s == 'Host' && audit.action == 'destroy' && audit.comment.present?
15
+
12
16
  "<tr><td>Comment</td><td>#{audit.comment}</td></tr>".html_safe
13
17
  end
14
18
  end
@@ -1,21 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ForemanExpireHosts
2
4
  module HostsHelperExtensions
3
5
  extend ActiveSupport::Concern
4
6
 
5
- module Overrides
6
- def multiple_actions
7
- actions = super
8
- actions << [_('Change Expiration'), select_multiple_expiration_hosts_path] if authorized_for(:controller => :hosts, :action => :select_multiple_expiration)
9
- actions
10
- end
11
- end
12
-
13
- included do
14
- prepend Overrides
15
- end
16
-
17
7
  def host_expiry_warning_message(host)
18
8
  return nil unless host.expires?
9
+
19
10
  if host.expired_past_grace_period?
20
11
  message = _('This host has expired %s ago and needs to be deleted manually.') % time_ago_in_words(host.expired_on)
21
12
  elsif host.expired?
@@ -32,18 +23,10 @@ module ForemanExpireHosts
32
23
  distance_of_time_in_words(to_time, Time.current, options)
33
24
  end
34
25
 
35
- def input_group_addon(content, options = {})
36
- content_tag :span, class: 'input-group-addon' do
37
- content_tag :span, content, options
38
- end
39
- end
40
-
41
- def datepicker_f(form, attr, options = {})
26
+ def datepicker_f(form, attr, options = {}, html_options = {})
42
27
  field(form, attr, options) do
43
28
  addClass options, 'form-control'
44
- date = form.text_field attr, options
45
- addon = input_group_addon('', :class => 'glyphicon glyphicon-calendar')
46
- input_group date, addon
29
+ form.date_select(attr, options, html_options)
47
30
  end
48
31
  end
49
32
  end
@@ -1,6 +1,9 @@
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?
6
+
4
7
  opts[:tense] ||= :future if Date.today < date
5
8
  opts[:tense] ||= :past if Date.today > date
6
9
 
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanExpireHosts
4
+ module HostsHelper
5
+ def expire_hosts_host_multiple_actions
6
+ actions = []
7
+ if authorized_for(:controller => :hosts, :action => :select_multiple_expiration)
8
+ actions << {
9
+ action: [_('Change Expiration'), select_multiple_expiration_hosts_path],
10
+ priority: 200
11
+ }
12
+ end
13
+ actions
14
+ end
15
+ end
16
+ end
@@ -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
 
@@ -80,6 +82,7 @@ class ExpireHostsMailer < ApplicationMailer
80
82
  def recipient_mail(recipient)
81
83
  return recipient if recipient.is_a?(Array)
82
84
  return recipient.mail if recipient.mail.present?
85
+
83
86
  admin_email
84
87
  end
85
88
 
@@ -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
 
@@ -40,27 +42,32 @@ module ForemanExpireHosts
40
42
 
41
43
  def expires_today?
42
44
  return false unless expires?
45
+
43
46
  expired_on.to_date == Date.today
44
47
  end
45
48
 
46
49
  def expired?
47
50
  return false unless expires?
51
+
48
52
  expired_on.to_date < Date.today
49
53
  end
50
54
 
51
55
  def expiration_grace_period_end_date
52
56
  return nil unless expires?
57
+
53
58
  expired_on + Setting[:days_to_delete_after_host_expiration].to_i
54
59
  end
55
60
 
56
61
  def expired_past_grace_period?
57
62
  return false unless expires?
63
+
58
64
  expiration_grace_period_end_date <= Date.today
59
65
  end
60
66
 
61
67
  def pending_expiration?
62
68
  return false unless expires?
63
69
  return false if expired?
70
+
64
71
  expired_on - Setting['notify1_days_before_host_expiry'].to_i <= Date.today
65
72
  end
66
73
 
@@ -70,6 +77,7 @@ module ForemanExpireHosts
70
77
  return true unless User.current
71
78
  return true if Authorizer.new(User.current).can?(:edit_host_expiry, self)
72
79
  return true if self.owner_type.nil? || self.owner.nil?
80
+
73
81
  Setting[:can_owner_modify_host_expiry_date] &&
74
82
  ((self.owner_type == 'User' && self.owner == User.current) ||
75
83
  (self.owner_type == 'Usergroup' && self.owner.all_users.include?(User.current)))