def copy_resource(container, resource, local_filename, contents_only=True): """ Copies a resource from a container to a compressed tarball and downloads it. :param container: Container name or id. :type container: unicode :param resource: Name of resource to copy. :type resource: unicode :param local_filename: Path to store the tarball locally. :type local_filename: unicode :param contents_only: In case ``resource`` is a directory, put all contents at the root of the tar file. If this is set to ``False``, the directory itself will be at the root instead. :type contents_only: bool """ with temp_dir() as remote_tmp: base_name = os.path.basename(resource) copy_path = posixpath.join(remote_tmp, 'copy_tmp') run(mkdir(copy_path, check_if_exists=True)) remote_name = posixpath.join(copy_path, base_name) archive_name = 'container_{0}.tar.gz'.format(container) archive_path = posixpath.join(remote_tmp, archive_name) run('docker cp {0}:{1} {2}'.format(container, resource, copy_path), shell=False) if contents_only and is_directory(remote_name): src_dir = remote_name src_files = '*' else: src_dir = copy_path src_files = base_name with cd(src_dir): run(targz(archive_path, src_files)) get(archive_path, local_filename)
def isolate_and_get(src_container, src_resources, local_dst_dir, **kwargs): """ Uses :func:`copy_resources` to copy resources from a container, but afterwards generates a compressed tarball and downloads it. :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 local_dst_dir: Local directory to store the compressed tarball in. Can also be a file name; the default file name is ``container_<container name>.tar.gz``. :type local_dst_dir: unicode :param kwargs: Additional kwargs for :func:`copy_resources`. """ with temp_dir() as remote_tmp: copy_path = posixpath.join(remote_tmp, 'copy_tmp') archive_path = posixpath.join(remote_tmp, 'container_{0}.tar.gz'.format(src_container)) copy_resources(src_container, src_resources, copy_path, **kwargs) with cd(copy_path): sudo(targz(archive_path, '*')) get(archive_path, local_dst_dir)
def isolate_and_get(src_container, src_resources, local_dst_dir, **kwargs): """ Uses :func:`copy_resources` to copy resources from a container, but afterwards generates a compressed tarball and downloads it. :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 local_dst_dir: Local directory to store the compressed tarball in. Can also be a file name; the default file name is ``container_<container name>.tar.gz``. :type local_dst_dir: unicode :param kwargs: Additional kwargs for :func:`copy_resources`. """ with temp_dir() as remote_tmp: copy_path = posixpath.join(remote_tmp, 'copy_tmp') archive_path = posixpath.join( remote_tmp, 'container_{0}.tar.gz'.format(src_container)) copy_resources(src_container, src_resources, copy_path, **kwargs) with cd(copy_path): sudo(targz(archive_path, '*')) get(archive_path, local_dst_dir)