def get( src, dest, add_deploy_dir=True, create_local_dir=False, force=False, ): """ Download a file from the remote system. + src: the remote filename to download + dest: the local filename to download the file to + add_deploy_dir: dest is relative to the deploy directory + create_local_dir: create the local directory if it doesn't exist + force: always download the file, even if the local copy matches Note: This operation is not suitable for large files as it may involve copying the remote file before downloading it. **Example:** .. code:: python files.get( name="Download a file from a remote", src="/etc/centos-release", dest="/tmp/whocares", ) """ if add_deploy_dir and state.cwd: dest = os.path.join(state.cwd, dest) if create_local_dir: local_pathname = os.path.dirname(dest) if not os.path.exists(local_pathname): os.makedirs(local_pathname) remote_file = host.get_fact(File, path=src) # No remote file, so assume exists and download it "blind" if not remote_file or force: yield FileDownloadCommand( src, dest, remote_temp_filename=state.get_temp_filename(dest)) # No local file, so always download elif not os.path.exists(dest): yield FileDownloadCommand( src, dest, remote_temp_filename=state.get_temp_filename(dest)) # Remote file exists - check if it matches our local else: local_sum = get_file_sha1(dest) remote_sum = host.get_fact(Sha1File, path=src) # Check sha1sum, upload if needed if local_sum != remote_sum: yield FileDownloadCommand( src, dest, remote_temp_filename=state.get_temp_filename(dest))
def get( state, host, remote_filename, local_filename, add_deploy_dir=True, create_local_dir=False, force=False, ): ''' Download a file from the remote system. + remote_filename: the remote filename to download + local_filename: the local filename to download the file to + add_deploy_dir: local_filename is relative to the deploy directory + create_local_dir: create the local directory if it doesn't exist + force: always download the file, even if the local copy matches Note: This operation is not suitable for large files as it may involve copying the remote file before downloading it. Example: .. code:: python files.get( {'Download a file from a remote'}, '/etc/centos-release', '/tmp/whocares', ) ''' if add_deploy_dir and state.deploy_dir: local_filename = path.join(state.deploy_dir, local_filename) if create_local_dir: local_pathname = path.dirname(local_filename) if not path.exists(local_pathname): makedirs(local_pathname) remote_file = host.fact.file(remote_filename) # No remote file, so assume exists and download it "blind" if not remote_file or force: yield ('download', remote_filename, local_filename) # No local file, so always download elif not path.exists(local_filename): yield ('download', remote_filename, local_filename) # Remote file exists - check if it matches our local else: local_sum = get_file_sha1(local_filename) remote_sum = host.fact.sha1_file(remote_filename) # Check sha1sum, upload if needed if local_sum != remote_sum: yield ('download', remote_filename, local_filename)
def get( src, dest, add_deploy_dir=True, create_local_dir=False, force=False, state=None, host=None, ): ''' Download a file from the remote system. + src: the remote filename to download + dest: the local filename to download the file to + add_deploy_dir: dest is relative to the deploy directory + create_local_dir: create the local directory if it doesn't exist + force: always download the file, even if the local copy matches Note: This operation is not suitable for large files as it may involve copying the remote file before downloading it. Example: .. code:: python files.get( name='Download a file from a remote', src='/etc/centos-release', dest='/tmp/whocares', ) ''' src = escape_unix_path(src) if add_deploy_dir and state.deploy_dir: dest = os_path.join(state.deploy_dir, dest) if create_local_dir: local_pathname = os_path.dirname(dest) if not os_path.exists(local_pathname): makedirs(local_pathname) remote_file = host.fact.file(src) # No remote file, so assume exists and download it "blind" if not remote_file or force: yield FileDownloadCommand(src, dest) # No local file, so always download elif not os_path.exists(dest): yield FileDownloadCommand(src, dest) # Remote file exists - check if it matches our local else: local_sum = get_file_sha1(dest) remote_sum = host.fact.sha1_file(src) # Check sha1sum, upload if needed if local_sum != remote_sum: yield FileDownloadCommand(src, dest)
def put( src, dest, user=None, group=None, mode=None, add_deploy_dir=True, create_remote_dir=True, force=False, assume_exists=False, state=None, host=None, ): ''' Upload a local file to the remote system. + src: local filename to upload + dest: remote filename to upload to + user: user to own the files + group: group to own the files + mode: permissions of the files + add_deploy_dir: src is relative to the deploy directory + create_remote_dir: create the remote directory if it doesn't exist + force: always upload the file, even if the remote copy matches + assume_exists: whether to assume the local file exists ``create_remote_dir``: If the remote directory does not exist it will be created using the same user & group as passed to ``files.put``. The mode will *not* be copied over, if this is required call ``files.directory`` separately. Note: This operation is not suitable for large files as it may involve copying the file before uploading it. Examples: .. code:: python # Note: This requires a 'files/motd' file on the local filesystem files.put( name='Update the message of the day file', src='files/motd', dest='/etc/motd', mode='644', ) ''' dest = escape_unix_path(dest) # Upload IO objects as-is if hasattr(src, 'read'): local_file = src # Assume string filename else: # Add deploy directory? if add_deploy_dir and state.deploy_dir: src = os_path.join(state.deploy_dir, src) local_file = src if not assume_exists and not os_path.isfile(local_file): raise IOError('No such file: {0}'.format(local_file)) mode = ensure_mode_int(mode) remote_file = host.fact.file(dest) if create_remote_dir: yield _create_remote_dir(state, host, dest, user, group) # No remote file, always upload and user/group/mode if supplied if not remote_file or force: yield FileUploadCommand(local_file, dest) if user or group: yield chown(dest, user, group) if mode: yield chmod(dest, mode) # File exists, check sum and check user/group/mode if supplied else: local_sum = get_file_sha1(src) remote_sum = host.fact.sha1_file(dest) # Check sha1sum, upload if needed if local_sum != remote_sum: yield FileUploadCommand(local_file, dest) if user or group: yield chown(dest, user, group) if mode: yield chmod(dest, mode) else: changed = False # Check mode if mode and remote_file['mode'] != mode: yield chmod(dest, mode) changed = True # Check user/group if ((user and remote_file['user'] != user) or (group and remote_file['group'] != group)): yield chown(dest, user, group) changed = True if not changed: host.noop('file {0} is already uploaded'.format(dest))
def put( state, host, local_filename, remote_filename, user=None, group=None, mode=None, add_deploy_dir=True, ): ''' Copy a local file to the remote system. + local_filename: local filename + remote_filename: remote filename + user: user to own the files + group: group to own the files + mode: permissions of the files ''' # Upload IO objects as-is if hasattr(local_filename, 'read'): local_file = local_filename # Assume string filename else: # Add deploy directory? if add_deploy_dir and state.deploy_dir: local_filename = path.join(state.deploy_dir, local_filename) local_file = local_filename if not path.isfile(local_file): raise IOError('No such file: {0}'.format(local_file)) mode = ensure_mode_int(mode) remote_file = host.fact.file(remote_filename) # No remote file, always upload and user/group/mode if supplied if not remote_file: yield (local_file, remote_filename) if user or group: yield chown(remote_filename, user, group) if mode: yield chmod(remote_filename, mode) # File exists, check sum and check user/group/mode if supplied else: local_sum = get_file_sha1(local_filename) remote_sum = host.fact.sha1_file(remote_filename) # Check sha1sum, upload if needed if local_sum != remote_sum: yield (local_file, remote_filename) if user or group: yield chown(remote_filename, user, group) if mode: yield chmod(remote_filename, mode) else: # Check mode if mode and remote_file['mode'] != mode: yield chmod(remote_filename, mode) # Check user/group if ( (user and remote_file['user'] != user) or (group and remote_file['group'] != group) ): yield chown(remote_filename, user, group)
def put( src, dest, user=None, group=None, mode=None, add_deploy_dir=True, create_remote_dir=True, force=False, assume_exists=False, ): """ Upload a local file, or file-like object, to the remote system. + src: filename or IO-like object to upload + dest: remote filename to upload to + user: user to own the files + group: group to own the files + mode: permissions of the files, use ``True`` to copy the local file + add_deploy_dir: src is relative to the deploy directory + create_remote_dir: create the remote directory if it doesn't exist + force: always upload the file, even if the remote copy matches + assume_exists: whether to assume the local file exists ``dest``: If this is a directory that already exists on the remote side, the local file will be uploaded to that directory with the same filename. ``mode``: When set to ``True`` the permissions of the local file are applied to the remote file after the upload is complete. ``create_remote_dir``: If the remote directory does not exist it will be created using the same user & group as passed to ``files.put``. The mode will *not* be copied over, if this is required call ``files.directory`` separately. Note: This operation is not suitable for large files as it may involve copying the file before uploading it. **Examples:** .. code:: python files.put( name="Update the message of the day file", src="files/motd", dest="/etc/motd", mode="644", ) files.put( name="Upload a StringIO object", src=StringIO("file contents"), dest="/etc/motd", ) """ # Upload IO objects as-is if hasattr(src, "read"): local_file = src local_sum = get_file_sha1(src) # Assume string filename else: # Add deploy directory? if add_deploy_dir and state.cwd: src = os.path.join(state.cwd, src) local_file = src if os.path.isfile(local_file): local_sum = get_file_sha1(local_file) elif assume_exists: local_sum = None else: raise IOError("No such file: {0}".format(local_file)) if mode is True: if os.path.isfile(local_file): mode = get_path_permissions_mode(local_file) else: logger.warning(( "No local file exists to get permissions from with `mode=True` ({0})" ).format(get_call_location(), ), ) else: mode = ensure_mode_int(mode) remote_file = host.get_fact(File, path=dest) if not remote_file and host.get_fact(Directory, path=dest): dest = unix_path_join(dest, os.path.basename(src)) remote_file = host.get_fact(File, path=dest) if create_remote_dir: yield from _create_remote_dir(state, host, dest, user, group) # No remote file, always upload and user/group/mode if supplied if not remote_file or force: yield FileUploadCommand( local_file, dest, remote_temp_filename=state.get_temp_filename(dest), ) if user or group: yield file_utils.chown(dest, user, group) if mode: yield file_utils.chmod(dest, mode) # File exists, check sum and check user/group/mode if supplied else: remote_sum = host.get_fact(Sha1File, path=dest) # Check sha1sum, upload if needed if local_sum != remote_sum: yield FileUploadCommand( local_file, dest, remote_temp_filename=state.get_temp_filename(dest), ) if user or group: yield file_utils.chown(dest, user, group) if mode: yield file_utils.chmod(dest, mode) else: changed = False # Check mode if mode and remote_file["mode"] != mode: yield file_utils.chmod(dest, mode) changed = True # Check user/group if (user and remote_file["user"] != user) or ( group and remote_file["group"] != group): yield file_utils.chown(dest, user, group) changed = True if not changed: host.noop("file {0} is already uploaded".format(dest)) # Now we've uploaded the file and ensured user/group/mode, update the relevant fact data host.create_fact(Sha1File, kwargs={"path": dest}, data=local_sum) host.create_fact( File, kwargs={"path": dest}, data={ "user": user, "group": group, "mode": mode }, )
def put( state, host, local_filename, remote_filename, user=None, group=None, mode=None, add_deploy_dir=True, create_remote_dir=False, ): ''' Copy a local file to the remote system. + local_filename: local filename + remote_filename: remote filename + user: user to own the files + group: group to own the files + mode: permissions of the files + add_deploy_dir: local_filename is relative to the deploy directory + create_remote_dir: create the remote directory if it doesn't exist ``create_remote_dir``: If the remote directory does not exist it will be created using the same user & group as passed to ``files.put``. The mode will *not* be copied over, if this is required call ``files.directory`` separately. ''' # Upload IO objects as-is if hasattr(local_filename, 'read'): local_file = local_filename # Assume string filename else: # Add deploy directory? if add_deploy_dir and state.deploy_dir: local_filename = path.join(state.deploy_dir, local_filename) local_file = local_filename if not path.isfile(local_file): raise IOError('No such file: {0}'.format(local_file)) mode = ensure_mode_int(mode) remote_file = host.fact.file(remote_filename) if create_remote_dir: yield _create_remote_dir(state, host, remote_filename, user, group) # No remote file, always upload and user/group/mode if supplied if not remote_file: yield (local_file, remote_filename) if user or group: yield chown(remote_filename, user, group) if mode: yield chmod(remote_filename, mode) # File exists, check sum and check user/group/mode if supplied else: local_sum = get_file_sha1(local_filename) remote_sum = host.fact.sha1_file(remote_filename) # Check sha1sum, upload if needed if local_sum != remote_sum: yield (local_file, remote_filename) if user or group: yield chown(remote_filename, user, group) if mode: yield chmod(remote_filename, mode) else: # Check mode if mode and remote_file['mode'] != mode: yield chmod(remote_filename, mode) # Check user/group if ((user and remote_file['user'] != user) or (group and remote_file['group'] != group)): yield chown(remote_filename, user, group)
def put( src, dest, user=None, group=None, mode=None, add_deploy_dir=True, create_remote_dir=True, force=False, assume_exists=False, ): """ Upload a local file to the remote system. + src: local filename to upload + dest: remote filename to upload to + user: user to own the files + group: group to own the files + mode: permissions of the files + add_deploy_dir: src is relative to the deploy directory + create_remote_dir: create the remote directory if it doesn't exist + force: always upload the file, even if the remote copy matches + assume_exists: whether to assume the local file exists ``create_remote_dir``: If the remote directory does not exist it will be created using the same user & group as passed to ``files.put``. The mode will *not* be copied over, if this is required call ``files.directory`` separately. Note: This operation is not suitable for large files as it may involve copying the file before uploading it. **Examples:** .. code:: python # Note: This requires a 'files/motd' file on the local filesystem files.put( name="Update the message of the day file", src="data/content.json", dest="C:\\data\\content.json", ) """ # Upload IO objects as-is if hasattr(src, "read"): local_file = src # Assume string filename else: # Add deploy directory? if add_deploy_dir and state.cwd: src = os.path.join(state.cwd, src) local_file = src if not assume_exists and not os.path.isfile(local_file): raise IOError("No such file: {0}".format(local_file)) mode = ensure_mode_int(mode) remote_file = host.get_fact(File, path=dest) if create_remote_dir: yield from _create_remote_dir(state, host, dest, user, group) # No remote file, always upload and user/group/mode if supplied if not remote_file or force: yield FileUploadCommand( local_file, dest, remote_temp_filename=state.get_temp_filename(dest), ) # if user or group: # yield chown(dest, user, group) # if mode: # yield chmod(dest, mode) # File exists, check sum and check user/group/mode if supplied else: local_sum = get_file_sha1(src) remote_sum = host.get_fact(Sha1File, path=dest) # Check sha1sum, upload if needed if local_sum != remote_sum: yield FileUploadCommand( local_file, dest, remote_temp_filename=state.get_temp_filename(dest), ) # if user or group: # yield chown(dest, user, group) # if mode: # yield chmod(dest, mode) else: changed = False # Check mode # if mode and remote_file['mode'] != mode: # yield chmod(dest, mode) # changed = True # Check user/group # if ( # (user and remote_file['user'] != user) # or (group and remote_file['group'] != group) # ): # yield chown(dest, user, group) # changed = True if not changed: host.noop("file {0} is already uploaded".format(dest))