From 7dddcb794b91645c04ef81f5f9f1761721962ba5 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Fri, 20 Dec 2019 23:10:47 +0100 Subject: [PATCH] Add libexec support and migrate cdist-dump --- cdist/argparse.py | 4 +++ {scripts => cdist/conf/libexec}/cdist-dump | 0 cdist/libexec.py | 41 ++++++++++++++++++++++ docs/src/cdist-troubleshooting.rst | 6 ++-- docs/src/man1/cdist-dump.rst | 10 +++--- scripts/cdist | 12 +++++-- setup.py | 2 +- 7 files changed, 63 insertions(+), 12 deletions(-) rename {scripts => cdist/conf/libexec}/cdist-dump (100%) create mode 100644 cdist/libexec.py diff --git a/cdist/argparse.py b/cdist/argparse.py index 7dc683f3..b1ee99da 100644 --- a/cdist/argparse.py +++ b/cdist/argparse.py @@ -6,6 +6,7 @@ import collections import functools import cdist.configuration import cdist.preos +import cdist.libexec as libexec # set of beta sub-commands @@ -436,6 +437,9 @@ def get_parsers(): ' should be POSIX compatible shell.')) parser['shell'].set_defaults(func=cdist.shell.Shell.commandline) + # Libexec + libexec.create_parsers(parser, parser['sub']) + for p in parser: parser[p].epilog = EPILOG diff --git a/scripts/cdist-dump b/cdist/conf/libexec/cdist-dump similarity index 100% rename from scripts/cdist-dump rename to cdist/conf/libexec/cdist-dump diff --git a/cdist/libexec.py b/cdist/libexec.py new file mode 100644 index 00000000..8aaa837f --- /dev/null +++ b/cdist/libexec.py @@ -0,0 +1,41 @@ +import os +import os.path +import cdist.argparse +import subprocess + + +libexec_delimiter = '-' +libexec_prefix = 'cdist' + libexec_delimiter +libexec_path = os.path.abspath( + os.path.join(os.path.dirname(cdist.__file__), 'conf', 'libexec')) + + +def scan(): + if os.path.isdir(libexec_path): + with os.scandir(libexec_path) as it: + for entry in it: + if (entry.name.startswith(libexec_prefix) and + entry.is_file() and + os.access(entry.path, os.X_OK)): + start = entry.name.find(libexec_delimiter) + 1 + yield entry.name[start:] + + +def is_libexec_command(name): + for x in scan(): + if name == x: + return True + return False + + +def create_parsers(parser, parent_parser): + for name in scan(): + parser[name] = parent_parser.add_parser(name, add_help=False) + + +def run(name, argv): + lib_name = libexec_prefix + name + lib_path = os.path.join(libexec_path, lib_name) + args = [lib_path, ] + args.extend(argv) + subprocess.check_call(args) diff --git a/docs/src/cdist-troubleshooting.rst b/docs/src/cdist-troubleshooting.rst index e639aafd..8ca67515 100644 --- a/docs/src/cdist-troubleshooting.rst +++ b/docs/src/cdist-troubleshooting.rst @@ -48,17 +48,17 @@ Using debug dump helper script ------------------------------ Since cdist stores data to local cache that can be used for debugging there is a helper script that dumps data from local cache, -`cdist-dump `_. +`cdist dump `_. For more info see: .. code-block:: sh - cdist-dump -h + cdist_dump -h Or from cdist git cloned directory: .. code-block:: sh - ./scripts/cdist-dump -h + ./bin/cdist dump -h diff --git a/docs/src/man1/cdist-dump.rst b/docs/src/man1/cdist-dump.rst index 907cd192..5a73b4a8 100644 --- a/docs/src/man1/cdist-dump.rst +++ b/docs/src/man1/cdist-dump.rst @@ -11,17 +11,17 @@ SYNOPSIS :: - cdist-dump [options] [host...] + cdist dump [options] [host...] DESCRIPTION ----------- -cdist-dump is a helper script that dumps data from local cdist cache for +cdist dump is a helper script that dumps data from local cdist cache for specified hosts. If host is not specified then all data from cache directory is dumped. Default cache directory is '~/.cdist/cache'. -cdist-dump can be used for debugging existing types, host configuration and +cdist dump can be used for debugging existing types, host configuration and new types. @@ -88,10 +88,10 @@ EXAMPLES .. code-block:: sh # Dump all - % cdist-dump -a + % cdist dump -a # Dump only code-* output - % cdist-dump -c + % cdist dump -c SEE ALSO diff --git a/scripts/cdist b/scripts/cdist index 7bf12c01..4fc74fb3 100755 --- a/scripts/cdist +++ b/scripts/cdist @@ -30,14 +30,20 @@ import cdist.config import cdist.install import cdist.shell import cdist.inventory +import cdist.libexec as libexec def commandline(): """Parse command line""" - # preos subcommand hack - if len(sys.argv) > 1 and sys.argv[1] == 'preos': - return cdist.preos.PreOS.commandline(sys.argv[1:]) + if len(sys.argv) > 1: + # preos subcommand hack + if sys.argv[1] == 'preos': + return cdist.preos.PreOS.commandline(sys.argv[1:]) + # libexec subcommands hack + if libexec.is_libexec_command(sys.argv[1]): + return libexec.run(sys.argv[1], sys.argv[2:]) + parser, cfg = cdist.argparse.parse_and_configure(sys.argv[1:]) args = cfg.get_args() diff --git a/setup.py b/setup.py index 7b000041..ac7965b7 100644 --- a/setup.py +++ b/setup.py @@ -56,7 +56,7 @@ setup( name="cdist", packages=["cdist", "cdist.core", "cdist.exec", "cdist.util", ], package_data={'cdist': package_data}, - scripts=["scripts/cdist", "scripts/cdist-dump", "scripts/cdist-new-type"], + scripts=["scripts/cdist", "scripts/cdist-new-type"], version=cdist.version.VERSION, description="A Usable Configuration Management System", author="Nico Schottelius",