1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Move diff code more into runner code.

This commit is contained in:
Michael DeHaan 2013-02-09 23:24:03 -05:00
parent 6f0c9592bb
commit 72a05ae2a0
4 changed files with 31 additions and 18 deletions

View file

@ -219,7 +219,8 @@ class DefaultRunnerCallbacks(object):
call_callback_module('runner_on_async_failed', host, res, jid) call_callback_module('runner_on_async_failed', host, res, jid)
def on_file_diff(self, host, before_string, after_string): def on_file_diff(self, host, before_string, after_string):
call_callback_module('runner_on_file_diff', before_string, after_string) if before_string and after_string:
call_callback_module('runner_on_file_diff', before_string, after_string)
######################################################################## ########################################################################
@ -286,9 +287,10 @@ class CliRunnerCallbacks(DefaultRunnerCallbacks):
utils.write_tree_file(self.options.tree, host, utils.jsonify(result2,format=True)) utils.write_tree_file(self.options.tree, host, utils.jsonify(result2,format=True))
def on_file_diff(self, host, before_string, after_string): def on_file_diff(self, host, before_string, after_string):
if self.options.diff: if before_string and after_string:
print utils.get_diff(before_string, after_string) if self.options.diff:
super(CliRunnerCallbacks, self).on_file_diff(host, before_string, after_string) print utils.get_diff(before_string, after_string)
super(CliRunnerCallbacks, self).on_file_diff(host, before_string, after_string)
######################################################################## ########################################################################
@ -422,8 +424,9 @@ class PlaybookRunnerCallbacks(DefaultRunnerCallbacks):
super(PlaybookRunnerCallbacks, self).on_async_failed(host,res,jid) super(PlaybookRunnerCallbacks, self).on_async_failed(host,res,jid)
def on_file_diff(self, host, before_string, after_string): def on_file_diff(self, host, before_string, after_string):
print utils.get_diff(before_string, after_string) if before_string and after_string:
super(PlaybookRunnerCallbacks, self).on_file_diff(host, before_string, after_string) print utils.get_diff(before_string, after_string)
super(PlaybookRunnerCallbacks, self).on_file_diff(host, before_string, after_string)
######################################################################## ########################################################################

View file

@ -449,6 +449,8 @@ class Runner(object):
ignore_errors = self.module_vars.get('ignore_errors', False) ignore_errors = self.module_vars.get('ignore_errors', False)
self.callbacks.on_failed(host, data, ignore_errors) self.callbacks.on_failed(host, data, ignore_errors)
else: else:
if self.diff:
self.callbacks.on_file_diff(conn.host, result.before_diff_value, result.after_diff_value)
self.callbacks.on_ok(host, data) self.callbacks.on_ok(host, data)
return result return result

View file

@ -83,15 +83,19 @@ class ActionModule(object):
# if showing diffs, we need to get the remote value # if showing diffs, we need to get the remote value
dest_contents = None dest_contents = None
if self.runner.diff: if self.runner.diff:
# using persist_files to keep the temp directory around to avoid needing to grab another # using persist_files to keep the temp directory around to avoid needing to grab another
dest_result = self.runner._execute_module(conn, tmp, 'slurp', "path=%s" % dest, inject=inject, persist_files=True) dest_result = self.runner._execute_module(conn, tmp, 'slurp', "path=%s" % dest, inject=inject, persist_files=True)
dest_contents = dest_result.result['content'] if 'content' in dest_result.result:
if dest_result.result['encoding'] == 'base64': dest_contents = dest_result.result['content']
dest_contents = base64.b64decode(dest_contents) if dest_result.result['encoding'] == 'base64':
dest_contents = base64.b64decode(dest_contents)
else:
raise Exception("unknown encoding, failed: %s" % dest_result.result)
else: else:
raise Exception("unknown encoding, failed: %s" % dest_result.result) dest_result = ''
xfered = self.runner._transfer_str(conn, tmp, 'source', resultant) xfered = self.runner._transfer_str(conn, tmp, 'source', resultant)
# fix file permissions when the copy is done as a different user # fix file permissions when the copy is done as a different user
@ -102,13 +106,11 @@ class ActionModule(object):
module_args = "%s src=%s dest=%s" % (module_args, xfered, dest) module_args = "%s src=%s dest=%s" % (module_args, xfered, dest)
if self.runner.check: if self.runner.check:
if self.runner.diff: return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True), before_diff_value=dest_contents, after_diff_value=resultant)
self.runner.callbacks.on_file_diff(conn.host, dest_contents, resultant)
return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True))
else: else:
res = self.runner._execute_module(conn, tmp, 'copy', module_args, inject=inject) res = self.runner._execute_module(conn, tmp, 'copy', module_args, inject=inject)
if self.runner.diff: res.before_diff_value = dest_contents
self.runner.callbacks.on_file_diff(conn.host, dest_contents, resultant) res.after_diff_value = resultant
return res return res
else: else:
return ReturnData(conn=conn, comm_ok=True, result=dict(changed=False)) return ReturnData(conn=conn, comm_ok=True, result=dict(changed=False))

View file

@ -20,9 +20,10 @@ from ansible import utils
class ReturnData(object): class ReturnData(object):
''' internal return class for runner execute methods, not part of public API signature ''' ''' internal return class for runner execute methods, not part of public API signature '''
__slots__ = [ 'result', 'comm_ok', 'host' ] __slots__ = [ 'result', 'comm_ok', 'host', 'before_diff_value', 'after_diff_value' ]
def __init__(self, conn=None, host=None, result=None, comm_ok=True): def __init__(self, conn=None, host=None, result=None,
comm_ok=True, before_diff_value=None, after_diff_value=None):
# which host is this ReturnData about? # which host is this ReturnData about?
if conn is not None: if conn is not None:
@ -38,6 +39,11 @@ class ReturnData(object):
self.result = result self.result = result
self.comm_ok = comm_ok self.comm_ok = comm_ok
# if these values are set and used with --diff we can show
# changes made to particular files
self.before_diff_value = before_diff_value
self.after_diff_value = after_diff_value
if type(self.result) in [ str, unicode ]: if type(self.result) in [ str, unicode ]:
self.result = utils.parse_json(self.result) self.result = utils.parse_json(self.result)