2012-03-10 18:35:46 +00:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-04-12 00:20:55 +00:00
|
|
|
import warnings
|
2012-03-29 02:51:16 +00:00
|
|
|
import traceback
|
2012-03-13 00:53:10 +00:00
|
|
|
import os
|
2012-04-06 23:38:27 +00:00
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
More robust remote sudo.
The basic idea is sudo /bin/sh -c 'quoted_command'. We use Paramiko's low-level API to set a timeout, get a pseudo tty, execute sudo and the (shell quoted) command atomically, wait just until sudo is ready to accept the password before sending it down the pipe, and then return the command's stdout and stderr.
This should be faster, as there are no unneeded sleeps. There are no permissions issues reading the output. It will raise socket.timeout if the command takes too long. However, this is a per-read timeout, not a total execution timeout, so as long as the command is writing output and you are reading it, it will not time out.
Local and non-sudo commands remain unchanged, but should probably adopt a similar approach.
Since this is a significant change, it needs a lot of testing. Also, someone smarter than I should double-check the quoting and execution, since it is a security issue.
2012-04-23 20:32:08 +00:00
|
|
|
import pipes
|
2012-04-27 04:46:17 +00:00
|
|
|
import socket
|
|
|
|
import random
|
2012-03-18 21:16:12 +00:00
|
|
|
from ansible import errors
|
2012-07-15 16:29:53 +00:00
|
|
|
|
|
|
|
# prevent paramiko warning noise -- see http://stackoverflow.com/questions/3920502/
|
2012-07-12 04:43:51 +00:00
|
|
|
HAVE_PARAMIKO=False
|
More robust remote sudo.
The basic idea is sudo /bin/sh -c 'quoted_command'. We use Paramiko's low-level API to set a timeout, get a pseudo tty, execute sudo and the (shell quoted) command atomically, wait just until sudo is ready to accept the password before sending it down the pipe, and then return the command's stdout and stderr.
This should be faster, as there are no unneeded sleeps. There are no permissions issues reading the output. It will raise socket.timeout if the command takes too long. However, this is a per-read timeout, not a total execution timeout, so as long as the command is writing output and you are reading it, it will not time out.
Local and non-sudo commands remain unchanged, but should probably adopt a similar approach.
Since this is a significant change, it needs a lot of testing. Also, someone smarter than I should double-check the quoting and execution, since it is a security issue.
2012-04-23 20:32:08 +00:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("ignore")
|
2012-07-12 04:43:51 +00:00
|
|
|
try:
|
|
|
|
import paramiko
|
|
|
|
HAVE_PARAMIKO=True
|
|
|
|
except ImportError:
|
|
|
|
pass
|
More robust remote sudo.
The basic idea is sudo /bin/sh -c 'quoted_command'. We use Paramiko's low-level API to set a timeout, get a pseudo tty, execute sudo and the (shell quoted) command atomically, wait just until sudo is ready to accept the password before sending it down the pipe, and then return the command's stdout and stderr.
This should be faster, as there are no unneeded sleeps. There are no permissions issues reading the output. It will raise socket.timeout if the command takes too long. However, this is a per-read timeout, not a total execution timeout, so as long as the command is writing output and you are reading it, it will not time out.
Local and non-sudo commands remain unchanged, but should probably adopt a similar approach.
Since this is a significant change, it needs a lot of testing. Also, someone smarter than I should double-check the quoting and execution, since it is a security issue.
2012-04-23 20:32:08 +00:00
|
|
|
|
2012-03-10 18:35:46 +00:00
|
|
|
class ParamikoConnection(object):
|
|
|
|
''' SSH based connections with Paramiko '''
|
|
|
|
|
2012-04-17 01:52:15 +00:00
|
|
|
def __init__(self, runner, host, port=None):
|
2012-03-10 18:35:46 +00:00
|
|
|
self.ssh = None
|
|
|
|
self.runner = runner
|
|
|
|
self.host = host
|
2012-04-17 01:52:15 +00:00
|
|
|
self.port = port
|
|
|
|
if port is None:
|
|
|
|
self.port = self.runner.remote_port
|
2012-03-10 18:35:46 +00:00
|
|
|
|
2012-07-15 16:29:53 +00:00
|
|
|
def connect(self):
|
|
|
|
''' activates the connection object '''
|
2012-07-12 04:43:51 +00:00
|
|
|
|
|
|
|
if not HAVE_PARAMIKO:
|
|
|
|
raise errors.AnsibleError("paramiko is not installed")
|
|
|
|
|
2012-04-24 13:48:55 +00:00
|
|
|
user = self.runner.remote_user
|
2012-03-29 02:51:16 +00:00
|
|
|
ssh = paramiko.SSHClient()
|
|
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
2012-03-29 00:58:34 +00:00
|
|
|
|
2012-03-10 18:35:46 +00:00
|
|
|
try:
|
2012-07-15 16:29:53 +00:00
|
|
|
ssh.connect(self.host, username=user, allow_agent=True, look_for_keys=True,
|
|
|
|
key_filename=self.runner.private_key_file, password=self.runner.remote_pass,
|
|
|
|
timeout=self.runner.timeout, port=self.port)
|
2012-03-10 18:35:46 +00:00
|
|
|
except Exception, e:
|
2012-06-01 21:16:02 +00:00
|
|
|
msg = str(e)
|
|
|
|
if "PID check failed" in msg:
|
2012-03-29 00:32:04 +00:00
|
|
|
raise errors.AnsibleError("paramiko version issue, please upgrade paramiko on the machine running ansible")
|
2012-06-01 21:16:02 +00:00
|
|
|
elif "Private key file is encrypted" in msg:
|
|
|
|
msg = 'ssh %s@%s:%s : %s\nTo connect as a different user, use -u <username>.' % (
|
|
|
|
user, self.host, self.port, msg)
|
|
|
|
raise errors.AnsibleConnectionFailed(msg)
|
2012-03-29 02:51:16 +00:00
|
|
|
else:
|
2012-06-01 21:16:02 +00:00
|
|
|
raise errors.AnsibleConnectionFailed(msg)
|
2012-03-29 00:58:34 +00:00
|
|
|
|
2012-07-15 16:29:53 +00:00
|
|
|
self.ssh = ssh
|
2012-03-10 18:35:46 +00:00
|
|
|
return self
|
|
|
|
|
2012-07-15 15:54:39 +00:00
|
|
|
def exec_command(self, cmd, tmp_path, sudo_user, sudoable=False):
|
2012-03-10 18:35:46 +00:00
|
|
|
''' run a command on the remote host '''
|
2012-07-15 16:29:53 +00:00
|
|
|
|
2012-04-27 05:25:38 +00:00
|
|
|
bufsize = 4096
|
2012-04-27 04:46:26 +00:00
|
|
|
chan = self.ssh.get_transport().open_session()
|
2012-08-07 00:07:02 +00:00
|
|
|
chan.get_pty()
|
2012-04-27 05:36:31 +00:00
|
|
|
|
2012-04-24 13:48:55 +00:00
|
|
|
if not self.runner.sudo or not sudoable:
|
2012-08-07 00:07:02 +00:00
|
|
|
quoted_command = '"$SHELL" -c ' + pipes.quote(cmd)
|
2012-04-27 04:46:26 +00:00
|
|
|
chan.exec_command(quoted_command)
|
2012-03-29 02:51:16 +00:00
|
|
|
else:
|
2012-08-07 00:07:02 +00:00
|
|
|
# Rather than detect if sudo wants a password this time, -k makes
|
2012-04-27 04:31:18 +00:00
|
|
|
# sudo always ask for a password if one is required. The "--"
|
|
|
|
# tells sudo that this is the end of sudo options and the command
|
|
|
|
# follows. Passing a quoted compound command to sudo (or sudo -s)
|
2012-08-07 00:07:02 +00:00
|
|
|
# directly doesn't work, so we shellquote it with pipes.quote()
|
2012-05-02 20:25:04 +00:00
|
|
|
# and pass the quoted string to the user's shell. We loop reading
|
|
|
|
# output until we see the randomly-generated sudo prompt set with
|
|
|
|
# the -p option.
|
2012-04-27 05:25:38 +00:00
|
|
|
randbits = ''.join(chr(random.randint(ord('a'), ord('z'))) for x in xrange(32))
|
|
|
|
prompt = '[sudo via ansible, key=%s] password: ' % randbits
|
2012-06-01 01:44:30 +00:00
|
|
|
sudocmd = 'sudo -k && sudo -p "%s" -u %s -- "$SHELL" -c %s' % (
|
|
|
|
prompt, sudo_user, pipes.quote(cmd))
|
2012-04-27 05:25:38 +00:00
|
|
|
sudo_output = ''
|
|
|
|
try:
|
|
|
|
chan.exec_command(sudocmd)
|
|
|
|
if self.runner.sudo_pass:
|
|
|
|
while not sudo_output.endswith(prompt):
|
|
|
|
chunk = chan.recv(bufsize)
|
|
|
|
if not chunk:
|
2012-08-02 06:08:51 +00:00
|
|
|
if 'unknown user' in sudo_output:
|
|
|
|
raise errors.AnsibleError(
|
|
|
|
'user %s does not exist' % sudo_user)
|
|
|
|
else:
|
|
|
|
raise errors.AnsibleError('ssh connection ' +
|
|
|
|
'closed waiting for password prompt')
|
2012-04-27 05:25:38 +00:00
|
|
|
sudo_output += chunk
|
|
|
|
chan.sendall(self.runner.sudo_pass + '\n')
|
|
|
|
except socket.timeout:
|
|
|
|
raise errors.AnsibleError('ssh timed out waiting for sudo.\n' + sudo_output)
|
|
|
|
|
2012-07-15 16:29:53 +00:00
|
|
|
return (chan.makefile('wb', bufsize), chan.makefile('rb', bufsize), '')
|
2012-03-10 18:35:46 +00:00
|
|
|
|
|
|
|
def put_file(self, in_path, out_path):
|
|
|
|
''' transfer a file from local to remote '''
|
2012-03-13 00:53:10 +00:00
|
|
|
if not os.path.exists(in_path):
|
2012-03-18 21:16:12 +00:00
|
|
|
raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path)
|
2012-03-10 18:35:46 +00:00
|
|
|
sftp = self.ssh.open_sftp()
|
2012-03-13 00:53:10 +00:00
|
|
|
try:
|
|
|
|
sftp.put(in_path, out_path)
|
|
|
|
except IOError:
|
2012-03-23 15:59:08 +00:00
|
|
|
raise errors.AnsibleError("failed to transfer file to %s" % out_path)
|
2012-03-10 18:35:46 +00:00
|
|
|
sftp.close()
|
|
|
|
|
2012-04-11 03:19:23 +00:00
|
|
|
def fetch_file(self, in_path, out_path):
|
2012-07-15 16:29:53 +00:00
|
|
|
''' save a remote file to the specified path '''
|
2012-04-11 03:19:23 +00:00
|
|
|
sftp = self.ssh.open_sftp()
|
|
|
|
try:
|
|
|
|
sftp.get(in_path, out_path)
|
|
|
|
except IOError:
|
|
|
|
raise errors.AnsibleError("failed to transfer file from %s" % in_path)
|
|
|
|
sftp.close()
|
|
|
|
|
2012-03-10 18:35:46 +00:00
|
|
|
def close(self):
|
|
|
|
''' terminate the connection '''
|
|
|
|
self.ssh.close()
|
|
|
|
|