Fix for backwards compat with dockerpy 2.

This commit is contained in:
Steve Pulec 2018-05-31 22:39:22 -04:00
parent 620530c4ee
commit cf71532fa9

View File

@ -4,6 +4,7 @@ import base64
from collections import defaultdict from collections import defaultdict
import copy import copy
import datetime import datetime
import docker
import docker.errors import docker.errors
import hashlib import hashlib
import io import io
@ -44,7 +45,7 @@ except ImportError:
_stderr_regex = re.compile(r'START|END|REPORT RequestId: .*') _stderr_regex = re.compile(r'START|END|REPORT RequestId: .*')
_orig_adapter_send = requests.adapters.HTTPAdapter.send _orig_adapter_send = requests.adapters.HTTPAdapter.send
docker_3 = docker.__version__.startswith("3")
def zip2tar(zip_bytes): def zip2tar(zip_bytes):
with TemporaryDirectory() as td: with TemporaryDirectory() as td:
@ -104,7 +105,11 @@ class _DockerDataVolumeContext:
# It doesn't exist so we need to create it # It doesn't exist so we need to create it
self._vol_ref.volume = self._lambda_func.docker_client.volumes.create(self._lambda_func.code_sha_256) self._vol_ref.volume = self._lambda_func.docker_client.volumes.create(self._lambda_func.code_sha_256)
container = self._lambda_func.docker_client.containers.run('alpine', 'sleep 100', volumes={self.name: {'bind': '/tmp/data', 'mode': 'rw'}}, detach=True) if docker_3:
volumes = {self.name: {'bind': '/tmp/data', 'mode': 'rw'}}
else:
volumes = {self.name: '/tmp/data'}
container = self._lambda_func.docker_client.containers.run('alpine', 'sleep 100', volumes=volumes, detach=True)
try: try:
tar_bytes = zip2tar(self._lambda_func.code_bytes) tar_bytes = zip2tar(self._lambda_func.code_bytes)
container.put_archive('/tmp/data', tar_bytes) container.put_archive('/tmp/data', tar_bytes)
@ -309,11 +314,15 @@ class LambdaFunction(BaseModel):
finally: finally:
if container: if container:
try: try:
exit_code = container.wait(timeout=300)['StatusCode'] exit_code = container.wait(timeout=300)
except requests.exceptions.ReadTimeout: except requests.exceptions.ReadTimeout:
exit_code = -1 exit_code = -1
container.stop() container.stop()
container.kill() container.kill()
else:
if docker_3:
exit_code = exit_code['StatusCode']
output = container.logs(stdout=False, stderr=True) output = container.logs(stdout=False, stderr=True)
output += container.logs(stdout=True, stderr=False) output += container.logs(stdout=True, stderr=False)
container.remove() container.remove()