Skip to content

Commit

Permalink
Merge pull request #18191 from jameswnl/v2vhost
Browse files Browse the repository at this point in the history
Use MiqQueue for enabling/disabling a conversion_host

(cherry picked from commit d4560c6)

https://bugzilla.redhat.com/show_bug.cgi?id=1628489
  • Loading branch information
agrare authored and simaishi committed Nov 26, 2018
1 parent 48e0ca7 commit 918f537
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/models/conversion_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class ConversionHost < ApplicationRecord
has_many :active_tasks, -> { where(:state => 'active') }, :class_name => ServiceTemplateTransformationPlanTask, :inverse_of => :conversion_host
delegate :ext_management_system, :hostname, :ems_ref, :to => :resource, :allow_nil => true

include_concern 'Configurations'

# To be eligible, a conversion host must have the following properties
# - A transport mechanism is configured for source (set by 3rd party)
# - Credentials are set on the resource and SSH connection works
Expand Down Expand Up @@ -75,12 +77,13 @@ def check_conversion_host_role
tag_resource_as('disabled')
end

def enable_conversion_host_role
def enable_conversion_host_role(v2v_vddk_package_url = nil)
install_conversion_host_module
v2v_vddk_package_url ||= "http://#{resource.ext_management_system.hostname}/vddk/VMware-vix-disklib-stable.tar.gz"
playbook = "/usr/share/ovirt-ansible-v2v-conversion-host/playbooks/conversion_host_enable.yml"
extra_vars = {
:v2v_vddk_package_name => "VMware-vix-disklib-stable.tar.gz",
:v2v_vddk_package_url => "http://#{resource.ext_management_system.hostname}/vddk/VMware-vix-disklib-stable.tar.gz"
:v2v_vddk_package_url => v2v_vddk_package_url
}
ansible_playbook(playbook, extra_vars)
ensure
Expand Down
75 changes: 75 additions & 0 deletions app/models/conversion_host/configurations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module ConversionHost::Configurations
extend ActiveSupport::Concern
module ClassMethods
def notify_configuration_result(op, success, resource_info)
Notification.create(
:type => success ? :conversion_host_config_success : :conversion_host_config_failure,
:options => {
:op_name => op,
:op_arg => resource_info,
}
)
end

def queue_configuration(op, instance_id, resource, params, auth_user = nil)
task_opts = {
:action => "Configuring a conversion_host: operation=#{op} resource=(type: #{resource.class.name} id:#{resource.id})",
:userid => auth_user
}
queue_opts = {
:class_name => name,
:method_name => op,
:instance_id => instance_id,
:role => 'ems_operations',
:zone => resource.ext_management_system.my_zone,
:args => params
}
MiqTask.generic_action_with_callback(task_opts, queue_opts)
end

def enable_queue(params, auth_user = nil)
resource_type = params[:resource_type]
resource_id = params[:resource_id]
resource = resource_type.constantize.find(resource_id)

queue_configuration('enable', nil, resource, [params], auth_user)
end

def enable(params)
_log.info("Enabling a conversion_host with parameters: #{params}")
params.delete(:task_id) # in case this is being called through *_queue which will stick in a :task_id
params.delete(:miq_task_id) # miq_queue.activate_miq_task will stick in a :miq_task_id

vddk_url = params.delete("param_v2v_vddk_package_url")
resource_id = params[:resource_id]
resource_type = params[:resource_type]
resource = resource_type.constantize.find(resource_id)

conversion_host = new(params.merge(:resource => resource))
conversion_host.enable_conversion_host_role(vddk_url)
conversion_host.save!
conversion_host
rescue StandardError => error
raise
ensure
resource_info = "type=#{params[:resource_type]} id=#{params[:resource_id]}"
notify_configuration_result('enable', error.nil?, resource_info)
end
end

def disable_queue(auth_user = nil)
self.class.queue_configuration('disable', id, resource, [], auth_user)
end

def disable
resource_info = "type=#{resource.class.name} id=#{resource.id}"
_log.info("Disabling a conversion_host #{resource_info}")

