01e51b12 |
#!/usr/bin/env python |
145a024d |
|
be4cb64c |
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com> |
9681e1fa |
# |
be4cb64c |
# This file is part of Ansible |
9681e1fa |
# |
be4cb64c |
# Ansible 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. |
9681e1fa |
# |
be4cb64c |
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>. |
9681e1fa |
|
bf967adf |
######################################################## |
f34b55ac |
from __future__ import (absolute_import, division, print_function) |
ce3ef7f4 |
__metaclass__ = type |
bf967adf |
|
fbadcfd4 |
__requires__ = ['ansible']
try:
import pkg_resources
except Exception:
# Use pkg_resources to find the correct versions of libraries and set
# sys.path appropriately when there are multiversion installs. But we
# have code that better expresses the errors in the places where the code
# is actually used (the deps are optional for many code paths) so we don't
# want to fail here.
pass
|
35def422 |
import os |
dcc5dfdf |
import shutil |
1427ade3 |
import sys |
6d50a261 |
import traceback |
fde7de80 |
|
0eb2844c |
# for debug
from multiprocessing import Lock
debug_lock = Lock()
|
dcc5dfdf |
import ansible.constants as C |
b85ce388 |
from ansible.errors import AnsibleError, AnsibleOptionsError, AnsibleParserError |
ce3ef7f4 |
from ansible.utils.display import Display |
de792ba3 |
from ansible.utils.unicode import to_unicode |
281f96b8 |
|
f4d68b88 |
|
f42b6237 |
########################################
### OUTPUT OF LAST RESORT ###
class LastResort(object): |
6d50a261 |
def display(self, msg): |
f42b6237 |
print(msg, file=sys.stderr)
|
e49575ff |
def error(self, msg, wrap_text=None):
print(msg, file=sys.stderr)
|
6d50a261 |
|
f42b6237 |
######################################## |
bf967adf |
|
281f96b8 |
if __name__ == '__main__': |
aa552685 |
|
f42b6237 |
display = LastResort() |
ce3ef7f4 |
cli = None |
fdeca372 |
me = os.path.basename(sys.argv[0]) |
ce3ef7f4 |
|
dfd2c6dc |
try: |
6a75125f |
display = Display() |
120b9a7a |
display.debug("starting run") |
6a75125f |
|
d0a22a7b |
sub = None |
56a2412d |
try: |
d0a22a7b |
if me.find('-') != -1:
target = me.split('-')
if len(target) > 1:
sub = target[1]
myclass = "%sCLI" % sub.capitalize()
mycli = getattr(__import__("ansible.cli.%s" % sub, fromlist=[myclass]), myclass)
elif me == 'ansible':
from ansible.cli.adhoc import AdHocCLI as mycli |
10fd717a |
else:
raise AnsibleError("Unknown Ansible alias: %s" % me)
except ImportError as e:
if e.message.endswith(' %s' % sub):
raise AnsibleError("Ansible sub-program not implemented: %s" % me)
else:
raise
|
318bfbb2 |
cli = mycli(sys.argv) |
56a2412d |
cli.parse() |
dcc5dfdf |
exit_code = cli.run() |
4ae98ed9 |
|
ce3ef7f4 |
except AnsibleOptionsError as e:
cli.parser.print_help() |
de792ba3 |
display.error(to_unicode(e), wrap_text=False) |
dcc5dfdf |
exit_code = 5 |
b85ce388 |
except AnsibleParserError as e: |
de792ba3 |
display.error(to_unicode(e), wrap_text=False) |
dcc5dfdf |
exit_code = 4 |
b85ce388 |
# TQM takes care of these, but leaving comment to reserve the exit codes
# except AnsibleHostUnreachable as e: |
f42b6237 |
# display.error(str(e)) |
dcc5dfdf |
# exit_code = 3 |
b85ce388 |
# except AnsibleHostFailed as e: |
f42b6237 |
# display.error(str(e)) |
dcc5dfdf |
# exit_code = 2 |
ce3ef7f4 |
except AnsibleError as e: |
de792ba3 |
display.error(to_unicode(e), wrap_text=False) |
dcc5dfdf |
exit_code = 1 |
ce3ef7f4 |
except KeyboardInterrupt: |
f42b6237 |
display.error("User interrupted execution") |
dcc5dfdf |
exit_code = 99 |
f42b6237 |
except Exception as e: |
6d50a261 |
have_cli_options = cli is not None and cli.options is not None |
de792ba3 |
display.error("Unexpected Exception: %s" % to_unicode(e), wrap_text=False) |
a5e6845c |
if not have_cli_options or have_cli_options and cli.options.verbosity > 2: |
f4d68b88 |
display.display(u"the full traceback was:\n\n%s" % to_unicode(traceback.format_exc())) |
6d50a261 |
else:
display.display("to see the full traceback, use -vvv") |
dcc5dfdf |
exit_code = 250
finally:
# Remove ansible tempdir
shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
sys.exit(exit_code) |