Skip to content

Commit

Permalink
Add support for using run_role_async
Browse files Browse the repository at this point in the history
Add the option to use the run_role_async using AnsibleWorkflow
  • Loading branch information
Boris Odnopozov committed Oct 22, 2018
1 parent 84a80a0 commit a4b7d0a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 199 deletions.
44 changes: 44 additions & 0 deletions app/models/manageiq/providers/ansible_playbook_workflow.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class ManageIQ::Providers::AnsiblePlaybookWorkflow < ManageIQ::Providers::AnsibleRunnerWorkflow
def self.job_options(env_vars, extra_vars, playbook_options, timeout, poll_interval)
{
:env_vars => env_vars,
:extra_vars => extra_vars,
:playbook_path => playbook_options[:playbook_path],
:timeout => timeout,
:poll_interval => poll_interval,
}
end

def pre_playbook
# A step before running the playbook for any optional setup tasks
queue_signal(:run_playbook)
end

def run_playbook
env_vars, extra_vars, playbook_path = options.values_at(:env_vars, :extra_vars, :playbook_path)

response = Ansible::Runner.run_async(env_vars, extra_vars, playbook_path)
if response.nil?
queue_signal(:abort, "Failed to run ansible playbook", "error")
else
context[:ansible_runner_response] = response.dump

started_on = Time.now.utc
update_attributes!(:context => context, :started_on => started_on)
miq_task.update_attributes!(:started_on => started_on)

queue_signal(:poll_runner)
end
end

def load_transitions
super.tap do |transactions|
transactions.merge!(
:start => {'waiting_to_start' => 'pre_playbook'},
:run_playbook => {'pre_playbook' => 'running'},
)
end
end

alias start pre_playbook
end
46 changes: 46 additions & 0 deletions app/models/manageiq/providers/ansible_role_workflow.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class ManageIQ::Providers::AnsibleRoleWorkflow < ManageIQ::Providers::AnsibleRunnerWorkflow
def self.job_options(env_vars, extra_vars, role_options, timeout, poll_interval)
{
:env_vars => env_vars,
:extra_vars => extra_vars,
:role_name => role_options[:role_name],
:roles_path => role_options[:roles_path],
:role_skip_facts => role_options[:role_skip_facts],
:timeout => timeout,
:poll_interval => poll_interval
}
end

def pre_role
# A step before running the playbook for any optional setup tasks
queue_signal(:run_role)
end

def run_role
env_vars, extra_vars, role_name, roles_path, role_skip_facts = options.values_at(:env_vars, :extra_vars, :role_name, :roles_path, :role_skip_facts)
role_skip_facts = true if role_skip_facts.nil?
response = Ansible::Runner.run_role_async(env_vars, extra_vars, role_name, :roles_path => roles_path, :role_skip_facts => role_skip_facts)
if response.nil?
queue_signal(:abort, "Failed to run ansible role", "error")
else
context[:ansible_runner_response] = response.dump

started_on = Time.now.utc
update_attributes!(:context => context, :started_on => started_on)
miq_task.update_attributes!(:started_on => started_on)

queue_signal(:poll_runner)
end
end

def load_transitions
super.tap do |transactions|
transactions.merge!(
:start => {'waiting_to_start' => 'pre_role'},
:run_role => {'pre_role' => 'running' },
)
end
end

alias start pre_role
end
42 changes: 9 additions & 33 deletions app/models/manageiq/providers/ansible_runner_workflow.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
class ManageIQ::Providers::AnsibleRunnerWorkflow < Job
def self.create_job(env_vars, extra_vars, playbook_path, timeout: 1.hour, poll_interval: 1.second)
options = {
:env_vars => env_vars,
:extra_vars => extra_vars,
:playbook_path => playbook_path,
:timeout => timeout,
:poll_interval => poll_interval,
}

super(name, options)
def self.create_job(env_vars, extra_vars, role_or_playbook_options, timeout: 1.hour, poll_interval: 1.second)
super(name, job_options(env_vars, extra_vars, role_or_playbook_options, timeout, poll_interval))
end

def pre_playbook
# A step before running the playbook for any optional setup tasks
queue_signal(:run_playbook)
end

def run_playbook
env_vars, extra_vars, playbook_path = options.values_at(:env_vars, :extra_vars, :playbook_path)

response = Ansible::Runner.run_async(env_vars, extra_vars, playbook_path)
if response.nil?
queue_signal(:abort, "Failed to run ansible playbook", "error")
else
context[:ansible_runner_response] = response.dump

started_on = Time.now.utc
update_attributes!(:context => context, :started_on => started_on)
miq_task.update_attributes!(:started_on => started_on)

queue_signal(:poll_runner)
end
def job_options(options)
raise(NotImplementedError, 'abstract')
end

def poll_runner
Expand Down Expand Up @@ -63,8 +37,12 @@ def post_playbook
queue_signal(:finish)
end

def fail_unimplamented
raise(NotImplementedError, "this is an abstract class, use a subclass that implaments a 'start' method")
end

alias initializing dispatch_start
alias start pre_playbook
alias start fail_unimplamented
alias finish process_finished
alias abort_job process_abort
alias cancel process_cancel
Expand Down Expand Up @@ -99,8 +77,6 @@ def load_transitions

{
:initializing => {'initialize' => 'waiting_to_start'},
:start => {'waiting_to_start' => 'pre_playbook'},
:run_playbook => {'pre_playbook' => 'running'},
:poll_runner => {'running' => 'running'},
:post_playbook => {'running' => 'post_playbook'},
:finish => {'*' => 'finished'},
Expand Down
166 changes: 0 additions & 166 deletions spec/models/manageiq/providers/ansible_runner_workflow_spec.rb

This file was deleted.

0 comments on commit a4b7d0a

Please sign in to comment.