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 23, 2018
1 parent 37d8d26 commit 1535271
Show file tree
Hide file tree
Showing 8 changed files with 301 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/default?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,64 @@
#
# Description: This method checks the refresh completion.
#

module ManageIQ
module Automate
module System
module Event
module StateMachines
module Refresh
class CheckRefreshed
STATE_FINISHED = 'Finished'.freeze
STATUS_OK = 'Ok'.freeze

def initialize(handle = $evm)
@handle = handle
end

def main
check_status(refresh_task)
end

private

def check_status(task)
case task.state
when STATE_FINISHED
if task.status == STATUS_OK
@handle.root['ae_result'] = 'ok'
else
@handle.root['ae_result'] = 'error'
@handle.log(:error, "Refresh task ended with error: #{task.message}")
end
else
@handle.root['ae_result'] = 'retry'
@handle.root['ae_retry_interval'] = '1.minute'
end
end

def refresh_task
task_id = @handle.get_state_var(:refresh_task_id).first
@handle.log(:info, "Stored refresh task ID: [#{task_id}]")
fetch_task(task_id)
end

def fetch_task(task_id)
@handle.vmdb(:miq_task).find_by(:id => task_id).tap do |task|
if task.nil?
@handle.log(:error, "Refresh task with id: #{task_id} not found")
raise "Refresh task with id: #{task_id} not found"
end
end
end
end
end
end
end
end
end
end

if $PROGRAM_NAME == __FILE__
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,51 @@
#
# 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)
raise "Refresh task not created" if task_id.blank?
@handle.set_state_var(:refresh_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
@handle.object["refresh_target"].tap do |target|
@handle.log(:info, "Refresh target: [#{target}]")
raise "Refresh target not found" if target.nil?
end
end
end
end
end
end
end
end
end

if $PROGRAM_NAME == __FILE__
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: default
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 1535271

Please sign in to comment.