Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions app/lib/actions/katello/content_view/auto_publish.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Actions
module Katello
module ContentView
class AutoPublish < Actions::ActionWithSubPlans
def plan(content_view, options)
plan_self(content_view_id: content_view.id,
description: options[:description],
triggered_by: options[:triggered_by])
end

def total_count
1
end

def content_view_locks
ForemanTasks::Lock.where(
resource_id: input[:content_view_id],
resource_type: ::Katello::ContentView.to_s)
end

def create_sub_plans
if content_view_locks.any?
Rails.logger.info "Locks found, sleeping"
try_again_later
else
begin
trigger(Publish, ::Katello::ContentView.find(input[:content_view_id]))
rescue ForemanTasks::Lock::LockConflict
Rails.logger.info "Got a lock conflict"
try_again_later
end
end
end

private

def try_again_later
output.delete(:total_count) # call initiate instead of resume in WithSubPlans
plan_event(nil, polling_interval)
suspend
end
end
end
end
end
16 changes: 8 additions & 8 deletions app/lib/actions/katello/content_view/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Publish < Actions::EntryAction
attr_accessor :version

execution_plan_hooks.use :trigger_capsule_sync, :on => :success
execution_plan_hooks.use :auto_publish_composites, :on => :success
execution_plan_hooks.use :notify_on_failure, :on => [:failure, :paused]

# rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
Expand Down Expand Up @@ -43,7 +44,7 @@ def plan(content_view, description = "", options = {importing: false, syncable:
:action => ::Katello::ContentViewHistory.actions[:publish],
:task => self.task,
:notes => description,
:triggered_by => options[:triggered_by]
:triggered_by_id => options[:triggered_by]
)
source_repositories = []
content_view.publish_repositories(options[:override_components]) do |repositories|
Expand Down Expand Up @@ -99,7 +100,6 @@ def plan(content_view, description = "", options = {importing: false, syncable:
plan_action(ContentView::ErrataMail, content_view, library) unless options[:skip_promotion]
plan_action(ContentView::Promote, version, find_environments(options[:environment_ids]), options[:is_force_promote]) if options[:environment_ids]&.any?
plan_self(history_id: history.id, content_view_id: content_view.id,
auto_publish_composite_ids: auto_publish_composite_ids(content_view),
content_view_version_name: version.name,
content_view_version_id: version.id,
environment_id: library.id, user_id: ::User.current.id, skip_promotion: options[:skip_promotion])
Expand All @@ -112,8 +112,7 @@ def humanized_name
end

def run
version = ::Katello::ContentViewVersion.find(input[:content_view_version_id])
version.auto_publish_composites!
sleep 5 # Testing for creating lock conflicts

output[:content_view_id] = input[:content_view_id]
output[:content_view_version_id] = input[:content_view_version_id]
Expand All @@ -140,6 +139,11 @@ def finalize
history.save!
end

def auto_publish_composites(_execution_plan)
version = ::Katello::ContentViewVersion.find(input[:content_view_version_id])
version.auto_publish_composites!
end

def trigger_capsule_sync(_execution_plan)
environment = ::Katello::KTEnvironment.find(input[:environment_id])
view = ::Katello::ContentView.find(input[:content_view_id])
Expand Down Expand Up @@ -213,10 +217,6 @@ def repos_to_delete(content_view)
end
end

def auto_publish_composite_ids(content_view)
content_view.auto_publish_components.pluck(:composite_content_view_id)
end

def version_for_publish(content_view, options)
if options[:minor] && options[:major]
if options[:override_components]
Expand Down
4 changes: 4 additions & 0 deletions app/models/katello/content_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ def auto_publish_components
component_composites.where(latest: true).joins(:composite_content_view).where(self.class.table_name => { auto_publish: true })
end

def auto_publish_component_composites
Katello::ContentView.where(id: auto_publish_components.pluck(:composite_content_view_id))
end

def publish_repositories(override_components = nil)
repositories = composite? ? repositories_to_publish_by_library_instance(override_components).values : repositories_to_publish
repositories.each do |repos|
Expand Down
16 changes: 9 additions & 7 deletions app/models/katello/content_view_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,18 @@ def update_content_counts!
save!
end

def auto_publish_composites!
metadata = {
def auto_publish_options
{
description: _("Auto Publish - Triggered by '%s'") % self.name,
triggered_by: self.id,
}
self.content_view.auto_publish_components.pluck(:composite_content_view_id).each do |composite_id|
::Katello::EventQueue.push_event(::Katello::Events::AutoPublishCompositeView::EVENT_TYPE, composite_id) do |attrs|
attrs[:metadata] = metadata
end
end
end

def auto_publish_composites!
auto_publish_composites = content_view.auto_publish_component_composites
return unless auto_publish_composites.any?

ForemanTasks.async_task(Actions::BulkAction, Actions::Katello::ContentView::AutoPublish, auto_publish_composites, auto_publish_options)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AutoPublish can change to handle N >1 sub plans so a BulkAction probably shouldn't / needn't be used

end

def repository_type_counts_map
Expand Down
41 changes: 0 additions & 41 deletions app/models/katello/events/auto_publish_composite_view.rb

This file was deleted.

1 change: 0 additions & 1 deletion lib/katello/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ class Engine < ::Rails::Engine
load 'katello/scheduled_jobs.rb'

Katello::EventQueue.register_event(Katello::Events::ImportPool::EVENT_TYPE, Katello::Events::ImportPool)
Katello::EventQueue.register_event(Katello::Events::AutoPublishCompositeView::EVENT_TYPE, Katello::Events::AutoPublishCompositeView)
Katello::EventQueue.register_event(Katello::Events::DeleteLatestContentViewVersion::EVENT_TYPE, Katello::Events::DeleteLatestContentViewVersion)
Katello::EventQueue.register_event(Katello::Events::GenerateHostApplicability::EVENT_TYPE, Katello::Events::GenerateHostApplicability)
Katello::EventQueue.register_event(Katello::Events::DeletePool::EVENT_TYPE, Katello::Events::DeletePool)
Expand Down
35 changes: 6 additions & 29 deletions test/actions/katello/content_view_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
module ::Actions::Katello::ContentView
class TestBase < ActiveSupport::TestCase
include Dynflow::Testing
include Support::Actions::Fixtures
include FactoryBot::Syntax::Methods

let(:action) { create_action action_class }
Expand Down Expand Up @@ -171,7 +170,6 @@ class PublishTest < TestBase
action.stubs(:separated_repo_mapping).returns(separated_repo_map)
action.stubs(:plan_self)
action.stubs(:find_environments).returns([])
action.stubs(:auto_publish_composite_ids).returns([])
action.stubs(:repos_to_delete).returns([])
::Katello::ContentViewHistory.stubs(:create!).returns(mock('history', id: 99))
content_view.stubs(:publish_repositories).yields([])
Expand Down Expand Up @@ -205,7 +203,6 @@ class PublishTest < TestBase
action.stubs(:separated_repo_mapping).returns(separated_repo_map)
action.stubs(:plan_self)
action.stubs(:find_environments).returns([])
action.stubs(:auto_publish_composite_ids).returns([])
action.stubs(:repos_to_delete).returns([])
::Katello::ContentViewHistory.stubs(:create!).returns(mock('history', id: 99))
content_view.stubs(:publish_repositories).yields([])
Expand Down Expand Up @@ -238,7 +235,6 @@ class PublishTest < TestBase
action.stubs(:separated_repo_mapping).returns(separated_repo_map)
action.stubs(:plan_self)
action.stubs(:find_environments).returns([])
action.stubs(:auto_publish_composite_ids).returns([])
action.stubs(:repos_to_delete).returns([])
::Katello::ContentViewHistory.stubs(:create!).returns(mock('history', id: 99))
content_view.stubs(:publish_repositories).yields([])
Expand All @@ -248,34 +244,15 @@ class PublishTest < TestBase
refute_action_planned action, ::Actions::Katello::Repository::MultiCloneToVersion
end

context 'run phase' do
it 'creates auto-publish events for non-composite views' do
composite_view = katello_content_views(:composite_view)
action.stubs(:task).returns(success_task)

FactoryBot.create(:katello_content_view_component,
latest: true,
composite_content_view: composite_view,
content_view: content_view)

plan_action action, content_view
run_action action

event = Katello::Event.find_by(event_type: Katello::Events::AutoPublishCompositeView::EVENT_TYPE, object_id: composite_view.id)
version = content_view.versions.last

assert_equal event.metadata[:triggered_by], version.id
assert_equal event.metadata[:description], "Auto Publish - Triggered by '#{version.name}'"
end
it 'can auto publish composite content views' do
action.stubs(:task).returns(success_task)

it 'does nothing for non-composite view' do
action.stubs(:task).returns(success_task)
plan_action action, katello_content_views(:no_environment_view)

plan_action action, katello_content_views(:no_environment_view)
run_action action
version = mock(:content_view_version, auto_publish_composites!: true)
Katello::ContentViewVersion.expects(:find).with(action.input[:content_view_version_id]).returns(version)

assert_empty Katello::Event.all
end
action.auto_publish_composites(nil)
end

context 'finalize phase' do
Expand Down
19 changes: 19 additions & 0 deletions test/models/content_view_version_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,24 @@ def test_filters_applied
assert_equal filters_json["applied_filters"].first["rules"].size, 1
assert_equal filters_json["applied_filters"].first["rules"].first["name"], "abc*"
end

def test_auto_publish_composites
composite_views = [mock(:composite_view)]
version = FactoryBot.build_stubbed(:katello_content_view_version)
version.content_view.expects(:auto_publish_component_composites).returns(composite_views)

ForemanTasks.expects(:async_task).with(Actions::BulkAction, Actions::Katello::ContentView::AutoPublish, composite_views, version.auto_publish_options)

version.auto_publish_composites!
end

def test_auto_publish_composites_no_composites
version = FactoryBot.build_stubbed(:katello_content_view_version)
version.content_view.expects(:auto_publish_component_composites).returns([])

ForemanTasks.expects(:async_task).never

version.auto_publish_composites!
end
end
end
37 changes: 0 additions & 37 deletions test/models/events/auto_publish_composite_view_test.rb

This file was deleted.