Skip to content

Commit

Permalink
Merge pull request #441 from fdupont-redhat/v2v_move_code_to_backend_1
Browse files Browse the repository at this point in the history
Refactor to delegate task and conversion host code to backend
  • Loading branch information
tinaafitz authored Oct 19, 2018
2 parents 5ea858a + b00873a commit 8e7ed4d
Show file tree
Hide file tree
Showing 58 changed files with 1,032 additions and 1,717 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(handle = $evm)
end

def main
if @task.get_option(:transformation_host_id).nil?
if @task.conversion_host.nil?
@handle.root['ae_result'] = 'retry'
@handle.root['ae_retry_server_affinity'] = true
@handle.root['ae_retry_interval'] = 15.seconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,13 @@ module Automate
module Transformation
module Common
class AssessTransformation
SUPPORTED_SOURCE_EMS_TYPES = ['vmwarews'].freeze
SUPPORTED_DESTINATION_EMS_TYPES = ['rhevm'].freeze

def initialize(handle = $evm)
@handle = handle
@task = ManageIQ::Automate::Transformation::Common::Utils.task(@handle)
@source_vm = ManageIQ::Automate::Transformation::Common::Utils.source_vm(@handle)
end

def virtv2v_networks
@source_vm.hardware.nics.select { |n| n.device_type == 'ethernet' }.collect do |nic|
source_network = nic.lan
destination_network = @task.transformation_destination(source_network)
raise "[#{@source_vm.name}] NIC #{nic.device_name} [#{source_network.name}] has no mapping. Aborting." if destination_network.nil?
{
:source => source_network.name,
:destination => destination_network.name,
:mac_address => nic.address
}
end
end

def virtv2v_disks
@source_vm.hardware.disks.select { |d| d.device_type == 'disk' }.collect do |disk|
source_storage = disk.storage
destination_storage = @task.transformation_destination(disk.storage)
raise "[#{@source_vm.name}] Disk #{disk.device_name} [#{source_storage.name}] has no mapping. Aborting." if destination_storage.nil?
{
:path => disk.filename,
:size => disk.size,
:percent => 0,
:weight => disk.size.to_f / @source_vm.allocated_disk_storage.to_f * 100
}
end
end

def source_cluster
@source_cluster ||= @source_vm.ems_cluster.tap do |cluster|
raise "No source cluster" if cluster.nil?
end
end

def source_ems
@source_ems ||= @source_vm.ext_management_system.tap do |ems|
raise "No source EMS" if ems.nil?
end
end

def destination_cluster
@destination_cluster ||= @task.transformation_destination(source_cluster).tap do |cluster|
raise "No destination cluster" if cluster.nil?
end
end

def destination_ems
@destination_ems ||= destination_cluster.ext_management_system.tap do |ems|
raise "No destination EMS" if ems.nil?
end
end

def transformation_type
raise "Unsupported source EMS type: #{source_ems.emstype}." unless SUPPORTED_SOURCE_EMS_TYPES.include?(source_ems.emstype)
raise "Unsupported destination EMS type: #{destination_ems.emstype}." unless SUPPORTED_DESTINATION_EMS_TYPES.include?(destination_ems.emstype)
@handle.set_state_var(:source_ems_type, source_ems.emstype)
@handle.set_state_var(:destination_ems_type, destination_ems.emstype)
"#{source_ems.emstype}2#{destination_ems.emstype}"
end

def populate_task_options
@task.set_option(:source_ems_id, source_ems.id)
@task.set_option(:destination_ems_id, destination_ems.id)
@task.set_option(:virtv2v_networks, virtv2v_networks)
@task.set_option(:virtv2v_disks, virtv2v_disks)
@task.set_option(:transformation_type, transformation_type)
@task.set_option(:source_vm_power_state, @source_vm.power_state)
@task.set_option(:collapse_snapshots, true)
@task.set_option(:power_off, true)
Expand All @@ -91,6 +24,7 @@ def populate_factory_config
end

def main
@task.set_option('cancel_requested', true) unless @task.preflight_check
%w(task_options factory_config).each { |ci| send("populate_#{ci}") }
rescue => e
@handle.set_state_var(:ae_state_progress, 'message' => e.message)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module ManageIQ
module Automate
module Transformation
module Common
class KillVirtV2V
def initialize(handle = $evm)
@handle = handle
@task = ManageIQ::Automate::Transformation::Common::Utils.task(@handle)
end

def task_virtv2v_state
return if @task.get_option(:virtv2v_started_on).blank? || @task.get_option(:virtv2v_finished_on).present? || @task.get_option(:virtv2v_wrapper).blank?
@task.get_conversion_state
end

def kill_signal
if @handle.get_state_var('virtv2v_graceful_kill')
'KILL'
else
@handle.set_state_var('virtv2v_graceful_kill', true)
@handle.root['ae_result'] = 'retry'
@handle.root['ae_retry_interval'] = '30.seconds'
'TERM'
end
end

def main
@task.kill_virtv2v(kill_signal) unless task_virtv2v_state.nil?
rescue => e
@handle.set_state_var(:ae_state_progress, 'message' => e.message)
raise
end
end
end
end
end
end

ManageIQ::Automate::Transformation::Common::KillVirtV2V.new.main
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ object_type: method
version: 1.0
object:
attributes:
name: Utils
name: KillVirtV2V
display_name:
description:
scope: instance
language: ruby
location: inline
embedded_methods:
- "/Transformation/Common/Utils"
options: {}
inputs: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module ManageIQ
module Automate
module Transformation
module Common
class SetMigrated
def initialize(handle = $evm)
@handle = handle
@task = ManageIQ::Automate::Transformation::Common::Utils.task(@handle)
end

def main
@task.mark_vm_migrated
rescue => e
@handle.set_state_var(:ae_state_progress, 'message' => e.message)
raise
end
end
end
end
end
end

ManageIQ::Automate::Transformation::Common::SetMigrated.new.main
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ object:
language: ruby
location: inline
embedded_methods:
- "/Transformation/Infrastructure/VM/vmwarews/Utils"
- "/Transformation/Common/Utils"
options: {}
inputs: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module ManageIQ
module Automate
module Transformation
module Common
class VMCheckTransformed
def initialize(handle = $evm)
@handle = handle
@task = ManageIQ::Automate::Transformation::Common::Utils.task(@handle)
end

def set_retry
@handle.root['ae_result'] = 'retry'
@handle.root['ae_retry_server_affinity'] = true
@handle.log(:info, "Disk transformation is not finished. Checking in #{@handle.root['ae_retry_interval']}")
end

def update_total_percentage
virtv2v_disks = @task[:options][:virtv2v_disks]
converted_disks = virtv2v_disks.reject { |d| d[:percent].zero? }
message, percent = nil, nil
if converted_disks.empty?
percent = 1
message = 'Disks transformation is initializing.'
else
percent = 0
converted_disks.each { |disk| percent += (disk[:percent].to_f * disk[:weight].to_f / 100.0) }
message = "Converting disk #{converted_disks.length} / #{virtv2v_disks.length} [#{percent.round(2)}%]."
end
[message, percent]
end

def main
@task.get_conversion_state

case @task.get_option(:virtv2v_status)
when 'active'
message, percent = update_total_percentage
@handle.set_state_var(:ae_state_progress, 'message' => message, 'percent' => percent.round(2))
set_retry
when 'failed'
@handle.set_state_var(:ae_state_progress, 'message' => 'Disks transformation failed.')
raise "Disks transformation failed."
when 'succeeded'
@handle.set_state_var(:ae_state_progress, 'message' => 'Disks transformation succeeded.', 'percent' => 100)
end
rescue => e
@handle.set_state_var(:ae_state_progress, 'message' => e.message)
raise
end
end
end
end
end
end

ManageIQ::Automate::Transformation::Common::VMCheckTransformed.new.main
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ object:
language: ruby
location: inline
embedded_methods:
- "/Transformation/TransformationHosts/Common/Utils"
- "/Transformation/TransformationHosts/ovirt_host/Utils"
- "/Transformation/Common/Utils"
options: {}
inputs: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module ManageIQ
module Automate
module Transformation
module Common
class VMTransform
def initialize(handle = $evm)
@debug = false
@handle = handle
@task = ManageIQ::Automate::Transformation::Common::Utils.task(@handle)
end

