def put_file(self, in_path, out_path): ''' transfer a file from local to remote ''' super(Connection, self).put_file(in_path, out_path) self._display.vvv("PUT {0} TO {1}".format(in_path, out_path), host=self.host) if not os.path.exists(in_path): raise AnsibleFileNotFound( "file or module does not exist: {0}".format(in_path)) cmd = self._password_cmd() if C.DEFAULT_SCP_IF_SSH: cmd.append('scp') cmd.extend(self._common_args) cmd.extend( [in_path, '{0}:{1}'.format(self.host, pipes.quote(out_path))]) indata = None else: cmd.append('sftp') cmd.extend(self._common_args) cmd.append(self.host) indata = "put {0} {1}\n".format(pipes.quote(in_path), pipes.quote(out_path)) (p, stdin) = self._run(cmd, indata) self._send_password() (returncode, stdout, stderr) = self._communicate(p, stdin, indata) if returncode != 0: raise AnsibleError( "failed to transfer file to {0}:\n{1}\n{2}".format( out_path, stdout, stderr))
def _get_file_contents(self, file_name): ''' Reads the file contents from the given file name If the contents are vault-encrypted, it will decrypt them and return the decrypted data :arg file_name: The name of the file to read. If this is a relative path, it will be expanded relative to the basedir :raises AnsibleFileNotFound: if the file_name does not refer to a file :raises AnsibleParserError: if we were unable to read the file :return: Returns a byte string of the file contents ''' if not file_name or not isinstance(file_name, (binary_type, text_type)): raise AnsibleParserError("Invalid filename: '%s'" % to_native(file_name)) b_file_name = to_bytes(self.path_dwim(file_name)) # This is what we really want but have to fix unittests to make it pass # if not os.path.exists(b_file_name) or not os.path.isfile(b_file_name): if not self.path_exists(b_file_name): raise AnsibleFileNotFound("Unable to retrieve file contents", file_name=file_name) try: with open(b_file_name, 'rb') as f: data = f.read() return self._decrypt_if_vault_data(data, b_file_name) except (IOError, OSError) as e: raise AnsibleParserError( "an error occurred while trying to read the file '%s': %s" % (file_name, to_native(e)), orig_exc=e)
def read_vault_password_file(self, vault_password_file): """ Read a vault password from a file or if executable, execute the script and retrieve password from STDOUT """ this_path = os.path.realpath(to_bytes(os.path.expanduser(vault_password_file), errors='strict')) if not os.path.exists(to_bytes(this_path, errors='strict')): raise AnsibleFileNotFound("The vault password file %s was not found" % this_path) if self.is_executable(this_path): try: # STDERR not captured to make it easier for users to prompt for input in their scripts p = subprocess.Popen(this_path, stdout=subprocess.PIPE) except OSError as e: raise AnsibleError("Problem running vault password script %s (%s). If this is not a script, remove the executable bit from the file." % (' '.join(this_path), e)) stdout, stderr = p.communicate() self.set_vault_password(stdout.strip('\r\n')) else: try: f = open(this_path, "rb") self.set_vault_password(f.read().strip()) f.close() except (OSError, IOError) as e: raise AnsibleError("Could not read vault password file %s: %s" % (this_path, e))
def set_vault_password(loader, filename): if version_info < (2,4): this_path = os.path.realpath(os.path.expanduser(filename)) if not os.path.exists(this_path): raise AnsibleFileNotFound("The vault password file %s was not found" % this_path) if loader.is_executable(this_path): try: p = subprocess.Popen(this_path, stdout=subprocess.PIPE) except OSError as e: raise AnsibleError("Problem running vault password script %s." % (' '.join(this_path))) stdout, stderr = p.communicate() try: loader.set_vault_password(stdout.strip('\r\n')) except TypeError: loader.set_vault_password(stdout.decode('utf-8').strip('\r\n')) else: try: f = open(this_path, "rb") try: loader.set_vault_password(f.read().strip()) except TypeError: loader.set_vault_password(f.read().decode('utf-8').strip()) f.close() except (OSError, IOError) as e: raise AnsibleError("Could not read vault password file %s: %s" % (this_path, e)) else: from ansible.parsing.vault import get_file_vault_secret file_vault_secret = get_file_vault_secret(filename, loader=loader) file_vault_secret.load() loader.set_vault_secrets([('default', file_vault_secret)])
def _get_file_contents(self, file_name): ''' Reads the file contents from the given file name, and will decrypt them if they are found to be vault-encrypted. ''' if not file_name or not isinstance(file_name, string_types): raise AnsibleParserError("Invalid filename: '%s'" % str(file_name)) b_file_name = to_bytes(file_name) if not self.path_exists(b_file_name) or not self.is_file(b_file_name): raise AnsibleFileNotFound("the file_name '%s' does not exist, or is not readable" % file_name) show_content = True try: with open(b_file_name, 'rb') as f: data = f.read() if self._vault.is_encrypted(data): data = self._vault.decrypt(data, filename=b_file_name) show_content = False data = to_unicode(data, errors='strict') return (data, show_content) except (IOError, OSError) as e: raise AnsibleParserError("an error occurred while trying to read the file '%s': %s" % (file_name, str(e)))
def _load_file(self, file_name): if not file_name or not isinstance(file_name, string_types): raise AnsibleParserError("Invalid filename: '%s'" % to_native(file_name)) b_file_name = to_bytes(self.loader.path_dwim(file_name)) if not self.loader.path_exists(b_file_name): raise AnsibleFileNotFound("Unable to retrieve file contents", file_name=file_name) try: (b_data, private) = self.loader._get_file_contents(file_name) return toml.loads(to_text(b_data, errors='surrogate_or_strict')) except toml.TomlDecodeError as e: raise AnsibleParserError('TOML file (%s) is invalid: %s' % (file_name, to_native(e)), orig_exc=e) except (IOError, OSError) as e: raise AnsibleParserError( "An error occurred while trying to read the file '%s': %s" % (file_name, to_native(e)), orig_exc=e) except Exception as e: raise AnsibleParserError( "An unexpected error occurred while parsing the file '%s': %s" % (file_name, to_native(e)), orig_exc=e)
def put_file(self, in_path, out_path): """ Transfer a file from local to the container """ super(Connection, self).put_file(in_path, out_path) display.vvv("PUT %s TO %s" % (in_path, out_path), host=self._play_context.remote_addr) out_path = self._prefix_login_path(out_path) if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')): raise AnsibleFileNotFound( "file or module does not exist: %s" % in_path) out_path = shlex_quote(out_path) # kubectl doesn't have native support for copying files into # running containers, so we use kubectl exec to implement this with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file: if not os.fstat(in_file.fileno()).st_size: count = ' count=0' else: count = '' args, dummy = self._build_exec_cmd([self._play_context.executable, "-c", "dd of=%s bs=%s%s" % (out_path, BUFSIZE, count)]) args = [to_bytes(i, errors='surrogate_or_strict') for i in args] try: p = subprocess.Popen(args, stdin=in_file, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except OSError: raise AnsibleError("kubectl connection requires dd command in the container to put files") stdout, stderr = p.communicate() if p.returncode != 0: raise AnsibleError("failed to transfer file %s to %s:\n%s\n%s" % (in_path, out_path, stdout, stderr))
def put_file(self, in_path, out_path): """Put file.""" super(Connection, self).put_file(in_path, out_path) if not exists(to_bytes(in_path, errors="surrogate_or_strict")): raise AnsibleFileNotFound("file or module does not exist: '%s'" % to_native(in_path)) try: put_url = self.fileManager.InitiateFileTransferToGuest( vm=self.vm, auth=self.vm_auth, guestFilePath=out_path, fileAttributes=vim.GuestFileAttributes(), fileSize=getsize(in_path), overwrite=True ) except vim.fault.NoPermission as e: raise AnsibleError("No Permission Error: %s %s" % (to_native(e.msg), to_native(e.privilegeId))) except vmodl.fault.SystemError as e: if e.reason == 'vix error codes = (3016, 0).\n': raise AnsibleConnectionFailure( "Connection failed, is the vm currently rebooting? Reason: %s" % ( to_native(e.reason) ) ) else: raise AnsibleConnectionFailure("Connection failed. Reason %s" % (to_native(e.reason))) except vim.fault.GuestOperationsUnavailable: raise AnsibleConnectionFailure("Cannot connect to guest. Native error: GuestOperationsUnavailable") url = self._fix_url_for_hosts(put_url) # file size of 'in_path' must be greater than 0 with open(in_path, "rb") as fd: response = requests.put(url, verify=self.validate_certs, data=fd) if response.status_code != 200: raise AnsibleError("File transfer failed")
def put_file(self, in_path, out_path): """Put file.""" super(Connection, self).put_file(in_path, out_path) if not exists(to_bytes(in_path, errors="surrogate_or_strict")): raise AnsibleFileNotFound("file or module does not exist: '%s'" % to_native(in_path)) try: put_url = self.fileManager.InitiateFileTransferToGuest( vm=self.vm, auth=self.vm_auth, guestFilePath=out_path, fileAttributes=vim.GuestFileAttributes(), fileSize=getsize(in_path), overwrite=True) except vim.fault.NoPermission as e: raise AnsibleError("No Permission Error: %s %s" % (to_native(e.msg), to_native(e.privilegeId))) url = self._fix_url_for_hosts(put_url) # file size of 'in_path' must be greater than 0 with open(in_path, "rb") as fd: response = requests.put(url, verify=self.validate_certs, data=fd) if response.status_code != 200: raise AnsibleError("File transfer failed")
def put_file(self, in_path, out_path): ''' transfer a file from local to remote ''' display.vvv("PUT %s TO %s" % (in_path, out_path), host=self._play_context.remote_addr) if not os.path.exists(in_path): raise AnsibleFileNotFound("file or module does not exist: %s" % in_path) fd = file(in_path, 'rb') fstat = os.stat(in_path) try: display.vvv("PUT file is %d bytes" % fstat.st_size, host=self._play_context.remote_addr) last = False while fd.tell() <= fstat.st_size and not last: display.vvvv("file position currently %ld, file size is %ld" % (fd.tell(), fstat.st_size), host=self._play_context.remote_addr) data = fd.read(CHUNK_SIZE) if fd.tell() >= fstat.st_size: last = True data = dict(mode='put', data=base64.b64encode(data), out_path=out_path, last=last) if self._play_context.become: data['user'] = self._play_context.become_user data = jsonify(data) data = keyczar_encrypt(self.key, data) if self.send_data(data): raise AnsibleError("failed to send the file to %s" % self._play_context.remote_addr) response = self.recv_data() if not response: raise AnsibleError("Failed to get a response from %s" % self._play_context.remote_addr) response = keyczar_decrypt(self.key, response) response = json.loads(response) if response.get('failed', False): raise AnsibleError( "failed to put the file in the requested location") finally: fd.close() display.vvvv("waiting for final response after PUT", host=self._play_context.remote_addr) response = self.recv_data() if not response: raise AnsibleError("Failed to get a response from %s" % self._play_context.remote_addr) response = keyczar_decrypt(self.key, response) response = json.loads(response) if response.get('failed', False): raise AnsibleError( "failed to put the file in the requested location")
def path_dwim_relative_stack(self, paths, dirname, source, is_role=False): ''' find one file in first path in stack taking roles into account and adding play basedir as fallback :arg paths: A list of text strings which are the paths to look for the filename in. :arg dirname: A text string representing a directory. The directory is prepended to the source to form the path to search for. :arg source: A text string which is the filename to search for :rtype: A text string :returns: An absolute path to the filename ``source`` if found :raises: An AnsibleFileNotFound Exception if the file is found to exist in the search paths ''' b_dirname = to_bytes(dirname, errors='surrogate_or_strict') b_source = to_bytes(source, errors='surrogate_or_strict') result = None search = [] if source is None: display.warning('Invalid request to find a file that matches a "null" value') elif source and (source.startswith('~') or source.startswith(os.path.sep)): # path is absolute, no relative needed, check existence and return source test_path = unfrackpath(b_source, follow=False) if os.path.exists(to_bytes(test_path, errors='surrogate_or_strict')): result = test_path else: display.debug(u'evaluation_path:\n\t%s' % '\n\t'.join(paths)) for path in paths: upath = unfrackpath(path, follow=False) b_upath = to_bytes(upath, errors='surrogate_or_strict') b_pb_base_dir = os.path.dirname(b_upath) # if path is in role and 'tasks' not there already, add it into the search if (is_role or self._is_role(path)) and b_pb_base_dir.endswith(b'/tasks'): search.append(os.path.join(os.path.dirname(b_pb_base_dir), b_dirname, b_source)) search.append(os.path.join(b_pb_base_dir, b_source)) else: # don't add dirname if user already is using it in source if b_source.split(b'/')[0] != dirname: search.append(os.path.join(b_upath, b_dirname, b_source)) search.append(os.path.join(b_upath, b_source)) # always append basedir as last resort # don't add dirname if user already is using it in source if b_source.split(b'/')[0] != dirname: search.append(os.path.join(to_bytes(self.get_basedir(), errors='surrogate_or_strict'), b_dirname, b_source)) search.append(os.path.join(to_bytes(self.get_basedir(), errors='surrogate_or_strict'), b_source)) display.debug(u'search_path:\n\t%s' % to_text(b'\n\t'.join(search))) for b_candidate in search: display.vvvvv(u'looking for "%s" at "%s"' % (source, to_text(b_candidate))) if os.path.exists(b_candidate): result = to_text(b_candidate) break if result is None: raise AnsibleFileNotFound(file_name=source, paths=[to_native(p) for p in search]) return result
def put_file(self, in_path, out_path): ''' transfer a file from local to remote ''' super(Connection, self).put_file(in_path, out_path) display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self.host) if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')): raise AnsibleFileNotFound("file or module does not exist: {0}".format(to_native(in_path))) # scp and sftp require square brackets for IPv6 addresses, but # accept them for hostnames and IPv4 addresses too. host = '[%s]' % self.host # since this can be a non-bool now, we need to handle it correctly scp_if_ssh = C.DEFAULT_SCP_IF_SSH if not isinstance(scp_if_ssh, bool): scp_if_ssh = scp_if_ssh.lower() if scp_if_ssh in BOOLEANS: scp_if_ssh = boolean(scp_if_ssh) elif scp_if_ssh != 'smart': raise AnsibleOptionsError('scp_if_ssh needs to be one of [smart|True|False]') # create a list of commands to use based on config options methods = ['sftp'] if scp_if_ssh == 'smart': methods.append('scp') elif scp_if_ssh: methods = ['scp'] success = False res = None for method in methods: if method == 'sftp': cmd = self._build_command('sftp', to_bytes(host)) in_data = u"put {0} {1}\n".format(pipes.quote(in_path), pipes.quote(out_path)) elif method == 'scp': cmd = self._build_command('scp', in_path, u'{0}:{1}'.format(host, pipes.quote(out_path))) in_data = None in_data = to_bytes(in_data, nonstring='passthru') (returncode, stdout, stderr) = self._run(cmd, in_data, checkrc=False) # Check the return code and rollover to next method if failed if returncode == 0: success = True break else: # If not in smart mode, the data will be printed by the raise below if scp_if_ssh == 'smart': display.warning(msg='%s transfer mechanism failed on %s. Use ANSIBLE_DEBUG=1 to see detailed information' % (method, host)) display.debug(msg='%s' % to_native(stdout)) display.debug(msg='%s' % to_native(stderr)) res = (returncode, stdout, stderr) if not success: raise AnsibleError("failed to transfer file to {0}:\n{1}\n{2}"\ .format(to_native(out_path), to_native(res[1]), to_native(res[2])))
def put_file(self, in_path, out_path): ''' transfer a file from local to remote ''' super(Connection, self).put_file(in_path, out_path) display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self.host) if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')): raise AnsibleFileNotFound("file or module does not exist: {0}".format(to_native(in_path))) self._file_transport_command(in_path, out_path, 'put')
def put_file(self, in_path, out_path): super(Connection, self).put_file(in_path, out_path) out_path = self._shell._unquote(out_path) display.vvv('PUT "%s" TO "%s"' % (in_path, out_path), host=self._winrm_host) if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')): raise AnsibleFileNotFound('file or module does not exist: "%s"' % in_path) script_template = u''' begin {{ $path = '{0}' $DebugPreference = "Continue" $ErrorActionPreference = "Stop" Set-StrictMode -Version 2 $fd = [System.IO.File]::Create($path) $sha1 = [System.Security.Cryptography.SHA1CryptoServiceProvider]::Create() $bytes = @() #initialize for empty file case }} process {{ $bytes = [System.Convert]::FromBase64String($input) $sha1.TransformBlock($bytes, 0, $bytes.Length, $bytes, 0) | Out-Null $fd.Write($bytes, 0, $bytes.Length) }} end {{ $sha1.TransformFinalBlock($bytes, 0, 0) | Out-Null $hash = [System.BitConverter]::ToString($sha1.Hash).Replace("-", "").ToLowerInvariant() $fd.Close() Write-Output "{{""sha1"":""$hash""}}" }} ''' script = script_template.format(self._shell._escape(out_path)) cmd_parts = self._shell._encode_script(script, as_list=True, strict_mode=False, preserve_rc=False) result = self._winrm_exec(cmd_parts[0], cmd_parts[1:], stdin_iterator=self._put_file_stdin_iterator(in_path, out_path)) # TODO: improve error handling if result.status_code != 0: raise AnsibleError(to_native(result.std_err)) put_output = json.loads(result.std_out) remote_sha1 = put_output.get("sha1") if not remote_sha1: raise AnsibleError("Remote sha1 was not returned") local_sha1 = secure_hash(in_path) if not remote_sha1 == local_sha1: raise AnsibleError("Remote sha1 hash {0} does not match local hash {1}".format(to_native(remote_sha1), to_native(local_sha1)))
def put_file(self, in_path, out_path): """ Transfer a file from local to docker container """ super(Connection, self).put_file(in_path, out_path) display.vvv("PUT %s TO %s" % (in_path, out_path), host=self._play_context.remote_addr) out_path = self._prefix_login_path(out_path) if not os.path.exists(in_path): raise AnsibleFileNotFound("file or module does not exist: %s" % in_path) if self.can_copy_bothways: # only docker >= 1.8.1 can do this natively args = [ self.docker_cmd, "cp", in_path, "%s:%s" % (self._play_context.remote_addr, out_path) ] args = map(to_bytes, args) p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() if p.returncode != 0: raise AnsibleError( "failed to transfer file %s to %s:\n%s\n%s" % (in_path, out_path, stdout, stderr)) else: out_path = pipes.quote(out_path) # Older docker doesn't have native support for copying files into # running containers, so we use docker exec to implement this executable = C.DEFAULT_EXECUTABLE.split( )[0] if C.DEFAULT_EXECUTABLE else '/bin/sh' args = [ self.docker_cmd, "exec", "-i", self._play_context.remote_addr, executable, "-c", "dd of=%s bs=%s" % (out_path, BUFSIZE) ] args = map(to_bytes, args) with open(in_path, 'rb') as in_file: try: p = subprocess.Popen(args, stdin=in_file, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except OSError: raise AnsibleError( "docker connection with docker < 1.8.1 requires dd command in the chroot" ) stdout, stderr = p.communicate() if p.returncode != 0: raise AnsibleError( "failed to transfer file %s to %s:\n%s\n%s" % (in_path, out_path, stdout, stderr))
def get_real_file(self, file_path, decrypt=True): """ If the file is vault encrypted return a path to a temporary decrypted file If the file is not encrypted then the path is returned Temporary files are cleanup in the destructor """ if not file_path or not isinstance(file_path, string_types): raise AnsibleParserError("Invalid filename: '%s'" % to_native(file_path)) b_file_path = to_bytes(file_path, errors='surrogate_or_strict') if not self.path_exists(b_file_path) or not self.is_file(b_file_path): raise AnsibleFileNotFound( "the file named '%s' does not exist, or is not accessible" % to_native(file_path)) if not self._vault: self._vault = VaultLib(b_password="") real_path = self.path_dwim(file_path) try: if decrypt: with open(to_bytes(real_path), 'rb') as f: # Limit how much of the file is read since we do not know # whether this is a vault file and therefore it could be very # large. if is_encrypted_file(f, count=len(b_HEADER)): # if the file is encrypted and no password was specified, # the decrypt call would throw an error, but we check first # since the decrypt function doesn't know the file name data = f.read() if not self._b_vault_password: raise AnsibleParserError( "A vault password must be specified to decrypt %s" % file_path) data = self._vault.decrypt(data, filename=real_path) # Make a temp file real_path = self._create_content_tempfile(data) self._tempfiles.add(real_path) return real_path except (IOError, OSError) as e: raise AnsibleParserError( "an error occurred while trying to read the file '%s': %s" % (to_native(real_path), to_native(e)), orig_exc=e)
def put_file(self, in_path, out_path): """ put a file from local to lxd """ super(Connection, self).put_file(in_path, out_path) self._display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self._host) if not os.path.isfile(to_bytes(in_path, errors='strict')): raise AnsibleFileNotFound("input path is not a file: %s" % in_path) local_cmd = [self._lxc_cmd, "file", "push", in_path, self._host + "/" + out_path] local_cmd = [to_bytes(i, errors='strict') for i in local_cmd] call(local_cmd)
def put_file(self, in_path, out_path): ''' transfer a file from local to local ''' super(Connection, self).put_file(in_path, out_path) display.vvv(u"{0} PUT {1} TO {2}".format(self._play_context.remote_addr, in_path, out_path)) if not os.path.exists(in_path): raise AnsibleFileNotFound("file or module does not exist: {0}".format(to_str(in_path))) try: shutil.copyfile(in_path, out_path) except shutil.Error: raise AnsibleError("failed to copy: {0} and {1} are the same".format(to_str(in_path), to_str(out_path))) except IOError as e: raise AnsibleError("failed to transfer file to {0}: {1}".format(to_str(out_path), to_str(e)))
def put_file(self, in_path, out_path): super(Connection, self).put_file(in_path, out_path) out_path = self._shell._unquote(out_path) self._display.vvv('PUT "%s" TO "%s"' % (in_path, out_path), host=self._play_context.remote_addr) if not os.path.exists(in_path): raise AnsibleFileNotFound('file or module does not exist: "%s"' % in_path) with open(in_path) as in_file: in_size = os.path.getsize(in_path) script_template = ''' $s = [System.IO.File]::OpenWrite("%s"); [void]$s.Seek(%d, [System.IO.SeekOrigin]::Begin); $b = [System.Convert]::FromBase64String("%s"); [void]$s.Write($b, 0, $b.length); [void]$s.SetLength(%d); [void]$s.Close(); ''' # Determine max size of data we can pass per command. script = script_template % (self._shell._escape(out_path), in_size, '', in_size) cmd = self._shell._encode_script(script) # Encode script with no data, subtract its length from 8190 (max # windows command length), divide by 2.67 (UTF16LE base64 command # encoding), then by 1.35 again (data base64 encoding). buffer_size = int(((8190 - len(cmd)) / 2.67) / 1.35) for offset in xrange(0, in_size, buffer_size): try: out_data = in_file.read(buffer_size) if offset == 0: if out_data.lower().startswith( '#!powershell' ) and not out_path.lower().endswith('.ps1'): out_path = out_path + '.ps1' b64_data = base64.b64encode(out_data) script = script_template % (self._shell._escape(out_path), offset, b64_data, in_size) self._display.vvvvv( 'WINRM PUT "%s" to "%s" (offset=%d size=%d)' % (in_path, out_path, offset, len(out_data)), host=self._play_context.remote_addr) cmd_parts = self._shell._encode_script(script, as_list=True) result = self._winrm_exec(cmd_parts[0], cmd_parts[1:]) if result.status_code != 0: raise IOError(to_unicode(result.std_err)) except Exception: traceback.print_exc() raise AnsibleError('failed to transfer file to "%s"' % out_path)
def put_file(self, in_path, out_path): ''' transfer a file from local to remote ''' self._display.vvv("PUT %s TO %s" % (in_path, out_path), host=self._connection_info.remote_addr) if not os.path.exists(in_path): raise AnsibleFileNotFound("file or module does not exist: %s" % in_path) try: self.sftp = self.ssh.open_sftp() except Exception, e: raise AnsibleError("failed to open a SFTP connection (%s)" % e)
def put_file(self, in_path, out_path): ''' transfer a file from local to local ''' super(Connection, self).put_file(in_path, out_path) display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self._play_context.remote_addr) if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')): raise AnsibleFileNotFound("file or module does not exist: {0}".format(to_native(in_path))) try: shutil.copyfile(to_bytes(in_path, errors='surrogate_or_strict'), to_bytes(out_path, errors='surrogate_or_strict')) except shutil.Error: raise AnsibleError("failed to copy: {0} and {1} are the same".format(to_native(in_path), to_native(out_path))) except IOError as e: raise AnsibleError("failed to transfer file to {0}: {1}".format(to_native(out_path), to_native(e)))
def find_file_in_search_path(self, myvars, subdir, needle): ''' Return a file (needle) in the task's expected search path. ''' if 'ansible_search_path' in myvars: paths = myvars['ansible_search_path'] else: paths = self.get_basedir(myvars) result = self._loader.path_dwim_relative_stack(paths, subdir, needle) if result is None: raise AnsibleFileNotFound("Unable to find '%s' in expected paths." % needle) return result
def _put_file(self, in_path, out_path): if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')): raise AnsibleFileNotFound( "file or module does not exist: {0}".format( to_native(in_path))) try: shutil.copyfile(to_bytes(in_path, errors='surrogate_or_strict'), to_bytes(out_path, errors='surrogate_or_strict')) except shutil.Error: raise AnsibleError( "failed to copy: {0} and {1} are the same".format( to_native(in_path), to_native(out_path))) except IOError as e: raise AnsibleError("failed to transfer file to {0}: {1}".format( to_native(out_path), to_native(e)))
def put_file(self, in_path, out_path): """ put a file from local to lxd """ super(Connection, self).put_file(in_path, out_path) self._display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self._host) if not os.path.isfile(to_bytes(in_path, errors='surrogate_or_strict')): raise AnsibleFileNotFound("input path is not a file: %s" % in_path) local_cmd = [self._lxc_cmd, "file", "push", in_path, self._host + "/" + out_path] local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd] process = Popen(local_cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) process.communicate()
def put_file(self, in_path, out_path): ''' transfer a file from local to local ''' super(Connection, self).put_file(in_path, out_path) self._display.vvv("{0} PUT {1} TO {2}".format(self._connection_info.remote_addr, in_path, out_path)) if not os.path.exists(in_path): raise AnsibleFileNotFound("file or module does not exist: {0}".format(in_path)) try: shutil.copyfile(in_path, out_path) except shutil.Error: traceback.print_exc() raise AnsibleError("failed to copy: {0} and {1} are the same".format(in_path, out_path)) except IOError: traceback.print_exc() raise AnsibleError("failed to transfer file to {0}".format(out_path))
def get_real_file(self, file_path): """ If the file is vault encrypted return a path to a temporary decrypted file If the file is not encrypted then the path is returned Temporary files are cleanup in the destructor """ if not file_path or not isinstance(file_path, string_types): raise AnsibleParserError("Invalid filename: '%s'" % to_str(file_path)) b_file_path = to_bytes(file_path, errors='strict') if not self.path_exists(b_file_path) or not self.is_file(b_file_path): raise AnsibleFileNotFound( "the file_name '%s' does not exist, or is not readable" % to_str(file_path)) if not self._vault: self._vault = VaultLib(password="") real_path = self.path_dwim(file_path) try: with open(to_bytes(real_path), 'rb') as f: if self._vault.is_encrypted(f): # if the file is encrypted and no password was specified, # the decrypt call would throw an error, but we check first # since the decrypt function doesn't know the file name data = f.read() if not self._vault_password: raise AnsibleParserError( "A vault password must be specified to decrypt %s" % file_path) data = self._vault.decrypt(data, filename=real_path) # Make a temp file real_path = self._create_content_tempfile(data) self._tempfiles.add(real_path) return real_path except (IOError, OSError) as e: raise AnsibleParserError( "an error occurred while trying to read the file '%s': %s" % (to_str(real_path), to_str(e)))
def has_mode(path, mode='0o644'): """ :param path: File or dir path :param mode: Expected mode of the target, e.g. '755', '1644' """ _assert_if_type_mismatch(path) _assert_if_type_mismatch(mode) if not os.path.exists(path): raise AnsibleFileNotFound("Does not exist: {}".format(repr(path))) if not re.match(r"^\d?\d{3}$", mode): raise AnsibleModuleError("mode must be in the form [0-9]+: " "{}".format(mode)) _mode = oct(os.stat(path).st_mode).replace('0o', '') return mode == _mode[len(_mode) - len(mode):]
def put_file(self, in_path, out_path): ''' transfer a file from local to remote ''' display.vvv("PUT %s TO %s" % (in_path, out_path), host=self._play_context.remote_addr) if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')): raise AnsibleFileNotFound("file or module does not exist: %s" % in_path) try: transport = self.ssh.get_transport() with transport.open_channel(kind='session') as channel: file_data = open('{}'.format(in_path), 'rb').read() channel.exec_command('shell cat > {}'.format(out_path)) channel.sendall(file_data) except Exception as e: raise AnsibleError("failed to transfer file (%s)" % e)
def put_file(self, in_path, out_path, task_vars=None): ''' transfer a file from local to remote ''' super(Connection, self).put_file(in_path, out_path) display.vvv("PUT %s TO %s" % (in_path, out_path), host=self._play_context.remote_addr) if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')): raise AnsibleFileNotFound("file or module does not exist: %s" % in_path) try: self.sftp = self.ssh.open_sftp() except Exception as e: raise AnsibleError("failed to open a SFTP connection (%s)" % e) try: callback = None if 'source' == os.path.basename(out_path) and task_vars: def callback(value, total): msg = dict( appid=task_vars['appid'], name=os.path.basename(in_path), value=value, plus=1 if value == total else 0, allsize=task_vars['total'], ) params = { 'udid': task_vars['webscoketid'], 'msg': json.dumps(msg) } url = settings.WEBSOCKET_NOTIFY_URL + '?' + urllib.urlencode( params) resp = urllib2.urlopen(urllib2.Request(url)) resp.close() print(value, total) self.sftp.put(to_bytes(in_path, errors='surrogate_or_strict'), to_bytes(out_path, errors='surrogate_or_strict'), callback) except IOError: raise AnsibleError("failed to transfer file to %s" % out_path)
def put_file(self, in_path, out_path): """ Transfer a file from local to docker container """ self._set_conn_data() super(Connection, self).put_file(in_path, out_path) display.vvv("PUT %s TO %s" % (in_path, out_path), host=self.get_option('remote_addr')) out_path = self._prefix_login_path(out_path) if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')): raise AnsibleFileNotFound("file or module does not exist: %s" % to_native(in_path)) out_path = shlex_quote(out_path) # Older docker doesn't have native support for copying files into # running containers, so we use docker exec to implement this # Although docker version 1.8 and later provide support, the # owner and group of the files are always set to root with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file: if not os.fstat(in_file.fileno()).st_size: count = ' count=0' else: count = '' args = self._build_exec_cmd([ self._play_context.executable, "-c", "dd of=%s bs=%s%s" % (out_path, BUFSIZE, count) ]) args = [to_bytes(i, errors='surrogate_or_strict') for i in args] try: p = subprocess.Popen(args, stdin=in_file, 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() if p.returncode != 0: raise AnsibleError( "failed to transfer file %s to %s:\n%s\n%s" % (to_native(in_path), to_native(out_path), to_native(stdout), to_native(stderr)))