24
24
import requests .exceptions
25
25
import six
26
26
27
+ from . import constants
28
+ from . import errors
27
29
from .auth import auth
28
30
from .unixconn import unixconn
29
31
from .ssladapter import ssladapter
30
32
from .utils import utils , check_resource
31
- from . import errors
32
33
from .tls import TLSConfig
33
34
34
35
35
36
if not six .PY3 :
36
37
import websocket
37
38
38
- DEFAULT_DOCKER_API_VERSION = '1.18'
39
- DEFAULT_TIMEOUT_SECONDS = 60
40
- STREAM_HEADER_SIZE_BYTES = 8
41
-
42
39
43
40
class Client (requests .Session ):
44
41
def __init__ (self , base_url = None , version = None ,
45
- timeout = DEFAULT_TIMEOUT_SECONDS , tls = False ):
42
+ timeout = constants . DEFAULT_TIMEOUT_SECONDS , tls = False ):
46
43
super (Client , self ).__init__ ()
47
44
48
45
if tls and not base_url .startswith ('https://' ):
@@ -70,7 +67,7 @@ def __init__(self, base_url=None, version=None,
70
67
71
68
# version detection needs to be after unix adapter mounting
72
69
if version is None :
73
- self ._version = DEFAULT_DOCKER_API_VERSION
70
+ self ._version = constants . DEFAULT_DOCKER_API_VERSION
74
71
elif isinstance (version , six .string_types ):
75
72
if version .lower () == 'auto' :
76
73
self ._version = self ._retrieve_server_version ()
@@ -218,7 +215,7 @@ def _multiplexed_buffer_helper(self, response):
218
215
if len (buf [walker :]) < 8 :
219
216
break
220
217
_ , length = struct .unpack_from ('>BxxxL' , buf [walker :])
221
- start = walker + STREAM_HEADER_SIZE_BYTES
218
+ start = walker + constants . STREAM_HEADER_SIZE_BYTES
222
219
end = start + length
223
220
walker = end
224
221
yield buf [start :end ]
@@ -236,7 +233,7 @@ def _multiplexed_response_stream_helper(self, response):
236
233
socket .settimeout (None )
237
234
238
235
while True :
239
- header = response .raw .read (STREAM_HEADER_SIZE_BYTES )
236
+ header = response .raw .read (constants . STREAM_HEADER_SIZE_BYTES )
240
237
if not header :
241
238
break
242
239
_ , length = struct .unpack ('>BxxxL' , header )
@@ -310,11 +307,18 @@ def attach_socket(self, container, params=None, ws=False):
310
307
def build (self , path = None , tag = None , quiet = False , fileobj = None ,
311
308
nocache = False , rm = False , stream = False , timeout = None ,
312
309
custom_context = False , encoding = None , pull = True ,
313
- forcerm = False , dockerfile = None ):
310
+ forcerm = False , dockerfile = None , container_limits = None ):
314
311
remote = context = headers = None
312
+ container_limits = container_limits or {}
315
313
if path is None and fileobj is None :
316
314
raise TypeError ("Either path or fileobj needs to be provided." )
317
315
316
+ for key in container_limits .keys ():
317
+ if key not in constants .CONTAINER_LIMITS_KEYS :
318
+ raise errors .DockerException (
319
+ 'Invalid container_limits key {0}' .format (key )
320
+ )
321
+
318
322
if custom_context :
319
323
if not fileobj :
320
324
raise TypeError ("You must specify fileobj with custom_context" )
@@ -357,8 +361,9 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
357
361
'rm' : rm ,
358
362
'forcerm' : forcerm ,
359
363
'pull' : pull ,
360
- 'dockerfile' : dockerfile
364
+ 'dockerfile' : dockerfile ,
361
365
}
366
+ params .update (container_limits )
362
367
363
368
if context is not None :
364
369
headers = {'Content-Type' : 'application/tar' }
0 commit comments