Skip to content

Commit

Permalink
Merge pull request #355 from fdupont-redhat/v2v_pre_post_migration_pl…
Browse files Browse the repository at this point in the history
…aybook_support

Add support for pre/post-migration playbook
(cherry picked from commit f5fdc82)

https://bugzilla.redhat.com/show_bug.cgi?id=1608420
  • Loading branch information
gmcculloug authored and simaishi committed Jul 25, 2018
1 parent b364c7f commit fb6f12c
Show file tree
Hide file tree
Showing 11 changed files with 387 additions and 9 deletions.
@@ -0,0 +1,33 @@
---
object_type: class
version: 1.0
object:
attributes:
description:
display_name:
name: Ansible
type:
inherits:
visibility:
owner:
schema:
- field:
aetype: method
name: execute
display_name:
datatype:
priority: 1
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
@@ -0,0 +1,51 @@
module ManageIQ
module Automate
module Transformation
module Ansible
class CheckPlaybookAsAService
def initialize(handle = $evm)
@handle = handle
end

def set_retry(message, interval = '1.minutes')
end

def main
transformation_hook = @handle.inputs['transformation_hook']
task = @handle.root['service_template_transformation_plan_task']
service_request_id = task.get_option("#{transformation_hook}_ansible_playbook_service_request_id".to_sym)

if service_request_id.present?
service_request = @handle.vmdb(:miq_request).find_by(:id => service_request_id)

playbooks_status = task.get_option(:playbooks) || {}
playbooks_status[transformation_hook] = { :job_state => service_request.request_state }

if service_request.request_state == 'finished'
@handle.log(:info, "Ansible playbook service request (id: #{service_request_id}) is finished.")
playbooks_status[transformation_hook][:job_status] = service_request.status
playbooks_status[transformation_hook][:job_id] = service_request.miq_request_tasks.first.destination.service_resources.first.resource.id
task.set_option(:playbooks, playbooks_status)
if service_request.status == 'Error' && transformation_hook == 'pre'
raise "Ansible playbook has failed (hook=#{transformation_hook})"
end
else
@handle.log(:info, "Playbook for #{transformation_hook} migration is not finished. Retrying.")
@handle.root['ae_result'] = 'retry'
@handle.root['ae_retry_interval'] = '15.seconds'
end
task.set_option(:playbooks, playbooks_status)
end
rescue => e
@handle.set_state_var(:ae_state_progress, 'message' => e.message)
raise
end
end
end
end
end
end

if $PROGRAM_NAME == __FILE__
ManageIQ::Automate::Transformation::Ansible::CheckPlaybookAsAService.new.main
end
@@ -0,0 +1,33 @@
---
object_type: method
version: 1.0
object:
attributes:
name: CheckPlaybookAsAService
display_name:
description:
scope: instance
language: ruby
location: inline
options: {}
inputs:
- field:
aetype:
name: transformation_hook
display_name:
datatype:
priority: 1
owner:
default_value: _
substitute: false
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
@@ -0,0 +1,45 @@
module ManageIQ
module Automate
module Transformation
module Ansible
class LaunchPlaybookAsAService
def initialize(handle = $evm)
@handle = handle
end

def target_host(task, transformation_hook)
target_host = nil
case transformation_hook
when 'pre'
target_host = task.source
when 'post'
target_host = @handle.vmdb(:vm).find_by(:id => task.get_option(:destination_vm_id))
end
target_host
end

def main
task = @handle.root['service_template_transformation_plan_task']
transformation_hook = @handle.inputs['transformation_hook']

return if transformation_hook == '_'
service_template = task.send("#{transformation_hook}_ansible_playbook_service_template")
return if service_template.nil?
target_host = target_host(task, transformation_hook)
return if target_host.nil? || target_host.power_state != 'on'
service_dialog_options = { :hosts => target_host.ipaddresses.first }
service_request = @handle.execute(:create_service_provision_request, service_template, service_dialog_options)
task.set_option("#{transformation_hook}_ansible_playbook_service_request_id".to_sym, service_request.id)
rescue => e
@handle.set_state_var(:ae_state_progress, 'message' => e.message)
raise
end
end
end
end
end
end

