Add some whitespace to make paramiko_ssh more browseable
This commit is contained in:
parent
24f6f656cc
commit
a7d2e729f8
1 changed files with 47 additions and 0 deletions
|
@ -39,6 +39,7 @@ from ansible.callbacks import vvv
|
||||||
from ansible import errors
|
from ansible import errors
|
||||||
from ansible import utils
|
from ansible import utils
|
||||||
from ansible import constants as C
|
from ansible import constants as C
|
||||||
|
from ansible.module_utils.basic import atomic_move
|
||||||
|
|
||||||
AUTHENTICITY_MSG="""
|
AUTHENTICITY_MSG="""
|
||||||
paramiko: The authenticity of host '%s' can't be established.
|
paramiko: The authenticity of host '%s' can't be established.
|
||||||
|
@ -149,12 +150,16 @@ class Connection(object):
|
||||||
|
|
||||||
if C.HOST_KEY_CHECKING:
|
if C.HOST_KEY_CHECKING:
|
||||||
ssh.load_system_host_keys()
|
ssh.load_system_host_keys()
|
||||||
|
|
||||||
ssh.set_missing_host_key_policy(MyAddPolicy(self.runner))
|
ssh.set_missing_host_key_policy(MyAddPolicy(self.runner))
|
||||||
|
|
||||||
allow_agent = True
|
allow_agent = True
|
||||||
|
|
||||||
if self.password is not None:
|
if self.password is not None:
|
||||||
allow_agent = False
|
allow_agent = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
if self.private_key_file:
|
if self.private_key_file:
|
||||||
key_filename = os.path.expanduser(self.private_key_file)
|
key_filename = os.path.expanduser(self.private_key_file)
|
||||||
elif self.runner.private_key_file:
|
elif self.runner.private_key_file:
|
||||||
|
@ -164,7 +169,9 @@ class Connection(object):
|
||||||
ssh.connect(self.host, username=self.user, allow_agent=allow_agent, look_for_keys=True,
|
ssh.connect(self.host, username=self.user, allow_agent=allow_agent, look_for_keys=True,
|
||||||
key_filename=key_filename, password=self.password,
|
key_filename=key_filename, password=self.password,
|
||||||
timeout=self.runner.timeout, port=self.port)
|
timeout=self.runner.timeout, port=self.port)
|
||||||
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
|
||||||
msg = str(e)
|
msg = str(e)
|
||||||
if "PID check failed" in msg:
|
if "PID check failed" in msg:
|
||||||
raise errors.AnsibleError("paramiko version issue, please upgrade paramiko on the machine running ansible")
|
raise errors.AnsibleError("paramiko version issue, please upgrade paramiko on the machine running ansible")
|
||||||
|
@ -184,23 +191,30 @@ class Connection(object):
|
||||||
raise errors.AnsibleError("Internal Error: this module does not support optimized module pipelining")
|
raise errors.AnsibleError("Internal Error: this module does not support optimized module pipelining")
|
||||||
|
|
||||||
bufsize = 4096
|
bufsize = 4096
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
chan = self.ssh.get_transport().open_session()
|
chan = self.ssh.get_transport().open_session()
|
||||||
self.ssh.get_transport().set_keepalive(5)
|
self.ssh.get_transport().set_keepalive(5)
|
||||||
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
|
||||||
msg = "Failed to open session"
|
msg = "Failed to open session"
|
||||||
if len(str(e)) > 0:
|
if len(str(e)) > 0:
|
||||||
msg += ": %s" % str(e)
|
msg += ": %s" % str(e)
|
||||||
raise errors.AnsibleConnectionFailed(msg)
|
raise errors.AnsibleConnectionFailed(msg)
|
||||||
|
|
||||||
if not (self.runner.sudo and sudoable) and not (self.runner.su and su):
|
if not (self.runner.sudo and sudoable) and not (self.runner.su and su):
|
||||||
|
|
||||||
if executable:
|
if executable:
|
||||||
quoted_command = executable + ' -c ' + pipes.quote(cmd)
|
quoted_command = executable + ' -c ' + pipes.quote(cmd)
|
||||||
else:
|
else:
|
||||||
quoted_command = cmd
|
quoted_command = cmd
|
||||||
vvv("EXEC %s" % quoted_command, host=self.host)
|
vvv("EXEC %s" % quoted_command, host=self.host)
|
||||||
chan.exec_command(quoted_command)
|
chan.exec_command(quoted_command)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
# sudo usually requires a PTY (cf. requiretty option), therefore
|
# sudo usually requires a PTY (cf. requiretty option), therefore
|
||||||
# we give it one by default (pty=True in ansble.cfg), and we try
|
# we give it one by default (pty=True in ansble.cfg), and we try
|
||||||
# to initialise from the calling environment
|
# to initialise from the calling environment
|
||||||
|
@ -213,17 +227,24 @@ class Connection(object):
|
||||||
elif self.runner.su or su:
|
elif self.runner.su or su:
|
||||||
shcmd, prompt, success_key = utils.make_su_cmd(su_user, executable, cmd)
|
shcmd, prompt, success_key = utils.make_su_cmd(su_user, executable, cmd)
|
||||||
prompt_re = re.compile(prompt)
|
prompt_re = re.compile(prompt)
|
||||||
|
|
||||||
vvv("EXEC %s" % shcmd, host=self.host)
|
vvv("EXEC %s" % shcmd, host=self.host)
|
||||||
sudo_output = ''
|
sudo_output = ''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
chan.exec_command(shcmd)
|
chan.exec_command(shcmd)
|
||||||
|
|
||||||
if self.runner.sudo_pass or self.runner.su_pass:
|
if self.runner.sudo_pass or self.runner.su_pass:
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
if success_key in sudo_output or \
|
if success_key in sudo_output or \
|
||||||
(self.runner.sudo_pass and sudo_output.endswith(prompt)) or \
|
(self.runner.sudo_pass and sudo_output.endswith(prompt)) or \
|
||||||
(self.runner.su_pass and prompt_re.match(sudo_output)):
|
(self.runner.su_pass and prompt_re.match(sudo_output)):
|
||||||
break
|
break
|
||||||
chunk = chan.recv(bufsize)
|
chunk = chan.recv(bufsize)
|
||||||
|
|
||||||
if not chunk:
|
if not chunk:
|
||||||
if 'unknown user' in sudo_output:
|
if 'unknown user' in sudo_output:
|
||||||
raise errors.AnsibleError(
|
raise errors.AnsibleError(
|
||||||
|
@ -232,33 +253,43 @@ class Connection(object):
|
||||||
raise errors.AnsibleError('ssh connection ' +
|
raise errors.AnsibleError('ssh connection ' +
|
||||||
'closed waiting for password prompt')
|
'closed waiting for password prompt')
|
||||||
sudo_output += chunk
|
sudo_output += chunk
|
||||||
|
|
||||||
if success_key not in sudo_output:
|
if success_key not in sudo_output:
|
||||||
|
|
||||||
if sudoable:
|
if sudoable:
|
||||||
chan.sendall(self.runner.sudo_pass + '\n')
|
chan.sendall(self.runner.sudo_pass + '\n')
|
||||||
elif su:
|
elif su:
|
||||||
chan.sendall(self.runner.su_pass + '\n')
|
chan.sendall(self.runner.su_pass + '\n')
|
||||||
|
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
|
|
||||||
raise errors.AnsibleError('ssh timed out waiting for sudo.\n' + sudo_output)
|
raise errors.AnsibleError('ssh timed out waiting for sudo.\n' + sudo_output)
|
||||||
|
|
||||||
stdout = ''.join(chan.makefile('rb', bufsize))
|
stdout = ''.join(chan.makefile('rb', bufsize))
|
||||||
stderr = ''.join(chan.makefile_stderr('rb', bufsize))
|
stderr = ''.join(chan.makefile_stderr('rb', bufsize))
|
||||||
|
|
||||||
return (chan.recv_exit_status(), '', stdout, stderr)
|
return (chan.recv_exit_status(), '', stdout, stderr)
|
||||||
|
|
||||||
def put_file(self, in_path, out_path):
|
def put_file(self, in_path, out_path):
|
||||||
''' transfer a file from local to remote '''
|
''' transfer a file from local to remote '''
|
||||||
|
|
||||||
vvv("PUT %s TO %s" % (in_path, out_path), host=self.host)
|
vvv("PUT %s TO %s" % (in_path, out_path), host=self.host)
|
||||||
|
|
||||||
if not os.path.exists(in_path):
|
if not os.path.exists(in_path):
|
||||||
raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path)
|
raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.sftp = self.ssh.open_sftp()
|
self.sftp = self.ssh.open_sftp()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
raise errors.AnsibleError("failed to open a SFTP connection (%s)" % e)
|
raise errors.AnsibleError("failed to open a SFTP connection (%s)" % e)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.sftp.put(in_path, out_path)
|
self.sftp.put(in_path, out_path)
|
||||||
except IOError:
|
except IOError:
|
||||||
raise errors.AnsibleError("failed to transfer file to %s" % out_path)
|
raise errors.AnsibleError("failed to transfer file to %s" % out_path)
|
||||||
|
|
||||||
def _connect_sftp(self):
|
def _connect_sftp(self):
|
||||||
|
|
||||||
cache_key = "%s__%s__" % (self.host, self.user)
|
cache_key = "%s__%s__" % (self.host, self.user)
|
||||||
if cache_key in SFTP_CONNECTION_CACHE:
|
if cache_key in SFTP_CONNECTION_CACHE:
|
||||||
return SFTP_CONNECTION_CACHE[cache_key]
|
return SFTP_CONNECTION_CACHE[cache_key]
|
||||||
|
@ -268,17 +299,21 @@ class Connection(object):
|
||||||
|
|
||||||
def fetch_file(self, in_path, out_path):
|
def fetch_file(self, in_path, out_path):
|
||||||
''' save a remote file to the specified path '''
|
''' save a remote file to the specified path '''
|
||||||
|
|
||||||
vvv("FETCH %s TO %s" % (in_path, out_path), host=self.host)
|
vvv("FETCH %s TO %s" % (in_path, out_path), host=self.host)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.sftp = self._connect_sftp()
|
self.sftp = self._connect_sftp()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
raise errors.AnsibleError("failed to open a SFTP connection (%s)", e)
|
raise errors.AnsibleError("failed to open a SFTP connection (%s)", e)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.sftp.get(in_path, out_path)
|
self.sftp.get(in_path, out_path)
|
||||||
except IOError:
|
except IOError:
|
||||||
raise errors.AnsibleError("failed to transfer file from %s" % in_path)
|
raise errors.AnsibleError("failed to transfer file from %s" % in_path)
|
||||||
|
|
||||||
def _any_keys_added(self):
|
def _any_keys_added(self):
|
||||||
|
|
||||||
added_any = False
|
added_any = False
|
||||||
for hostname, keys in self.ssh._host_keys.iteritems():
|
for hostname, keys in self.ssh._host_keys.iteritems():
|
||||||
for keytype, key in keys.iteritems():
|
for keytype, key in keys.iteritems():
|
||||||
|
@ -301,24 +336,32 @@ class Connection(object):
|
||||||
os.makedirs(path)
|
os.makedirs(path)
|
||||||
|
|
||||||
f = open(filename, 'w')
|
f = open(filename, 'w')
|
||||||
|
|
||||||
for hostname, keys in self.ssh._host_keys.iteritems():
|
for hostname, keys in self.ssh._host_keys.iteritems():
|
||||||
|
|
||||||
for keytype, key in keys.iteritems():
|
for keytype, key in keys.iteritems():
|
||||||
|
|
||||||
# was f.write
|
# was f.write
|
||||||
added_this_time = getattr(key, '_added_by_ansible_this_time', False)
|
added_this_time = getattr(key, '_added_by_ansible_this_time', False)
|
||||||
if not added_this_time:
|
if not added_this_time:
|
||||||
f.write("%s %s %s\n" % (hostname, keytype, key.get_base64()))
|
f.write("%s %s %s\n" % (hostname, keytype, key.get_base64()))
|
||||||
|
|
||||||
for hostname, keys in self.ssh._host_keys.iteritems():
|
for hostname, keys in self.ssh._host_keys.iteritems():
|
||||||
|
|
||||||
for keytype, key in keys.iteritems():
|
for keytype, key in keys.iteritems():
|
||||||
added_this_time = getattr(key, '_added_by_ansible_this_time', False)
|
added_this_time = getattr(key, '_added_by_ansible_this_time', False)
|
||||||
if added_this_time:
|
if added_this_time:
|
||||||
f.write("%s %s %s\n" % (hostname, keytype, key.get_base64()))
|
f.write("%s %s %s\n" % (hostname, keytype, key.get_base64()))
|
||||||
|
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
''' terminate the connection '''
|
''' terminate the connection '''
|
||||||
|
|
||||||
cache_key = self._cache_key()
|
cache_key = self._cache_key()
|
||||||
SSH_CONNECTION_CACHE.pop(cache_key, None)
|
SSH_CONNECTION_CACHE.pop(cache_key, None)
|
||||||
SFTP_CONNECTION_CACHE.pop(cache_key, None)
|
SFTP_CONNECTION_CACHE.pop(cache_key, None)
|
||||||
|
|
||||||
if self.sftp is not None:
|
if self.sftp is not None:
|
||||||
self.sftp.close()
|
self.sftp.close()
|
||||||
|
|
||||||
|
@ -332,12 +375,16 @@ class Connection(object):
|
||||||
|
|
||||||
KEY_LOCK = open(lockfile, 'w')
|
KEY_LOCK = open(lockfile, 'w')
|
||||||
fcntl.lockf(KEY_LOCK, fcntl.LOCK_EX)
|
fcntl.lockf(KEY_LOCK, fcntl.LOCK_EX)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# just in case any were added recently
|
# just in case any were added recently
|
||||||
|
|
||||||
self.ssh.load_system_host_keys()
|
self.ssh.load_system_host_keys()
|
||||||
self.ssh._host_keys.update(self.ssh._system_host_keys)
|
self.ssh._host_keys.update(self.ssh._system_host_keys)
|
||||||
self._save_ssh_host_keys(self.keyfile)
|
self._save_ssh_host_keys(self.keyfile)
|
||||||
|
|
||||||
except:
|
except:
|
||||||
|
|
||||||
# unable to save keys, including scenario when key was invalid
|
# unable to save keys, including scenario when key was invalid
|
||||||
# and caught earlier
|
# and caught earlier
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
Loading…
Reference in a new issue