|
| 1 | +############################################################### |
| 2 | +# Copyright 2022 Lawrence Livermore National Security, LLC |
| 3 | +# (c.f. AUTHORS, NOTICE.LLNS, COPYING) |
| 4 | +# |
| 5 | +# This file is part of the Flux resource manager framework. |
| 6 | +# For details, see https://github.com/flux-framework. |
| 7 | +# |
| 8 | +# SPDX-License-Identifier: LGPL-3.0 |
| 9 | +############################################################### |
| 10 | + |
| 11 | +import os |
| 12 | +import argparse |
| 13 | +from abc import abstractmethod |
| 14 | + |
| 15 | +import flux |
| 16 | +from flux.importer import import_plugins, import_path |
| 17 | +from flux.job import Jobspec |
| 18 | + |
| 19 | + |
| 20 | +class FrobnicatorPlugin: |
| 21 | + """Base class for plugins which modify jobspec in place""" |
| 22 | + |
| 23 | + def __init__(self, parser): |
| 24 | + """Initialize a FrobnicatorPlugin""" |
| 25 | + |
| 26 | + def configure(self, args, config): |
| 27 | + """Configure a FrobnicatorPlugin. Run after arguments are parsed |
| 28 | +
|
| 29 | + Args: |
| 30 | + args (:obj:`Namespace`): The resulting namespace after calling |
| 31 | + argparse.parse_args() |
| 32 | +
|
| 33 | + config (:obj:`dict`): The current broker config, stored as a Python |
| 34 | + dictionary. |
| 35 | + """ |
| 36 | + |
| 37 | + @abstractmethod |
| 38 | + def frob(self, jobspec, userid, urgency, flags): |
| 39 | + """Modify jobspec. A FrobnicatorPlugin must implement this method. |
| 40 | +
|
| 41 | + The plugin should modify the jobspec parameter directly. Extra |
| 42 | + job information (user, urgency, flags) are available in the |
| 43 | + ``info`` parameter. |
| 44 | +
|
| 45 | + Args: |
| 46 | + jobspec (:obj:`Jobspec`): The jobspec to modify |
| 47 | +
|
| 48 | + userid (:obj:`int`): Submitting user |
| 49 | +
|
| 50 | + urgency (:obj:`int`): Initial job urgency |
| 51 | +
|
| 52 | + flags (:obj:`int`): Job submission flags |
| 53 | +
|
| 54 | + Returns: |
| 55 | + None or raises exception. |
| 56 | + """ |
| 57 | + raise NotImplementedError |
| 58 | + |
| 59 | + |
| 60 | +# pylint: disable=too-many-instance-attributes |
| 61 | +class JobFrobnicator: |
| 62 | + """A plugin-based job modification class |
| 63 | +
|
| 64 | + JobFrobnicator loads an ordered stack of plugins that implement the |
| 65 | + FrobnicatorPlugin interface from the 'flux.job.frobnicator.plugins' |
| 66 | + namespace. |
| 67 | + """ |
| 68 | + |
| 69 | + plugin_namespace = "flux.job.frobnicator.plugins" |
| 70 | + |
| 71 | + def __init__(self, argv, pluginpath=None, parser=None): |
| 72 | + |
| 73 | + self.frobnicators = [] |
| 74 | + self.config = {} |
| 75 | + |
| 76 | + if pluginpath is None: |
| 77 | + pluginpath = [] |
| 78 | + |
| 79 | + if parser is None: |
| 80 | + parser = argparse.ArgumentParser( |
| 81 | + formatter_class=flux.util.help_formatter(), add_help=False |
| 82 | + ) |
| 83 | + |
| 84 | + self.parser = parser |
| 85 | + self.parser_group = self.parser.add_argument_group("Options") |
| 86 | + self.plugins_group = self.parser.add_argument_group( |
| 87 | + "Options provided by plugins" |
| 88 | + ) |
| 89 | + |
| 90 | + self.parser_group.add_argument("--plugins", action="append", default=[]) |
| 91 | + |
| 92 | + args, self.remaining_args = self.parser.parse_known_args(argv) |
| 93 | + if args.plugins: |
| 94 | + args.plugins = [x for xs in args.plugins for x in xs.split(",")] |
| 95 | + |
| 96 | + # Load all available frobnicator plugins |
| 97 | + self.plugins = import_plugins(self.plugin_namespace, pluginpath) |
| 98 | + self.args = args |
| 99 | + |
| 100 | + def start(self): |
| 101 | + """Read broker config, select and configure frobnicator plugins""" |
| 102 | + |
| 103 | + self.config = flux.Flux().rpc("config.get").get() |
| 104 | + |
| 105 | + for name in self.args.plugins: |
| 106 | + if name not in self.plugins: |
| 107 | + try: |
| 108 | + self.plugins[name] = import_path(name) |
| 109 | + except: |
| 110 | + raise ValueError(f"frobnicator plugin '{name}' not found") |
| 111 | + plugin = self.plugins[name].Frobnicator(parser=self.plugins_group) |
| 112 | + self.frobnicators.append(plugin) |
| 113 | + |
| 114 | + # Parse remaining args and pass result to loaded plugins |
| 115 | + args = self.parser.parse_args(self.remaining_args) |
| 116 | + for frobnicator in self.frobnicators: |
| 117 | + frobnicator.configure(args, config=self.config) |
| 118 | + |
| 119 | + def frob(self, jobspec, user=None, flags=None, urgency=16): |
| 120 | + """Modify jobspec using stack of loaded frobnicator plugins |
| 121 | +
|
| 122 | + Args: |
| 123 | + jobspec (:obj:`Jobspec`): A Jobspec or JobspecV1 object |
| 124 | + which will be modified in place |
| 125 | +
|
| 126 | + userid (:obj:`int`): Submitting user |
| 127 | +
|
| 128 | + flags (:obj:`int`): Job submission flags |
| 129 | +
|
| 130 | + urgency (:obj:`int`): Initial job urgency |
| 131 | +
|
| 132 | + Returns: |
| 133 | + :obj:`dict`: A dictionary containing a result object, |
| 134 | + including keys:: |
| 135 | +
|
| 136 | + { |
| 137 | + 'errnum': 0, |
| 138 | + 'errmsg': "An error message", |
| 139 | + 'data': jobspec or None |
| 140 | + } |
| 141 | +
|
| 142 | + """ |
| 143 | + if not isinstance(jobspec, Jobspec): |
| 144 | + raise ValueError("jobspec not an instance of Jobspec") |
| 145 | + |
| 146 | + if user is None: |
| 147 | + user = os.getuid() |
| 148 | + |
| 149 | + for frob in self.frobnicators: |
| 150 | + frob.frob(jobspec, user, flags, urgency) |
| 151 | + return {"errnum": 0, "data": jobspec} |
0 commit comments