Update build method in docker and legacy files

pull/579/head
Gladys Nalvarte 2019-02-19 10:45:48 +01:00
rodzic 5bf2eadcf8
commit 7609e7f0aa
4 zmienionych plików z 30 dodań i 23 usunięć

Wyświetl plik

@ -532,8 +532,7 @@ class Repo2Docker(Application):
run_kwargs.update(self.extra_run_kwargs) run_kwargs.update(self.extra_run_kwargs)
container = client.containers.run( container = client.containers.run(self.output_image_spec, **run_kwargs)
self.output_image_spec, **run_kwargs)
while container.status == 'created': while container.status == 'created':
time.sleep(0.5) time.sleep(0.5)

Wyświetl plik

@ -19,7 +19,7 @@ class DockerBuildPack(BuildPack):
with open(Dockerfile) as f: with open(Dockerfile) as f:
return f.read() return f.read()
def build(self, client, image_spec, memory_limit, build_args, cache_from): def build(self, client, image_spec, memory_limit, build_args, cache_from, extra_build_kwargs):
"""Build a Docker image based on the Dockerfile in the source repo.""" """Build a Docker image based on the Dockerfile in the source repo."""
limits = { limits = {
# Always disable memory swap for building, since mostly # Always disable memory swap for building, since mostly
@ -28,7 +28,8 @@ class DockerBuildPack(BuildPack):
} }
if memory_limit: if memory_limit:
limits['memory'] = memory_limit limits['memory'] = memory_limit
for line in client.build(
build_kwargs = dict(
path=os.getcwd(), path=os.getcwd(),
dockerfile=self.binder_path(self.dockerfile), dockerfile=self.binder_path(self.dockerfile),
tag=image_spec, tag=image_spec,
@ -38,5 +39,9 @@ class DockerBuildPack(BuildPack):
rm=True, rm=True,
container_limits=limits, container_limits=limits,
cache_from=cache_from cache_from=cache_from
): )
build_kwargs.update(extra_build_kwargs)
for line in client.build(**build_kwargs):
yield line yield line

Wyświetl plik

@ -83,7 +83,7 @@ class LegacyBinderDockerBuildPack(DockerBuildPack):
'legacy/python3.frozen.yml': '/tmp/python3.frozen.yml', 'legacy/python3.frozen.yml': '/tmp/python3.frozen.yml',
} }
def build(self, client, image_spec, memory_limit, build_args, cache_from): def build(self, client, image_spec, memory_limit, build_args, cache_from, extra_build_kwargs):
"""Build a legacy Docker image.""" """Build a legacy Docker image."""
with open(self.dockerfile, 'w') as f: with open(self.dockerfile, 'w') as f:
f.write(self.render()) f.write(self.render())
@ -94,7 +94,7 @@ class LegacyBinderDockerBuildPack(DockerBuildPack):
env_file, env_file,
) )
shutil.copy(src_path, env_file) shutil.copy(src_path, env_file)
return super().build(client, image_spec, memory_limit, build_args, cache_from) return super().build(client, image_spec, memory_limit, build_args, cache_from, extra_build_kwargs)
def detect(self): def detect(self):
"""Check if current repo should be built with the Legacy BuildPack. """Check if current repo should be built with the Legacy BuildPack.

Wyświetl plik

@ -17,10 +17,11 @@ def test_cache_from_base(tmpdir):
fake_log_value = {'stream': 'fake'} fake_log_value = {'stream': 'fake'}
fake_client = MagicMock(spec=docker.APIClient) fake_client = MagicMock(spec=docker.APIClient)
fake_client.build.return_value = iter([fake_log_value]) fake_client.build.return_value = iter([fake_log_value])
extra_build_kwargs = {'somekey': 'somevalue'}
# Test base image build pack # Test base image build pack
tmpdir.chdir() tmpdir.chdir()
for line in BaseImage().build(fake_client, 'image-2', '1Gi', {}, cache_from): for line in BaseImage().build(fake_client, 'image-2', '1Gi', {}, cache_from, extra_build_kwargs):
assert line == fake_log_value assert line == fake_log_value
called_args, called_kwargs = fake_client.build.call_args called_args, called_kwargs = fake_client.build.call_args
assert 'cache_from' in called_kwargs assert 'cache_from' in called_kwargs
@ -36,13 +37,14 @@ def test_cache_from_docker(tmpdir):
fake_log_value = {'stream': 'fake'} fake_log_value = {'stream': 'fake'}
fake_client = MagicMock(spec=docker.APIClient) fake_client = MagicMock(spec=docker.APIClient)
fake_client.build.return_value = iter([fake_log_value]) fake_client.build.return_value = iter([fake_log_value])
extra_build_kwargs = {'somekey': 'somevalue'}
tmpdir.chdir() tmpdir.chdir()
# test dockerfile # test dockerfile
with tmpdir.join("Dockerfile").open('w') as f: with tmpdir.join("Dockerfile").open('w') as f:
f.write('FROM scratch\n') f.write('FROM scratch\n')
for line in DockerBuildPack().build(fake_client, 'image-2', '1Gi', {}, cache_from): for line in DockerBuildPack().build(fake_client, 'image-2', '1Gi', {}, cache_from, extra_build_kwargs):
assert line == fake_log_value assert line == fake_log_value
called_args, called_kwargs = fake_client.build.call_args called_args, called_kwargs = fake_client.build.call_args
assert 'cache_from' in called_kwargs assert 'cache_from' in called_kwargs
@ -57,12 +59,13 @@ def test_cache_from_legacy(tmpdir):
fake_log_value = {'stream': 'fake'} fake_log_value = {'stream': 'fake'}
fake_client = MagicMock(spec=docker.APIClient) fake_client = MagicMock(spec=docker.APIClient)
fake_client.build.return_value = iter([fake_log_value]) fake_client.build.return_value = iter([fake_log_value])
extra_build_kwargs = {'somekey': 'somevalue'}
# Test legacy docker image # Test legacy docker image
with tmpdir.join("Dockerfile").open('w') as f: with tmpdir.join("Dockerfile").open('w') as f:
f.write('FROM andrewosh/binder-base\n') f.write('FROM andrewosh/binder-base\n')
for line in LegacyBinderDockerBuildPack().build(fake_client, 'image-2', '1Gi', {}, cache_from): for line in LegacyBinderDockerBuildPack().build(fake_client, 'image-2', '1Gi', {}, cache_from, extra_build_kwargs):
assert line == fake_log_value assert line == fake_log_value
called_args, called_kwargs = fake_client.build.call_args called_args, called_kwargs = fake_client.build.call_args
assert 'cache_from' in called_kwargs assert 'cache_from' in called_kwargs