mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-02 06:30:19 -07:00
parent
2f33c1a1a1
commit
5553b20828
206 changed files with 1853 additions and 1870 deletions
|
@ -59,8 +59,8 @@ class ConnectionBase(with_metaclass(ABCMeta, object)):
|
|||
'''
|
||||
|
||||
has_pipelining = False
|
||||
has_native_async = False # eg, winrm
|
||||
always_pipeline_modules = False # eg, winrm
|
||||
has_native_async = False # eg, winrm
|
||||
always_pipeline_modules = False # eg, winrm
|
||||
become_methods = C.BECOME_METHODS
|
||||
# When running over this connection type, prefer modules written in a certain language
|
||||
# as discovered by the specified file extension. An empty string as the
|
||||
|
|
|
@ -44,7 +44,7 @@ except ImportError:
|
|||
# ((1400-8)/4)*3) = 1044
|
||||
# which leaves room for the TCP/IP header. We set this to a
|
||||
# multiple of the value to speed up file reads.
|
||||
CHUNK_SIZE=1044*20
|
||||
CHUNK_SIZE = 1044 * 20
|
||||
|
||||
|
||||
class Connection(ConnectionBase):
|
||||
|
@ -73,7 +73,7 @@ class Connection(ConnectionBase):
|
|||
host=self._play_context.remote_addr)
|
||||
while tries > 0:
|
||||
try:
|
||||
self.conn.connect((self._play_context.remote_addr,self._play_context.accelerate_port))
|
||||
self.conn.connect((self._play_context.remote_addr, self._play_context.accelerate_port))
|
||||
break
|
||||
except socket.error:
|
||||
display.vvvv("connection to %s failed, retrying..." % self._play_context.remote_addr, host=self._play_context.remote_addr)
|
||||
|
@ -107,11 +107,11 @@ class Connection(ConnectionBase):
|
|||
sock.close()
|
||||
|
||||
def send_data(self, data):
|
||||
packed_len = struct.pack('!Q',len(data))
|
||||
packed_len = struct.pack('!Q', len(data))
|
||||
return self.conn.sendall(packed_len + data)
|
||||
|
||||
def recv_data(self):
|
||||
header_len = 8 # size of a packed unsigned long long
|
||||
header_len = 8 # size of a packed unsigned long long
|
||||
data = b""
|
||||
try:
|
||||
display.vvvv("in recv_data(), waiting for the header", host=self._play_context.remote_addr)
|
||||
|
@ -122,7 +122,7 @@ class Connection(ConnectionBase):
|
|||
return None
|
||||
data += d
|
||||
display.vvvv("got the header, unpacking", host=self._play_context.remote_addr)
|
||||
data_len = struct.unpack('!Q',data[:header_len])[0]
|
||||
data_len = struct.unpack('!Q', data[:header_len])[0]
|
||||
data = data[header_len:]
|
||||
display.vvvv("data received so far (expecting %d): %d" % (data_len, len(data)), host=self._play_context.remote_addr)
|
||||
while len(data) < data_len:
|
||||
|
@ -252,7 +252,7 @@ class Connection(ConnectionBase):
|
|||
response = keyczar_decrypt(self.key, response)
|
||||
response = json.loads(response)
|
||||
|
||||
if response.get('failed',False):
|
||||
if response.get('failed', False):
|
||||
raise AnsibleError("failed to put the file in the requested location")
|
||||
finally:
|
||||
fd.close()
|
||||
|
@ -263,7 +263,7 @@ class Connection(ConnectionBase):
|
|||
response = keyczar_decrypt(self.key, response)
|
||||
response = json.loads(response)
|
||||
|
||||
if response.get('failed',False):
|
||||
if response.get('failed', False):
|
||||
raise AnsibleError("failed to put the file in the requested location")
|
||||
|
||||
def fetch_file(self, in_path, out_path):
|
||||
|
|
|
@ -96,7 +96,7 @@ class Connection(ConnectionBase):
|
|||
display.vvv("EXEC %s" % (local_cmd), host=self.chroot)
|
||||
local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd]
|
||||
p = subprocess.Popen(local_cmd, shell=False, stdin=stdin,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
|
||||
return p
|
||||
|
||||
|
|
|
@ -235,7 +235,7 @@ class Connection(ConnectionBase):
|
|||
with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file:
|
||||
try:
|
||||
p = subprocess.Popen(args, stdin=in_file,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
except OSError:
|
||||
raise AnsibleError("docker connection requires dd command in the container to put files")
|
||||
stdout, stderr = p.communicate()
|
||||
|
@ -257,7 +257,7 @@ class Connection(ConnectionBase):
|
|||
args = [to_bytes(i, errors='surrogate_or_strict') for i in args]
|
||||
|
||||
p = subprocess.Popen(args, stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
p.communicate()
|
||||
|
||||
# Rename if needed
|
||||
|
|
|
@ -43,6 +43,7 @@ except ImportError:
|
|||
from ansible.utils.display import Display
|
||||
display = Display()
|
||||
|
||||
|
||||
class Connection(object):
|
||||
''' Func-based connections '''
|
||||
|
||||
|
|
|
@ -65,4 +65,3 @@ class Connection(Jail):
|
|||
raise AnsibleError(u"iocage returned an error: {}".format(stdout))
|
||||
|
||||
return stdout.strip('\n')
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ class Connection(ConnectionBase):
|
|||
super(Connection, self).__init__(play_context, new_stdin, *args, **kwargs)
|
||||
|
||||
self.jail = self._play_context.remote_addr
|
||||
if self.modified_jailname_key in kwargs :
|
||||
if self.modified_jailname_key in kwargs:
|
||||
self.jail = kwargs[self.modified_jailname_key]
|
||||
|
||||
if os.geteuid() != 0:
|
||||
|
@ -122,7 +122,7 @@ class Connection(ConnectionBase):
|
|||
display.vvv("EXEC %s" % (local_cmd,), host=self.jail)
|
||||
local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd]
|
||||
p = subprocess.Popen(local_cmd, shell=False, stdin=stdin,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
|
||||
return p
|
||||
|
||||
|
|
|
@ -97,7 +97,7 @@ class Connection(ConnectionBase):
|
|||
display.vvv("EXEC %s" % (local_cmd,), host=self.lxc)
|
||||
local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd]
|
||||
p = subprocess.Popen(local_cmd, shell=False, stdin=stdin,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
|
||||
return p
|
||||
|
||||
|
|
|
@ -90,7 +90,7 @@ class Connection(ConnectionBase):
|
|||
p = subprocess.Popen(
|
||||
cmd,
|
||||
shell=isinstance(cmd, (text_type, binary_type)),
|
||||
executable=executable, #cwd=...
|
||||
executable=executable, # cwd=...
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
|
|
|
@ -68,7 +68,7 @@ class Connection(ConnectionBase):
|
|||
raise errors.AnsibleError("%s is not running" % self.container_name)
|
||||
|
||||
def _communicate(self, pid, in_data, stdin, stdout, stderr):
|
||||
buf = { stdout: [], stderr: [] }
|
||||
buf = {stdout: [], stderr: []}
|
||||
read_fds = [stdout, stderr]
|
||||
if in_data:
|
||||
write_fds = [stdin]
|
||||
|
@ -109,7 +109,7 @@ class Connection(ConnectionBase):
|
|||
|
||||
read_stdout, write_stdout = None, None
|
||||
read_stderr, write_stderr = None, None
|
||||
read_stdin, write_stdin = None, None
|
||||
read_stdin, write_stdin = None, None
|
||||
|
||||
try:
|
||||
read_stdout, write_stdout = os.pipe()
|
||||
|
|
|
@ -52,7 +52,7 @@ except ImportError:
|
|||
display = Display()
|
||||
|
||||
|
||||
AUTHENTICITY_MSG="""
|
||||
AUTHENTICITY_MSG = """
|
||||
paramiko: The authenticity of host '%s' can't be established.
|
||||
The %s key fingerprint is %s.
|
||||
Are you sure you want to continue connecting (yes/no)?
|
||||
|
@ -62,12 +62,12 @@ Are you sure you want to continue connecting (yes/no)?
|
|||
SETTINGS_REGEX = re.compile(r'(\w+)(?:\s*=\s*|\s+)(.+)')
|
||||
|
||||
# prevent paramiko warning noise -- see http://stackoverflow.com/questions/3920502/
|
||||
HAVE_PARAMIKO=False
|
||||
HAVE_PARAMIKO = False
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore")
|
||||
try:
|
||||
import paramiko
|
||||
HAVE_PARAMIKO=True
|
||||
HAVE_PARAMIKO = True
|
||||
logging.getLogger("paramiko").setLevel(logging.WARNING)
|
||||
except ImportError:
|
||||
pass
|
||||
|
@ -109,7 +109,7 @@ class MyAddPolicy(object):
|
|||
|
||||
self.connection.connection_unlock()
|
||||
|
||||
if inp not in ['yes','y','']:
|
||||
if inp not in ['yes', 'y', '']:
|
||||
raise AnsibleError("host connection rejected by user")
|
||||
|
||||
key._added_by_ansible_this_time = True
|
||||
|
@ -213,11 +213,11 @@ class Connection(ConnectionBase):
|
|||
if C.HOST_KEY_CHECKING:
|
||||
for ssh_known_hosts in ("/etc/ssh/ssh_known_hosts", "/etc/openssh/ssh_known_hosts"):
|
||||
try:
|
||||
#TODO: check if we need to look at several possible locations, possible for loop
|
||||
# TODO: check if we need to look at several possible locations, possible for loop
|
||||
ssh.load_system_host_keys(ssh_known_hosts)
|
||||
break
|
||||
except IOError:
|
||||
pass # file was not found, but not required to function
|
||||
pass # file was not found, but not required to function
|
||||
ssh.load_system_host_keys()
|
||||
|
||||
sock_kwarg = self._parse_proxy_command(port)
|
||||
|
@ -305,10 +305,10 @@ class Connection(ConnectionBase):
|
|||
display.debug("chunk is: %s" % chunk)
|
||||
if not chunk:
|
||||
if b'unknown user' in become_output:
|
||||
raise AnsibleError( 'user %s does not exist' % self._play_context.become_user)
|
||||
raise AnsibleError('user %s does not exist' % self._play_context.become_user)
|
||||
else:
|
||||
break
|
||||
#raise AnsibleError('ssh connection closed waiting for password prompt')
|
||||
# raise AnsibleError('ssh connection closed waiting for password prompt')
|
||||
become_output += chunk
|
||||
|
||||
# need to check every line because we might get lectured
|
||||
|
@ -441,7 +441,7 @@ class Connection(ConnectionBase):
|
|||
# (This doesn't acquire the connection lock because it needs
|
||||
# to exclude only other known_hosts writers, not connections
|
||||
# that are starting up.)
|
||||
lockfile = self.keyfile.replace("known_hosts",".known_hosts.lock")
|
||||
lockfile = self.keyfile.replace("known_hosts", ".known_hosts.lock")
|
||||
dirname = os.path.dirname(self.keyfile)
|
||||
makedirs_safe(dirname)
|
||||
|
||||
|
@ -457,7 +457,7 @@ class Connection(ConnectionBase):
|
|||
# gather information about the current key file, so
|
||||
# we can ensure the new file has the correct mode/owner
|
||||
|
||||
key_dir = os.path.dirname(self.keyfile)
|
||||
key_dir = os.path.dirname(self.keyfile)
|
||||
if os.path.exists(self.keyfile):
|
||||
key_stat = os.stat(self.keyfile)
|
||||
mode = key_stat.st_mode
|
||||
|
|
|
@ -135,6 +135,7 @@ class AnsibleControlPersistBrokenPipeError(AnsibleError):
|
|||
''' ControlPersist broken pipe '''
|
||||
pass
|
||||
|
||||
|
||||
def _ssh_retry(func):
|
||||
"""
|
||||
Decorator to retry ssh/scp/sftp in the case of a connection failure
|
||||
|
@ -365,12 +366,14 @@ class Connection(ConnectionBase):
|
|||
|
||||
user = self._play_context.remote_user
|
||||
if user:
|
||||
self._add_args(b_command,
|
||||
(b"-o", b"User=" + to_bytes(self._play_context.remote_user, errors='surrogate_or_strict')),
|
||||
self._add_args(
|
||||
b_command,
|
||||
(b"-o", b"User=" + to_bytes(self._play_context.remote_user, errors='surrogate_or_strict')),
|
||||
u"ANSIBLE_REMOTE_USER/remote_user/ansible_user/user/-u set"
|
||||
)
|
||||
|
||||
self._add_args(b_command,
|
||||
self._add_args(
|
||||
b_command,
|
||||
(b"-o", b"ConnectTimeout=" + to_bytes(self._play_context.timeout, errors='surrogate_or_strict', nonstring='simplerepr')),
|
||||
u"ANSIBLE_TIMEOUT/timeout set"
|
||||
)
|
||||
|
@ -459,7 +462,7 @@ class Connection(ConnectionBase):
|
|||
display_line = to_text(b_line).rstrip('\r\n')
|
||||
suppress_output = False
|
||||
|
||||
#display.debug("Examining line (source=%s, state=%s): '%s'" % (source, state, display_line))
|
||||
# display.debug("Examining line (source=%s, state=%s): '%s'" % (source, state, display_line))
|
||||
if self._play_context.prompt and self.check_password_prompt(b_line):
|
||||
display.debug("become_prompt: (source=%s, state=%s): '%s'" % (source, state, display_line))
|
||||
self._flags['become_prompt'] = True
|
||||
|
@ -594,7 +597,7 @@ class Connection(ConnectionBase):
|
|||
for fd in (p.stdout, p.stderr):
|
||||
fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)
|
||||
|
||||
### TODO: bcoca would like to use SelectSelector() when open
|
||||
# TODO: bcoca would like to use SelectSelector() when open
|
||||
# filehandles is low, then switch to more efficient ones when higher.
|
||||
# select is faster when filehandles is low.
|
||||
selector = selectors.DefaultSelector()
|
||||
|
@ -839,8 +842,8 @@ class Connection(ConnectionBase):
|
|||
if returncode == 255:
|
||||
raise AnsibleConnectionFailure("Failed to connect to the host via %s: %s" % (method, to_native(stderr)))
|
||||
else:
|
||||
raise AnsibleError("failed to transfer file to {0} {1}:\n{2}\n{3}"\
|
||||
.format(to_native(in_path), to_native(out_path), to_native(stdout), to_native(stderr)))
|
||||
raise AnsibleError("failed to transfer file to %s %s:\n%s\n%s" %
|
||||
(to_native(in_path), to_native(out_path), to_native(stdout), to_native(stderr)))
|
||||
|
||||
#
|
||||
# Main public methods
|
||||
|
@ -852,7 +855,6 @@ class Connection(ConnectionBase):
|
|||
|
||||
display.vvv(u"ESTABLISH SSH CONNECTION FOR USER: {0}".format(self._play_context.remote_user), host=self._play_context.remote_addr)
|
||||
|
||||
|
||||
# we can only use tty when we are not pipelining the modules. piping
|
||||
# data into /usr/bin/python inside a tty automatically invokes the
|
||||
# python interactive-mode but the modules are not compatible with the
|
||||
|
|
|
@ -74,15 +74,15 @@ class Connection(ConnectionBase):
|
|||
become_methods = ['runas']
|
||||
allow_executable = False
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
||||
self.has_pipelining = True
|
||||
self.has_pipelining = True
|
||||
self.always_pipeline_modules = True
|
||||
self.has_native_async = True
|
||||
self.protocol = None
|
||||
self.shell_id = None
|
||||
self.delegate = None
|
||||
self._shell_type = 'powershell'
|
||||
self.protocol = None
|
||||
self.shell_id = None
|
||||
self.delegate = None
|
||||
self._shell_type = 'powershell'
|
||||
# FUTURE: Add runas support
|
||||
|
||||
super(Connection, self).__init__(*args, **kwargs)
|
||||
|
@ -109,13 +109,13 @@ class Connection(ConnectionBase):
|
|||
self._become_user = self._play_context.become_user
|
||||
self._become_pass = self._play_context.become_pass
|
||||
|
||||
self._kinit_cmd = hostvars.get('ansible_winrm_kinit_cmd', 'kinit')
|
||||
self._kinit_cmd = hostvars.get('ansible_winrm_kinit_cmd', 'kinit')
|
||||
|
||||
if hasattr(winrm, 'FEATURE_SUPPORTED_AUTHTYPES'):
|
||||
self._winrm_supported_authtypes = set(winrm.FEATURE_SUPPORTED_AUTHTYPES)
|
||||
else:
|
||||
# for legacy versions of pywinrm, use the values we know are supported
|
||||
self._winrm_supported_authtypes = set(['plaintext','ssl','kerberos'])
|
||||
self._winrm_supported_authtypes = set(['plaintext', 'ssl', 'kerberos'])
|
||||
|
||||
# TODO: figure out what we want to do with auto-transport selection in the face of NTLM/Kerb/CredSSP/Cert/Basic
|
||||
transport_selector = 'ssl' if self._winrm_scheme == 'https' else 'plaintext'
|
||||
|
@ -191,7 +191,7 @@ class Connection(ConnectionBase):
|
|||
Establish a WinRM connection over HTTP/HTTPS.
|
||||
'''
|
||||
display.vvv("ESTABLISH WINRM CONNECTION FOR USER: %s on PORT %s TO %s" %
|
||||
(self._winrm_user, self._winrm_port, self._winrm_host), host=self._winrm_host)
|
||||
(self._winrm_user, self._winrm_port, self._winrm_host), host=self._winrm_host)
|
||||
netloc = '%s:%d' % (self._winrm_host, self._winrm_port)
|
||||
endpoint = urlunsplit((self._winrm_scheme, netloc, self._winrm_path, '', ''))
|
||||
errors = []
|
||||
|
@ -320,7 +320,7 @@ class Connection(ConnectionBase):
|
|||
payload_bytes = to_bytes(payload)
|
||||
byte_count = len(payload_bytes)
|
||||
for i in range(0, byte_count, buffer_size):
|
||||
yield payload_bytes[i:i+buffer_size], i+buffer_size >= byte_count
|
||||
yield payload_bytes[i:i + buffer_size], i + buffer_size >= byte_count
|
||||
|
||||
def exec_command(self, cmd, in_data=None, sudoable=True):
|
||||
super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable)
|
||||
|
@ -349,7 +349,6 @@ class Connection(ConnectionBase):
|
|||
|
||||
return (result.status_code, result.std_out, result.std_err)
|
||||
|
||||
|
||||
def exec_command_old(self, cmd, in_data=None, sudoable=True):
|
||||
super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable)
|
||||
cmd_parts = shlex.split(to_bytes(cmd), posix=False)
|
||||
|
@ -406,7 +405,7 @@ class Connection(ConnectionBase):
|
|||
in_size = os.path.getsize(to_bytes(in_path, errors='surrogate_or_strict'))
|
||||
offset = 0
|
||||
with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file:
|
||||
for out_data in iter((lambda:in_file.read(buffer_size)), ''):
|
||||
for out_data in iter((lambda: in_file.read(buffer_size)), ''):
|
||||
offset += len(out_data)
|
||||
self._display.vvvvv('WINRM PUT "%s" to "%s" (offset=%d size=%d)' % (in_path, out_path, offset, len(out_data)), host=self._winrm_host)
|
||||
# yes, we're double-encoding over the wire in this case- we want to ensure that the data shipped to the end PS pipeline is still b64-encoded
|
||||
|
|
|
@ -71,8 +71,8 @@ class Connection(ConnectionBase):
|
|||
|
||||
def list_zones(self):
|
||||
process = subprocess.Popen([self.zoneadm_cmd, 'list', '-ip'],
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
|
||||
zones = []
|
||||
for l in process.stdout.readlines():
|
||||
|
@ -84,13 +84,13 @@ class Connection(ConnectionBase):
|
|||
return zones
|
||||
|
||||
def get_zone_path(self):
|
||||
#solaris10vm# zoneadm -z cswbuild list -p
|
||||
#-:cswbuild:installed:/zones/cswbuild:479f3c4b-d0c6-e97b-cd04-fd58f2c0238e:native:shared
|
||||
# solaris10vm# zoneadm -z cswbuild list -p
|
||||
# -:cswbuild:installed:/zones/cswbuild:479f3c4b-d0c6-e97b-cd04-fd58f2c0238e:native:shared
|
||||
process = subprocess.Popen([self.zoneadm_cmd, '-z', to_bytes(self.zone), 'list', '-p'],
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
|
||||
#stdout, stderr = p.communicate()
|
||||
# stdout, stderr = p.communicate()
|
||||
path = process.stdout.readlines()[0].split(':')[3]
|
||||
return path + '/root'
|
||||
|
||||
|
@ -109,7 +109,7 @@ class Connection(ConnectionBase):
|
|||
compared to exec_command() it looses some niceties like being able to
|
||||
return the process's exit code immediately.
|
||||
'''
|
||||
# Note: zlogin invokes a shell (just like ssh does) so we do not pass
|
||||
# NOTE: zlogin invokes a shell (just like ssh does) so we do not pass
|
||||
# this through /bin/sh -c here. Instead it goes through the shell
|
||||
# that zlogin selects.
|
||||
local_cmd = [self.zlogin_cmd, self.zone, cmd]
|
||||
|
@ -117,7 +117,7 @@ class Connection(ConnectionBase):
|
|||
|
||||
display.vvv("EXEC %s" % (local_cmd), host=self.zone)
|
||||
p = subprocess.Popen(local_cmd, shell=False, stdin=stdin,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
|
||||
return p
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue