From d5f0925b8c982e68af44b3a151a4befea0cb05a5 Mon Sep 17 00:00:00 2001 From: Lukas Brabec Date: Jul 19 2017 13:15:49 +0000 Subject: mash, python and yumrepoinfo modules --- diff --git a/taskotron/mash.py b/taskotron/mash.py new file mode 100644 index 0000000..f8f48c6 --- /dev/null +++ b/taskotron/mash.py @@ -0,0 +1,29 @@ +#!/usr/bin/python +# Make coding more python3-ish +from __future__ import (absolute_import, division) +__metaclass__ = type + +from libtaskotron.directives import mash_directive +from ansible.module_utils.basic import AnsibleModule + +def main(): + mod = AnsibleModule( + argument_spec=dict( + rpmdir=dict(required=True), + arch=dict(required=True), + outdir=dict(required=False, default=None), + dodelta=dict(required=False, default=False, type="bool") + ) + ) + + try: + directive = mash_directive.MashDirective() + output = directive.process(mod.params, {}) + except Exception, e: + mod.exit_fail(msg=e) + + mod.exit_json(output=output) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/taskotron/python.py b/taskotron/python.py new file mode 100644 index 0000000..5701ed8 --- /dev/null +++ b/taskotron/python.py @@ -0,0 +1,86 @@ +#!/usr/bin/python + +from ansible.module_utils.basic import AnsibleModule + +import imp +import os +import re +from libtaskotron.directives import BaseDirective +from libtaskotron.exceptions import TaskotronDirectiveError +from libtaskotron.logger import log + + +def main(): + mod = AnsibleModule( + argument_spec=dict( + file=dict(required=True), + callable=dict(required=True), + kwargs=dict(required=False, default={}, type="dict") + ) + ) + + try: + directive = PythonDirective() + data = directive.process(mod.params, {'task': './neco'}) + except TaskotronDirectiveError, e: + mod.fail_json(msg=e) + + mod.exit_json(changed=True, output=data) + + +class PythonDirective(BaseDirective): + def _do_getattr(self, module, name): + """Isolation of getattr to make the code more easily testable + :param module module: module from which to getattr from + :param str name: name of object to getattr + :returns: object retrieved from module + """ + return getattr(module, name) + def execute(self, task_module, method_name, kwargs): + """Execute a callable in the specified module + :param module task_module: module containing the specified callable + :param str method_name: name of callable to execute + :param dict kwargs: kwargs to pass as parameters into the callable + :returns: output from the executed method + :raise TaskotronDirectiveError: if the callable returns something else + than ``None`` or ``basestring`` + """ + task_method = self._do_getattr(task_module, method_name) + module = re.sub('.py[co]?$', '', os.path.basename(task_module.__file__)) + log.info("Executing Python: %s.%s() with args %s", module, method_name, + kwargs) + output = task_method(**kwargs) + return output + def load_pyfile(self, filename): + """Import python code specified + :param str filename: absolute path to the python file + """ + task_importname = 'runtask_%s' % os.path.basename(filename).rstrip( + '.py') + task = imp.load_source(task_importname, filename) + return task_importname, task + def checkfile(self, filename): + """Check to see if the file exists + :param str filename: abspath to the filename + :raises TaskotronDirectiveError: if the file does not exist + """ + if not os.path.exists(filename): + raise TaskotronDirectiveError("The file %s does not exist!" + % filename) + def process(self, params, arg_data): + if 'callable' not in params or 'file' not in params: + detected_args = ', '.join(params.keys()) + raise TaskotronDirectiveError( + "The python directive requires both 'callable' and 'file' " + "arguments. Detected arguments: %s" % detected_args) + method_name = params.pop('callable') + basedir = os.path.dirname(arg_data['task']) + pyfile = os.path.join(basedir, params.pop('file')) + self.checkfile(pyfile) + _, task = self.load_pyfile(pyfile) + output = self.execute(task, method_name, params['kwargs']) + return output + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/taskotron/yumrepoinfo.py b/taskotron/yumrepoinfo.py new file mode 100644 index 0000000..2bbed5c --- /dev/null +++ b/taskotron/yumrepoinfo.py @@ -0,0 +1,25 @@ +#!/usr/bin/python +# Make coding more python3-ish +from __future__ import (absolute_import, division) +__metaclass__ = type + +from libtaskotron.directives import yumrepoinfo_directive +from ansible.module_utils.basic import AnsibleModule + +def main(): + mod = AnsibleModule( + argument_spec=dict( + koji_tag=dict(required=True), + arch=dict(required=True) + ) + ) + + directive = yumrepoinfo_directive.YumrepoinfoDirective() + + output = directive.process(mod.params, {}) + + mod.exit_json(output=output) + + +if __name__ == "__main__": + main() \ No newline at end of file