def main
# WARNING: Enable at your own risk, as it may lead to sensitive data leak
# @handle.log(:info, "JSON Input:\n#{JSON.pretty_generate(@task.conversion_options)}") if @debug

@task.run_conversion
rescue => e
@handle.set_state_var(:ae_state_progress, 'message' => e.message)
raise
end
end
end
end
end
end

ManageIQ::Automate::Transformation::Common::VMTransform.new.main
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ object_type: method
version: 1.0
object:
attributes:
name: Utils
name: VMTransform
display_name:
description:
scope: instance
language: ruby
location: inline
embedded_methods:
- "/Transformation/Common/Utils"
options: {}
inputs: []
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ module Common
class CheckPoweredOn
def initialize(handle = $evm)
@handle = handle
@task = ManageIQ::Automate::Transformation::Common::Utils.task(@handle)
transformation_phase = ManageIQ::Automate::Transformation::Common::Utils.transformation_phase(@handle)
case transformation_phase
when 'transformation'
@vm = ManageIQ::Automate::Transformation::Common::Utils.destination_vm(@handle)
when 'cleanup'
@vm = ManageIQ::Automate::Transformation::Common::Utils.source_vm(@handle)
end
end

def main
if @handle.root['service_template_transformation_plan_task'].blank?
task = @handle.vmdb(:service_template_transformation_plan_task).find_by(:id => @handle.root['service_template_transformation_plan_task_id'])
vm = task.source if task.present?
else
task = @handle.root['service_template_transformation_plan_task']
vm = @handle.vmdb(:vm).find_by(:id => task.get_option(:destination_vm_id)) if task.present?
end
return if vm.blank?
@handle.log(:info, "Target VM: #{vm.name} [#{vm.vendor}]")
return if task.get_option(:source_vm_power_state) != 'on'
if vm.power_state != 'on'
return if @vm.blank?
return if @task.get_option(:source_vm_power_state) != 'on'
if @vm.power_state != 'on'
@handle.root["ae_result"] = "retry"
@handle.root["ae_retry_interval"] = "15.seconds"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ object:
scope: instance
language: ruby
location: inline
embedded_methods:
- "/Transformation/Common/Utils"
options: {}
inputs: []
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,17 @@ module Common
class CheckVmInInventory
def initialize(handle = $evm)
@handle = handle
end

def log_and_raise(message)
@handle.log(:error, message)
raise "ERROR - #{message}"
end

def task
@task ||= @handle.root["service_template_transformation_plan_task"].tap do |task|
log_and_raise('A service_template_transformation_plan_task is needed for this method to continue') if task.nil?
end
end

def source_vm
@source_vm ||= task.source.tap do |vm|
log_and_raise('Source VM has not been defined in the task') if vm.nil?
end
end

def destination_vm
destination_ems = task.transformation_destination(source_vm.ems_cluster).ext_management_system
@destination_vm ||= @handle.vmdb(:vm).find_by(:name => source_vm.name, :ems_id => destination_ems.id)
end

def set_retry(message = nil, interval = '1.minutes')
@handle.log(:info, message) if message.present?
@handle.root['ae_result'] = 'retry'
@handle.root['ae_retry_interval'] = interval
@task = ManageIQ::Automate::Transformation::Common::Utils.task(@handle)
@source_vm = ManageIQ::Automate::Transformation::Common::Utils.source_vm(@handle)
end

def main
if destination_vm.blank?
set_retry('VM is not yet in the destination provider inventory', '15.seconds')
destination_vm = @handle.vmdb(:vm).find_by(:name => @source_vm.name, :ems_id => @task.destination_ems.id)
if destination_vm.nil?
@handle.root['ae_result'] = 'retry'
@handle.root['ae_retry_interval'] = '15.seconds'
else
task.set_option(:destination_vm_id, destination_vm.id)
@task.set_option(:destination_vm_id, destination_vm.id)
end
rescue => e
@handle.set_state_var(:ae_state_progress, 'message' => e.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ object:
scope: instance
language: ruby
location: inline
embedded_methods:
- "/Transformation/Common/Utils"
options: {}
inputs: []
Loading

0 comments on commit 8e7ed4d

Please sign in to comment.