forked from ungleich-public/cdist
		
	Add plugins-dir preos option
This commit is contained in:
		
					parent
					
						
							
								839e7a408e
							
						
					
				
			
			
				commit
				
					
						4735df1bed
					
				
			
		
					 4 changed files with 95 additions and 61 deletions
				
			
		| 
						 | 
					@ -103,7 +103,7 @@ def get_parsers():
 | 
				
			||||||
                                   name="log level"),
 | 
					                                   name="log level"),
 | 
				
			||||||
            help=('Set the specified verbosity level. '
 | 
					            help=('Set the specified verbosity level. '
 | 
				
			||||||
                  'The levels, in order from the lowest to the highest, are: '
 | 
					                  '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 '
 | 
					                  'TRACE (4 or higher). If used along with -v then -v '
 | 
				
			||||||
                  'increases last set value and -l overwrites last set '
 | 
					                  'increases last set value and -l overwrites last set '
 | 
				
			||||||
                  'value.'),
 | 
					                  'value.'),
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,6 +5,8 @@ import inspect
 | 
				
			||||||
import argparse
 | 
					import argparse
 | 
				
			||||||
import cdist
 | 
					import cdist
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
 | 
					import re
 | 
				
			||||||
 | 
					import cdist.argparse
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_PREOS_CALL = "commandline"
 | 
					_PREOS_CALL = "commandline"
 | 
				
			||||||
| 
						 | 
					@ -12,15 +14,24 @@ _PREOS_NAME = "_preos_name"
 | 
				
			||||||
_PREOS_MARKER = "_cdist_preos"
 | 
					_PREOS_MARKER = "_cdist_preos"
 | 
				
			||||||
_PLUGINS_DIR = "preos"
 | 
					_PLUGINS_DIR = "preos"
 | 
				
			||||||
_PLUGINS_PATH = [os.path.join(os.path.dirname(__file__), _PLUGINS_DIR), ]
 | 
					_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()
 | 
					cdist_home = cdist.home_dir()
 | 
				
			||||||
if cdist_home:
 | 
					if cdist_home:
 | 
				
			||||||
    cdist_home_preos = os.path.join(cdist_home, "preos")
 | 
					    extend_plugins_path((cdist_home, ))
 | 
				
			||||||
    if os.path.isdir(cdist_home_preos):
 | 
					x = 'CDIST_PATH'
 | 
				
			||||||
        _PLUGINS_PATH.append(cdist_home_preos)
 | 
					if x in os.environ:
 | 
				
			||||||
sys.path.extend(_PLUGINS_PATH)
 | 
					    vals = re.split(r'(?<!\\):', os.environ[x])
 | 
				
			||||||
 | 
					    vals = [x for x in vals if x]
 | 
				
			||||||
 | 
					    extend_plugins_path(vals)
 | 
				
			||||||
log = logging.getLogger("PreOS")
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def preos_plugin(obj):
 | 
					def preos_plugin(obj):
 | 
				
			||||||
