more logging and exception handling in ansible-connection (#20619)
adds more logging to handle display being called from plugins. Also rearranges some of the exception handling to better catch exceptions and log to local syslogpull/4420/head
parent
d8b18d79fa
commit
6673673042
|
@ -49,6 +49,8 @@ from ansible.module_utils.six.moves import cPickle, StringIO
|
|||
from ansible.playbook.play_context import PlayContext
|
||||
from ansible.plugins import connection_loader
|
||||
from ansible.utils.path import unfrackpath, makedirs_safe
|
||||
from ansible.errors import AnsibleConnectionFailure
|
||||
from ansible.utils.display import Display
|
||||
|
||||
|
||||
def do_fork():
|
||||
|
@ -101,9 +103,9 @@ def recv_data(s):
|
|||
data += d
|
||||
return data
|
||||
|
||||
def log(msg, host=None):
|
||||
def log(msg, host=None, **kwargs):
|
||||
if host:
|
||||
msg = '[%s] %s' % (host, msg)
|
||||
msg = '<%s> %s' % (host, msg)
|
||||
facility = getattr(syslog, C.DEFAULT_SYSLOG_FACILITY, syslog.LOG_USER)
|
||||
syslog.openlog('ansible-connection', 0, facility)
|
||||
syslog.syslog(syslog.LOG_INFO, str(msg))
|
||||
|
@ -117,17 +119,14 @@ class Server():
|
|||
|
||||
self._start_time = datetime.datetime.now()
|
||||
|
||||
try:
|
||||
self.log('loading connection plugin %s' % str(play_context.connection))
|
||||
self.conn = connection_loader.get(play_context.connection, play_context, sys.stdin)
|
||||
self.conn._connect()
|
||||
self.conn = connection_loader.get(play_context.connection, play_context, sys.stdin)
|
||||
rc, out, err = self.conn._connect()
|
||||
if rc != 0:
|
||||
raise AnsibleConnectionFailure(err)
|
||||
|
||||
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
self.socket.bind(path)
|
||||
self.socket.listen(1)
|
||||
except Exception as exc:
|
||||
self.log(traceback.format_exc())
|
||||
raise
|
||||
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
self.socket.bind(path)
|
||||
self.socket.listen(1)
|
||||
|
||||
signal.signal(signal.SIGALRM, self.alarm_handler)
|
||||
|
||||
|
@ -137,7 +136,7 @@ class Server():
|
|||
return meth(*args, **kwargs)
|
||||
|
||||
def log(self, msg):
|
||||
log(msg, self.play_context.remote_addr)
|
||||
log(msg, host=self.play_context.remote_addr)
|
||||
|
||||
def alarm_handler(self, signum, frame):
|
||||
'''
|
||||
|
@ -259,6 +258,8 @@ def main():
|
|||
print(traceback.format_exc())
|
||||
sys.exit(1)
|
||||
|
||||
display.verbosity = pc.verbosity
|
||||
|
||||
# here we create a hash to use later when creating the socket file,
|
||||
# so we can hide the info about the target host/user/etc.
|
||||
m = hashlib.sha256()
|
||||
|
@ -280,7 +281,12 @@ def main():
|
|||
if not os.path.exists(sf_path):
|
||||
pid = do_fork()
|
||||
if pid == 0:
|
||||
server = Server(sf_path, pc)
|
||||
try:
|
||||
server = Server(sf_path, pc)
|
||||
except AnsibleConnectionFailure as exc:
|
||||
log(str(exc), pc.remote_addr)
|
||||
except Exception as exc:
|
||||
log(traceback.format_exc(), pc.remote_addr)
|
||||
fcntl.lockf(lock_fd, fcntl.LOCK_UN)
|
||||
os.close(lock_fd)
|
||||
server.run()
|
||||
|
@ -334,4 +340,6 @@ def main():
|
|||
sys.exit(rc)
|
||||
|
||||
if __name__ == '__main__':
|
||||
display = Display()
|
||||
display.display = log
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue