2016-12-03 17:24:37 +00:00
|
|
|
import argparse
|
|
|
|
import cdist
|
|
|
|
import multiprocessing
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import collections
|
|
|
|
|
|
|
|
|
2016-12-07 17:36:19 +00:00
|
|
|
# set of beta sub-commands
|
|
|
|
BETA_COMMANDS = set(('install', ))
|
|
|
|
# set of beta arguments for sub-commands
|
2016-12-03 17:24:37 +00:00
|
|
|
BETA_ARGS = {
|
2016-12-07 17:36:19 +00:00
|
|
|
'config': set(('jobs', )),
|
2016-12-03 17:24:37 +00:00
|
|
|
}
|
|
|
|
EPILOG = "Get cdist at http://www.nico.schottelius.org/software/cdist/"
|
|
|
|
# Parser others can reuse
|
|
|
|
parser = None
|
|
|
|
|
|
|
|
|
2017-06-28 08:18:53 +00:00
|
|
|
_verbosity_level_off = -2
|
|
|
|
_verbosity_level_debug = 3
|
2016-12-03 17:24:37 +00:00
|
|
|
_verbosity_level = {
|
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-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)
|
|
|
|
|
|
|
|
|
|
|
|
def check_positive_int(value):
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
try:
|
|
|
|
val = int(value)
|
|
|
|
except ValueError:
|
|
|
|
raise argparse.ArgumentTypeError(
|
|
|
|
"{} is invalid int value".format(value))
|
|
|
|
if val <= 0:
|
|
|
|
raise argparse.ArgumentTypeError(
|
|
|
|
"{} is invalid positive int value".format(val))
|
|
|
|
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)
|
|
|
|
parser['loglevel'].add_argument(
|
|
|
|
'-d', '--debug',
|
|
|
|
help=('Set log level to debug (deprecated, use -vvv instead)'),
|
|
|
|
action='store_true', default=False)
|
2017-06-28 08:18:53 +00:00
|
|
|
parser['loglevel'].add_argument(
|
|
|
|
'-q', '--quiet',
|
|
|
|
help='Quiet mode: disables logging, including WARNING and ERROR',
|
|
|
|
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) '
|
|
|
|
'TRACE (4 or higher).'),
|
2016-12-03 17:24:37 +00:00
|
|
|
action='count', default=0)
|
|
|
|
|
|
|
|
parser['beta'] = argparse.ArgumentParser(add_help=False)
|
|
|
|
parser['beta'].add_argument(
|
|
|
|
'-b', '--beta',
|
2017-07-01 07:28:41 +00:00
|
|
|
help=('Enable beta functionality. '
|
2016-12-03 17:24:37 +00:00
|
|
|
'Can also be enabled using CDIST_BETA env var.'),
|
|
|
|
action='store_true', dest='beta',
|
|
|
|
default='CDIST_BETA' in os.environ)
|
|
|
|
|
|
|
|
# Main subcommand parser
|
|
|
|
parser['main'] = argparse.ArgumentParser(
|
|
|
|
description='cdist ' + cdist.VERSION, parents=[parser['loglevel']])
|
|
|
|
parser['main'].add_argument(
|
|
|
|
'-V', '--version', help='Show version', action='version',
|
|
|
|
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)
|
|
|
|
|
|
|
|
# Config
|
|
|
|
parser['config_main'] = argparse.ArgumentParser(add_help=False)
|
2017-07-01 21:59:51 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-C', '--cache-path-pattern',
|
|
|
|
help=('Specify custom cache path pattern. It can also be set '
|
|
|
|
'by CDIST_CACHE_PATH_PATTERN environment variable. If '
|
|
|
|
'it is not set then default hostdir is used.'),
|
|
|
|
dest='cache_path_pattern',
|
|
|
|
default=os.environ.get('CDIST_CACHE_PATH_PATTERN'))
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-c', '--conf-dir',
|
|
|
|
help=('Add configuration directory (can be repeated, '
|
|
|
|
'last one wins)'), action='append')
|
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-i', '--initial-manifest',
|
|
|
|
help='path to a cdist manifest or \'-\' to read from stdin.',
|
|
|
|
dest='manifest', required=False)
|
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-j', '--jobs', nargs='?',
|
|
|
|
type=check_positive_int,
|
2017-06-13 20:54:40 +00:00
|
|
|
help=('Specify the maximum number of parallel jobs. Global'
|
|
|
|
'explorers, object prepare and object run are supported'
|
|
|
|
'(currently in beta'),
|
2016-12-03 17:24:37 +00:00
|
|
|
action='store', dest='jobs',
|
|
|
|
const=multiprocessing.cpu_count())
|
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-n', '--dry-run',
|
|
|
|
help='do not execute code', action='store_true')
|
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'-o', '--out-dir',
|
|
|
|
help='directory to save cdist output in', dest="out_path")
|
|
|
|
|
|
|
|
# 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
|
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'--remote-copy',
|
|
|
|
help='Command to use for remote copy (should behave like scp)',
|
|
|
|
action='store', dest='remote_copy',
|
|
|
|
default=os.environ.get('CDIST_REMOTE_COPY'))
|
|
|
|
parser['config_main'].add_argument(
|
|
|
|
'--remote-exec',
|
|
|
|
help=('Command to use for remote execution '
|
|
|
|
'(should behave like ssh)'),
|
|
|
|
action='store', dest='remote_exec',
|
|
|
|
default=os.environ.get('CDIST_REMOTE_EXEC'))
|
|
|
|
|
|
|
|
# Config
|
|
|
|
parser['config_args'] = argparse.ArgumentParser(add_help=False)
|
|
|
|
parser['config_args'].add_argument(
|
|
|
|
'host', nargs='*', help='host(s) to operate on')
|
|
|
|
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 '
|
|
|
|
'line). If no host or host file is specified then, by '
|
|
|
|
'default, read hosts from stdin.'),
|
2016-12-03 17:24:37 +00:00
|
|
|
dest='hostfile', required=False)
|
|
|
|
parser['config_args'].add_argument(
|
|
|
|
'-p', '--parallel',
|
|
|
|
help='operate on multiple hosts in parallel',
|
|
|
|
action='store_true', dest='parallel')
|
2017-01-20 14:34:17 +00:00
|
|
|
parser['config_args'].add_argument(
|
|
|
|
'-r', '--remote-out-dir',
|
|
|
|
help='Directory to save cdist output in on the target host',
|
|
|
|
dest="remote_out_path")
|
2017-06-27 15:47:30 +00:00
|
|
|
parser['config_args'].add_argument(
|
|
|
|
'-s', '--sequential',
|
|
|
|
help='operate on multiple hosts sequentially (default)',
|
|
|
|
action='store_false', dest='parallel')
|
2016-12-03 17:24:37 +00:00
|
|
|
parser['config'] = parser['sub'].add_parser(
|
|
|
|
'config', parents=[parser['loglevel'], parser['beta'],
|
|
|
|
parser['config_main'],
|
|
|
|
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)
|
|
|
|
|
|
|
|
# Shell
|
|
|
|
parser['shell'] = parser['sub'].add_parser(
|
|
|
|
'shell', parents=[parser['loglevel']])
|
|
|
|
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)
|
|
|
|
|
|
|
|
for p in parser:
|
|
|
|
parser[p].epilog = EPILOG
|
|
|
|
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
def handle_loglevel(args):
|
|
|
|
if args.debug:
|
|
|
|
retval = "-d/--debug is deprecated, use -vvv instead"
|
2017-06-28 08:18:53 +00:00
|
|
|
args.verbose = _verbosity_level_debug
|
2016-12-03 17:24:37 +00:00
|
|
|
else:
|
|
|
|
retval = None
|
|
|
|
|
2017-06-28 08:18:53 +00:00
|
|
|
if args.quiet:
|
|
|
|
args.verbose = _verbosity_level_off
|
|
|
|
|
2016-12-03 17:24:37 +00:00
|
|
|
logging.root.setLevel(_verbosity_level[args.verbose])
|
|
|
|
|
|
|
|
return retval
|