| 
						 | 
					@ -71,31 +82,54 @@ def check_root():
 | 
				
			||||||
        raise cdist.Error("Must be run with root privileges")
 | 
					        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):
 | 
					class PreOS(object):
 | 
				
			||||||
    preoses = None
 | 
					    preoses = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def commandline(cls, argv):
 | 
					    def commandline(cls, argv):
 | 
				
			||||||
 | 
					        cdist_parser = cdist.argparse.get_parsers()
 | 
				
			||||||
 | 
					        parser = argparse.ArgumentParser(
 | 
				
			||||||
 | 
					            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 not cls.preoses:
 | 
					        if args.conf_dir:
 | 
				
			||||||
 | 
					            extend_plugins_path(args.conf_dir)
 | 
				
			||||||
 | 
					        sys.path.extend(_PLUGINS_PATH)
 | 
				
			||||||
        cls.preoses = find_preoses()
 | 
					        cls.preoses = find_preoses()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        parser = argparse.ArgumentParser(
 | 
					        if args.list_preoses or not args.preos:
 | 
				
			||||||
            description="Create PreOS", prog="cdist preos")
 | 
					            print(get_available_preoses_string(cls))
 | 
				
			||||||
        parser.add_argument('preos', help='PreOS to create, one of: {}'.format(
 | 
					            sys.exit(0)
 | 
				
			||||||
            set(cls.preoses)))
 | 
					 | 
				
			||||||
        args = parser.parse_args(argv[1:2])
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        preos_name = args.preos
 | 
					        preos_name = args.preos
 | 
				
			||||||
        if preos_name in cls.preoses:
 | 
					        if preos_name in cls.preoses:
 | 
				
			||||||
            preos = cls.preoses[preos_name]
 | 
					            preos = cls.preoses[preos_name]
 | 
				
			||||||
            func = getattr(preos, _PREOS_CALL)
 | 
					            func = getattr(preos, _PREOS_CALL)
 | 
				
			||||||
            if inspect.ismodule(preos):
 | 
					            if inspect.ismodule(preos):
 | 
				
			||||||
                func_args = [preos, argv[2:], ]
 | 
					                func_args = [preos, args.remainder_args, ]
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                func_args = [argv[2:], ]
 | 
					                func_args = [args.remainder_args, ]
 | 
				
			||||||
            log.info("Running preos : {}".format(preos_name))
 | 
					            log.info("Running preos : {}".format(preos_name))
 | 
				
			||||||
            func(*func_args)
 | 
					            func(*func_args)
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            log.error("Unknown preos: {}, available preoses: {}".format(
 | 
					            raise cdist.Error(
 | 
				
			||||||
                preos_name, set(cls.preoses.keys())))
 | 
					                "Invalid PreOS {}. {}".format(
 | 
				
			||||||
 | 
					                    preos_name, get_available_preoses_string(cls)))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -88,16 +88,12 @@ When you try to run this new preos you will get:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. code-block:: sh
 | 
					.. code-block:: sh
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $ cdist preos -h
 | 
					    $ cdist preos -L
 | 
				
			||||||
    usage: cdist preos [-h] preos
 | 
					    Available PreOS-es:
 | 
				
			||||||
 | 
					        - debian
 | 
				
			||||||
    Create PreOS
 | 
					        - devuan
 | 
				
			||||||
 | 
					        - netbsd
 | 
				
			||||||
    positional arguments:
 | 
					        - ubuntu
 | 
				
			||||||
      preos       PreOS to create, one of: {'netbsd', 'debian', 'ubuntu'}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    optional arguments:
 | 
					 | 
				
			||||||
      -h, --help  show this help message and exit
 | 
					 | 
				
			||||||
    $ cdist preos netbsd
 | 
					    $ cdist preos netbsd
 | 
				
			||||||
    NetBSD PreOS: []
 | 
					    NetBSD PreOS: []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -121,15 +117,11 @@ When you try to run this new preos you will get:
 | 
				
			||||||
.. code-block:: sh
 | 
					.. code-block:: sh
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $ cdist preos -h
 | 
					    $ cdist preos -h
 | 
				
			||||||
    usage: cdist preos [-h] preos
 | 
					    Available PreOS-es:
 | 
				
			||||||
 | 
					        - debian
 | 
				
			||||||
    Create PreOS
 | 
					        - devuan
 | 
				
			||||||
 | 
					        - freebsd
 | 
				
			||||||
    positional arguments:
 | 
					        - ubuntu
 | 
				
			||||||
      preos       PreOS to create, one of: {'freebsd', 'debian', 'ubuntu'}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    optional arguments:
 | 
					 | 
				
			||||||
      -h, --help  show this help message and exit
 | 
					 | 
				
			||||||
    $ cdist preos freebsd
 | 
					    $ cdist preos freebsd
 | 
				
			||||||
    FreeBSD dummy preos: []
 | 
					    FreeBSD dummy preos: []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -59,23 +59,23 @@ SYNOPSIS
 | 
				
			||||||
                         [-I INVENTORY_DIR] [-a] [-f HOSTFILE] [-H] [-t]
 | 
					                         [-I INVENTORY_DIR] [-a] [-f HOSTFILE] [-H] [-t]
 | 
				
			||||||
                         [host [host ...]]
 | 
					                         [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]
 | 
					    cdist preos [preos-options] debian [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-a ARCH] [-B]
 | 
				
			||||||
                                       [-C] [-c CDIST_PARAMS] [-D DRIVE] [-e REMOTE_EXEC]
 | 
					                                       [-C] [-c CDIST_PARAMS] [-D DRIVE] [-e REMOTE_EXEC]
 | 
				
			||||||
                                       [-i MANIFEST] [-k KEYFILE ] [-m MIRROR]
 | 
					                                       [-i MANIFEST] [-k KEYFILE ] [-m MIRROR]
 | 
				
			||||||
                                       [-P ROOT_PASSWORD] [-p PXE_BOOT_DIR] [-r]
 | 
					                                       [-P ROOT_PASSWORD] [-p PXE_BOOT_DIR] [-r]
 | 
				
			||||||
                                       [-S SCRIPT] [-s SUITE] [-y REMOTE_COPY]
 | 
					                                       [-S SCRIPT] [-s SUITE] [-y REMOTE_COPY]
 | 
				
			||||||
                                       target_dir
 | 
					                                       target_dir
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    cdist preos devuan [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-a ARCH] [-B]
 | 
					    cdist preos [preos-options] devuan [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-a ARCH] [-B]
 | 
				
			||||||
                                       [-C] [-c CDIST_PARAMS] [-D DRIVE] [-e REMOTE_EXEC]
 | 
					                                       [-C] [-c CDIST_PARAMS] [-D DRIVE] [-e REMOTE_EXEC]
 | 
				
			||||||
                                       [-i MANIFEST] [-k KEYFILE ] [-m MIRROR]
 | 
					                                       [-i MANIFEST] [-k KEYFILE ] [-m MIRROR]
 | 
				
			||||||
                                       [-P ROOT_PASSWORD] [-p PXE_BOOT_DIR] [-r]
 | 
					                                       [-P ROOT_PASSWORD] [-p PXE_BOOT_DIR] [-r]
 | 
				
			||||||
                                       [-S SCRIPT] [-s SUITE] [-y REMOTE_COPY]
 | 
					                                       [-S SCRIPT] [-s SUITE] [-y REMOTE_COPY]
 | 
				
			||||||
                                       target_dir
 | 
					                                       target_dir
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    cdist preos ubuntu [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-a ARCH] [-B]
 | 
					    cdist preos [preos-options] ubuntu [-h] [-l LOGLEVEL] [-q] [-v] [-b] [-a ARCH] [-B]
 | 
				
			||||||
                                       [-C] [-c CDIST_PARAMS] [-D DRIVE] [-e REMOTE_EXEC]
 | 
					                                       [-C] [-c CDIST_PARAMS] [-D DRIVE] [-e REMOTE_EXEC]
 | 
				
			||||||
                                       [-i MANIFEST] [-k KEYFILE ] [-m MIRROR]
 | 
					                                       [-i MANIFEST] [-k KEYFILE ] [-m MIRROR]
 | 
				
			||||||
                                       [-P ROOT_PASSWORD] [-p PXE_BOOT_DIR] [-r]
 | 
					                                       [-P ROOT_PASSWORD] [-p PXE_BOOT_DIR] [-r]
 | 
				
			||||||
| 
						 | 
					@ -103,7 +103,7 @@ All commands accept the following options:
 | 
				
			||||||
**-l LOGLEVEL, --log-level LOGLEVEL**
 | 
					**-l LOGLEVEL, --log-level LOGLEVEL**
 | 
				
			||||||
    Set the specified verbosity level. The levels, in
 | 
					    Set the specified verbosity level. The levels, in
 | 
				
			||||||
    order from the lowest to the highest, are: ERROR (-1),
 | 
					    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
 | 
					    or higher). If used along with -v then -v increases
 | 
				
			||||||
    last set value and -l overwrites last set value.
 | 
					    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.
 | 
					    value is 0 which includes ERROR and WARNING levels.
 | 
				
			||||||
    The levels, in order from the lowest to the highest,
 | 
					    The levels, in order from the lowest to the highest,
 | 
				
			||||||
    are: ERROR (-1), WARNING (0), INFO (1), VERBOSE (2),
 | 
					    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
 | 
					    then -l overwrites last set value and -v increases
 | 
				
			||||||
    last set value.
 | 
					    last set value.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -457,7 +457,15 @@ List inventory database.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
PREOS
 | 
					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
 | 
					* debian
 | 
				
			||||||
* ubuntu
 | 
					* ubuntu
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue