diff --git a/app/models/manageiq/providers/openstack/infra_manager/host.rb b/app/models/manageiq/providers/openstack/infra_manager/host.rb index 666108662..52e033024 100644 --- a/app/models/manageiq/providers/openstack/infra_manager/host.rb +++ b/app/models/manageiq/providers/openstack/infra_manager/host.rb @@ -1,8 +1,6 @@ require 'manageiq/providers/openstack/legacy/openstack_configuration_parser' class ManageIQ::Providers::Openstack::InfraManager::Host < ::Host - include HostOperationsMixin - belongs_to :availability_zone has_many :host_service_group_openstacks, :foreign_key => :host_id, :dependent => :destroy, @@ -208,216 +206,112 @@ def disconnect_ems(e = nil) end def manageable_queue(userid = "system", _options = {}) - log_target = "#{self.class.name} name: [#{name}], id: [#{id}]" - - task = MiqTask.create(:name => "Setting node '#{name}' to manageable", :userid => userid) - - _log.info("Requesting manageable of #{log_target}") - begin - MiqEvent.raise_evm_job_event(self, :type => "manageable", :prefix => "request") - rescue => err - _log.warn("Error raising request manageable for #{log_target}: #{err.message}") - return - end - - _log.info("Queuing provide of #{log_target}") - cb = {:class_name => task.class.name, :instance_id => task.id, :method_name => :queue_callback_on_exceptions, :args => ['Finished']} - MiqQueue.put( - :class_name => self.class.name, - :instance_id => id, - :args => [task.id], - :method_name => "manageable", - :miq_callback => cb, - :msg_timeout => ::Settings.host_manageable.queue_timeout.to_i_with_method, - :zone => my_zone - ) - end - - def manageable(taskid = nil) - unless taskid.nil? - task = MiqTask.find_by(:id => taskid) - task.state_active if task - end - - log_target = "#{self.class.name} name: [#{name}], id: [#{id}]" - - _log.info("Setting to manageable #{log_target}...") - - task.update_status("Active", "Ok", "Setting to manageable") if task - - status = "Fail" - task_status = "Ok" - _dummy, t = Benchmark.realtime_block(:total_time) do - begin - connection = ext_management_system.openstack_handle.detect_baremetal_service - response = connection.set_node_provision_state(name, "manage") - - if response.status == 202 - status = "Success" - EmsRefresh.queue_refresh(ext_management_system) - end - rescue => err - task_status = "Error" - status = err - end - - begin - MiqEvent.raise_evm_job_event(self, :type => "manageable", :suffix => "complete") - rescue => err - _log.warn("Error raising complete manageable event for #{log_target}: #{err.message}") - end + task_opts = { + :action => "Setting node to manageable", + :userid => userid + } + queue_opts = { + :class_name => self.class.name, + :instance_id => id, + :method_name => "manageable", + :priority => MiqQueue::HIGH_PRIORITY, + :role => 'ems_operations', + :zone => my_zone, + :msg_timeout => ::Settings.host_manageable.queue_timeout.to_i_with_method, + :args => [] + } + MiqTask.generic_action_with_callback(task_opts, queue_opts) + end + + def manageable + connection = ext_management_system.openstack_handle.detect_baremetal_service + response = connection.set_node_provision_state(name, "manage") + + if response.status == 202 + EmsRefresh.queue_refresh(ext_management_system) end - - task.update_status("Finished", task_status, "Setting to Manageable Complete with #{status}") if task - _log.info("Setting to Manageable #{log_target}...Complete - Timings: #{t.inspect}") + rescue => e + _log.error "host=[#{name}], error: #{e}" + raise MiqException::MiqOpenstackInfraHostSetManageableError, e.to_s, e.backtrace end def introspect_queue(userid = "system", _options = {}) - log_target = "#{self.class.name} name: [#{name}], id: [#{id}]" - - task = MiqTask.create(:name => "Hardware Introspection for '#{name}' ", :userid => userid) - - _log.info("Requesting Hardware Introspection of #{log_target}") - begin - MiqEvent.raise_evm_job_event(self, :type => "introspect", :prefix => "request") - rescue => err - _log.warn("Error raising request introspection for #{log_target}: #{err.message}") - return - end - - _log.info("Queuing introspection of #{log_target}") - cb = {:class_name => task.class.name, :instance_id => task.id, :method_name => :queue_callback_on_exceptions, :args => ['Finished']} - MiqQueue.put( - :class_name => self.class.name, - :instance_id => id, - :args => [task.id], - :method_name => "introspect", - :miq_callback => cb, - :msg_timeout => ::Settings.host_introspect.queue_timeout.to_i_with_method, - :zone => my_zone - ) - end - - def introspect(taskid = nil) - unless taskid.nil? - task = MiqTask.find_by(:id => taskid) - task.state_active if task + task_opts = { + :action => "Introspect node", + :userid => userid + } + queue_opts = { + :class_name => self.class.name, + :instance_id => id, + :method_name => "introspect", + :priority => MiqQueue::HIGH_PRIORITY, + :role => 'ems_operations', + :zone => my_zone, + :msg_timeout => ::Settings.host_introspect.queue_timeout.to_i_with_method, + :args => [] + } + MiqTask.generic_action_with_callback(task_opts, queue_opts) + end + + def introspect + connection = ext_management_system.openstack_handle.detect_workflow_service + workflow = "tripleo.baremetal.v1.introspect" + input = { :node_uuids => [name] } + response = connection.create_execution(workflow, input) + workflow_state = response.body["state"] + workflow_execution_id = response.body["id"] + + while workflow_state == "RUNNING" + sleep 5 + response = connection.get_execution(workflow_execution_id) + workflow_state = response.body["state"] end - log_target = "#{self.class.name} name: [#{name}], id: [#{id}]" - - _log.info("Introspecting #{log_target}...") - - task.update_status("Active", "Ok", "Introspecting") if task - - workflow_state = "" - task_status = "Ok" - _dummy, t = Benchmark.realtime_block(:total_time) do - begin - connection = ext_management_system.openstack_handle.detect_workflow_service - workflow = "tripleo.baremetal.v1.introspect" - input = { :node_uuids => [name] } - response = connection.create_execution(workflow, input) - workflow_state = response.body["state"] - workflow_execution_id = response.body["id"] - - while workflow_state == "RUNNING" - sleep 5 - response = connection.get_execution(workflow_execution_id) - workflow_state = response.body["state"] - end - rescue => err - task_status = "Error" - workflow_state = err - end - - if workflow_state == "SUCCESS" - EmsRefresh.queue_refresh(ext_management_system) - end - - begin - MiqEvent.raise_evm_job_event(self, :type => "introspect", :suffix => "complete") - rescue => err - _log.warn("Error raising complete introspect event for #{log_target}: #{err.message}") - end + if workflow_state == "SUCCESS" + EmsRefresh.queue_refresh(ext_management_system) end - - task.update_status("Finished", task_status, "Introspecting Complete with #{workflow_state}") if task - _log.info("Introspecting #{log_target}...Complete - Timings: #{t.inspect}") + rescue => e + _log.error "host=[#{name}], error: #{e}" + raise MiqException::MiqOpenstackInfraHostIntrospectError, e.to_s, e.backtrace end def provide_queue(userid = "system", _options = {}) - log_target = "#{self.class.name} name: [#{name}], id: [#{id}]" - - task = MiqTask.create(:name => "Providing node '#{name}' ", :userid => userid) - - _log.info("Requesting Provide of #{log_target}") - begin - MiqEvent.raise_evm_job_event(self, :type => "provide", :prefix => "request") - rescue => err - _log.warn("Error raising request provide for #{log_target}: #{err.message}") - return - end - - _log.info("Queuing provide of #{log_target}") - cb = {:class_name => task.class.name, :instance_id => task.id, :method_name => :queue_callback_on_exceptions, :args => ['Finished']} - MiqQueue.put( - :class_name => self.class.name, - :instance_id => id, - :args => [task.id], - :method_name => "provide", - :miq_callback => cb, - :msg_timeout => ::Settings.host_provide.queue_timeout.to_i_with_method, - :zone => my_zone - ) - end - - def provide(taskid = nil) - unless taskid.nil? - task = MiqTask.find_by(:id => taskid) - task.state_active if task + task_opts = { + :action => "Provide Host (Setting Host to available state)", + :userid => userid + } + queue_opts = { + :class_name => self.class.name, + :instance_id => id, + :method_name => "provide", + :priority => MiqQueue::HIGH_PRIORITY, + :role => 'ems_operations', + :zone => my_zone, + :msg_timeout => ::Settings.host_provide.queue_timeout.to_i_with_method, + :args => [] + } + MiqTask.generic_action_with_callback(task_opts, queue_opts) + end + + def provide + connection = ext_management_system.openstack_handle.detect_workflow_service + workflow = "tripleo.baremetal.v1.provide" + input = { :node_uuids => [name] } + response = connection.create_execution(workflow, input) + workflow_state = response.body["state"] + workflow_execution_id = response.body["id"] + + while workflow_state == "RUNNING" + sleep 5 + response = connection.get_execution(workflow_execution_id) + workflow_state = response.body["state"] end - - log_target = "#{self.class.name} name: [#{name}], id: [#{id}]" - - _log.info("Provide #{log_target}...") - - task.update_status("Active", "Ok", "Provide") if task - - workflow_state = "" - task_status = "Ok" - _dummy, t = Benchmark.realtime_block(:total_time) do - begin - connection = ext_management_system.openstack_handle.detect_workflow_service - workflow = "tripleo.baremetal.v1.provide" - input = { :node_uuids => [name] } - response = connection.create_execution(workflow, input) - workflow_state = response.body["state"] - workflow_execution_id = response.body["id"] - - while workflow_state == "RUNNING" - sleep 5 - response = connection.get_execution(workflow_execution_id) - workflow_state = response.body["state"] - end - rescue => err - task_status = "Error" - workflow_state = err - end - - if workflow_state == "SUCCESS" - EmsRefresh.queue_refresh(ext_management_system) - end - - begin - MiqEvent.raise_evm_job_event(self, :type => "provide", :suffix => "complete") - rescue => err - _log.warn("Error raising complete provide event for #{log_target}: #{err.message}") - end + if workflow_state == "SUCCESS" + EmsRefresh.queue_refresh(ext_management_system) end - - task.update_status("Finished", task_status, "Provide Complete with #{workflow_state}") if task - _log.info("Provide #{log_target}...Complete - Timings: #{t.inspect}") + rescue => e + _log.error "host=[#{name}], error: #{e}" + raise MiqException::MiqOpenstackInfraHostProvideError, e.to_s, e.backtrace end def validate_start @@ -429,7 +323,7 @@ def validate_start end def start(userid = "system") - ironic_set_power_state_queue(userid, "power on", "Starting", "start", :host_start) + ironic_set_power_state_queue(userid, "power on") end def validate_stop @@ -441,7 +335,7 @@ def validate_stop end def stop(userid = "system") - ironic_set_power_state_queue(userid, "power off", "Stopping", "stop", :host_stop) + ironic_set_power_state_queue(userid, "power off") end def validate_destroy @@ -457,65 +351,34 @@ def destroy_queue end def destroy_ironic_queue(userid = "system") - log_target = "#{self.class.name} name: [#{name}], id: [#{id}]" - - task = MiqTask.create(:name => "Deleting Ironic node '#{name}'", :userid => userid) - - _log.info("Requesting Ironic delete of #{log_target}") - begin - MiqEvent.raise_evm_job_event(self, :type => "destroy_ironic", :prefix => "request") - rescue => err - $log.warn("Error raising request delete for #{log_target}: #{err.message}") - return - end - - _log.info("Queuing destroy_ironic of #{log_target}") - cb = {:class_name => task.class.name, - :instance_id => task.id, - :method_name => :queue_callback_on_exceptions, - :args => ['Finished']} - MiqQueue.put( - :class_name => self.class.name, - :instance_id => id, - :args => [task.id], - :method_name => "destroy_ironic", - :miq_callback => cb, - :msg_timeout => ::Settings.host_delete.queue_timeout.to_i_with_method, - :zone => my_zone - ) - end - - def destroy_ironic(taskid = nil) - unless taskid.nil? - task = MiqTask.find_by(:id => taskid) - task.state_active if task - end - - log_target = "#{self.class.name} name: [#{name}], id: [#{id}]" - - _log.info("Deleting Ironic node #{log_target}...") - - task.update_status("Active", "Ok", "Deleting Ironic Node") if task - - status = "Fail" - task_status = "Ok" - _dummy, t = Benchmark.realtime_block(:total_time) do - begin - connection = ext_management_system.openstack_handle.detect_baremetal_service - response = connection.delete_node(name) - - if response.status == 204 - Host.destroy_queue(id) - status = "Success" - end - rescue => err - task_status = "Error" - status = err - end + task_opts = { + :action => "Deleting Ironic node: #{uid_ems} for user #{userid}", + :userid => userid + } + + queue_opts = { + :class_name => self.class.name, + :method_name => "destroy_ironic", + :instance_id => id, + :priority => MiqQueue::HIGH_PRIORITY, + :role => 'ems_operations', + :zone => my_zone, + :msg_timeout => ::Settings.host_delete.queue_timeout.to_i_with_method, + :args => [] + } + MiqTask.generic_action_with_callback(task_opts, queue_opts) + end + + def destroy_ironic + connection = ext_management_system.openstack_handle.detect_baremetal_service + response = connection.delete_node(name) + + if response.status == 204 + Host.destroy_queue(id) end - - task.update_status("Finished", task_status, "Delete Ironic node #{log_target} finished with #{status}") if task - _log.info("Delete Ironic node #{log_target}...Complete - Timings: #{t.inspect}") + rescue => e + _log.error "ironic node=[#{uid_ems}], error: #{e}" + raise MiqException::MiqOpenstackInfraHostDestroyError, e.to_s, e.backtrace end def refresh_network_interfaces(ssu) diff --git a/app/models/manageiq/providers/openstack/infra_manager/host/operations.rb b/app/models/manageiq/providers/openstack/infra_manager/host/operations.rb index 91ac35fc6..ac7cadc74 100644 --- a/app/models/manageiq/providers/openstack/infra_manager/host/operations.rb +++ b/app/models/manageiq/providers/openstack/infra_manager/host/operations.rb @@ -51,4 +51,36 @@ def unset_node_maintenance def external_get_node_maintenance ironic_fog_node.maintenance end + + def ironic_set_power_state_queue(userid = "system", + power_state = "power off") + task_opts = { + :action => "Set Ironic Node Power State", + :user_id => userid + } + + queue_opts = { + :class_name => self.class.name, + :method_name => "ironic_set_power_state", + :instance_id => id, + :priority => MiqQueue::HIGH_PRIORITY, + :role => 'ems_operations', + :args => [power_state], + :zone => my_zone + } + + MiqTask.generic_action_with_callback(task_opts, queue_opts) + end + + def ironic_set_power_state(power_state = "power off") + connection = ext_management_system.openstack_handle.detect_baremetal_service + response = connection.set_node_power_state(uid_ems, power_state) + + if response.status == 202 + EmsRefresh.queue_refresh(ext_management_system) + end + rescue => e + _log.error "node=[#{name}], error: #{e}" + raise MiqException::MiqOpenstackInfraHostSetPowerStateError, e.to_s, e.backtrace + end end diff --git a/app/models/manageiq/providers/openstack/infra_manager/host_operations_mixin.rb b/app/models/manageiq/providers/openstack/infra_manager/host_operations_mixin.rb deleted file mode 100644 index 58d3b8c2e..000000000 --- a/app/models/manageiq/providers/openstack/infra_manager/host_operations_mixin.rb +++ /dev/null @@ -1,79 +0,0 @@ -module ManageIQ::Providers::Openstack::InfraManager::HostOperationsMixin - include Vmdb::Logging - - def ironic_set_power_state_queue(userid = "system", - power_state = "power off", - power_state_text_verb = "Stopping", - power_state_text = "stop", - feature = :host_stop) - log_target = "#{self.class.name} name: [#{name}], id: [#{id}]" - - task = MiqTask.create(:name => "#{power_state_text_verb} Ironic node '#{name}'", :userid => userid) - - _log.info("#{power_state_text_verb} Ironic node #{log_target}") - begin - MiqEvent.raise_evm_job_event(self, :type => power_state_text, :prefix => "request") - rescue => err - _log.warn("Error raising request #{power_state_text} for #{log_target}: #{err.message}") - return - end - - _log.info("Queuing #{power_state_text} for #{log_target}") - timeout = ::Settings[feature].queue_timeout.to_i_with_method - cb = {:class_name => task.class.name, - :instance_id => task.id, - :method_name => :queue_callback_on_exceptions, - :args => ['Finished']} - MiqQueue.put( - :class_name => self.class.name, - :instance_id => id, - :args => [task.id, power_state, power_state_text_verb, power_state_text], - :method_name => "ironic_set_power_state", - :miq_callback => cb, - :msg_timeout => timeout, - :zone => my_zone - ) - end - - def ironic_set_power_state(taskid = nil, - power_state = "power off", - power_state_text_verb = "Stopping", - power_state_text = "stop") - unless taskid.nil? - task = MiqTask.find_by(:id => taskid) - task.state_active if task - end - - log_target = "#{self.class.name} name: [#{name}], id: [#{id}]" - - _log.info("#{power_state_text_verb} #{log_target}...") - - task.update_status("Active", "Ok", power_state_text.capitalize) if task - - status = "Fail" - task_status = "Ok" - _dummy, t = Benchmark.realtime_block(:total_time) do - begin - connection = ext_management_system.openstack_handle.detect_baremetal_service - response = connection.set_node_power_state(uid_ems, power_state) - - if response.status == 202 - status = "Success" - EmsRefresh.queue_refresh(ext_management_system) - end - rescue => err - task_status = "Error" - status = err - end - - begin - MiqEvent.raise_evm_job_event(self, :type => power_state_text, :suffix => "complete") - rescue => err - _log.warn("Error raising complete #{power_state_text} event for #{log_target}: #{err.message}") - end - end - - task.update_status("Finished", task_status, "#{power_state_text.capitalize} Complete with #{status}") if task - _log.info("#{power_state_text_verb} #{log_target}...Complete - Timings: #{t.inspect}") - end -end diff --git a/spec/models/manageiq/providers/openstack/infra_manager/host_spec.rb b/spec/models/manageiq/providers/openstack/infra_manager/host_spec.rb index b6518531a..8a0ab4fc0 100644 --- a/spec/models/manageiq/providers/openstack/infra_manager/host_spec.rb +++ b/spec/models/manageiq/providers/openstack/infra_manager/host_spec.rb @@ -511,21 +511,19 @@ allow(ext_management_system).to receive(:openstack_handle).and_return(openstack_handle) expect(MiqQueue.where(:method_name => "refresh").count).to eq(0) - task = FactoryGirl.create(:miq_task) - host.introspect(task.id) + host.introspect expect(MiqQueue.where(:method_name => "refresh").count).to eq(1) MiqQueue.all.map(&:delete) expect(MiqQueue.where(:method_name => "refresh").count).to eq(0) - task = FactoryGirl.create(:miq_task) - host.provide(task.id) + + host.provide expect(MiqQueue.where(:method_name => "refresh").count).to eq(1) MiqQueue.all.map(&:delete) expect(MiqQueue.where(:method_name => "refresh").count).to eq(0) - task = FactoryGirl.create(:miq_task) - host.manageable(task.id) - expect(task.status).to eq("Ok") + + host.manageable expect(MiqQueue.where(:method_name => "refresh").count).to eq(1) end