@@ -786,7 +786,8 @@ def kill(self, container, signal=None):
786
786
787
787
@utils .check_resource ('container' )
788
788
def logs (self , container , stdout = True , stderr = True , stream = False ,
789
- timestamps = False , tail = 'all' , since = None , follow = None ):
789
+ timestamps = False , tail = 'all' , since = None , follow = None ,
790
+ until = None ):
790
791
"""
791
792
Get logs from a container. Similar to the ``docker logs`` command.
792
793
@@ -805,6 +806,8 @@ def logs(self, container, stdout=True, stderr=True, stream=False,
805
806
since (datetime or int): Show logs since a given datetime or
806
807
integer epoch (in seconds)
807
808
follow (bool): Follow log output
809
+ until (datetime or int): Show logs that occurred before the given
810
+ datetime or integer epoch (in seconds)
808
811
809
812
Returns:
810
813
(generator or str)
@@ -827,21 +830,35 @@ def logs(self, container, stdout=True, stderr=True, stream=False,
827
830
params ['tail' ] = tail
828
831
829
832
if since is not None :
830
- if utils .compare_version ( '1.19' , self . _version ) < 0 :
833
+ if utils .version_lt ( self . _version , '1.19' ) :
831
834
raise errors .InvalidVersion (
832
- 'since is not supported in API < 1.19'
835
+ 'since is not supported for API version < 1.19'
833
836
)
837
+ if isinstance (since , datetime ):
838
+ params ['since' ] = utils .datetime_to_timestamp (since )
839
+ elif (isinstance (since , int ) and since > 0 ):
840
+ params ['since' ] = since
834
841
else :
835
- if isinstance (since , datetime ):
836
- params ['since' ] = utils .datetime_to_timestamp (since )
837
- elif (isinstance (since , int ) and since > 0 ):
838
- params ['since' ] = since
839
- else :
840
- raise errors .InvalidArgument (
841
- 'since value should be datetime or positive int, '
842
- 'not {}' .
843
- format (type (since ))
844
- )
842
+ raise errors .InvalidArgument (
843
+ 'since value should be datetime or positive int, '
844
+ 'not {}' .format (type (since ))
845
+ )
846
+
847
+ if until is not None :
848
+ if utils .version_lt (self ._version , '1.35' ):
849
+ raise errors .InvalidVersion (
850
+ 'until is not supported for API version < 1.35'
851
+ )
852
+ if isinstance (until , datetime ):
853
+ params ['until' ] = utils .datetime_to_timestamp (until )
854
+ elif (isinstance (until , int ) and until > 0 ):
855
+ params ['until' ] = until
856
+ else :
857
+ raise errors .InvalidArgument (
858
+ 'until value should be datetime or positive int, '
859
+ 'not {}' .format (type (until ))
860
+ )
861
+
845
862
url = self ._url ("/containers/{0}/logs" , container )
846
863
res = self ._get (url , params = params , stream = stream )
847
864
return self ._get_result (container , stream , res )
@@ -1241,7 +1258,7 @@ def update_container(
1241
1258
return self ._result (res , True )
1242
1259
1243
1260
@utils .check_resource ('container' )
1244
- def wait (self , container , timeout = None ):
1261
+ def wait (self , container , timeout = None , condition = None ):
1245
1262
"""
1246
1263
Block until a container stops, then return its exit code. Similar to
1247
1264
the ``docker wait`` command.
@@ -1250,10 +1267,13 @@ def wait(self, container, timeout=None):
1250
1267
container (str or dict): The container to wait on. If a dict, the
1251
1268
``Id`` key is used.
1252
1269
timeout (int): Request timeout
1270
+ condition (str): Wait until a container state reaches the given
1271
+ condition, either ``not-running`` (default), ``next-exit``,
1272
+ or ``removed``
1253
1273
1254
1274
Returns:
1255
- (int): The exit code of the container. Returns ``-1`` if the API
1256
- responds without a ``StatusCode`` attribute .
1275
+ (int or dict ): The exit code of the container. Returns the full API
1276
+ response if no ``StatusCode`` field is included .
1257
1277
1258
1278
Raises:
1259
1279
:py:class:`requests.exceptions.ReadTimeout`
@@ -1262,9 +1282,17 @@ def wait(self, container, timeout=None):
1262
1282
If the server returns an error.
1263
1283
"""
1264
1284
url = self ._url ("/containers/{0}/wait" , container )
1265
- res = self ._post (url , timeout = timeout )
1285
+ params = {}
1286
+ if condition is not None :
1287
+ if utils .version_lt (self ._version , '1.30' ):
1288
+ raise errors .InvalidVersion (
1289
+ 'wait condition is not supported for API version < 1.30'
1290
+ )
1291
+ params ['condition' ] = condition
1292
+
1293
+ res = self ._post (url , timeout = timeout , params = params )
1266
1294
self ._raise_for_status (res )
1267
1295
json_ = res .json ()
1268
1296
if 'StatusCode' in json_ :
1269
1297
return json_ ['StatusCode' ]
1270
- return - 1
1298
+ return json_
0 commit comments