Change v2_playbook_on_start logic to positively detect legacy plugins

In order to support legacy plugins, the following two method signatures
are allowed for `CallbackBase.v2_playbook_on_start`:

def v2_playbook_on_start(self):
def v2_playbook_on_start(self, playbook):

Previously, the logic to handle this divergence checked to see if the
callback plugin being called supported an argument named `playbook`
in its `v2_playbook_on_start` method. This was fragile in a few ways:
 - if a plugin author did not use the literal `playbook` to name their
   method argument, their plugin would not be called correctly
 - if a plugin author wrapped their `v2_playbook_on_start` method and
   by doing so changed the argspec to no longer expose an argument
   with that literal name, their plugin would not be called correctly

In order to continue to support both types of callback for backwards
compatibility while making the call more robust for plugin authors,
the logic can be reversed in order to have a positive check for the old
method signature instead of a positive check for the new one.

Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
This commit is contained in:
Steve Kuznetsov 2016-10-12 13:55:42 -04:00 committed by Toshio Kuratomi
commit 0bc35354ce
2 changed files with 151 additions and 7 deletions

View file

@ -353,17 +353,20 @@ class TaskQueueManager:
for method in methods:
try:
# temporary hack, required due to a change in the callback API, so
# we don't break backwards compatibility with callbacks which were
# designed to use the original API
# Previously, the `v2_playbook_on_start` callback API did not accept
# any arguments. In recent versions of the v2 callback API, the play-
# book that started execution is given. In order to support both of
# these method signatures, we need to use this `inspect` hack to send
# no arguments to the methods that don't accept them. This way, we can
# not break backwards compatibility until that API is deprecated.
# FIXME: target for removal and revert to the original code here after a year (2017-01-14)
if method_name == 'v2_playbook_on_start':
import inspect
(f_args, f_varargs, f_keywords, f_defaults) = inspect.getargspec(method)
if 'playbook' in f_args:
method(*args, **kwargs)
else:
argspec = inspect.getargspec(method)
if argspec.args == ['self']:
method()
else:
method(*args, **kwargs)
else:
method(*args, **kwargs)
except Exception as e: