Merge branch 'preos-plugins-dir-opt' into 'master'

Add conf-dir preos option

Closes #794

See merge request ungleich-public/cdist!821
This commit is contained in:
poljakowski 2019-12-09 09:56:10 +01:00
commit 27b682bfe9
4 changed files with 95 additions and 61 deletions

View file

@ -103,7 +103,7 @@ def get_parsers():
name="log level"),
help=('Set the specified verbosity level. '
'The levels, in order from the lowest to the highest, are: '
'ERROR (-1), WARNING (0), INFO (1), VERBOSE (2), DEBUG (3) '
'ERROR (-1), WARNING (0), INFO (1), VERBOSE (2), DEBUG (3), '
'TRACE (4 or higher). If used along with -v then -v '
'increases last set value and -l overwrites last set '
'value.'),

View file

@ -5,6 +5,8 @@ import inspect
import argparse
import cdist
import logging
import re
import cdist.argparse
_PREOS_CALL = "commandline"
@ -12,15 +14,24 @@ _PREOS_NAME = "_preos_name"
_PREOS_MARKER = "_cdist_preos"
_PLUGINS_DIR = "preos"
_PLUGINS_PATH = [os.path.join(os.path.dirname(__file__), _PLUGINS_DIR), ]
log = logging.getLogger("PreOS")
def extend_plugins_path(dirs):
for dir in dirs:
preos_dir = os.path.expanduser(os.path.join(dir, "preos"))
if os.path.isdir(preos_dir):
_PLUGINS_PATH.append(preos_dir)
cdist_home = cdist.home_dir()
if cdist_home:
cdist_home_preos = os.path.join(cdist_home, "preos")
if os.path.isdir(cdist_home_preos):
_PLUGINS_PATH.append(cdist_home_preos)
sys.path.extend(_PLUGINS_PATH)
log = logging.getLogger("PreOS")
extend_plugins_path((cdist_home, ))
x = 'CDIST_PATH'
if x in os.environ:
vals = re.split(r'(?<!\\):', os.environ[x])
vals = [x for x in vals if x]
extend_plugins_path(vals)
def preos_plugin(obj):
@ -71,31 +82,54 @@ def check_root():
raise cdist.Error("Must be run with root privileges")
def get_available_preoses_string(cls):
preoses = [' - {}'.format(x) for x in sorted(set(cls.preoses))]
return "Available PreOS-es:\n{}".format("\n".join(preoses))
class PreOS(object):
preoses = None
@classmethod
def commandline(cls, argv):
if not cls.preoses:
cls.preoses = find_preoses()
cdist_parser = cdist.argparse.get_parsers()
parser = argparse.ArgumentParser(
description="Create PreOS", prog="cdist preos")
parser.add_argument('preos', help='PreOS to create, one of: {}'.format(
set(cls.preoses)))
args = parser.parse_args(argv[1:2])
description="Create PreOS", prog="cdist preos",
parents=[cdist_parser['loglevel'], ])
parser.add_argument('preos', help='PreOS to create',
nargs='?', default=None)
parser.add_argument('-c', '--conf-dir',
help=('Add configuration directory (one that '
'contains "preos" subdirectory)'),
action='append')
parser.add_argument('-L', '--list-preoses',
help='List available PreOS-es',
action='store_true', default=False)
parser.add_argument('remainder_args', nargs=argparse.REMAINDER)
args = parser.parse_args(argv[1:])
cdist.argparse.handle_loglevel(args)
log.debug("preos args : {}".format(args))
if args.conf_dir:
extend_plugins_path(args.conf_dir)
sys.path.extend(_PLUGINS_PATH)
cls.preoses = find_preoses()
if args.list_preoses or not args.preos:
print(get_available_preoses_string(cls))
sys.exit(0)
preos_name = args.preos
if preos_name in cls.preoses:
preos = cls.preoses[preos_name]
func = getattr(preos, _PREOS_CALL)
if inspect.ismodule(preos):
func_args = [preos, argv[2:], ]
func_args = [preos, args.remainder_args, ]
else:
func_args = [argv[2:], ]
func_args = [args.remainder_args, ]
log.info("Running preos : {}".format(preos_name))
func(*func_args)
else:
log.error("Unknown preos: {}, available preoses: {}".format(
preos_name, set(cls.preoses.keys())))
raise cdist.Error(
"Invalid PreOS {}. {}".format(
preos_name, get_available_preoses_string(cls)))

View file

