2016-12-03 17:24:37 +00:00
|
|
|
import argparse
|
|
|
|
import cdist
|
|
|
|
import multiprocessing
|
|
|
|
import logging
|
|
|
|
import collections
|
2017-09-09 19:17:29 +00:00
|
|
|
import functools
|
2017-08-30 21:02:17 +00:00
|
|
|
import cdist.configuration
|
2020-06-01 17:11:58 +00:00
|
|
|
import cdist.log
|
2019-09-20 05:15:37 +00:00
|
|
|
import cdist.preos
|
2019-12-30 08:32:15 +00:00
|
|
|
import cdist.info
|
2020-10-29 22:16:08 +00:00
|
|
|
import cdist.scan.commandline
|
2016-12-03 17:24:37 +00:00
|
|
|
|
|
|
|
|
2016-12-07 17:36:19 +00:00
|
|
|
# set of beta sub-commands
|
2020-10-29 22:16:08 +00:00
|
|
|
BETA_COMMANDS = set(('install', 'inventory', 'scan', ))
|
2016-12-07 17:36:19 +00:00
|
|
|
# set of beta arguments for sub-commands
|
2016-12-03 17:24:37 +00:00
|
|
|
BETA_ARGS = {
|
2019-04-13 19:18:27 +00:00
|
|
|
'config': set(('tag', 'all_tagged_hosts', 'use_archiving', )),
|
2016-12-03 17:24:37 +00:00
|
|
|
}
|
2019-05-09 06:26:42 +00:00
|
|
|
EPILOG = "Get cdist at https://code.ungleich.ch/ungleich-public/cdist"
|
2016-12-03 17:24:37 +00:00
|
|
|
# Parser others can reuse
|
|
|
|
parser = None
|
|
|
|
|
|
|
|
|
2017-06-28 08:18:53 +00:00
|
|
|
_verbosity_level_off = -2
|
2016-12-03 17:24:37 +00:00
|
|
|
_verbosity_level = {
|
2019-09-20 17:50:55 +00:00
|
|
|
None: logging.WARNING,
|
2017-06-28 08:18:53 +00:00
|
|
|
_verbosity_level_off: logging.OFF,
|
|
|
|
-1: logging.ERROR,
|
|
|
|
0: logging.WARNING,
|
|
|
|
1: logging.INFO,
|
|
|
|
2: logging.VERBOSE,
|
|
|
|
3: logging.DEBUG,
|
|
|
|
4: logging.TRACE,
|
2016-12-03 17:24:37 +00:00
|
|
|
}
|
2017-08-17 06:24:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Generate verbosity level constants:
|
|
|
|
# VERBOSE_OFF, VERBOSE_ERROR, VERBOSE_WARNING, VERBOSE_INFO, VERBOSE_VERBOSE,
|
|
|
|
# VERBOSE_DEBUG, VERBOSE_TRACE.
|
|
|
|
this_globals = globals()
|
|
|
|
for level in _verbosity_level:
|
|
|
|
const = 'VERBOSE_' + logging.getLevelName(_verbosity_level[level])
|
|
|
|
this_globals[const] = level
|
|
|
|
|
|
|
|
|
2017-06-28 08:18:53 +00:00
|
|
|
# All verbosity levels above 4 are TRACE.
|
2016-12-03 17:24:37 +00:00
|
|
|
_verbosity_level = collections.defaultdict(
|
2017-06-28 08:18:53 +00:00
|
|
|
lambda: logging.TRACE, _verbosity_level)
|
2016-12-03 17:24:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_beta_command(cmd):
|
2016-12-07 17:36:19 +00:00
|
|
|
BETA_COMMANDS.add(cmd)
|
2016-12-03 17:24:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_beta_arg(cmd, arg):
|
|
|
|
if cmd in BETA_ARGS:
|
|
|
|
if arg not in BETA_ARGS[cmd]:
|
|
|
|
BETA_ARGS[cmd].append(arg)
|
|
|
|
else:
|
2016-12-07 17:36:19 +00:00
|
|
|
BETA_ARGS[cmd] = set((arg, ))
|
2016-12-03 17:24:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_beta(args_dict):
|
|
|
|
if 'beta' not in args_dict:
|
|
|
|
args_dict['beta'] = False
|
|
|
|
# Check only if beta is not enabled: if beta option is specified then
|
|
|
|
# raise error.
|
|
|
|
if not args_dict['beta']:
|
|
|
|
cmd = args_dict['command']
|
|
|
|
# first check if command is beta
|
|
|
|
if cmd in BETA_COMMANDS:
|
|
|
|
raise cdist.CdistBetaRequired(cmd)
|
|
|
|
# then check if some command's argument is beta
|
|
|
|
if cmd in BETA_ARGS:
|
|
|
|
for arg in BETA_ARGS[cmd]:
|
|
|
|
if arg in args_dict and args_dict[arg]:
|
|
|
|
raise cdist.CdistBetaRequired(cmd, arg)
|
|
|
|
|
|
|
|
|
2017-09-09 19:17:29 +00:00
|
|
|
def check_lower_bounded_int(value, lower_bound, name):
|
2016-12-03 17:24:37 +00:00
|
|
|
try:
|
|
|
|
val = int(value)
|
|
|
|
except ValueError:
|
|
|
|
raise argparse.ArgumentTypeError(
|
|
|
|
"{} is invalid int value".format(value))
|
2017-09-09 19:17:29 +00:00
|
|
|
if val < lower_bound:
|
2016-12-03 17:24:37 +00:00
|
|
|
raise argparse.ArgumentTypeError(
|
2017-09-09 19:17:29 +00:00
|
|
|
"{} is invalid {} value".format(val, name))
|
2016-12-03 17:24:37 +00:00
|
|
|
return val
|
|
|
|
|
|
|
|
|
|
|
|
def get_parsers():
|
|
|
|
global parser
|
|
|
|
|
|
|
|
# Construct parser others can reuse
|
|
|
|
if parser:
|
|
|
|
return parser
|
|
|
|
else:
|
|
|
|
parser = {}
|
|
|
|
# Options _all_ parsers have in common
|
|
|
|
parser['loglevel'] = argparse.ArgumentParser(add_help=False)
|
2017-09-09 19:17:29 +00:00
|
|
|
parser['loglevel'].add_argument(
|
|
|
|
'-l', '--log-level', metavar='LOGLEVEL',
|
|
|
|
type=functools.partial(check_lower_bounded_int, lower_bound=-1,
|
|
|
|
name="log level"),
|
|
|
|
help=('Set the specified verbosity level. '
|
|
|
|
'The levels, in order from the lowest to the highest, are: '
|
2019-12-06 18:40:05 +00:00
|
|
|
'ERROR (-1), WARNING (0), INFO (1), VERBOSE (2), DEBUG (3), '
|
2017-09-09 19:17:29 +00:00
|
|
|
'TRACE (4 or higher). If used along with -v then -v '
|
|
|
|
'increases last set value and -l overwrites last set '
|
|
|
|
'value.'),
|
|
|
|
action='store', dest='verbose', required=False)
|
2017-06-28 08:18:53 +00:00
|
|
|
parser['loglevel'].add_argument(
|
|
|
|
'-q', '--quiet',
|
2017-08-17 06:24:58 +00:00
|
|
|
help='Quiet mode: disables logging, including WARNING and ERROR.',
|
2017-06-28 08:18:53 +00:00
|
|
|
action='store_true', default=False)
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['loglevel'].add_argument(
|
|
|
|
'-v', '--verbose',
|
2017-01-28 12:49:19 +00:00
|
|
|
help=('Increase the verbosity level. Every instance of -v '
|
2017-03-08 18:07:02 +00:00
|
|
|
'increments the verbosity level by one. Its default value '
|
2017-06-28 08:18:53 +00:00
|
|
|
'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) '
|
2017-09-09 19:17:29 +00:00
|
|
|
'TRACE (4 or higher). If used along with -l then -l '
|
|
|
|
'overwrites last set value and -v increases last set '
|
|
|
|
'value.'),
|
2017-09-07 14:13:46 +00:00
|
|
|
action='count', default=None)
|
2016-12-03 17:24:37 +00:00
|
|
|
|
2020-06-01 17:11:58 +00:00
|
|
|
parser['colored_output'] = argparse.ArgumentParser(add_help=False)
|
|
|
|
parser['colored_output'].add_argument(
|
2020-06-06 11:39:29 +00:00
|
|
|
'--colors', metavar='WHEN',
|
|
|
|
help="Colorize cdist's output based on log level; "
|
|
|
|
"WHEN is 'always', 'never', or 'auto'.",
|
2020-06-01 17:11:58 +00:00
|
|
|
action='store', dest='colored_output', required=False,
|
2020-06-03 19:45:04 +00:00
|
|
|
choices=cdist.configuration.ColoredOutputOption.CHOICES)
|
2020-06-01 17:11:58 +00:00
|
|
|
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['beta'] = argparse.ArgumentParser(add_help=False)
|
|
|
|
parser['beta'].add_argument(
|
|
|
|
'-b', '--beta',
|
2017-08-17 06:24:58 +00:00
|
|
|
help=('Enable beta functionality. '),
|
2018-03-01 18:33:22 +00:00
|
|
|
action='store_true', dest='beta', default=None)
|
2016-12-03 17:24:37 +00:00
|
|
|
|
|
|
|
# Main subcommand parser
|
|
|
|
parser['main'] = argparse.ArgumentParser(
|
2018-02-28 22:29:45 +00:00
|
|
|
description='cdist ' + cdist.VERSION)
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['main'].add_argument(
|
2017-08-17 06:24:58 +00:00
|
|
|
'-V', '--version', help='Show version.', action='version',
|
2016-12-03 17:24:37 +00:00
|
|
|
version='%(prog)s ' + cdist.VERSION)
|
|
|
|
parser['sub'] = parser['main'].add_subparsers(
|
|
|
|
title="Commands", dest="command")
|
|
|
|
|
|
|
|
# Banner
|
|
|
|
parser['banner'] = parser['sub'].add_parser(
|
|
|
|
'banner', parents=[parser['loglevel']])
|
|
|
|
parser['banner'].set_defaults(func=cdist.banner.banner)
|
|
|
|
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['inventory_common'] = argparse.ArgumentParser(add_help=False)
|
|
|
|
parser['inventory_common'].add_argument(
|
|
|
|
'-I', '--inventory',
|
|
|
|
help=('Use specified custom inventory directory. '
|
|
|
|
'Inventory directory is set up by the following rules: '
|
2017-08-30 21:02:17 +00:00
|
|
|
'if cdist configuration resolves this value then specified '
|
|
|
|
'directory is used, '
|
|
|
|
'if HOME env var is set then ~/.cdist/inventory is '
|
2017-07-20 20:04:44 +00:00
|
|
|
'used, otherwise distribution inventory directory is used.'),
|
|
|
|
dest="inventory_dir", required=False)
|
|
|
|
|
2017-08-30 21:02:17 +00:00
|
|
|
parser['common'] = argparse.ArgumentParser(add_help=False)
|
|
|
|
parser['common'].add_argument(
|
|
|
|
'-g', '--config-file',
|
|
|
|
help=('Use specified custom configuration file.'),
|
|
|
|
dest="config_file", required=False)
|
|
|
|
|
2016-12-03 17:24:37 +00:00
|
|
|
# Config
|
|
|
|
parser['config_main'] = argparse.ArgumentParser(add_help=False)
|
2019-01-28 18:09:36 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-4', '--force-ipv4',
|
|
|
|
help=('Force to use IPv4 addresses only. No influence for custom'
|
|
|
|
' remote commands.'),
|
|
|
|
action='store_const', dest='force_ipv', const=4)
|
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-6', '--force-ipv6',
|
|
|
|
help=('Force to use IPv6 addresses only. No influence for custom'
|
|
|
|
' remote commands.'),
|
|
|
|
action='store_const', dest='force_ipv', const=6)
|
2017-07-01 21:59:51 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-C', '--cache-path-pattern',
|
2017-08-17 06:24:58 +00:00
|
|
|
help=('Specify custom cache path pattern. If '
|
2017-07-01 21:59:51 +00:00
|
|
|
'it is not set then default hostdir is used.'),
|
|
|
|
dest='cache_path_pattern',
|
2017-08-30 21:02:17 +00:00
|
|
|
default=None)
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-c', '--conf-dir',
|
|
|
|
help=('Add configuration directory (can be repeated, '
|
2017-08-17 06:24:58 +00:00
|
|
|
'last one wins).'), action='append')
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-i', '--initial-manifest',
|
2017-08-17 06:24:58 +00:00
|
|
|
help='Path to a cdist manifest or \'-\' to read from stdin.',
|
2016-12-03 17:24:37 +00:00
|
|
|
dest='manifest', required=False)
|
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-j', '--jobs', nargs='?',
|
2017-09-09 19:17:29 +00:00
|
|
|
type=functools.partial(check_lower_bounded_int, lower_bound=1,
|
|
|
|
name="positive int"),
|
2017-07-25 09:12:18 +00:00
|
|
|
help=('Operate in parallel in specified maximum number of jobs. '
|
|
|
|
'Global explorers, object prepare and object run are '
|
2019-04-13 19:18:27 +00:00
|
|
|
'supported. Without argument CPU count is used by default. '),
|
2016-12-03 17:24:37 +00:00
|
|
|
action='store', dest='jobs',
|
|
|
|
const=multiprocessing.cpu_count())
|
2020-06-09 10:47:50 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'--log-server',
|
|
|
|
action='store_true',
|
|
|
|
help=('Start a log server for sub processes to use. '
|
|
|
|
'This is mainly useful when running cdist nested '
|
|
|
|
'from a code-local script. Log server is alwasy '
|
|
|
|
'implicitly started for \'install\' command.'))
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-n', '--dry-run',
|
2017-08-17 06:24:58 +00:00
|
|
|
help='Do not execute code.', action='store_true')
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-o', '--out-dir',
|
2017-08-17 06:24:58 +00:00
|
|
|
help='Directory to save cdist output in.', dest="out_path")
|
2019-01-28 18:09:36 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-P', '--timestamp',
|
|
|
|
help=('Timestamp log messages with the current local date and time '
|
|
|
|
'in the format: YYYYMMDDHHMMSS.us.'),
|
|
|
|
action='store_true', dest='timestamp')
|
2017-08-04 10:51:03 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-R', '--use-archiving', nargs='?',
|
|
|
|
choices=('tar', 'tgz', 'tbz2', 'txz',),
|
|
|
|
help=('Operate by using archiving with compression where '
|
2017-08-25 08:50:11 +00:00
|
|
|
'appropriate. Supported values are: tar - tar archive, '
|
2017-08-04 10:51:03 +00:00
|
|
|
'tgz - gzip tar archive (the default), '
|
2017-08-17 06:24:58 +00:00
|
|
|
'tbz2 - bzip2 tar archive and txz - lzma tar archive. '
|
|
|
|
'Currently in beta.'),
|
2017-08-04 10:51:03 +00:00
|
|
|
action='store', dest='use_archiving',
|
|
|
|
const='tgz')
|
2016-12-03 17:24:37 +00:00
|
|
|
|
|
|
|
# remote-copy and remote-exec defaults are environment variables
|
|
|
|
# if set; if not then None - these will be futher handled after
|
|
|
|
# parsing to determine implementation default
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-r', '--remote-out-dir',
|
2017-08-17 06:24:58 +00:00
|
|
|
help='Directory to save cdist output in on the target host.',
|
2017-07-20 20:04:44 +00:00
|
|
|
dest="remote_out_path")
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'--remote-copy',
|
2017-08-17 06:24:58 +00:00
|
|
|
help='Command to use for remote copy (should behave like scp).',
|
2016-12-03 17:24:37 +00:00
|
|
|
action='store', dest='remote_copy',
|
2017-08-30 21:02:17 +00:00
|
|
|
default=None)
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'--remote-exec',
|
|
|
|
help=('Command to use for remote execution '
|
2017-08-17 06:24:58 +00:00
|
|
|
'(should behave like ssh).'),
|
2016-12-03 17:24:37 +00:00
|
|
|
action='store', dest='remote_exec',
|
2017-08-30 21:02:17 +00:00
|
|
|
default=None)
|
2019-01-28 18:09:36 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-S', '--disable-saving-output-streams',
|
|
|
|
help='Disable saving output streams.',
|
|
|
|
action='store_false', dest='save_output_streams', default=True)
|
2016-12-03 17:24:37 +00:00
|
|
|
|
|
|
|
# Config
|
|
|
|
parser['config_args'] = argparse.ArgumentParser(add_help=False)
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['config_args'].add_argument(
|
|
|
|
'-A', '--all-tagged',
|
2017-08-17 06:24:58 +00:00
|
|
|
help=('Use all hosts present in tags db. Currently in beta.'),
|
2017-07-20 20:04:44 +00:00
|
|
|
action="store_true", dest="all_tagged_hosts", default=False)
|
|
|
|
parser['config_args'].add_argument(
|
|
|
|
'-a', '--all',
|
2017-08-17 06:24:58 +00:00
|
|
|
help=('List hosts that have all specified tags, '
|
|
|
|
'if -t/--tag is specified.'),
|
2017-07-20 20:04:44 +00:00
|
|
|
action="store_true", dest="has_all_tags", default=False)
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['config_args'].add_argument(
|
|
|
|
'-f', '--file',
|
2017-07-01 07:28:41 +00:00
|
|
|
help=('Read specified file for a list of additional hosts to '
|
|
|
|
'operate on or if \'-\' is given, read stdin (one host per '
|
2020-09-21 07:04:05 +00:00
|
|
|
'line).'),
|
2016-12-03 17:24:37 +00:00
|
|
|
dest='hostfile', required=False)
|
|
|
|
parser['config_args'].add_argument(
|
2017-07-25 09:12:18 +00:00
|
|
|
'-p', '--parallel', nargs='?', metavar='HOST_MAX',
|
2017-09-09 19:17:29 +00:00
|
|
|
type=functools.partial(check_lower_bounded_int, lower_bound=1,
|
|
|
|
name="positive int"),
|
2017-07-25 09:12:18 +00:00
|
|
|
help=('Operate on multiple hosts in parallel for specified maximum '
|
|
|
|
'hosts at a time. Without argument CPU count is used by '
|
|
|
|
'default.'),
|
|
|
|
action='store', dest='parallel',
|
|
|
|
const=multiprocessing.cpu_count())
|
2017-06-27 15:47:30 +00:00
|
|
|
parser['config_args'].add_argument(
|
|
|
|
'-s', '--sequential',
|
2017-08-17 06:24:58 +00:00
|
|
|
help='Operate on multiple hosts sequentially (default).',
|
2017-07-25 09:12:18 +00:00
|
|
|
action='store_const', dest='parallel', const=0)
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['config_args'].add_argument(
|
|
|
|
'-t', '--tag',
|
2017-08-17 06:24:58 +00:00
|
|
|
help=('Host is specified by tag, not hostname/address; '
|
|
|
|
'list all hosts that contain any of specified tags. '
|
|
|
|
'Currently in beta.'),
|
2017-07-20 20:04:44 +00:00
|
|
|
dest='tag', required=False, action="store_true", default=False)
|
2019-01-28 18:09:36 +00:00
|
|
|
parser['config_args'].add_argument(
|
|
|
|
'host', nargs='*', help='Host(s) to operate on.')
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['config'] = parser['sub'].add_parser(
|
|
|
|
'config', parents=[parser['loglevel'], parser['beta'],
|
2020-06-01 17:11:58 +00:00
|
|
|
parser['colored_output'],
|
2017-08-30 21:02:17 +00:00
|
|
|
parser['common'],
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['config_main'],
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['inventory_common'],
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['config_args']])
|
|
|
|
parser['config'].set_defaults(func=cdist.config.Config.commandline)
|
|
|
|
|
|
|
|
# Install
|
|
|
|
parser['install'] = parser['sub'].add_parser('install', add_help=False,
|
|
|
|
parents=[parser['config']])
|
|
|
|
parser['install'].set_defaults(func=cdist.install.Install.commandline)
|
|
|
|
|
2017-07-20 20:04:44 +00:00
|
|
|
# Inventory
|
2018-02-28 22:29:45 +00:00
|
|
|
parser['inventory'] = parser['sub'].add_parser('inventory')
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['invsub'] = parser['inventory'].add_subparsers(
|
|
|
|
title="Inventory commands", dest="subcommand")
|
|
|
|
|
|
|
|
parser['add-host'] = parser['invsub'].add_parser(
|
|
|
|
'add-host', parents=[parser['loglevel'], parser['beta'],
|
2020-06-01 17:11:58 +00:00
|
|
|
parser['colored_output'],
|
2017-08-30 21:02:17 +00:00
|
|
|
parser['common'],
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['inventory_common']])
|
|
|
|
parser['add-host'].add_argument(
|
2017-08-17 06:24:58 +00:00
|
|
|
'host', nargs='*', help='Host(s) to add.')
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['add-host'].add_argument(
|
|
|
|
'-f', '--file',
|
|
|
|
help=('Read additional hosts to add from specified file '
|
2020-09-21 07:04:05 +00:00
|
|
|
'or from stdin if \'-\' (each host on separate line). '),
|
2017-07-20 20:04:44 +00:00
|
|
|
dest='hostfile', required=False)
|
|
|
|
|
|
|
|
parser['add-tag'] = parser['invsub'].add_parser(
|
|
|
|
'add-tag', parents=[parser['loglevel'], parser['beta'],
|
2020-06-01 17:11:58 +00:00
|
|
|
parser['colored_output'],
|
2017-08-30 21:02:17 +00:00
|
|
|
parser['common'],
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['inventory_common']])
|
|
|
|
parser['add-tag'].add_argument(
|
|
|
|
'host', nargs='*',
|
2017-08-17 06:24:58 +00:00
|
|
|
help='List of host(s) for which tags are added.')
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['add-tag'].add_argument(
|
|
|
|
'-f', '--file',
|
|
|
|
help=('Read additional hosts to add tags from specified file '
|
2020-09-21 07:04:05 +00:00
|
|
|
'or from stdin if \'-\' (each host on separate line). '),
|
2017-07-20 20:04:44 +00:00
|
|
|
dest='hostfile', required=False)
|
|
|
|
parser['add-tag'].add_argument(
|
|
|
|
'-T', '--tag-file',
|
|
|
|
help=('Read additional tags to add from specified file '
|
2020-09-21 07:04:05 +00:00
|
|
|
'or from stdin if \'-\' (each tag on separate line). '),
|
2017-07-20 20:04:44 +00:00
|
|
|
dest='tagfile', required=False)
|
|
|
|
parser['add-tag'].add_argument(
|
|
|
|
'-t', '--taglist',
|
|
|
|
help=("Tag list to be added for specified host(s), comma separated"
|
2017-08-17 06:24:58 +00:00
|
|
|
" values."),
|
2017-07-20 20:04:44 +00:00
|
|
|
dest="taglist", required=False)
|
|
|
|
|
|
|
|
parser['del-host'] = parser['invsub'].add_parser(
|
|
|
|
'del-host', parents=[parser['loglevel'], parser['beta'],
|
2020-06-01 17:11:58 +00:00
|
|
|
parser['colored_output'],
|
2017-08-30 21:02:17 +00:00
|
|
|
parser['common'],
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['inventory_common']])
|
|
|
|
parser['del-host'].add_argument(
|
2017-08-17 06:24:58 +00:00
|
|
|
'host', nargs='*', help='Host(s) to delete.')
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['del-host'].add_argument(
|
2017-08-17 06:24:58 +00:00
|
|
|
'-a', '--all', help=('Delete all hosts.'),
|
2017-07-20 20:04:44 +00:00
|
|
|
dest='all', required=False, action="store_true", default=False)
|
|
|
|
parser['del-host'].add_argument(
|
|
|
|
'-f', '--file',
|
|
|
|
help=('Read additional hosts to delete from specified file '
|
2020-09-21 07:04:05 +00:00
|
|
|
'or from stdin if \'-\' (each host on separate line). '),
|
2017-07-20 20:04:44 +00:00
|
|
|
dest='hostfile', required=False)
|
|
|
|
|
|
|
|
parser['del-tag'] = parser['invsub'].add_parser(
|
|
|
|
'del-tag', parents=[parser['loglevel'], parser['beta'],
|
2020-06-01 17:11:58 +00:00
|
|
|
parser['colored_output'],
|
2017-08-30 21:02:17 +00:00
|
|
|
parser['common'],
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['inventory_common']])
|
|
|
|
parser['del-tag'].add_argument(
|
|
|
|
'host', nargs='*',
|
2017-08-17 06:24:58 +00:00
|
|
|
help='List of host(s) for which tags are deleted.')
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['del-tag'].add_argument(
|
|
|
|
'-a', '--all',
|
2017-08-17 06:24:58 +00:00
|
|
|
help=('Delete all tags for specified host(s).'),
|
2017-07-20 20:04:44 +00:00
|
|
|
dest='all', required=False, action="store_true", default=False)
|
|
|
|
parser['del-tag'].add_argument(
|
|
|
|
'-f', '--file',
|
|
|
|
help=('Read additional hosts to delete tags for from specified '
|
2020-09-21 07:04:05 +00:00
|
|
|
'file or from stdin if \'-\' (each host on separate '
|
|
|
|
'line). '),
|
2017-07-20 20:04:44 +00:00
|
|
|
dest='hostfile', required=False)
|
|
|
|
parser['del-tag'].add_argument(
|
|
|
|
'-T', '--tag-file',
|
|
|
|
help=('Read additional tags from specified file '
|
2020-09-21 07:04:05 +00:00
|
|
|
'or from stdin if \'-\' (each tag on separate line). '),
|
2017-07-20 20:04:44 +00:00
|
|
|
dest='tagfile', required=False)
|
|
|
|
parser['del-tag'].add_argument(
|
|
|
|
'-t', '--taglist',
|
|
|
|
help=("Tag list to be deleted for specified host(s), "
|
2017-08-17 06:24:58 +00:00
|
|
|
"comma separated values."),
|
2017-07-20 20:04:44 +00:00
|
|
|
dest="taglist", required=False)
|
|
|
|
|
|
|
|
parser['list'] = parser['invsub'].add_parser(
|
|
|
|
'list', parents=[parser['loglevel'], parser['beta'],
|
2020-06-01 17:11:58 +00:00
|
|
|
parser['colored_output'],
|
2017-08-30 21:02:17 +00:00
|
|
|
parser['common'],
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['inventory_common']])
|
|
|
|
parser['list'].add_argument(
|
2017-08-17 06:24:58 +00:00
|
|
|
'host', nargs='*', help='Host(s) to list.')
|
2017-07-20 20:04:44 +00:00
|
|
|
parser['list'].add_argument(
|
|
|
|
'-a', '--all',
|
2017-08-17 06:24:58 +00:00
|
|
|
help=('List hosts that have all specified tags, '
|
|
|
|
'if -t/--tag is specified.'),
|
2017-07-20 20:04:44 +00:00
|
|
|
action="store_true", dest="has_all_tags", default=False)
|
|
|
|
parser['list'].add_argument(
|
|
|
|
'-f', '--file',
|
|
|
|
help=('Read additional hosts to list from specified file '
|
|
|
|
'or from stdin if \'-\' (each host on separate line). '
|
|
|
|
'If no host or host file is specified then, by default, '
|
|
|
|
'list all.'), dest='hostfile', required=False)
|
|
|
|
parser['list'].add_argument(
|
2017-08-17 06:24:58 +00:00
|
|
|
'-H', '--host-only', help=('Suppress tags listing.'),
|
2017-07-20 20:04:44 +00:00
|
|
|
action="store_true", dest="list_only_host", default=False)
|
|
|
|
parser['list'].add_argument(
|
|
|
|
'-t', '--tag',
|
2017-08-17 06:24:58 +00:00
|
|
|
help=('Host is specified by tag, not hostname/address; '
|
|
|
|
'list all hosts that contain any of specified tags.'),
|
2017-07-20 20:04:44 +00:00
|
|
|
action="store_true", default=False)
|
|
|
|
|
|
|
|
parser['inventory'].set_defaults(
|
|
|
|
func=cdist.inventory.Inventory.commandline)
|
|
|
|
|
2019-12-05 22:14:27 +00:00
|
|
|
# PreOS
|
2019-09-20 05:15:37 +00:00
|
|
|
parser['preos'] = parser['sub'].add_parser('preos', add_help=False)
|
|
|
|
|
2016-12-03 17:24:37 +00:00
|
|
|
# Shell
|
|
|
|
parser['shell'] = parser['sub'].add_parser(
|
2020-06-01 17:11:58 +00:00
|
|
|
'shell', parents=[parser['loglevel'], parser['colored_output']])
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['shell'].add_argument(
|
|
|
|
'-s', '--shell',
|
|
|
|
help=('Select shell to use, defaults to current shell. Used shell'
|
|
|
|
' should be POSIX compatible shell.'))
|
|
|
|
parser['shell'].set_defaults(func=cdist.shell.Shell.commandline)
|
|
|
|
|
2019-12-30 08:32:15 +00:00
|
|
|
# Info
|
|
|
|
parser['info'] = parser['sub'].add_parser('info')
|
|
|
|
parser['info'].add_argument(
|
|
|
|
'-a', '--all', help='Display all info. This is the default.',
|
|
|
|
action='store_true', default=False)
|
|
|
|
parser['info'].add_argument(
|
|
|
|
'-c', '--conf-dir',
|
|
|
|
help='Add configuration directory (can be repeated).',
|
|
|
|
action='append')
|
|
|
|
parser['info'].add_argument(
|
|
|
|
'-e', '--global-explorers',
|
|
|
|
help='Display info for global explorers.', action='store_true',
|
|
|
|
default=False)
|
|
|
|
parser['info'].add_argument(
|
|
|
|
'-F', '--fixed-string',
|
|
|
|
help='Interpret pattern as a fixed string.', action='store_true',
|
|
|
|
default=False)
|
|
|
|
parser['info'].add_argument(
|
|
|
|
'-f', '--full', help='Display full details.',
|
|
|
|
action='store_true', default=False)
|
|
|
|
parser['info'].add_argument(
|
|
|
|
'-g', '--config-file',
|
|
|
|
help='Use specified custom configuration file.',
|
|
|
|
dest="config_file", required=False)
|
|
|
|
parser['info'].add_argument(
|
|
|
|
'-t', '--types', help='Display info for types.',
|
|
|
|
action='store_true', default=False)
|
|
|
|
parser['info'].add_argument(
|
|
|
|
'pattern', nargs='?', help='Glob pattern.')
|
|
|
|
parser['info'].set_defaults(func=cdist.info.Info.commandline)
|
|
|
|
|
2020-10-29 22:16:08 +00:00
|
|
|
# Scan = config + further
|
|
|
|
parser['scan'] = parser['sub'].add_parser('scan', add_help=False,
|
2020-11-03 05:43:57 +00:00
|
|
|
parents=[parser['config']])
|
2020-10-29 22:16:08 +00:00
|
|
|
|
|
|
|
parser['scan'] = parser['sub'].add_parser(
|
|
|
|
'scan', parents=[parser['loglevel'],
|
|
|
|
parser['beta'],
|
|
|
|
parser['colored_output'],
|
|
|
|
parser['common'],
|
|
|
|
parser['config_main']])
|
|
|
|
|
|
|
|
parser['scan'].add_argument(
|
|
|
|
'-m', '--mode', help='Which modes should run',
|
|
|
|
action='append', default=[],
|
|
|
|
choices=['scan', 'trigger'])
|
2021-04-22 08:20:49 +00:00
|
|
|
parser['scan'].add_argument(
|
|
|
|
'--list',
|
|
|
|
action='store_true',
|
|
|
|
help='List the known hosts and exit')
|
2020-10-29 22:16:08 +00:00
|
|
|
parser['scan'].add_argument(
|
|
|
|
'--config',
|
|
|
|
action='store_true',
|
|
|
|
help='Try to configure detected hosts')
|
|
|
|
parser['scan'].add_argument(
|
|
|
|
'-I', '--interfaces',
|
2021-04-22 07:29:53 +00:00
|
|
|
action='append', default=[], required=True,
|
2020-10-29 22:16:08 +00:00
|
|
|
help='On which interfaces to scan/trigger')
|
|
|
|
parser['scan'].add_argument(
|
|
|
|
'-d', '--delay',
|
2021-04-22 07:29:53 +00:00
|
|
|
action='store', default=3600, type=int,
|
|
|
|
help='How long (seconds) to wait before reconfiguring after last try')
|
|
|
|
parser['scan'].add_argument(
|
|
|
|
'-t', '--trigger-delay',
|
|
|
|
action='store', default=5, type=int,
|
|
|
|
help='How long (seconds) to wait between ICMPv6 echo requests')
|
2020-10-29 22:16:08 +00:00
|
|
|
parser['scan'].set_defaults(func=cdist.scan.commandline.commandline)
|
|
|
|
|
2016-12-03 17:24:37 +00:00
|
|
|
for p in parser:
|
|
|
|
parser[p].epilog = EPILOG
|
|
|
|
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
def handle_loglevel(args):
|
2018-03-10 10:12:31 +00:00
|
|
|
if hasattr(args, 'quiet') and args.quiet:
|
2017-06-28 08:18:53 +00:00
|
|
|
args.verbose = _verbosity_level_off
|
|
|
|
|
2020-06-03 19:45:04 +00:00
|
|
|
logging.getLogger().setLevel(_verbosity_level[args.verbose])
|
|
|
|
|
|
|
|
|
|
|
|
def handle_log_colors(args):
|
|
|
|
if cdist.configuration.ColoredOutputOption.translate(args.colored_output):
|
2020-06-11 12:16:37 +00:00
|
|
|
cdist.log.CdistFormatter.USE_COLORS = True
|
2017-08-30 21:02:17 +00:00
|
|
|
|
|
|
|
|
2017-09-01 12:08:50 +00:00
|
|
|
def parse_and_configure(argv, singleton=True):
|
2017-08-30 21:02:17 +00:00
|
|
|
parser = get_parsers()
|
|
|
|
parser_args = parser['main'].parse_args(argv)
|
2017-09-07 14:13:46 +00:00
|
|
|
try:
|
2017-09-07 14:37:49 +00:00
|
|
|
cfg = cdist.configuration.Configuration(parser_args,
|
|
|
|
singleton=singleton)
|
2017-09-07 14:13:46 +00:00
|
|
|
args = cfg.get_args()
|
|
|
|
except ValueError as e:
|
|
|
|
raise cdist.Error(str(e))
|
2017-08-30 21:02:17 +00:00
|
|
|
# Loglevels are handled globally in here
|
|
|
|
handle_loglevel(args)
|
2020-06-03 19:45:04 +00:00
|
|
|
handle_log_colors(args)
|
2017-08-30 21:02:17 +00:00
|
|
|
|
|
|
|
log = logging.getLogger("cdist")
|
|
|
|
|
2021-03-30 05:56:38 +00:00
|
|
|
log.verbose("version %s", cdist.VERSION)
|
|
|
|
log.trace('command line args: %s', cfg.command_line_args)
|
|
|
|
log.trace('configuration: %s', cfg.get_config())
|
|
|
|
log.trace('configured args: %s', args)
|
2017-08-30 21:02:17 +00:00
|
|
|
|
|
|
|
check_beta(vars(args))
|
|
|
|
|
|
|
|
return parser, cfg
|