2012-10-25 21:37:15 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2016-12-03 17:24:37 +00:00
|
|
|
# 2010-2016 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-11-30 22:19:53 +00:00
|
|
|
import logging
|
2020-10-13 23:02:45 +00:00
|
|
|
import os
|
2017-09-09 19:17:29 +00:00
|
|
|
import sys
|
2020-10-13 23:02:45 +00:00
|
|
|
|
2020-11-14 09:13:58 +00:00
|
|
|
# See if this file's parent is cdist module
|
|
|
|
# and if so add it to module search path.
|
|
|
|
cdist_dir = os.path.realpath(
|
|
|
|
os.path.join(
|
|
|
|
os.path.dirname(os.path.realpath(__file__)),
|
|
|
|
os.pardir))
|
|
|
|
cdist_init_dir = os.path.join(cdist_dir, 'cdist', '__init__.py')
|
|
|
|
if os.path.exists(cdist_init_dir):
|
2020-10-16 10:42:16 +00:00
|
|
|
sys.path.insert(0, cdist_dir)
|
2020-11-14 09:13:58 +00:00
|
|
|
|
|
|
|
import cdist # noqa 402
|
|
|
|
import cdist.argparse # noqa 402
|
|
|
|
import cdist.banner # noqa 402
|
|
|
|
import cdist.config # noqa 402
|
|
|
|
import cdist.install # noqa 402
|
|
|
|
import cdist.shell # noqa 402
|
|
|
|
import cdist.inventory # noqa 402
|
2016-03-10 19:20:41 +00:00
|
|
|
|
2016-11-30 22:19:53 +00:00
|
|
|
|
2012-10-25 21:37:15 +00:00
|
|
|
def commandline():
|
|
|
|
"""Parse command line"""
|
|
|
|
|
2019-09-20 05:15:37 +00:00
|
|
|
# preos subcommand hack
|
|
|
|
if len(sys.argv) > 1 and sys.argv[1] == 'preos':
|
|
|
|
return cdist.preos.PreOS.commandline(sys.argv[1:])
|
2017-08-30 21:02:17 +00:00
|
|
|
parser, cfg = cdist.argparse.parse_and_configure(sys.argv[1:])
|
|
|
|
args = cfg.get_args()
|
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
|
|
|
|
2017-08-22 08:58:30 +00:00
|
|
|
|
2012-10-25 21:37:15 +00:00
|
|
|
if __name__ == "__main__":
|
2021-04-17 07:57:10 +00:00
|
|
|
if sys.version_info[:3] < cdist.MIN_SUPPORTED_PYTHON_VERSION:
|
|
|
|
print(
|
|
|
|
'Python >= {} is required on the source host.'.format(
|
|
|
|
".".join(map(str, cdist.MIN_SUPPORTED_PYTHON_VERSION))),
|
|
|
|
file=sys.stderr)
|
2012-10-25 21:37:15 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
exit_code = 0
|
|
|
|
|
|
|
|
try:
|
|
|
|
import re
|
2017-08-30 21:02:17 +00:00
|
|
|
import os
|
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:
|
2018-09-23 19:38:03 +00:00
|
|
|
log = logging.getLogger("cdist")
|
2012-10-25 21:37:15 +00:00
|
|
|
log.error(e)
|
|
|
|
exit_code = 1
|
|
|
|
|
|
|
|
sys.exit(exit_code)
|