Finish code of ssh trans back end, ready to hook in and validate!

This commit is contained in:
Thomas S Hatch 2013-06-26 15:32:04 -06:00
parent e3e73beecf
commit 7bd41a9f2c

View File

@ -10,7 +10,7 @@ import subprocess
import salt.utils
import salt.utils.nb_popen
def _key_opts(user=None, port=None, priv=None, timeout=None):
def _key_opts(user=None, port=None, priv=None, timeout=60):
'''
Return options for the ssh command base for Salt to call
'''
@ -21,7 +21,7 @@ def _key_opts(user=None, port=None, priv=None, timeout=None):
'GSSAPIAuthentication=no',
'PasswordAuthentication=no',
]
options.append('ConnectTimeout={0}'.format(timeout if timeout else 5))
options.append('ConnectTimeout={0}'.format(timeout))
if port:
options.append('Port={0}'.format(port))
if priv:
@ -32,7 +32,7 @@ def _key_opts(user=None, port=None, priv=None, timeout=None):
return options
def _passwd_opts(user=None, port=None, passwd=None, timeout=None):
def _passwd_opts(user=None, port=None, passwd=None, timeout=60):
'''
Return options to pass to sshpass
'''
@ -41,7 +41,7 @@ def _passwd_opts(user=None, port=None, passwd=None, timeout=None):
'StrictHostKeyChecking=no',
'GSSAPIAuthentication=no',
]
options.append('ConnectTimeout={0}'.format(timeout if timeout else 5))
options.append('ConnectTimeout={0}'.format(timeout))
if port:
options.append('Port={0}'.format(port))
if user:
@ -52,6 +52,7 @@ def _passwd_opts(user=None, port=None, passwd=None, timeout=None):
def _cmd_str(
cmd,
host='',
ssh='ssh',
user=None,
port=None,
@ -68,8 +69,9 @@ def _cmd_str(
port,
priv,
timeout)
return '{0} {1} -o {2} -c {3}'.format(
return '{0} {1} {2} -o {3} -c {4}'.format(
ssh,
host,
'-t -t' if tty else '',
','.join(opts),
cmd)
@ -81,39 +83,20 @@ def _cmd_str(
port,
priv,
timeout)
return 'sshpass -p {0} {1} {2} -o {3} -c {4}'.format(
return 'sshpass -p {0} {1} {2} {3} -o {4} -c {5}'.format(
passwd,
ssh,
host,
'-t -t' if tty else '',
','.join(opts),
cmd)
return None
def exec_cmd(
cmd,
user=None,
port=None,
passwd=None,
priv=None,
timeout=None,
sudo=False):
def _run_cmd(cmd):
'''
Execute a remote command
Cleanly execute the command string
'''
if sudo:
cmd = 'sudo {0}'.format(cmd)
tty = True
else:
tty = False
cmd = _cmd_str(
cmd,
user=user,
port=port,
passwd=passwd,
priv=priv,
timeout=timeout,
tty=tty)
try:
proc = salt.utils.nb_open.NonBlockingPopen(
cmd,
@ -130,3 +113,63 @@ def exec_cmd(
pass
# Signal an error
return ()
def exec_cmd(
cmd,
host,
user=None,
port=None,
passwd=None,
priv=None,
timeout=None,
sudo=False):
'''
Execute a remote command
'''
if sudo:
cmd = 'sudo {0}'.format(cmd)
tty = True
else:
tty = False
cmd = _cmd_str(
cmd,
host,
user=user,
port=port,
passwd=passwd,
priv=priv,
timeout=timeout,
tty=tty)
return _run_cmd(cmd)
def send(
local,
remote,
host,
user=None,
port=None,
passwd=None,
priv=None,
timeout=None,
sudo=False):
'''
scp a file or files to a remote system
'''
cmd = '{0} {1}:{2}'.format(local, host, remote)
if sudo:
cmd = 'sudo {0}'.format(cmd)
tty = True
else:
tty = False
cmd = _cmd_str(
cmd,
ssh='scp',
user=user,
port=port,
passwd=passwd,
priv=priv,
timeout=timeout,
tty=tty)
return _run_cmd(cmd)