|
|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
"""
|
|
|
|
|
Core shepherd module, tying together main service functionality.
|
|
|
|
|
Core shepherd module, tying together main service functionality. Provides main CLI.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -41,7 +41,10 @@ def cli(ctx, default_config_path, local_operation, only_default_layer):
|
|
|
|
|
ending with ".toml" will be used.
|
|
|
|
|
"""
|
|
|
|
|
version_text = pkg_resources.get_distribution("shepherd")
|
|
|
|
|
log.info(F"Initialising Shepherd Agent {version_text}")
|
|
|
|
|
log.info(F"Shepherd Agent {version_text}")
|
|
|
|
|
|
|
|
|
|
if ctx.invoked_subcommand == "template":
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if not default_config_path:
|
|
|
|
|
default_config_path = sorted(glob.glob("./shepherd*.toml"))[:1]
|
|
|
|
|
@ -91,6 +94,81 @@ def test():
|
|
|
|
|
print("test!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
|
@click.argument('plugin_name', required=False)
|
|
|
|
|
@click.option('-a', '--include-all', is_flag=True,
|
|
|
|
|
help="Include all optional fields in the template")
|
|
|
|
|
@click.option('-c', '--config', 'config_path', type=click.Path(),
|
|
|
|
|
help="Path to append or create config tempalate")
|
|
|
|
|
@click.option('-d', '--plugin-dir', type=click.Path(),
|
|
|
|
|
help="Directory to search for plugin modules, in addition to built in Shepherd"
|
|
|
|
|
" plugins and the global import path. Defaults to current directory.")
|
|
|
|
|
@click.pass_context
|
|
|
|
|
def template(ctx, plugin_name, include_all, config_path, plugin_dir):
|
|
|
|
|
"""
|
|
|
|
|
Generate a template config TOML file for PLUGIN, or for the Shepherd core if
|
|
|
|
|
PLUGIN is not provided.
|
|
|
|
|
|
|
|
|
|
If config path is provided ("-c"), append to that file (if it exists) or write to
|
|
|
|
|
a new file (if it doesn't yet exist).
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
if not plugin_dir:
|
|
|
|
|
plugin_dir = Path.cwd()
|
|
|
|
|
|
|
|
|
|
confspec = ConfigSpecification()
|
|
|
|
|
if (not plugin_name) or (plugin_name=="shepherd"):
|
|
|
|
|
plugin_name = "shepherd"
|
|
|
|
|
define_core_config(confspec)
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
plugin_class = plugin.find_plugins([plugin_name], plugin_dir)[0]
|
|
|
|
|
except plugin.PluginLoadError as e:
|
|
|
|
|
log.error(e.args[0])
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
plugin_class.define_config(confspec)
|
|
|
|
|
|
|
|
|
|
template_dict = confspec.get_template(include_all)
|
|
|
|
|
template_toml = toml.dumps({plugin_name: template_dict})
|
|
|
|
|
|
|
|
|
|
log.info(F"Config template for [{plugin_name}]: \n\n"+template_toml)
|
|
|
|
|
|
|
|
|
|
if not config_path:
|
|
|
|
|
# reuse parent "-c" for convenience
|
|
|
|
|
config_path = ctx.parent.params["default_config_path"]
|
|
|
|
|
|
|
|
|
|
if not config_path:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if Path(config_path).is_file():
|
|
|
|
|
try:
|
|
|
|
|
existing_config = toml.load(config_path)
|
|
|
|
|
except Exception:
|
|
|
|
|
click.confirm(
|
|
|
|
|
F"File {config_path} already exists and is not a valid TOML file. Overwrite?",
|
|
|
|
|
default=True, abort=True)
|
|
|
|
|
|
|
|
|
|
click.echo(F"Writing [{plugin_name}] template to {config_path}")
|
|
|
|
|
with open(config_path, 'w+') as f:
|
|
|
|
|
f.write(template_toml)
|
|
|
|
|
else:
|
|
|
|
|
if plugin_name in existing_config:
|
|
|
|
|
click.confirm(F"Overwrite [{plugin_name}] section in {config_path}?",
|
|
|
|
|
default=True, abort=True)
|
|
|
|
|
click.echo(F"Overwriting [{plugin_name}] section in {config_path}")
|
|
|
|
|
else:
|
|
|
|
|
click.confirm(F"Add [{plugin_name}] section to {config_path}?",
|
|
|
|
|
default=True, abort=True)
|
|
|
|
|
click.echo(F"Adding [{plugin_name}] section to {config_path}")
|
|
|
|
|
existing_config[plugin_name] = template_dict
|
|
|
|
|
with open(config_path, 'w+') as f:
|
|
|
|
|
f.write(toml.dumps(existing_config))
|
|
|
|
|
else:
|
|
|
|
|
click.echo(F"Writing [{plugin_name}] template to {config_path}")
|
|
|
|
|
with open(config_path, 'w+') as f:
|
|
|
|
|
f.write(template_toml)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def compile_config_and_get_plugins(confman, default_config_path, layers_disabled):
|
|
|
|
|
"""
|
|
|
|
|
Run through the process of assembling the various config layers, falling back to working
|
|
|
|
|
|