diff --git a/app/models/manageiq/providers/ansible_tower/configuration_manager/event_catcher.rb b/app/models/manageiq/providers/ansible_tower/configuration_manager/event_catcher.rb new file mode 100644 index 00000000000..47056293105 --- /dev/null +++ b/app/models/manageiq/providers/ansible_tower/configuration_manager/event_catcher.rb @@ -0,0 +1,3 @@ +class ManageIQ::Providers::AnsibleTower::ConfigurationManager::EventCatcher < ManageIQ::Providers::BaseManager::EventCatcher + require_nested :Runner +end diff --git a/app/models/manageiq/providers/ansible_tower/configuration_manager/event_catcher/runner.rb b/app/models/manageiq/providers/ansible_tower/configuration_manager/event_catcher/runner.rb new file mode 100644 index 00000000000..d75e3465306 --- /dev/null +++ b/app/models/manageiq/providers/ansible_tower/configuration_manager/event_catcher/runner.rb @@ -0,0 +1,30 @@ +class ManageIQ::Providers::AnsibleTower::ConfigurationManager::EventCatcher::Runner < ManageIQ::Providers::BaseManager::EventCatcher::Runner + def stop_event_monitor + event_monitor_handle.stop + end + + def monitor_events + event_monitor_handle.start + event_monitor_handle.poll do |event| + _log.debug { "#{log_prefix} Received event #{event.id}" } + event_monitor_running + @queue.enq event + end + ensure + stop_event_monitor + end + + def queue_event(event) + _log.info "#{log_prefix} Caught event [#{event.id}]" + event_hash = ManageIQ::Providers::AnsibleTower::ConfigurationManager::EventParser.event_to_hash(event, @cfg[:ems_id]) + EmsEvent.add_queue('add', @cfg[:ems_id], event_hash) + end + + private + + def event_monitor_handle + @event_monitor_handle ||= begin + ManageIQ::Providers::AnsibleTower::ConfigurationManager::EventCatcher::Stream.new(@ems) + end + end +end diff --git a/app/models/manageiq/providers/ansible_tower/configuration_manager/event_catcher/stream.rb b/app/models/manageiq/providers/ansible_tower/configuration_manager/event_catcher/stream.rb new file mode 100644 index 00000000000..8787aef69e6 --- /dev/null +++ b/app/models/manageiq/providers/ansible_tower/configuration_manager/event_catcher/stream.rb @@ -0,0 +1,46 @@ +class ManageIQ::Providers::AnsibleTower::ConfigurationManager::EventCatcher::Stream + class ProviderUnreachable < ManageIQ::Providers::BaseManager::EventCatcher::Runner::TemporaryFailure + end + + def initialize(ems) + @ems = ems + @last_activity = nil + @stop_polling = false + end + + def start + @stop_polling = false + end + + def stop + @stop_polling = true + end + + def poll + @ems.with_provider_connection do |ansible| + catch(:stop_polling) do + begin + loop do + ansible.api.activity_stream.all(filter).each do |activity| + throw :stop_polling if @stop_polling + yield activity + @last_activity = activity + end + sleep(20) + end + rescue => exception + raise ProviderUnreachable, exception.message + end + end + end + end + + private + + def filter + { + :order_by => 'timestamp', + :timestamp__gt => @last_activity ? @last_activity.timestamp : 1.minute.ago.to_s(:db) + } + end +end diff --git a/app/models/manageiq/providers/ansible_tower/configuration_manager/event_parser.rb b/app/models/manageiq/providers/ansible_tower/configuration_manager/event_parser.rb new file mode 100644 index 00000000000..4c355e42fb2 --- /dev/null +++ b/app/models/manageiq/providers/ansible_tower/configuration_manager/event_parser.rb @@ -0,0 +1,12 @@ +module ManageIQ::Providers::AnsibleTower::ConfigurationManager::EventParser + def self.event_to_hash(event, ems_id) + { + :event_type => event.operation, + :source => "ANSIBLE_TOWER", + :message => event.changes.to_s, + :timestamp => event.timestamp, + :full_data => event.to_h, + :ems_id => ems_id + } + end +end diff --git a/app/models/miq_server/worker_management/monitor/class_names.rb b/app/models/miq_server/worker_management/monitor/class_names.rb index 5faa9fc67a9..7e6e4a3a8dd 100644 --- a/app/models/miq_server/worker_management/monitor/class_names.rb +++ b/app/models/miq_server/worker_management/monitor/class_names.rb @@ -41,6 +41,7 @@ module MiqServer::WorkerManagement::Monitor::ClassNames ManageIQ::Providers::Vmware::InfraManager::RefreshWorker ManageIQ::Providers::Nuage::NetworkManager::RefreshWorker ManageIQ::Providers::Amazon::CloudManager::EventCatcher + ManageIQ::Providers::AnsibleTower::ConfigurationManager::EventCatcher ManageIQ::Providers::Azure::CloudManager::EventCatcher ManageIQ::Providers::Hawkular::MiddlewareManager::EventCatcher ManageIQ::Providers::Google::CloudManager::EventCatcher @@ -130,6 +131,7 @@ module MiqServer::WorkerManagement::Monitor::ClassNames ManageIQ::Providers::StorageManager::CinderManager::EventCatcher ManageIQ::Providers::Amazon::CloudManager::EventCatcher ManageIQ::Providers::Azure::CloudManager::EventCatcher + ManageIQ::Providers::AnsibleTower::ConfigurationManager::EventCatcher ManageIQ::Providers::Hawkular::MiddlewareManager::EventCatcher ManageIQ::Providers::Google::CloudManager::EventCatcher ManageIQ::Providers::Kubernetes::ContainerManager::EventCatcher