def poll(self):
for host in six.viewvalues(self.hosts_to_ssh):
if host['ssh']['proc']:
host['ssh']['proc'].poll()
while (True):
try:
line = host['ssh']['proc'].stderr.readline()
except IOError:
break
if line:
logging.info("SSH {}: {}".format(
host['name'], line))
else:
break
# logging.info("%s %s",host, host['sshproc'].returncode if host['sshproc'] else '???')
if host['ssh']['proc'] is None or host['ssh']['proc'].returncode != None:
if host['ssh']['proc'] and host['ssh']['proc'].returncode != None:
log = logging.info if host['ssh']['proc'].returncode == 0 else logging.error
log("SSH:RC %s=%s",
host['sshcmd'],
host['ssh']['proc'].returncode)
host['ssh']['proc'] = None
# Only try to start once every 30 seconds
if (host['ssh'].get('last_connection') and
(datetime.datetime.now() - host[
'ssh']['last_connection']).seconds < 30):
continue
logging.info("SSH:START %s", host['sshcmd'])
try:
host['ssh']['proc'] = subprocess.Popen(
host['sshcmd'],
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=ON_POSIX)
host['ssh']['last_connection'] = datetime.datetime.now()
except Exception as ex:
logging.warning("SSH:START:EXC: %s", ex)
fl = fcntl.fcntl(host['ssh']['proc'].stderr, fcntl.F_GETFL)
fcntl.fcntl(host['ssh']['proc'].stderr, fcntl.F_SETFL,
fl | os.O_NONBLOCK)
评论列表
文章目录