Skip to content
This repository has been archived by the owner on Dec 14, 2022. It is now read-only.

Commit

Permalink
Use latest docker version
Browse files Browse the repository at this point in the history
Signed-off-by: Emerson Knapp <[email protected]>
  • Loading branch information
emersonknapp committed Aug 16, 2020
1 parent 2f6fe2e commit 027545d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
45 changes: 29 additions & 16 deletions ros_cross_compile/docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,33 @@
from typing import Optional

import docker
from tqdm import tqdm

from docker.utils import kwargs_from_env as docker_kwargs_from_env

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('Docker Client')

DEFAULT_COLCON_DEFAULTS_FILE = 'defaults.yaml'


class GeneratorStream(io.RawIOBase):
def __init__(self, generator):
self.leftover = None
self.generator = generator

def readable(self):
return True

def readinto(self, b):
try:
length = len(b) # : We're supposed to return at most this much
chunk = self.leftover or next(self.generator)
output, self.leftover = chunk[:length], chunk[length:]
b[:len(output)] = output
return len(output)
except StopIteration:
return 0 # : Indicate EOF


class DockerClient:
"""Simplified Docker API for this package's usage patterns."""

Expand Down Expand Up @@ -66,7 +84,8 @@ def build_image(
:raises docker.errors.BuildError: on build error
"""
# Use low-level API to expose logs for image building
docker_api = docker.APIClient(base_url='unix://var/run/docker.sock')
docker_api = docker.APIClient(**docker_kwargs_from_env())
logger.info('Sending context to Docker client')
log_generator = docker_api.build(
path=str(dockerfile_dir) if dockerfile_dir else self._default_docker_dir,
dockerfile=dockerfile_name,
Expand All @@ -89,15 +108,10 @@ def _process_build_log(self, log_generator) -> None:
raise docker.errors.BuildError(error_line)
line = chunk.get('stream', '')
line = line.rstrip()
if not line:
continue
# if line.startswith('Step '):
# # e.g. "Step X/Y"
# progress = line.split()[1]
# current, total = progress.split('/')
# current = int(current)
# total = int(total)
logger.info(line)
if line:
logger.info(line)



def run_container(
self,
Expand Down Expand Up @@ -159,8 +173,7 @@ def get_image_size(self, img_name: str) -> int:

def export_image_filesystem(self, image_tag: str):
container = self._client.containers.run(image=image_tag, detach=True)
export_response = container.export()
tar_bytes = export_response.read()
tar_file = io.BytesIO(tar_bytes)
tar = tarfile.open(fileobj=tar_file)
export_generator = container.export()
stream = io.BufferedReader(GeneratorStream(export_generator))
tar = tarfile.open(fileobj=stream, mode='r|*')
return tar
1 change: 1 addition & 0 deletions ros_cross_compile/ros_cross_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def parse_args(args: List[str]) -> argparse.Namespace:
'--skip-steps',
nargs='+',
choices=[stage.name for stage in stages],
default=[],
help='Skip these steps')
parser.add_argument(
'--create-runtime-image',
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@
package_name: ['docker/*.*', 'mixins/*.*'],
},
install_requires=[
'docker>=2,<3',
'docker==4.*',
'setuptools',
'tqdm',
],
zip_safe=True,
entry_points={
Expand Down

0 comments on commit 027545d

Please sign in to comment.