Fix missing configuration file usage, support -g

PreOS code did not use configuration support. This fix adds support
for using cdist configuration, which takes into account cdist
configuration file, environment variables and command line options,
especially conf_dir.

It also adds support for -g, --config-file option, for specifying
custom configuration file.
This commit is contained in:
Darko Poljak 2020-01-11 14:16:33 +01:00
parent 51ba4a49d8
commit 11f569959d
3 changed files with 18 additions and 14 deletions

View file

@ -5,8 +5,9 @@ import inspect
import argparse import argparse
import cdist import cdist
import logging import logging
import re
import cdist.argparse import cdist.argparse
import cdist.configuration
import cdist.exec.util as util
_PREOS_CALL = "commandline" _PREOS_CALL = "commandline"
@ -24,16 +25,6 @@ def extend_plugins_path(dirs):
_PLUGINS_PATH.append(preos_dir) _PLUGINS_PATH.append(preos_dir)
cdist_home = cdist.home_dir()
if cdist_home:
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): def preos_plugin(obj):
"""It is preos if _PREOS_MARKER is True and has _PREOS_CALL.""" """It is preos if _PREOS_MARKER is True and has _PREOS_CALL."""
if hasattr(obj, _PREOS_MARKER): if hasattr(obj, _PREOS_MARKER):
@ -102,6 +93,9 @@ class PreOS(object):
help=('Add configuration directory (one that ' help=('Add configuration directory (one that '
'contains "preos" subdirectory)'), 'contains "preos" subdirectory)'),
action='append') action='append')
parser.add_argument('-g', '--config-file',
help='Use specified custom configuration file.',
dest="config_file", required=False)
parser.add_argument('-L', '--list-preoses', parser.add_argument('-L', '--list-preoses',
help='List available PreOS-es', help='List available PreOS-es',
action='store_true', default=False) action='store_true', default=False)
@ -110,8 +104,12 @@ class PreOS(object):
cdist.argparse.handle_loglevel(args) cdist.argparse.handle_loglevel(args)
log.debug("preos args : {}".format(args)) log.debug("preos args : {}".format(args))
if args.conf_dir: cfg = cdist.configuration.Configuration(args)
extend_plugins_path(args.conf_dir) configuration = cfg.get_config(section='GLOBAL')
conf_dirs = util.resolve_conf_dirs(configuration,
args.conf_dir)
extend_plugins_path(conf_dirs)
sys.path.extend(_PLUGINS_PATH) sys.path.extend(_PLUGINS_PATH)
cls.preoses = find_preoses() cls.preoses = find_preoses()

View file

@ -3,6 +3,7 @@ Changelog
next: next:
* Type __acl: Add --entry parameter to replace --acl, deprecate --acl (Ander Punnar) * Type __acl: Add --entry parameter to replace --acl, deprecate --acl (Ander Punnar)
* Core: preos: Fix missing configuration file usage, support -g, --config-file option (Darko Poljak)
6.4.0: 2020-01-04 6.4.0: 2020-01-04
* Type __consul_agent: Don't deploy init script on Alpine anymore, it ships with one itself (Nico Schottelius) * Type __consul_agent: Don't deploy init script on Alpine anymore, it ships with one itself (Nico Schottelius)

View file

@ -59,7 +59,9 @@ SYNOPSIS
[-I INVENTORY_DIR] [-a] [-f HOSTFILE] [-H] [-t] [-I INVENTORY_DIR] [-a] [-f HOSTFILE] [-H] [-t]
[host [host ...]] [host [host ...]]
cdist preos [-h] [-l LOGLEVEL] [-q] [-v] [-c CONF_DIR] [-L] [preos] ... cdist preos [-h] [-l LOGLEVEL] [-q] [-v] [-c CONF_DIR] [-g CONFIG_FILE]
[-L]
[preos] ...
cdist preos [preos-options] 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]
@ -464,6 +466,9 @@ Create PreOS.
**-c CONF_DIR, --conf-dir CONF_DIR** **-c CONF_DIR, --conf-dir CONF_DIR**
Add configuration directory (one that contains "preos" subdirectory). Add configuration directory (one that contains "preos" subdirectory).
**-g CONFIG_FILE, --config-file CONFIG_FILE**
Use specified custom configuration file.
**-L, --list-preoses** **-L, --list-preoses**
List available PreOS-es. List available PreOS-es.