def log(msg, level = logging.DEBUG, out = None, err = None):
if out:
- msg += " - OUT: %s " % out
+ msg += " - OUT: {} ".format(out)
if err:
- msg += " - ERROR: %s " % err
+ msg += " - ERROR: {} ".format(err)
logger.log(level, msg)
if hasattr(os, "devnull"):
extras = {} if PY2 else {'universal_newlines' : True}
p = subprocess.Popen(
"ip -o addr list",
- shell=True,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
+ shell = True,
+ stdout = subprocess.PIPE,
+ stderr = subprocess.PIPE,
**extras
)
stdout, stderr = p.communicate()
hostbyname = resolve_hostname(host)
hostbyname_cache[host] = hostbyname
- msg = " Added hostbyname %s - %s " % (host, hostbyname)
+ msg = " Added hostbyname {} - {} ".format(host, hostbyname)
log(msg, logging.DEBUG)
return hostbyname
stdin = null,
**extras
)
- out,err = proc.communicate()
+ out, err = proc.communicate()
proc.wait()
vre = re.compile(r'OpenSSH_(?:[6-9]|5[.][8-9]|5[.][1-9][0-9]|[1-9][0-9]).*', re.I)
"""
if port is not None:
- host = '%s:%s' % (host, str(port))
+ host = '{}:{}'.format(host, str(port))
# Create a temporary server key file
tmp_known_hosts = tempfile.NamedTemporaryFile()
hostbyname = gethostbyname(host)
# Add the intended host key
- tmp_known_hosts.write('%s,%s %s\n' % (host, hostbyname, server_key))
+ tmp_known_hosts.write('{},{} {}\n'.format(host, hostbyname, server_key))
# If we're not in strict mode, add user-configured keys
- if os.environ.get('NEPI_STRICT_AUTH_MODE',"").lower() not in ('1','true','on'):
- user_hosts_path = '%s/.ssh/known_hosts' % (os.environ.get('HOME',""),)
+ if os.environ.get('NEPI_STRICT_AUTH_MODE', "").lower() not in ('1', 'true', 'on'):
+ user_hosts_path = '{}/.ssh/known_hosts'.format(os.environ.get('HOME', ""))
if os.access(user_hosts_path, os.R_OK):
with open(user_hosts_path, "r") as f:
tmp_known_hosts.write(f.read())
ctrl_path = "/tmp/nepi_ssh"
if agent:
- ctrl_path +="_a"
+ ctrl_path += "_a"
if forward_x11:
- ctrl_path +="_x"
+ ctrl_path += "_x"
ctrl_path += "-%r@%h:%p"
else:
# unsafe string - escape
def escape(c):
- if (32 <= ord(c) < 127 or c in ('\r','\n','\t')) and c not in ("'",'"'):
+ if (32 <= ord(c) < 127 or c in ('\r', '\n', '\t')) and c not in ("'", '"'):
return c
else:
- return "'$'\\x%02x''" % (ord(c),)
+ return "'$'\\x{:02x}''".format(ord(c))
s = ''.join(map(escape, s))
- return "'%s'" % (s,)
+ return "'{}'".format(s)
def eintr_retry(func):
"""Retries a function invocation when a EINTR occurs"""
return rv
def rexec(command, host, user,
- port = None,
- gwuser = None,
- gw = None,
- agent = True,
- sudo = False,
- identity = None,
- server_key = None,
- env = None,
- tty = False,
- connect_timeout = 30,
- retry = 3,
- persistent = True,
- forward_x11 = False,
- blocking = True,
- strict_host_checking = True):
+ port = None,
+ gwuser = None,
+ gw = None,
+ agent = True,
+ sudo = False,
+ identity = None,
+ server_key = None,
+ env = None,
+ tty = False,
+ connect_timeout = 30,
+ retry = 3,
+ persistent = True,
+ forward_x11 = False,
+ blocking = True,
+ strict_host_checking = True):
"""
- Executes a remote command, returns ((stdout,stderr),process)
+ Executes a remote command, returns ((stdout, stderr), process)
"""
tmp_known_hosts = None
args = ['ssh', '-C',
# Don't bother with localhost. Makes test easier
'-o', 'NoHostAuthenticationForLocalhost=yes',
- '-o', 'ConnectTimeout=%d' % (int(connect_timeout),),
+ '-o', 'ConnectTimeout={}'.format(connect_timeout),
'-o', 'ConnectionAttempts=3',
'-o', 'ServerAliveInterval=30',
'-o', 'TCPKeepAlive=yes',
if persistent and openssh_has_persist():
args.extend([
'-o', 'ControlMaster=auto',
- '-o', 'ControlPath=%s' % (make_control_path(agent, forward_x11),),
+ '-o', 'ControlPath={}'.format(make_control_path(agent, forward_x11)),
'-o', 'ControlPersist=60' ])
if not strict_host_checking:
args.append('-A')
if port:
- args.append('-p%d' % port)
+ args.append('-p{}'.format(port))
if identity:
identity = os.path.expanduser(identity)
if server_key:
# Create a temporary server key file
tmp_known_hosts = make_server_key_args(server_key, host, port)
- args.extend(['-o', 'UserKnownHostsFile=%s' % (tmp_known_hosts.name,)])
+ args.extend(['-o', 'UserKnownHostsFile={}'.format(tmp_known_hosts.name)])
if sudo:
command = "sudo " + command
args.append(command)
- log_msg = " rexec - host %s - command %s " % (str(host), " ".join(map(str, args)))
+ log_msg = " rexec - host {} - command {} ".format(host, " ".join(map(str, args)))
stdout = stderr = stdin = subprocess.PIPE
if forward_x11:
# Parse destination as <user>@<server>:<path>
if isinstance(dest, str) and ':' in dest:
- remspec, path = dest.split(':',1)
+ remspec, path = dest.split(':', 1)
elif isinstance(source, str) and ':' in source:
- remspec, path = source.split(':',1)
+ remspec, path = source.split(':', 1)
else:
raise ValueError("Both endpoints cannot be local")
- user,host = remspec.rsplit('@',1)
+ user, host = remspec.rsplit('@', 1)
# plain scp
tmp_known_hosts = None
'-o', 'TCPKeepAlive=yes' ]
if port:
- args.append('-P%d' % port)
+ args.append('-P{}'.format(port))
if gw:
proxycommand = _proxy_command(gw, gwuser, identity)
if server_key:
# Create a temporary server key file
tmp_known_hosts = make_server_key_args(server_key, host, port)
- args.extend(['-o', 'UserKnownHostsFile=%s' % (tmp_known_hosts.name,)])
+ args.extend(['-o', 'UserKnownHostsFile={}'.format(tmp_known_hosts.name)])
if not strict_host_checking:
# Do not check for Host key. Unsafe.
if openssh_has_persist():
args.extend([
'-o', 'ControlMaster=auto',
- '-o', 'ControlPath=%s' % (make_control_path(False, False),)
+ '-o', 'ControlPath={}'.format(make_control_path(False, False))
])
args.append(source)
else:
args.append(dest)
- log_msg = " rcopy - host %s - command %s " % (str(host), " ".join(map(str, args)))
+ log_msg = " rcopy - host {} - command {} ".format(host, " ".join(map(str, args)))
return _retry_rexec(args, log_msg, env = None, retry = retry,
tmp_known_hosts = tmp_known_hosts,
else:
stderr = ' ' + stderr
- daemon_command = '{ { %(command)s > %(stdout)s 2>%(stderr)s < %(stdin)s & } ; echo $! 1 > %(pidfile)s ; }' % {
- 'command' : command,
- 'pidfile' : shell_escape(pidfile),
- 'stdout' : stdout,
- 'stderr' : stderr,
- 'stdin' : stdin,
- }
+ daemon_command = '{{ {{ {command} > {stdout} 2>{stderr} < {stdin} & }} ; echo $! 1 > {pidfile} ; }}'\
+ .format(command = command,
+ pidfile = shell_escape(pidfile),
+ stdout = stdout,
+ stderr = stderr,
+ stdin = stdin)
- cmd = "%(create)s%(gohome)s rm -f %(pidfile)s ; %(sudo)s nohup bash -c %(command)s " % {
- 'command' : shell_escape(daemon_command),
- 'sudo' : 'sudo -S' if sudo else '',
- 'pidfile' : shell_escape(pidfile),
- 'gohome' : 'cd %s ; ' % (shell_escape(home),) if home else '',
- 'create' : 'mkdir -p %s ; ' % (shell_escape(home),) if create_home and home else '',
- }
-
- (out,err),proc = rexec(
+ cmd = "{create}{gohome} rm -f {pidfile} ; {sudo} nohup bash -c {command} "\
+ .format(command = shell_escape(daemon_command),
+ sudo = 'sudo -S' if sudo else '',
+ pidfile = shell_escape(pidfile),
+ gohome = 'cd {} ; '.format(shell_escape(home)) if home else '',
+ create = 'mkdir -p {} ; '.format(shell_escape(home)) if create_home and home else '')
+
+ (out, err), proc = rexec(
cmd,
host = host,
port = port,
server_key = server_key,
tty = tty,
strict_host_checking = strict_host_checking ,
- )
+ )
if proc.wait():
- raise RuntimeError("Failed to set up application on host %s: %s %s" % (host, out,err,))
+ raise RuntimeError("Failed to set up application on host {}: {} {}".format(host, out, err))
return ((out, err), proc)
or None if the pidfile isn't valid yet (can happen when process is staring up)
"""
- (out,err),proc = rexec(
- "cat %(pidfile)s" % {
- 'pidfile' : pidfile,
- },
+ (out, err), proc = rexec(
+ "cat {}".format(pidfile),
host = host,
port = port,
user = user,
identity = identity,
server_key = server_key,
strict_host_checking = strict_host_checking
- )
+ )
if proc.wait():
return None
:rtype: int (One of NOT_STARTED, RUNNING, FINISHED)
"""
- (out,err),proc = rexec(
+ (out, err), proc = rexec(
# Check only by pid. pid+ppid does not always work (especially with sudo)
- " (( ps --pid %(pid)d -o pid | grep -c %(pid)d && echo 'wait') || echo 'done' ) | tail -n 1" % {
- 'ppid' : ppid,
- 'pid' : pid,
- },
+ " (( ps --pid {pid} -o pid | grep -c {pid} && echo 'wait') || echo 'done' ) | tail -n 1"\
+ .format(**locals()),
host = host,
port = port,
user = user,
identity = identity,
server_key = server_key,
strict_host_checking = strict_host_checking
- )
+ )
if proc.wait():
return ProcStatus.NOT_STARTED
:type sudo: bool
"""
- subkill = "$(ps --ppid %(pid)d -o pid h)" % { 'pid' : pid }
- cmd = """
-SUBKILL="%(subkill)s" ;
-%(sudo)s kill -- -%(pid)d $SUBKILL || /bin/true
-%(sudo)s kill %(pid)d $SUBKILL || /bin/true
+ subkill = "$(ps --ppid {} -o pid h)".format(pid)
+ cmd_format = """
+SUBKILL="{subkill}" ;
+{sudo} kill -- -{pid} $SUBKILL || /bin/true
+{sudo} kill {pid} $SUBKILL || /bin/true
for x in 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 ; do
sleep 0.2
- if [ `ps --pid %(pid)d -o pid | grep -c %(pid)d` == '0' ]; then
+ if [ `ps --pid {pid} -o pid | grep -c {pid}` == '0' ]; then
break
else
- %(sudo)s kill -- -%(pid)d $SUBKILL || /bin/true
- %(sudo)s kill %(pid)d $SUBKILL || /bin/true
+ {sudo} kill -- -{pid} $SUBKILL || /bin/true
+ {sudo} kill {pid} $SUBKILL || /bin/true
fi
sleep 1.8
done
-if [ `ps --pid %(pid)d -o pid | grep -c %(pid)d` != '0' ]; then
- %(sudo)s kill -9 -- -%(pid)d $SUBKILL || /bin/true
- %(sudo)s kill -9 %(pid)d $SUBKILL || /bin/true
+if [ `ps --pid {pid} -o pid | grep -c {pid}` != '0' ]; then
+ {sudo} kill -9 -- -{pid} $SUBKILL || /bin/true
+ {sudo} kill -9 {pid} $SUBKILL || /bin/true
fi
"""
if nowait:
- cmd = "( %s ) >/dev/null 2>/dev/null </dev/null &" % (cmd,)
-
- (out,err),proc = rexec(
- cmd % {
- 'ppid' : ppid,
- 'pid' : pid,
- 'sudo' : 'sudo -S' if sudo else '',
- 'subkill' : subkill,
- },
+ cmd_format = "( {} ) >/dev/null 2>/dev/null </dev/null &".format(cmd_format)
+
+ sudo = 'sudo -S' if sudo else ''
+ (out, err), proc = rexec(
+ cmd_format.format(**locals()),
host = host,
port = port,
user = user,
if skip:
t = x*2
- msg = "SLEEPING %d ... ATEMPT %d - command %s " % (
- t, x, " ".join(args))
+ msg = "SLEEPING {} ... ATEMPT {} - command {} "\
+ .format(t, x, " ".join(args))
log(msg, logging.DEBUG)
time.sleep(t)
continue
break
except RuntimeError as e:
- msg = " rexec EXCEPTION - TIMEOUT -> %s \n %s" % ( e.args, log_msg )
+ msg = " rexec EXCEPTION - TIMEOUT -> {} \n {}".format(e.args, log_msg)
log(msg, logging.DEBUG, out, err)
if retry <= 0:
proxycommand = 'ProxyCommand=ssh -q '
if gwidentity:
- proxycommand += '-i %s ' % os.path.expanduser(gwidentity)
+ proxycommand += '-i {} '.format(os.path.expanduser(gwidentity))
if gwuser:
- proxycommand += '%s' % gwuser
+ proxycommand += '{}'.format(gwuser)
else:
proxycommand += '%r'
- proxycommand += '@%s -W %%h:%%p' % gw
+ proxycommand += '@{} -W %h:%p'.format(gw)
return proxycommand