if $PROGRAM_NAME == __FILE__
ManageIQ::Automate::Transformation::Ansible::LaunchPlaybookAsAService.new.main
end
@@ -0,0 +1,33 @@
---
object_type: method
version: 1.0
object:
attributes:
name: LaunchPlaybookAsAService
display_name:
description:
scope: instance
language: ruby
location: inline
options: {}
inputs:
- field:
aetype:
name: transformation_hook
display_name:
datatype:
priority: 1
owner:
default_value: _
substitute: false
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
@@ -0,0 +1,12 @@
---
object_type: instance
version: 1.0
object:
attributes:
display_name:
name: ".missing"
inherits:
description:
fields:
- execute:
value: "${#_missing_instance}"
Expand Up @@ -13,10 +13,7 @@ def main
task = @handle.root['service_template_transformation_plan_task']
if task.get_option(:source_vm_power_state) == 'on'
destination_vm = @handle.vmdb(:vm).find_by(:id => task.get_option(:destination_vm_id))
destination_ems = destination_vm.ext_management_system
destination_vm_sdk = ManageIQ::Automate::Transformation::Infrastructure::VM::RedHat::Utils.new(destination_ems).vm_find_by_name(destination_vm.name)
@handle.log(:info, "Status of VM '#{destination_vm.name}': #{destination_vm_sdk.status}")
unless destination_vm_sdk.status == OvirtSDK4::VmStatus::UP
unless destination_vm.power_state == 'on'
@handle.root["ae_result"] = "retry"
@handle.root["ae_retry_interval"] = "15.seconds"
end
Expand Down
Expand Up @@ -9,7 +9,5 @@ object:
scope: instance
language: ruby
location: inline
embedded_methods:
- "/Transformation/Infrastructure/VM/rhevm/Utils"
options: {}
inputs: []
@@ -0,0 +1,133 @@
---
object_type: class
version: 1.0
object:
attributes:
description:
display_name:
name: Ansible
type:
inherits:
visibility:
owner:
schema:
- field:
aetype: state
name: State1
display_name:
datatype:
priority: 1
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: state
name: State2
display_name:
datatype:
priority: 2
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: state
name: State3
display_name:
datatype:
priority: 3
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: state
name: State4
display_name:
datatype:
priority: 4
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: state
name: State5
display_name:
datatype:
priority: 5
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
- field:
aetype: state
name: State6
display_name:
datatype:
priority: 6
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
@@ -0,0 +1,27 @@
---
object_type: instance
version: 1.0
object:
attributes:
display_name:
name: TransformationPlaybook
inherits:
description:
fields:
- State2:
value: "/Transformation/Ansible/LaunchPlaybookAsAService"
on_entry: /System/CommonMethods/MiqAe.WeightedUpdateStatus(weight => 20, description
=> "Launch ${#transformation_hook} migration playbook", task_message => "Migrating")
on_exit: /System/CommonMethods/MiqAe.WeightedUpdateStatus(weight => 20, description
=> "Launch ${#transformation_hook} migration playbook", task_message => "Migrating")
on_error: /System/CommonMethods/MiqAe.WeightedUpdateStatus(weight => 20, description
=> "Launch ${#transformation_hook} migration playbook", task_message => "Migrating")
- State5:
value: "/Transformation/Ansible/CheckPlaybookAsAService"
on_entry: /System/CommonMethods/MiqAe.WeightedUpdateStatus(weight => 80, description
=> "Check ${#transformation_hook} migration playbook", task_message => "Migrating")
on_exit: /System/CommonMethods/MiqAe.WeightedUpdateStatus(weight => 80, description
=> "Check ${#transformation_hook} migration playbook", task_message => "Migrating")
on_error: /System/CommonMethods/MiqAe.WeightedUpdateStatus(weight => 80, description
=> "Check ${#transformation_hook} migration playbook", task_message => "Migrating")
max_retries: '1500'

0 comments on commit fb6f12c

Please sign in to comment.