Skip to content

Commit

Permalink
Add event state machine for refresh.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfu committed Jan 19, 2018
1 parent a694a1d commit 4daa344
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ object:
description:
fields:
- rel4:
value: "/System/event_handlers/event_action_refresh_sync?target=src_vm"
- rel5:
value: "/System/event_handlers/event_action_policy?target=src_vm&policy_event=vm_reconfigure&param="
value: "/System/Event/StateMachines/Refresh/sync?refresh_target=src_vm&policy_event=vm_reconfigure&policy_target=src_vm"
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
object_type: class
version: 1.0
object:
attributes:
description: Refresh State Machine
display_name:
name: Refresh
type:
inherits:
visibility:
owner:
schema:
- field:
aetype: state
name: pre
display_name:
datatype: string
priority: 1
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry: ''
on_exit: ''
on_error: ''
max_retries: '100'
max_time:
- field:
aetype: state
name: refresh
display_name:
datatype: string
priority: 2
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries: '100'
max_time:
- field:
aetype: state
name: check_refreshed
display_name:
datatype: string
priority: 3
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries: '100'
max_time:
- field:
aetype: state
name: post
display_name:
datatype: string
priority: 4
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry: ''
on_exit: ''
on_error: ''
max_retries: '100'
max_time:
- field:
aetype: state
name: policy
display_name:
datatype: string
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: finished
display_name:
datatype: string
priority: 6
owner:
default_value:
substitute: true
message: create
visibility:
collect:
scope:
description:
condition:
on_entry:
on_exit:
on_error:
max_retries:
max_time:
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#
# Description: This method checks the refresh completion.
#

module ManageIQ
module Automate
module System
module Event
module StateMachines
module Refresh
class CheckRefreshed
def initialize(handle = $evm)
@handle = handle
end

def main
check_status(refresh_task)
end

private

def check_status(task)
case task.state
when 'Finished'
@handle.root['ae_result'] = 'ok'
else
@handle.root['ae_result'] = 'retry'
@handle.root['ae_retry_interval'] = 1.minute
end
end

def refresh_task
task_id = @handle.get_state_var(:task_id).first
@handle.log(:info, "Stored task ID: [#{task_id}]")
if task_id.nil?
@handle.log(:error, 'Refresh task id not found')
exit(MIQ_ERROR)
end
fetch_task(task_id)
end

def fetch_task(task_id)
$evm.object.attributes.sort.each { |k, v| $evm.log("info", "#{k}: #{v}") }
task = @handle.vmdb(:miq_task).find(task_id)
if task.nil?
@handle.log(:error, 'Refresh task with id : #{task_id} not found')
exit(MIQ_ERROR)
end
task
end
end
end
end
end
end
end
end

if __FILE__ == $PROGRAM_NAME
ManageIQ::Automate::System::Event::StateMachines::Refresh::CheckRefreshed.new.main
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
object_type: method
version: 1.0
object:
attributes:
name: check_refreshed
display_name:
description:
scope: instance
language: ruby
location: inline
options: {}
inputs: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# Description: This method starts a synchronous refresh and save the
# task id in the state variables so we can use it when
# waiting for the refresh to finish.
#

module ManageIQ
module Automate
module System
module Event
module StateMachines
module Refresh
class TargetRefresh
def initialize(handle = $evm)
@handle = handle
end

def main
task_id = event.refresh(refresh_target, true)
@handle.set_state_var(:task_id, task_id)
end

private

def event
@handle.root["event_stream"].tap do |event|
if event.nil?
@handle.log(:error, 'Event object is nil')
raise 'Event object not found'
end
end
end

def refresh_target
target = @handle.object["refresh_target"]
@handle.log(:info, "Refresh target: [#{target}]")
raise "Refresh target not found" if target.nil?
target
end
end
end
end
end
end
end
end

if __FILE__ == $PROGRAM_NAME
ManageIQ::Automate::System::Event::StateMachines::Refresh::TargetRefresh.new.main
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
object_type: method
version: 1.0
object:
attributes:
name: target_refresh
display_name:
description:
scope: instance
language: ruby
location: inline
options: {}
inputs: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
object_type: instance
version: 1.0
object:
attributes:
display_name:
name: sync
inherits:
description:
fields:
- refresh:
value: METHOD::target_refresh
- check_refreshed:
value: METHOD::check_refreshed
- policy:
value: "/System/event_handlers/event_action_policy?target=${#policy_target}&policy_event=${#policy_event}&param="
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
object_type: namespace
version: 1.0
object:
attributes:
name: StateMachines
description:
display_name:
priority:
enabled:

0 comments on commit 4daa344

Please sign in to comment.