conductor-worker-python/pyexecplugins/pyexecplugins.py

59 lines
2.0 KiB
Python
Raw Normal View History

2020-10-14 18:06:28 +02:00
import requests;
import json;
import logging;
2020-06-19 10:50:02 +02:00
class PyExecPlugins(type):
def __init__(cls, name, bases, attrs):
if not hasattr(cls, "plugins"):
logging.getLogger("pyexec").debug("Initializing plugins")
2020-06-19 10:50:02 +02:00
cls.plugins = {}
2020-10-14 18:06:28 +02:00
cls.alias = {}
2020-06-19 10:50:02 +02:00
else:
logging.getLogger("pyexec").debug("Appending a plugin %s - %s ", cls.name, cls)
2020-06-19 10:50:02 +02:00
cls.plugins[cls.name] = cls
2020-10-14 18:06:28 +02:00
#Alias plugin also with taskdefinition so it will be lookup-able with poth Operation name and taskdefinition
if cls.taskdef is not None:
cls.alias[cls.taskdef["name"]] = cls
2020-06-19 10:50:02 +02:00
def getPlugins(cls):
return cls.plugins
2020-10-14 18:06:28 +02:00
def getPluginNames(cls):
return [*cls.plugins.keys()]
2020-06-19 10:50:02 +02:00
def get(cls, name):
return cls.plugins.get(name)
2020-10-14 18:06:28 +02:00
def getAlias(cls, name):
return cls.alias.get(name)
def registerTaskDefinitions(cls, server, auth):
2024-02-14 20:21:38 +01:00
url = server.rstrip("/") + "/metadata/taskdefs"
2020-10-14 18:06:28 +02:00
#pyexec generic taskdefinition
taskdefs = []
2020-10-14 18:06:28 +02:00
for plg in cls.getPluginNames():
if cls.plugins[plg].taskdef is not None:
taskdef = cls.plugins[plg].taskdef
taskdefs.append(taskdef)
#Post all new (or not) task definitions to orchestrator
logging.getLogger("pyexec").debug("Recording task definitions %s", taskdefs)
2020-10-14 18:06:28 +02:00
headers = {'Content-Type': 'application/json'}
if auth != None:
headers['Authorization'] = 'Basic ' + auth
2020-10-14 18:06:28 +02:00
try:
response = requests.request("POST", url, headers=headers, data=json.dumps(taskdefs), timeout=(5, 10))
2020-10-14 18:06:28 +02:00
except Exception as e:
logging.getLogger("pyexec").warning("Unable to register task defs %s", e)
2020-06-19 10:50:02 +02:00
class PyExecPlugin(object, metaclass=PyExecPlugins):
2021-03-22 16:36:35 +01:00
def __init__(self, data=None, config=None):
2020-06-19 10:50:02 +02:00
self.data = data
2021-03-22 16:36:35 +01:00
self.config = config
2020-06-19 10:50:02 +02:00
def hasDefinition(self):
2020-10-14 18:06:28 +02:00
return (self.taskdef is not None)
def isAutomatic(self):
return False