plugins: Refactor plugin identification and access

This change moves all plugin handling code into the kas.plugins module.
New accessor functions `plugins.get(name)` and `plugins.all()` are
provided to wrap the plugins dictionary so that the kas main function
doesn't need to worry about how this is accessed. Plugins are loaded at
runtime rather than at parse time by calling `plugins.load()` which
gives us an improved ability to handle errors.

The `@kasplugin` decorator is removed as it modified and attribute on
the kasplugin function itself when a plugin module was loaded. Importing
a module should not result in changes to a variable in a different
module as it leads to an initialization code flow which is difficult to
reason about. Instead, plugin modules should now list the plugins which
they introduce in a `__KAS_PLUGINS__` list which will be walked at
runtime by `plugins.load()`.

Signed-off-by: Paul Barker <pbarker@konsulko.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
Paul Barker
2020-10-16 19:31:47 +02:00
committed by Jan Kiszka
parent 7b9bce8e46
commit 5e3ca820b3
4 changed files with 44 additions and 24 deletions

View File

@@ -35,13 +35,11 @@ from kas.libcmds import (Macro, Command, SetupDir, SetupEnviron,
CleanupSSHAgent, SetupSSHAgent,
Loop, InitSetupRepos, FinishSetupRepos,
SetupReposStep)
from kas.plugins import kasplugin
__license__ = 'MIT'
__copyright__ = 'Copyright (c) Siemens AG, 2017-2018'
@kasplugin
class Shell:
"""
Implements a kas plugin that opens a shell within the kas environment.
@@ -129,3 +127,6 @@ class ShellCommand(Command):
if ret != 0:
logging.error('Shell returned non-zero exit status %d', ret)
sys.exit(ret)
__KAS_PLUGINS__ = [Shell]