@ -88,16 +88,12 @@ When you try to run this new preos you will get:
.. code-block:: sh
$ cdist preos -h
usage: cdist preos [-h] preos
Create PreOS
positional arguments:
preos PreOS to create, one of: {'netbsd', 'debian', 'ubuntu'}
optional arguments:
-h, --help show this help message and exit
$ cdist preos -L
Available PreOS-es:
- debian
- devuan
- netbsd
- ubuntu
$ cdist preos netbsd
NetBSD PreOS: []
@ -121,15 +117,11 @@ When you try to run this new preos you will get:
.. code-block:: sh
$ cdist preos -h
usage: cdist preos [-h] preos
Create PreOS
positional arguments:
preos PreOS to create, one of: {'freebsd', 'debian', 'ubuntu'}
optional arguments:
-h, --help show this help message and exit
Available PreOS-es:
- debian
- devuan
- freebsd
- ubuntu
$ cdist preos freebsd
FreeBSD dummy preos: []

View file

@ -59,28 +59,28 @@ SYNOPSIS
[-I INVENTORY_DIR] [-a] [-f HOSTFILE] [-H] [-t]
[host [host ...]]
cdist preos [-h] preos
cdist preos [-h] [-l LOGLEVEL] [-q] [-v] [-c CONF_DIR] [-L] [preos] ...
cdist preos debian [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-a ARCH] [-B]
[-C] [-c CDIST_PARAMS] [-D DRIVE] [-e REMOTE_EXEC]
[-i MANIFEST] [-k KEYFILE ] [-m MIRROR]
[-P ROOT_PASSWORD] [-p PXE_BOOT_DIR] [-r]
[-S SCRIPT] [-s SUITE] [-y REMOTE_COPY]
target_dir
cdist preos [preos-options] debian [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-a ARCH] [-B]
[-C] [-c CDIST_PARAMS] [-D DRIVE] [-e REMOTE_EXEC]
[-i MANIFEST] [-k KEYFILE ] [-m MIRROR]
[-P ROOT_PASSWORD] [-p PXE_BOOT_DIR] [-r]
[-S SCRIPT] [-s SUITE] [-y REMOTE_COPY]
target_dir
cdist preos devuan [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-a ARCH] [-B]
[-C] [-c CDIST_PARAMS] [-D DRIVE] [-e REMOTE_EXEC]
[-i MANIFEST] [-k KEYFILE ] [-m MIRROR]
[-P ROOT_PASSWORD] [-p PXE_BOOT_DIR] [-r]
[-S SCRIPT] [-s SUITE] [-y REMOTE_COPY]
target_dir
cdist preos [preos-options] devuan [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-a ARCH] [-B]
[-C] [-c CDIST_PARAMS] [-D DRIVE] [-e REMOTE_EXEC]
[-i MANIFEST] [-k KEYFILE ] [-m MIRROR]
[-P ROOT_PASSWORD] [-p PXE_BOOT_DIR] [-r]
[-S SCRIPT] [-s SUITE] [-y REMOTE_COPY]
target_dir
cdist preos ubuntu [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-a ARCH] [-B]
[-C] [-c CDIST_PARAMS] [-D DRIVE] [-e REMOTE_EXEC]
[-i MANIFEST] [-k KEYFILE ] [-m MIRROR]
[-P ROOT_PASSWORD] [-p PXE_BOOT_DIR] [-r]
[-S SCRIPT] [-s SUITE] [-y REMOTE_COPY]
target_dir
cdist preos [preos-options] ubuntu [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-a ARCH] [-B]
[-C] [-c CDIST_PARAMS] [-D DRIVE] [-e REMOTE_EXEC]
[-i MANIFEST] [-k KEYFILE ] [-m MIRROR]
[-P ROOT_PASSWORD] [-p PXE_BOOT_DIR] [-r]
[-S SCRIPT] [-s SUITE] [-y REMOTE_COPY]
target_dir
cdist shell [-h] [-l LOGLEVEL] [-q] [-v] [-s SHELL]
@ -103,7 +103,7 @@ All commands accept the following options:
**-l LOGLEVEL, --log-level LOGLEVEL**
Set the specified verbosity level. The levels, in
order from the lowest to the highest, are: ERROR (-1),
WARNING (0), INFO (1), VERBOSE (2), DEBUG (3) TRACE (4
WARNING (0), INFO (1), VERBOSE (2), DEBUG (3), TRACE (4
or higher). If used along with -v then -v increases
last set value and -l overwrites last set value.
@ -116,7 +116,7 @@ All commands accept the following options:
value is 0 which includes ERROR and WARNING levels.
The levels, in order from the lowest to the highest,
are: ERROR (-1), WARNING (0), INFO (1), VERBOSE (2),
DEBUG (3) TRACE (4 or higher). If used along with -l
DEBUG (3), TRACE (4 or higher). If used along with -l
then -l overwrites last set value and -v increases
last set value.
@ -457,7 +457,15 @@ List inventory database.
PREOS
-----
Create PreOS. Currently, the following PreOS-es are supported:
Create PreOS.
**-c CONF_DIR, --conf-dir CONF_DIR**
Add configuration directory (one that contains "preos" subdirectory).
**-L, --list-preoses**
List available PreOS-es.
Currently, the following PreOS-es are supported:
* debian
* ubuntu