2016-05-06 17:37:41 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Matt Martz <matt@sivel.net>
|
|
|
|
# Copyright (C) 2016 Rackspace US, Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import imp
|
|
|
|
import sys
|
|
|
|
|
2018-01-12 00:41:53 +01:00
|
|
|
from contextlib import contextmanager
|
2016-05-06 17:37:41 +02:00
|
|
|
|
2018-01-12 00:41:53 +01:00
|
|
|
from ansible.module_utils.six import reraise
|
|
|
|
|
2016-05-06 17:37:41 +02:00
|
|
|
|
|
|
|
class AnsibleModuleCallError(RuntimeError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-01-12 00:41:53 +01:00
|
|
|
class AnsibleModuleImportError(ImportError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-12-18 18:44:17 +01:00
|
|
|
class _FakeAnsibleModuleInit:
|
|
|
|
def __init__(self):
|
|
|
|
self.args = tuple()
|
|
|
|
self.kwargs = {}
|
|
|
|
self.called = False
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
|
|
|
self.called = True
|
|
|
|
raise AnsibleModuleCallError('AnsibleModuleCallError')
|
|
|
|
|
|
|
|
|
|
|
|
def _fake_load_params():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-01-12 00:41:53 +01:00
|
|
|
@contextmanager
|
2018-12-18 18:44:17 +01:00
|
|
|
def setup_env(filename):
|
2018-01-19 23:33:16 +01:00
|
|
|
# Used to clean up imports later
|
|
|
|
pre_sys_modules = list(sys.modules.keys())
|
2016-05-06 17:37:41 +02:00
|
|
|
|
2018-12-18 18:44:17 +01:00
|
|
|
fake = _FakeAnsibleModuleInit()
|
|
|
|
module = __import__('ansible.module_utils.basic').module_utils.basic
|
|
|
|
_original_init = module.AnsibleModule.__init__
|
|
|
|
_original_load_params = module._load_params
|
|
|
|
setattr(module.AnsibleModule, '__init__', fake)
|
|
|
|
setattr(module, '_load_params', _fake_load_params)
|
2018-01-12 00:41:53 +01:00
|
|
|
|
2019-01-08 16:03:49 +01:00
|
|
|
try:
|
|
|
|
yield fake
|
|
|
|
finally:
|
|
|
|
setattr(module.AnsibleModule, '__init__', _original_init)
|
|
|
|
setattr(module, '_load_params', _original_load_params)
|
|
|
|
|
|
|
|
# Clean up imports to prevent issues with mutable data being used in modules
|
|
|
|
for k in list(sys.modules.keys()):
|
|
|
|
# It's faster if we limit to items in ansible.module_utils
|
|
|
|
# But if this causes problems later, we should remove it
|
|
|
|
if k not in pre_sys_modules and k.startswith('ansible.module_utils.'):
|
|
|
|
del sys.modules[k]
|
2018-01-19 23:33:16 +01:00
|
|
|
|
2016-05-06 17:37:41 +02:00
|
|
|
|
|
|
|
def get_argument_spec(filename):
|
2018-12-18 18:44:17 +01:00
|
|
|
with setup_env(filename) as fake:
|
2018-01-12 00:41:53 +01:00
|
|
|
try:
|
2018-12-18 18:44:17 +01:00
|
|
|
# We use ``module`` here instead of ``__main__``
|
|
|
|
# which helps with some import issues in this tool
|
|
|
|
# where modules may import things that conflict
|
2018-01-12 00:41:53 +01:00
|
|
|
mod = imp.load_source('module', filename)
|
2018-12-18 18:44:17 +01:00
|
|
|
if not fake.called:
|
2018-01-12 00:41:53 +01:00
|
|
|
mod.main()
|
|
|
|
except AnsibleModuleCallError:
|
|
|
|
pass
|
|
|
|
except Exception as e:
|
|
|
|
reraise(AnsibleModuleImportError, AnsibleModuleImportError('%s' % e), sys.exc_info()[2])
|
2016-05-06 17:37:41 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
2018-12-18 18:44:17 +01:00
|
|
|
return fake.kwargs['argument_spec'], fake.args, fake.kwargs
|
2016-05-06 17:37:41 +02:00
|
|
|
except KeyError:
|
2018-12-18 18:44:17 +01:00
|
|
|
return fake.args[0], fake.args, fake.kwargs
|
2016-05-06 17:37:41 +02:00
|
|
|
except TypeError:
|
2018-01-17 17:11:30 +01:00
|
|
|
return {}, (), {}
|