2012-10-25 21:37:15 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2013-08-27 10:46:00 +00:00
|
|
|
# 2010-2013 Nico Schottelius (nico-cdist at schottelius.org)
|
2016-03-10 19:20:41 +00:00
|
|
|
# 2016 Darko Poljak (darko.poljak at gmail.com)
|
2012-10-25 21:37:15 +00:00
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2016-03-18 22:34:02 +00:00
|
|
|
def inspect_ssh_mux_opts(control_path_dir="~/.ssh/"):
|
|
|
|
"""Inspect whether or not ssh supports multiplexing options"""
|
2016-03-10 19:40:37 +00:00
|
|
|
import subprocess
|
2016-03-18 22:34:02 +00:00
|
|
|
import os
|
2016-03-10 19:40:37 +00:00
|
|
|
|
2016-04-01 08:42:32 +00:00
|
|
|
control_path = os.path.join(control_path_dir,
|
|
|
|
"cdist.socket.master-%l-%r@%h:%p")
|
2016-03-10 19:20:41 +00:00
|
|
|
wanted_mux_opts = {
|
2016-03-18 22:34:02 +00:00
|
|
|
"ControlPath": control_path,
|
|
|
|
"ControlMaster": "auto",
|
|
|
|
"ControlPersist": "125",
|
2016-03-10 19:20:41 +00:00
|
|
|
}
|
2016-03-18 22:34:02 +00:00
|
|
|
mux_opts = " ".join([" -o {}={}".format(x,
|
|
|
|
wanted_mux_opts[x]) for x in wanted_mux_opts])
|
|
|
|
try:
|
|
|
|
subprocess.check_output("ssh {}".format(mux_opts),
|
|
|
|
stderr=subprocess.STDOUT, shell=True)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
subproc_output = e.output.decode().lower()
|
|
|
|
if "bad configuration option" in subproc_output:
|
|
|
|
return ""
|
|
|
|
return mux_opts
|
2016-03-10 19:20:41 +00:00
|
|
|
|
2012-10-25 21:37:15 +00:00
|
|
|
def commandline():
|
|
|
|
"""Parse command line"""
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
import cdist.banner
|
|
|
|
import cdist.config
|
2013-08-07 07:24:10 +00:00
|
|
|
import cdist.shell
|
2016-03-18 22:34:02 +00:00
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
import os
|
2012-10-25 21:37:15 +00:00
|
|
|
|
|
|
|
# Construct parser others can reuse
|
|
|
|
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', action='store_true',
|
|
|
|
default=False)
|
|
|
|
parser['loglevel'].add_argument('-v', '--verbose',
|
|
|
|
help='Set log level to info, be more verbose',
|
|
|
|
action='store_true', default=False)
|
|
|
|
|
|
|
|
# Main subcommand parser
|
2016-05-22 07:22:39 +00:00
|
|
|
parser['main'] = argparse.ArgumentParser(description='cdist '
|
|
|
|
+ cdist.VERSION,
|
2012-10-25 21:37:15 +00:00
|
|
|
parents=[parser['loglevel']])
|
|
|
|
parser['main'].add_argument('-V', '--version',
|
|
|
|
help='Show version', action='version',
|
|
|
|
version='%(prog)s ' + cdist.VERSION)
|
2016-05-22 07:22:39 +00:00
|
|
|
parser['sub'] = parser['main'].add_subparsers(title="Commands",
|
|
|
|
dest="command")
|
2012-10-25 21:37:15 +00:00
|
|
|
|
|
|
|
# Banner
|
|
|
|
parser['banner'] = parser['sub'].add_parser('banner',
|
|
|
|
parents=[parser['loglevel']])
|
|
|
|
parser['banner'].set_defaults(func=cdist.banner.banner)
|
|
|
|
|
2013-08-29 19:56:53 +00:00
|
|
|
# Config
|
|
|
|
parser['config'] = parser['sub'].add_parser('config',
|
|
|
|
parents=[parser['loglevel']])
|
2016-05-22 07:22:39 +00:00
|
|
|
parser['config'].add_argument('host', nargs='*',
|
2016-05-25 05:25:21 +00:00
|
|
|
help='host(s) to operate on')
|
2013-08-29 19:56:53 +00:00
|
|
|
parser['config'].add_argument('-c', '--conf-dir',
|
2016-05-22 07:22:39 +00:00
|
|
|
help=('Add configuration directory (can be repeated, '
|
|
|
|
'last one wins)'), action='append')
|
|
|
|
parser['config'].add_argument('-f', '--file',
|
2016-05-22 07:45:08 +00:00
|
|
|
help=('Read additional hosts to operate on from specified file '
|
|
|
|
'or from stdin if \'-\' (each host on separate line). '
|
|
|
|
'If no host or host file is specified then, by default, '
|
|
|
|
'read hosts from stdin.'),
|
2016-05-22 07:22:39 +00:00
|
|
|
dest='hostfile', required=False)
|
2013-08-29 19:56:53 +00:00
|
|
|
parser['config'].add_argument('-i', '--initial-manifest',
|
2012-10-25 21:37:15 +00:00
|
|
|
help='Path to a cdist manifest or \'-\' to read from stdin.',
|
|
|
|
dest='manifest', required=False)
|
2013-08-29 19:56:53 +00:00
|
|
|
parser['config'].add_argument('-n', '--dry-run',
|
2013-05-02 14:41:16 +00:00
|
|
|
help='Do not execute code', action='store_true')
|
2013-08-29 19:56:53 +00:00
|
|
|
parser['config'].add_argument('-o', '--out-dir',
|
2013-08-28 14:46:35 +00:00
|
|
|
help='Directory to save cdist output in', dest="out_path")
|
2013-08-29 19:56:53 +00:00
|
|
|
parser['config'].add_argument('-p', '--parallel',
|
2012-10-25 21:37:15 +00:00
|
|
|
help='Operate on multiple hosts in parallel',
|
|
|
|
action='store_true', dest='parallel')
|
2013-08-29 19:56:53 +00:00
|
|
|
parser['config'].add_argument('-s', '--sequential',
|
2012-10-25 21:37:15 +00:00
|
|
|
help='Operate on multiple hosts sequentially (default)',
|
|
|
|
action='store_false', dest='parallel')
|
2016-03-22 07:41:51 +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
|
2013-08-29 19:56:53 +00:00
|
|
|
parser['config'].add_argument('--remote-copy',
|
2012-10-25 21:37:15 +00:00
|
|
|
help='Command to use for remote copy (should behave like scp)',
|
|
|
|
action='store', dest='remote_copy',
|
2016-03-22 07:41:51 +00:00
|
|
|
default=os.environ.get('CDIST_REMOTE_COPY'))
|
2013-08-29 19:56:53 +00:00
|
|
|
parser['config'].add_argument('--remote-exec',
|
2016-05-22 07:22:39 +00:00
|
|
|
help=('Command to use for remote execution '
|
|
|
|
'(should behave like ssh)'),
|
2012-10-25 21:37:15 +00:00
|
|
|
action='store', dest='remote_exec',
|
2016-03-22 07:41:51 +00:00
|
|
|
default=os.environ.get('CDIST_REMOTE_EXEC'))
|
2013-08-07 07:24:10 +00:00
|
|
|
parser['config'].set_defaults(func=cdist.config.Config.commandline)
|
|
|
|
|
|
|
|
# Shell
|
|
|
|
parser['shell'] = parser['sub'].add_parser('shell',
|
|
|
|
parents=[parser['loglevel']])
|
2013-08-07 15:52:34 +00:00
|
|
|
parser['shell'].add_argument('-s', '--shell',
|
|
|
|
help='Select shell to use, defaults to current shell')
|
2013-08-07 07:24:10 +00:00
|
|
|
parser['shell'].set_defaults(func=cdist.shell.Shell.commandline)
|
|
|
|
|
2012-10-25 21:37:15 +00:00
|
|
|
|
|
|
|
for p in parser:
|
|
|
|
parser[p].epilog = "Get cdist at http://www.nico.schottelius.org/software/cdist/"
|
|
|
|
|
|
|
|
args = parser['main'].parse_args(sys.argv[1:])
|
|
|
|
|
|
|
|
# Loglevels are handled globally in here and debug wins over verbose
|
|
|
|
if args.verbose:
|
|
|
|
logging.root.setLevel(logging.INFO)
|
|
|
|
if args.debug:
|
|
|
|
logging.root.setLevel(logging.DEBUG)
|
2016-03-22 07:41:51 +00:00
|
|
|
args_dict = vars(args)
|
|
|
|
# if command with remote_copy and remote_exec params
|
|
|
|
if 'remote_copy' in args_dict and 'remote_exec' in args_dict:
|
|
|
|
# if remote-exec and/or remote-copy args are None then user
|
|
|
|
# didn't specify command line options nor env vars:
|
|
|
|
# inspect multiplexing options for default cdist.REMOTE_COPY/EXEC
|
|
|
|
if args_dict['remote_copy'] is None or args_dict['remote_exec'] is None:
|
2016-04-01 08:42:32 +00:00
|
|
|
control_path_dir = tempfile.mkdtemp()
|
|
|
|
import atexit
|
|
|
|
atexit.register(lambda: shutil.rmtree(control_path_dir))
|
2016-03-22 07:41:51 +00:00
|
|
|
mux_opts = inspect_ssh_mux_opts(control_path_dir)
|
|
|
|
if args_dict['remote_exec'] is None:
|
|
|
|
args.remote_exec = cdist.REMOTE_EXEC + mux_opts
|
|
|
|
if args_dict['remote_copy'] is None:
|
|
|
|
args.remote_copy = cdist.REMOTE_COPY + mux_opts
|
2012-10-25 21:37:15 +00:00
|
|
|
|
2016-05-22 07:22:39 +00:00
|
|
|
if args.command == 'config':
|
|
|
|
if args.manifest == '-' and args.hostfile == '-':
|
|
|
|
print('cdist config: error: cannot read both, manifest and host file, from stdin')
|
|
|
|
sys.exit(1)
|
|
|
|
|
2012-10-25 21:37:15 +00:00
|
|
|
log.debug(args)
|
2013-08-21 16:52:35 +00:00
|
|
|
log.info("version %s" % cdist.VERSION)
|
Remove ugly argumentparser bug
Before:
[21:09] bento:~% cdist
Traceback (most recent call last):
File "/home/users/nico/p/cdist/cdist/bin/../scripts/cdist", line 232, in <module>
commandline()
File "/home/users/nico/p/cdist/cdist/bin/../scripts/cdist", line 106, in commandline
args.func(args)
AttributeError: 'Namespace' object has no attribute 'func'
After:
[21:11] bento:~% cdist
usage: cdist [-h] [-d] [-v] [-V] {banner,config} ...
cdist 2.1.1-48-gfd72c60
optional arguments:
-h, --help show this help message and exit
-d, --debug Set log level to debug
-v, --verbose Set log level to info, be more verbose
-V, --version Show version
Commands:
{banner,config}
Get cdist at http://www.nico.schottelius.org/software/cdist/
[21:11] bento:~%
Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
2013-06-07 19:10:57 +00:00
|
|
|
|
|
|
|
# Work around python 3.3 bug:
|
|
|
|
# http://bugs.python.org/issue16308
|
|
|
|
# http://bugs.python.org/issue9253
|
2013-08-07 16:49:47 +00:00
|
|
|
|
|
|
|
# FIXME: catching AttributeError also hides
|
|
|
|
# real problems.. try a different way
|
|
|
|
|
|
|
|
# FIXME: we always print main help, not
|
|
|
|
# the help of the actual parser being used!
|
Remove ugly argumentparser bug
Before:
[21:09] bento:~% cdist
Traceback (most recent call last):
File "/home/users/nico/p/cdist/cdist/bin/../scripts/cdist", line 232, in <module>
commandline()
File "/home/users/nico/p/cdist/cdist/bin/../scripts/cdist", line 106, in commandline
args.func(args)
AttributeError: 'Namespace' object has no attribute 'func'
After:
[21:11] bento:~% cdist
usage: cdist [-h] [-d] [-v] [-V] {banner,config} ...
cdist 2.1.1-48-gfd72c60
optional arguments:
-h, --help show this help message and exit
-d, --debug Set log level to debug
-v, --verbose Set log level to info, be more verbose
-V, --version Show version
Commands:
{banner,config}
Get cdist at http://www.nico.schottelius.org/software/cdist/
[21:11] bento:~%
Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
2013-06-07 19:10:57 +00:00
|
|
|
try:
|
2013-08-07 16:49:47 +00:00
|
|
|
getattr(args, "func")
|
Remove ugly argumentparser bug
Before:
[21:09] bento:~% cdist
Traceback (most recent call last):
File "/home/users/nico/p/cdist/cdist/bin/../scripts/cdist", line 232, in <module>
commandline()
File "/home/users/nico/p/cdist/cdist/bin/../scripts/cdist", line 106, in commandline
args.func(args)
AttributeError: 'Namespace' object has no attribute 'func'
After:
[21:11] bento:~% cdist
usage: cdist [-h] [-d] [-v] [-V] {banner,config} ...
cdist 2.1.1-48-gfd72c60
optional arguments:
-h, --help show this help message and exit
-d, --debug Set log level to debug
-v, --verbose Set log level to info, be more verbose
-V, --version Show version
Commands:
{banner,config}
Get cdist at http://www.nico.schottelius.org/software/cdist/
[21:11] bento:~%
Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
2013-06-07 19:10:57 +00:00
|
|
|
except AttributeError:
|
|
|
|
parser['main'].print_help()
|
2013-08-07 16:49:47 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
args.func(args)
|
2012-10-25 21:37:15 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# Sys is needed for sys.exit()
|
|
|
|
import sys
|
|
|
|
|
|
|
|
cdistpythonversion = '3.2'
|
|
|
|
if sys.version < cdistpythonversion:
|
2013-09-02 09:31:45 +00:00
|
|
|
print('Python >= ' + cdistpythonversion +
|
|
|
|
' is required on the source host.', file=sys.stderr)
|
2012-10-25 21:37:15 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
exit_code = 0
|
|
|
|
|
|
|
|
try:
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import cdist
|
2013-08-18 23:38:28 +00:00
|
|
|
import cdist.log
|
2012-10-25 21:37:15 +00:00
|
|
|
|
2013-08-18 23:38:28 +00:00
|
|
|
logging.setLoggerClass(cdist.log.Log)
|
2012-10-25 21:37:15 +00:00
|
|
|
logging.basicConfig(format='%(levelname)s: %(message)s')
|
2013-08-18 23:38:28 +00:00
|
|
|
log = logging.getLogger("cdist")
|
2012-10-25 21:37:15 +00:00
|
|
|
|
|
|
|
if re.match("__", os.path.basename(sys.argv[0])):
|
2013-09-02 08:49:11 +00:00
|
|
|
import cdist.emulator
|
|
|
|
emulator = cdist.emulator.Emulator(sys.argv)
|
|
|
|
emulator.run()
|
2012-10-25 21:37:15 +00:00
|
|
|
else:
|
|
|
|
commandline()
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
2013-08-21 16:52:35 +00:00
|
|
|
exit_code = 2
|
2012-10-25 21:37:15 +00:00
|
|
|
|
|
|
|
except cdist.Error as e:
|
|
|
|
log.error(e)
|
|
|
|
exit_code = 1
|
|
|
|
|
|
|
|
sys.exit(exit_code)
|