-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #36735 - Add a notication on RHEL lifecycle expiry
- Loading branch information
Showing
8 changed files
with
176 additions
and
42 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
app/jobs/create_host_lifecycle_expire_soon_notifications.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateHostLifecycleExpireSoonNotifications < ApplicationJob | ||
def perform | ||
Katello::UINotifications::Hosts::LifecycleExpireSoon.deliver! | ||
ensure | ||
self.class.set(:wait => 1.week).perform_later | ||
end | ||
|
||
def humanized_name | ||
_('Host lifecycle support expiration notification') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/services/katello/ui_notifications/hosts/lifecycle_expire_soon.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module Katello | ||
module UINotifications | ||
module Hosts | ||
class LifecycleExpireSoon | ||
def self.deliver! | ||
::Katello::RhelLifecycleStatus.lifecycles_expire_soon.each do |release, schedule| | ||
schedule.each do |lifecycle, end_date| | ||
count = hosts_with_index(release).count | ||
next if count == 0 | ||
|
||
message = message(count: count, release: release, lifecycle: lifecycle, end_date: end_date) | ||
if (notification = existing_notification(release)) | ||
/[^:]+: (?<number_of_hosts>\d+) hosts/ =~ notification.message | ||
next if number_of_hosts == count.to_s | ||
notification.update(message: message) | ||
else | ||
::Notification.create!( | ||
:initiator => User.anonymous_admin, | ||
:audience => Notification::AUDIENCE_GLOBAL, | ||
:message => message, | ||
:expired_at => end_date.strftime('%Y-%m-%d'), | ||
:notification_blueprint => blueprint | ||
) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def self.existing_notification(release) | ||
blueprint.notifications.where("message like ?", "#{release}%").first | ||
end | ||
|
||
def self.message(options) | ||
::UINotifications::StringParser.new( | ||
blueprint.message, | ||
:number_of_hosts => options[:count], | ||
:release => options[:release], | ||
:lifecycle => options[:lifecycle].gsub(/_/, " "), | ||
:end_date => options[:end_date].strftime('%Y-%m-%d'), | ||
:audience => Notification::AUDIENCE_GLOBAL | ||
) | ||
end | ||
|
||
def self.hosts_with_index(release) | ||
/RHEL(?<major>\d+)/ =~ release | ||
::Host::Managed.joins(:operatingsystem, :fact_values, :fact_names) | ||
.where(fact_names: {name: "distribution::name"}) | ||
.where("fact_values.value like ?", "Red Hat Enterprise Linux%") | ||
.where(operatingsystem: {major: major}) | ||
end | ||
|
||
def self.blueprint | ||
@blueprint ||= NotificationBlueprint.find_by(name: 'host_lifecycle_expire_soon') | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# First, we check if there's a job already enqueued for any notifications | ||
::Foreman::Application.dynflow.config.on_init do |world| | ||
[CreateExpiredManifestNotifications, CreatePulpDiskSpaceNotifications, SendExpireSoonNotifications].each do |job_class| | ||
[CreateExpiredManifestNotifications, CreateHostLifecycleExpireSoonNotifications, CreatePulpDiskSpaceNotifications, SendExpireSoonNotifications].each do |job_class| | ||
job_class.spawn_if_missing(world) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
test/services/katello/ui_notifications/hosts/lifecycle_expire_soon_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
require 'katello_test_helper' | ||
|
||
# 'RHEL9' => { | ||
# 'maintenance_support' => end_of_day('2032-05-31'), | ||
# 'extended_support' => end_of_day('2035-05-31') | ||
# }, | ||
# 'RHEL8' => { | ||
# 'maintenance_support' => end_of_day('2029-05-31'), | ||
# 'extended_support' => end_of_day('2032-05-31') | ||
# }, | ||
# 'RHEL7' => { | ||
# 'maintenance_support' => end_of_day('2024-06-30'), | ||
# 'extended_support' => end_of_day('2028-06-30') | ||
# }, | ||
# 'RHEL6' => { | ||
# 'maintenance_support' => end_of_day('2020-11-30'), | ||
# 'extended_support' => end_of_day('2024-06-30') | ||
# }, | ||
|
||
module Katello | ||
module UINotifications | ||
module Hosts | ||
class LifecycleExpireSoonTest < ::ActiveSupport::TestCase | ||
def setup | ||
blueprint = FactoryBot.create(:notification_blueprint, :name => 'host_lifecycle_expire_soon') | ||
@subject = Katello::UINotifications::Hosts::LifecycleExpireSoon | ||
@subject.stubs(:blueprint).returns(blueprint) | ||
end | ||
|
||
def teardown | ||
NotificationBlueprint.find_by(name: 'host_lifecycle_expire_soon').notifications.destroy_all | ||
end | ||
|
||
def test_with_year_2024_1_1 | ||
Time.stubs(:now).returns(Time.utc(2024, 1, 1)) | ||
@subject.expects(:hosts_with_index).with("RHEL6").returns([mock('rhel6')]) | ||
@subject.expects(:hosts_with_index).with("RHEL7").returns([mock('rhel7')]) | ||
@subject.deliver! | ||
assert_equal 2, NotificationBlueprint.find_by(name: 'host_lifecycle_expire_soon').notifications.count | ||
end | ||
|
||
def test_with_year_2025_1_1 | ||
Time.stubs(:now).returns(Time.utc(2025, 1, 1)) | ||
@subject.deliver! | ||
assert_equal 0, NotificationBlueprint.find_by(name: 'host_lifecycle_expire_soon').notifications.count | ||
end | ||
|
||
def test_with_year_2026_6_1 | ||
Time.stubs(:now).returns(Time.utc(2026, 6, 1)) | ||
@subject.deliver! | ||
assert_equal 0, NotificationBlueprint.find_by(name: 'host_lifecycle_expire_soon').notifications.count | ||
end | ||
|
||
def test_with_year_2027_7_1 | ||
Time.stubs(:now).returns(Time.utc(2027, 7, 1)) | ||
@subject.expects(:hosts_with_index).with("RHEL7").returns([mock('rhel7')]) | ||
@subject.deliver! | ||
assert_equal 1, NotificationBlueprint.find_by(name: 'host_lifecycle_expire_soon').notifications.count | ||
end | ||
|
||
def test_with_year_2028_6_1 | ||
Time.stubs(:now).returns(Time.utc(2028, 6, 1)) | ||
@subject.expects(:hosts_with_index).with("RHEL7").returns([mock('rhel7')]) | ||
@subject.expects(:hosts_with_index).with("RHEL8").returns([mock('rhel8')]) | ||
@subject.deliver! | ||
assert_equal 2, NotificationBlueprint.find_by(name: 'host_lifecycle_expire_soon').notifications.count | ||
end | ||
|
||
def test_with_year_2029_6_1 | ||
Time.stubs(:now).returns(Time.utc(2029, 6, 1)) | ||
@subject.deliver! | ||
assert_equal 0, NotificationBlueprint.find_by(name: 'host_lifecycle_expire_soon').notifications.count | ||
end | ||
|
||
def test_with_year_2030_6_1 | ||
Time.stubs(:now).returns(Time.utc(2030, 6, 1)) | ||
@subject.deliver! | ||
assert_equal 0, NotificationBlueprint.find_by(name: 'host_lifecycle_expire_soon').notifications.count | ||
end | ||
|
||
def test_with_year_2031_6_1 | ||
Time.stubs(:now).returns(Time.utc(2031, 6, 1)) | ||
@subject.expects(:hosts_with_index).with("RHEL8").returns([mock('rhel8')]) | ||
@subject.expects(:hosts_with_index).with("RHEL9").returns([mock('rhel9')]) | ||
@subject.deliver! | ||
assert_equal 2, NotificationBlueprint.find_by(name: 'host_lifecycle_expire_soon').notifications.count | ||
end | ||
end | ||
end | ||
end | ||
end |