disable_conversion_host_role
destroy!
rescue StandardError => error
raise
ensure
self.class.notify_configuration_result('disable', error.nil?, resource_info)
end
end
10 changes: 10 additions & 0 deletions db/fixtures/notification_types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@
:expires_in: 24.hours
:level: :error
:audience: global
- :name: conversion_host_config_success
:message: 'Conversion Host %{op_name} of resource %{resource} completed successfully.'
:expires_in: 24.hours
:level: :success
:audience: global
- :name: conversion_host_config_failure
:message: 'Conversion Host %{op_name} of resource %{resource} failed. Please check the logs for further details.'
:expires_in: 24.hours
:level: :error
:audience: global
- :name: cloud_volume_attach_success
:message: 'Attaching Volume %{subject} to Instance %{instance_name} completed successfully.'
:expires_in: 24.hours
Expand Down
5 changes: 5 additions & 0 deletions spec/factories/conversion_host.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryGirl.define do
factory :conversion_host do
sequence(:name) { |n| "conversion_host_#{seq_padded_for_sorting(n)}" }
end
end
111 changes: 111 additions & 0 deletions spec/models/conversion_host/configurations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
describe ConversionHost do
let(:conversion_host) { FactoryGirl.create(:conversion_host, :resource => vm) }
let(:params) do
{
:name => 'transformer',
:resource_type => vm.class.name,
:resource_id => vm.id
}
end

context "processing configuration requests" do
let(:vm) { FactoryGirl.create(:vm) }
before(:each) do
allow(ConversionHost).to receive(:new).and_return(conversion_host)
end
context ".enable" do
let(:expected_notify) do
{
:type => :conversion_host_config_success,
:options => {
:op_name => "enable",
:op_arg => "type=#{params[:resource_type]} id=#{params[:resource_id]}"
}
}
end
it "to succeed and send notification" do
allow(conversion_host).to receive(:enable_conversion_host_role)
expect(Notification).to receive(:create).with(expected_notify)
expect(described_class.enable(params)).to be_a(described_class)
end

it "to fail and send notification" do
expected_notify[:type] = :conversion_host_config_failure
allow(conversion_host).to receive(:enable_conversion_host_role).and_raise
expect(Notification).to receive(:create).with(expected_notify)
expect { described_class.enable(params) }.to raise_error(StandardError)
end
end

context "#disable" do
let(:expected_notify) do
{
:type => :conversion_host_config_success,
:options => {
:op_name => "disable",
:op_arg => "type=#{vm.class.name} id=#{vm.id}"
}
}
end

it "to succeed and send notification" do
allow(conversion_host).to receive(:disable_conversion_host_role)
expect(Notification).to receive(:create).with(expected_notify)
conversion_host.disable
end

it "to fail and send notification" do
expected_notify[:type] = :conversion_host_config_failure
allow(conversion_host).to receive(:disable_conversion_host_role).and_raise
expect(Notification).to receive(:create).with(expected_notify)
expect { conversion_host.disable }.to raise_error(StandardError)
end
end
end

context "queuing configuration requests" do
let(:ext_management_system) { FactoryGirl.create(:ext_management_system) }
let(:vm) { FactoryGirl.create(:vm, :ext_management_system => ext_management_system) }
let(:expected_task_action) { "Configuring a conversion_host: operation=#{op} resource=(type: #{vm.class.name} id:#{vm.id})" }

context ".enable_queue" do
let(:op) { 'enable' }

it "to queue with a task" do
task_id = described_class.enable_queue(params)
expect(MiqTask.find(task_id)).to have_attributes(:name => expected_task_action)
expect(MiqQueue.first).to have_attributes(
:args => [params],
:class_name => described_class.name,
:method_name => "enable",
:priority => MiqQueue::NORMAL_PRIORITY,
:role => "ems_operations",
:zone => vm.ext_management_system.my_zone
)
end

it "to fail when the resource is not found" do
vm.destroy!
expect { described_class.enable_queue(params) }.to raise_error(ActiveRecord::RecordNotFound)
end
end

context "#disable_queue" do
let(:op) { 'disable' }

it "to queue with a task" do
task_id = conversion_host.disable_queue
expect(MiqTask.find(task_id)).to have_attributes(:name => expected_task_action)
expect(MiqQueue.first).to have_attributes(
:args => [],
:class_name => described_class.name,
:instance_id => conversion_host.id,
:method_name => "disable",
:priority => MiqQueue::NORMAL_PRIORITY,
:role => "ems_operations",
:zone => conversion_host.resource.ext_management_system.my_zone
)
end
end
end
end

0 comments on commit 918f537

Please sign in to comment.