def temp_dir(apply_chown=None, apply_chmod=None, remove_using_sudo=None, remove_force=False): """ Creates a temporary directory on the remote machine. The directory is removed when no longer needed. Failure to do so will be ignored. :param apply_chown: Optional; change the owner of the directory. :type apply_chown: unicode :param apply_chmod: Optional; change the permissions of the directory. :type apply_chmod: unicode :param remove_using_sudo: Use sudo for removing the directory. ``None`` (default) means it is used depending on whether ``apply_chown`` has been set. :type remove_using_sudo: bool | NoneType :param remove_force: Force the removal. :type remove_force: bool :return: Path to the temporary directory. :rtype: unicode """ path = get_remote_temp() try: if apply_chmod: run(chmod(apply_chmod, path)) if apply_chown: if remove_using_sudo is None: remove_using_sudo = True sudo(chown(apply_chown, path)) yield path finally: remove_ignore(path, use_sudo=remove_using_sudo, force=remove_force)
def copy_resources(src_container, src_resources, storage_dir, dst_directories=None, apply_chown=None, apply_chmod=None): """ Copies files and directories from a Docker container. Multiple resources can be copied and additional options are available than in :func:`copy_resource`. Unlike in :func:`copy_resource`, Resources are copied as they are and not compressed to a tarball, and they are left on the remote machine. :param src_container: Container name or id. :type src_container: unicode :param src_resources: Resources, as (file or directory) names to copy. :type src_resources: iterable :param storage_dir: Remote directory to store the copied objects in. :type storage_dir: unicode :param dst_directories: Optional dictionary of destination directories, in the format ``resource: destination``. If not set, resources will be in the same relative structure to one another as inside the container. For setting a common default, use ``*`` as the resource key. :type dst_directories: dict :param apply_chown: Owner to set for the copied resources. Can be a user name or id, group name or id, both in the notation ``user:group``, or as a tuple ``(user, group)``. :type apply_chown: unicode or tuple :param apply_chmod: File system permissions to set for the copied resources. Can be any notation as accepted by `chmod`. :type apply_chmod: unicode """ def _copy_resource(resource): default_dest_path = generic_path if generic_path is not None else resource dest_path = directories.get(resource, default_dest_path).strip(posixpath.sep) head, tail = posixpath.split(dest_path) rel_path = posixpath.join(storage_dir, head) run(mkdir(rel_path, check_if_exists=True)) run('docker cp {0}:{1} {2}'.format(src_container, resource, rel_path), shell=False) directories = dst_directories or {} generic_path = directories.get('*') for res in src_resources: _copy_resource(res) if apply_chmod: run(chmod(apply_chmod, storage_dir)) if apply_chown: sudo(chown(apply_chown, storage_dir))
def temp_dir(apply_chown=None, apply_chmod=None): """ Creates a temporary directory on the remote machine. The directory is removed when no longer needed. Failure to do so will be ignored. :param apply_chown: Optional; change the owner of the directory. :type apply_chown: bool :param apply_chmod: Optional; change the permissions of the directory. :type apply_chmod: bool :return: Path to the temporary directory. :rtype: unicode """ path = get_remote_temp() if apply_chmod: run(chmod(apply_chmod, path)) if apply_chown: sudo(chown(apply_chown, path)) yield path remove_ignore(path, True)