Skip to content

Commit

Permalink
Make Response object work also for the async run
Browse files Browse the repository at this point in the history
Make Response object work also for the async run
  • Loading branch information
Ladas committed Jul 27, 2018
1 parent 2174451 commit 99c2851
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
16 changes: 3 additions & 13 deletions lib/ansible/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ def response(base_dir, ansible_runner_method, result)
if async?(ansible_runner_method)
Ansible::Runner::ResponseAsync.new(:base_dir => base_dir)
else
Ansible::Runner::Response.new(:return_code => return_code(base_dir),
:stdout => result.output,
:stderr => result.error)
Ansible::Runner::Response.new(:base_dir => base_dir,
:stdout => result.output,
:stderr => result.error)
end
end

Expand Down Expand Up @@ -220,16 +220,6 @@ def shared_params(extra_vars:)
{:cmdline => "--extra-vars '#{JSON.dump(extra_vars)}'"}
end

# Reads a return code from a file used by ansible-runner
#
# @return [Integer] Return code of the ansible-runner run, 0 == ok, others mean failure
def return_code(base_dir)
File.read(File.join(base_dir, "artifacts/result/rc")).to_i
rescue
_log.warn("Couldn't find ansible-runner return code")
1
end

# Asserts passed parameters are correct, if not throws an exception.
#
# @param env_vars [Hash] Hash with key/value pairs that will be passed as environment variables to the
Expand Down
39 changes: 36 additions & 3 deletions lib/ansible/runner/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,36 @@ class Runner
class Response
include Vmdb::Logging

attr_reader :return_code, :stdout, :stderr, :parsed_stdout
attr_reader :base_dir, :ident

# Response object designed for holding full response from ansible-runner
#
# @param base_dir [String] ansible-runner private_data_dir parameter
# @param return_code [Integer] Return code of the ansible-runner run, 0 == ok, others mean failure
# @param stdout [String] Stdout from ansible-runner run
# @param stderr [String] Stderr from ansible-runner run
def initialize(return_code:, stdout:, stderr:)
# @param ident [String] ansible-runner ident parameter
def initialize(base_dir:, return_code: nil, stdout: nil, stderr: nil, ident: "result")
@base_dir = base_dir
@ident = ident
@return_code = return_code
@stdout = stdout
@parsed_stdout = parse_stdout(stdout)
@parsed_stdout = parse_stdout(stdout) if stdout
@stderr = stderr
end

def return_code
@return_code ||= load_return_code
end

def stdout
@stdout ||= load_stdout
end

def parsed_stdout
@parsed_stdout ||= parse_stdout(stdout)
end

private

# Parses stdout to array of hashes
Expand All @@ -40,6 +56,23 @@ def parse_stdout(stdout)

parsed_stdout
end

# Reads a return code from a file used by ansible-runner
#
# @return [Integer] Return code of the ansible-runner run, 0 == ok, others mean failure
def load_return_code
File.read(File.join(base_dir, "artifacts", ident, "rc")).to_i
rescue
_log.warn("Couldn't find ansible-runner return code in #{base_dir}")
1
end

def load_stdout
File.read(File.join(base_dir, "artifacts", ident, "stdout"))
rescue
_log.warn("Couldn't find ansible-runner stdout in #{base_dir}")
""
end
end
end
end
11 changes: 7 additions & 4 deletions lib/ansible/runner/response_async.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ def initialize(base_dir:, ident: "result")

# @return [Boolean] true if the ansible job is still running, false when it's finished
def running?

false # TODO(lsmola) can't get running? from ansible-runner https://github.com/ansible/ansible-runner/issues/99
end

# @return [Ansible::Runner::Response] Response object with all details about the ansible run
def response
response = nil # TODO(lsmola) add Ansible::Runner::Response.new(...)
return if running?
return @response if @response

@response = Ansible::Runner::Response.new(:base_dir => base_dir, :ident => ident)

FileUtils.remove_entry(base_dir) # Clean up the temp dir, when the response is generated

response
@response
end

def dump
Expand All @@ -39,7 +42,7 @@ def dump
end

def self.load(kwargs)
self.new(kwargs)
new(kwargs)
end
end
end
Expand Down

0 comments on commit 99c2851

Please sign in to comment.