2012-03-27 02:07:04 +00:00
|
|
|
#!/usr/bin/env python
|
2012-02-23 19:56:14 +00:00
|
|
|
|
2012-02-29 00:08:09 +00:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
2012-02-24 00:42:05 +00:00
|
|
|
#
|
2012-02-29 00:08:09 +00:00
|
|
|
# This file is part of Ansible
|
2012-02-24 00:42:05 +00:00
|
|
|
#
|
2012-02-29 00:08:09 +00:00
|
|
|
# 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.
|
2012-02-24 00:42:05 +00:00
|
|
|
#
|
2012-02-29 00:08:09 +00:00
|
|
|
# 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/>.
|
2012-02-24 00:42:05 +00:00
|
|
|
|
2012-03-03 02:11:43 +00:00
|
|
|
########################################################
|
2015-10-20 01:36:19 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
2015-05-04 02:47:26 +00:00
|
|
|
__metaclass__ = type
|
2012-03-03 02:11:43 +00:00
|
|
|
|
2014-12-16 17:20:11 +00:00
|
|
|
__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
|
|
|
|
|
2014-02-26 16:00:48 +00:00
|
|
|
import os
|
2012-02-28 09:00:31 +00:00
|
|
|
import sys
|
2015-07-07 18:31:15 +00:00
|
|
|
import traceback
|
2012-03-18 21:41:44 +00:00
|
|
|
|
2016-04-08 17:07:46 +00:00
|
|
|
# for debug
|
|
|
|
from multiprocessing import Lock
|
|
|
|
debug_lock = Lock()
|
|
|
|
|
2015-05-13 15:15:04 +00:00
|
|
|
from ansible.errors import AnsibleError, AnsibleOptionsError, AnsibleParserError
|
2015-05-04 02:47:26 +00:00
|
|
|
from ansible.utils.display import Display
|
2015-10-08 14:04:15 +00:00
|
|
|
from ansible.utils.unicode import to_unicode
|
2012-02-28 08:54:41 +00:00
|
|
|
|
2016-01-26 03:17:46 +00:00
|
|
|
|
2015-07-05 21:46:51 +00:00
|
|
|
########################################
|
|
|
|
### OUTPUT OF LAST RESORT ###
|
|
|
|
class LastResort(object):
|
2015-07-07 18:31:15 +00:00
|
|
|
def display(self, msg):
|
2015-07-05 21:46:51 +00:00
|
|
|
print(msg, file=sys.stderr)
|
|
|
|
|
2015-07-24 15:24:31 +00:00
|
|
|
def error(self, msg, wrap_text=None):
|
|
|
|
print(msg, file=sys.stderr)
|
|
|
|
|
2015-07-07 18:31:15 +00:00
|
|
|
|
2015-07-05 21:46:51 +00:00
|
|
|
########################################
|
2012-03-03 02:11:43 +00:00
|
|
|
|
2012-02-28 08:54:41 +00:00
|
|
|
if __name__ == '__main__':
|
2013-04-27 14:24:26 +00:00
|
|
|
|
2015-07-05 21:46:51 +00:00
|
|
|
display = LastResort()
|
2015-05-04 02:47:26 +00:00
|
|
|
cli = None
|
2015-06-09 21:29:46 +00:00
|
|
|
me = os.path.basename(sys.argv[0])
|
2015-05-04 02:47:26 +00:00
|
|
|
|
2012-03-13 03:11:54 +00:00
|
|
|
try:
|
2015-07-05 21:24:15 +00:00
|
|
|
display = Display()
|
2015-12-10 23:03:25 +00:00
|
|
|
display.debug("starting run")
|
2015-07-05 21:24:15 +00:00
|
|
|
|
2015-11-02 19:34:08 +00:00
|
|
|
sub = None
|
2015-11-02 17:46:04 +00:00
|
|
|
try:
|
2015-11-02 19:34:08 +00:00
|
|
|
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
|
2015-11-03 03:17:13 +00:00
|
|
|
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
|
|
|
|
|
2015-11-10 19:40:55 +00:00
|
|
|
cli = mycli(sys.argv)
|
2015-11-02 17:46:04 +00:00
|
|
|
cli.parse()
|
|
|
|
sys.exit(cli.run())
|
2012-03-14 00:59:05 +00:00
|
|
|
|
2015-05-04 02:47:26 +00:00
|
|
|
except AnsibleOptionsError as e:
|
|
|
|
cli.parser.print_help()
|
2015-10-08 14:04:15 +00:00
|
|
|
display.error(to_unicode(e), wrap_text=False)
|
2015-05-13 15:15:04 +00:00
|
|
|
sys.exit(5)
|
|
|
|
except AnsibleParserError as e:
|
2015-10-08 14:04:15 +00:00
|
|
|
display.error(to_unicode(e), wrap_text=False)
|
2015-05-13 15:15:04 +00:00
|
|
|
sys.exit(4)
|
|
|
|
# TQM takes care of these, but leaving comment to reserve the exit codes
|
|
|
|
# except AnsibleHostUnreachable as e:
|
2015-07-05 21:46:51 +00:00
|
|
|
# display.error(str(e))
|
2015-05-13 15:15:04 +00:00
|
|
|
# sys.exit(3)
|
|
|
|
# except AnsibleHostFailed as e:
|
2015-07-05 21:46:51 +00:00
|
|
|
# display.error(str(e))
|
2015-05-13 15:15:04 +00:00
|
|
|
# sys.exit(2)
|
2015-05-04 02:47:26 +00:00
|
|
|
except AnsibleError as e:
|
2015-10-08 14:04:15 +00:00
|
|
|
display.error(to_unicode(e), wrap_text=False)
|
2015-05-13 15:15:04 +00:00
|
|
|
sys.exit(1)
|
2015-05-04 02:47:26 +00:00
|
|
|
except KeyboardInterrupt:
|
2015-07-05 21:46:51 +00:00
|
|
|
display.error("User interrupted execution")
|
2015-05-13 15:15:04 +00:00
|
|
|
sys.exit(99)
|
2015-07-05 21:46:51 +00:00
|
|
|
except Exception as e:
|
2015-07-07 18:31:15 +00:00
|
|
|
have_cli_options = cli is not None and cli.options is not None
|
2015-10-08 14:04:15 +00:00
|
|
|
display.error("Unexpected Exception: %s" % to_unicode(e), wrap_text=False)
|
2015-08-06 14:02:40 +00:00
|
|
|
if not have_cli_options or have_cli_options and cli.options.verbosity > 2:
|
2016-01-26 03:17:46 +00:00
|
|
|
display.display(u"the full traceback was:\n\n%s" % to_unicode(traceback.format_exc()))
|
2015-07-07 18:31:15 +00:00
|
|
|
else:
|
|
|
|
display.display("to see the full traceback, use -vvv")
|
2015-08-06 14:02:40 +00:00
|
|
|
sys.exit(250)
|