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