Skip to content

Commit

Permalink
Fixes #TBFL - Opt-in email notification on proxy sync failure
Browse files Browse the repository at this point in the history
  • Loading branch information
adamruzicka committed Sep 18, 2023
1 parent 581ec07 commit 10745fc
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 9 deletions.
38 changes: 29 additions & 9 deletions app/lib/actions/katello/capsule_content/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def resource_locks
:link
end

execution_plan_hooks.use :notify_on_failure, :on => :failure

input_format do
param :name
end
Expand All @@ -19,20 +21,16 @@ def humanized_input
end

def plan(smart_proxy, options = {})
input[:options] = options

action_subject(smart_proxy)
smart_proxy.verify_ueber_certs
environment_id = options.fetch(:environment_id, nil)
environment = ::Katello::KTEnvironment.find(environment_id) if environment_id
repository_id = options.fetch(:repository_id, nil)
repository = ::Katello::Repository.find(repository_id) if repository_id
content_view_id = options.fetch(:content_view_id, nil)
content_view = ::Katello::ContentView.find(content_view_id) if content_view_id

subjects = subjects(options)

fail _("Action not allowed for the default smart proxy.") if smart_proxy.pulp_primary?

refresh_options = options.merge(content_view: content_view,
environment: environment,
repository: repository)
refresh_options = options.merge(subjects)
sequence do
if smart_proxy.has_feature?(SmartProxy::PULP3_FEATURE)
plan_action(Actions::Pulp3::ContentGuard::Refresh, smart_proxy)
Expand All @@ -49,6 +47,28 @@ def finalize
smart_proxy&.audit_capsule_sync
end
end

def notify_on_failure(_plan)
notification = MailNotification[:capsule_sync_failure]
proxy = SmartProxy.find(input.fetch(:smart_proxy, {})[:id])
subjects = subjects(input[:options]).merge(smart_proxy: proxy)
notification.users.each do |user|
notification.deliver(subjects.merge(user: user, task: task))
end
end

def subjects(options = {})
environment_id = options.fetch(:environment_id, nil)
environment = ::Katello::KTEnvironment.find(environment_id) if environment_id

repository_id = options.fetch(:repository_id, nil)
repository = ::Katello::Repository.find(repository_id) if repository_id

content_view_id = options.fetch(:content_view_id, nil)
content_view = ::Katello::ContentView.find(content_view_id) if content_view_id

{content_view: content_view, environment: environment, repository: repository}
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions app/mailers/katello/task_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,17 @@ def cv_promote_failure(options)
end
end
end

def proxy_sync_failure(options)
user, @environment, @repo, @content_view, @task, @smart_proxy = options.values_at(:user, :environment, :repository, :content_view, :task, :smart_proxy)

::User.as(user.login) do
subject = _("%{label} failed") % { :label => @task.action }

set_locale_for(user) do
mail(:to => user.mail, :subject => subject)
end
end
end
end
end
45 changes: 45 additions & 0 deletions app/views/katello/task_mailer/proxy_sync_failure.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<p>
<%= _("%{label} failed.") % { :label => @task.action } %>
</p>

<div class="dashboard">
<table>
<% if @environment %>
<tr>
<td><%= _('Environment') %></td>
<td><%= link_to @environment.name, "#{katello_url}lifecycle_environments/#{@environment.id}" %></td>
</tr>
<% end %>
<% if @content_view %>
<tr>
<td><%= _('Content view') %></td>
<td><%= link_to @content_view.name, "#{katello_url}content_views/#{@content_view.id}" %></td>
</tr>
<% end %>
<% if @repo %>
<tr>
<td><%= _('Repository') %></td>
<td><%= link_to @repo.name, "#{katello_url}products/#{@repo.product.id}/repositories/#{@repo.id}" %></td>
</tr>
<% end %>
<tr>
<td><%= _('Task ID') %></td>
<td><%= link_to @task.id, foreman_tasks_task_url(@task.id) %></td>
</tr>
<tr>
<td><%= _('Task state') %></td>
<td><%= @task.state %></td>
</tr>
<tr>
<td><%= _('Task result') %></td>
<td><%= @task.result %></td>
</tr>
</table>

Errors:
<ul>
<% @task.execution_plan.errors.each do |error| %>
<li><%= error.exception_class %>: <%= error.message %></li>
<% end %>
</ul>
</div>
25 changes: 25 additions & 0 deletions app/views/katello/task_mailer/proxy_sync_failure.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= _("%{label} failed.") % { :label => @task.action } %>

<%= _('Smart proxy ID') %>: <%= @smart_proxy.id %>
<%= _('Smart proxy name') %>: <%= @smart_proxy.name %>
<% if @environment %>
<%= _('Environment ID') %>: <%= @environment&.id %>
<%= _('Environment name') %>: <%= @environment&.name %>
<% end %>
<% if @content_view %>
<%= _('Content view ID') %>: <%= @content_view&.id %>
<%= _('Content view name') %>: <%= @content_view&.name %>
<% end %>
<% if @repo %>
<%= _('Repository ID') %>: <%= @repo&.id %>
<%= _('Repository name') %>: <%= @repo&.name %>
<% end %>
<%= _('Task ID') %>: <%= @task.id %>
<%= _('Task state') %>: <%= @task.state %>
<%= _('Task result') %>: <%= @task.result %>
<%= _('Task details') %>: <%= foreman_tasks_task_url(@task) %>

Errors:
<% @task.execution_plan.errors.each do |error| %>
- <%= error.exception_class %>: <%= error.message %>
<% end %>
8 changes: 8 additions & 0 deletions db/seeds.d/106-mail_notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
N_('Repository sync failure')
N_('Content view publish failure')
N_('Content view promote failure')
N_('Proxy sync failure')

# Mail Notifications
notifications = [
Expand Down Expand Up @@ -63,6 +64,13 @@
:mailer => 'Katello::TaskMailer',
:method => 'cv_promote_failure',
:subscription_type => 'alert',
},

{:name => :proxy_sync_failure,
:description => N_('A notification about failed proxy sync'),
:mailer => 'Katello::TaskMailer',
:method => 'proxy_sync_failure',
:subscription_type => 'alert',
}
]

Expand Down

0 comments on commit 10745fc

Please sign in to comment.