Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 8 additions & 42 deletions docker/api/build.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import logging
import os
import re

from .. import auth
from .. import constants
Expand All @@ -14,7 +13,7 @@

class BuildApiMixin(object):
def build(self, path=None, tag=None, quiet=False, fileobj=None,
nocache=False, rm=False, stream=False, timeout=None,
nocache=False, rm=False, timeout=None,
custom_context=False, encoding=None, pull=False,
forcerm=False, dockerfile=None, container_limits=None,
decode=False, buildargs=None, gzip=False, shmsize=None,
Expand Down Expand Up @@ -67,9 +66,6 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
rm (bool): Remove intermediate containers. The ``docker build``
command now defaults to ``--rm=true``, but we have kept the old
default of `False` to preserve backward compatibility
stream (bool): *Deprecated for API version > 1.8 (always True)*.
Return a blocking generator you can iterate over to retrieve
build output as it happens
timeout (int): HTTP timeout
custom_context (bool): Optional if using ``fileobj``
encoding (str): The encoding for a stream. Set to ``gzip`` for
Expand Down Expand Up @@ -154,17 +150,6 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
)
encoding = 'gzip' if gzip else encoding

if utils.compare_version('1.8', self._version) >= 0:
stream = True

if dockerfile and utils.compare_version('1.17', self._version) < 0:
raise errors.InvalidVersion(
'dockerfile was only introduced in API version 1.17'
)

if utils.compare_version('1.19', self._version) < 0:
pull = 1 if pull else 0

u = self._url('/build')
params = {
't': tag,
Expand All @@ -179,12 +164,7 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
params.update(container_limits)

if buildargs:
if utils.version_gte(self._version, '1.21'):
params.update({'buildargs': json.dumps(buildargs)})
else:
raise errors.InvalidVersion(
'buildargs was only introduced in API version 1.21'
)
params.update({'buildargs': json.dumps(buildargs)})

if shmsize:
if utils.version_gte(self._version, '1.22'):
Expand Down Expand Up @@ -256,30 +236,21 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
if encoding:
headers['Content-Encoding'] = encoding

if utils.compare_version('1.9', self._version) >= 0:
self._set_auth_headers(headers)
self._set_auth_headers(headers)

response = self._post(
u,
data=context,
params=params,
headers=headers,
stream=stream,
stream=True,
timeout=timeout,
)

if context is not None and not custom_context:
context.close()

if stream:
return self._stream_helper(response, decode=decode)
else:
output = self._result(response)
srch = r'Successfully built ([0-9a-f]+)'
match = re.search(srch, output)
if not match:
return None, output
return match.group(1), output
return self._stream_helper(response, decode=decode)

def _set_auth_headers(self, headers):
log.debug('Looking for auth config')
Expand Down Expand Up @@ -316,13 +287,8 @@ def _set_auth_headers(self, headers):
)
)

if utils.compare_version('1.19', self._version) >= 0:
headers['X-Registry-Config'] = auth.encode_header(
auth_data
)
else:
headers['X-Registry-Config'] = auth.encode_header({
'configs': auth_data
})
headers['X-Registry-Config'] = auth.encode_header(
auth_data
)
else:
log.debug('No auth config found')
27 changes: 5 additions & 22 deletions docker/api/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import struct
import warnings
from functools import partial

import requests
Expand All @@ -27,7 +26,7 @@
MINIMUM_DOCKER_API_VERSION
)
from ..errors import (
DockerException, TLSParameterError,
DockerException, InvalidVersion, TLSParameterError,
create_api_error_from_http_exception
)
from ..tls import TLSConfig
Expand Down Expand Up @@ -160,11 +159,9 @@ def __init__(self, base_url=None, version=None,
)
)
if utils.version_lt(self._version, MINIMUM_DOCKER_API_VERSION):
warnings.warn(
'The minimum API version supported is {}, but you are using '
'version {}. It is recommended you either upgrade Docker '
'Engine or use an older version of Docker SDK for '
'Python.'.format(MINIMUM_DOCKER_API_VERSION, self._version)
raise InvalidVersion(
'API versions below {} are no longer supported by this '
'library.'.format(MINIMUM_DOCKER_API_VERSION)
)

def _retrieve_server_version(self):
Expand Down Expand Up @@ -353,17 +350,8 @@ def _multiplexed_response_stream_helper(self, response):
break
yield data

def _stream_raw_result_old(self, response):
''' Stream raw output for API versions below 1.6 '''
self._raise_for_status(response)
for line in response.iter_lines(chunk_size=1,
decode_unicode=True):
# filter out keep-alive new lines
if line:
yield line

def _stream_raw_result(self, response):
''' Stream result for TTY-enabled container above API 1.6 '''
''' Stream result for TTY-enabled container '''
self._raise_for_status(response)
for out in response.iter_content(chunk_size=1, decode_unicode=True):
yield out
Expand Down Expand Up @@ -419,11 +407,6 @@ def _get_result(self, container, stream, res):
return self._get_result_tty(stream, res, self._check_is_tty(container))

def _get_result_tty(self, stream, res, is_tty):
# Stream multi-plexing was only introduced in API v1.6. Anything
# before that needs old-style streaming.
if utils.compare_version('1.6', self._version) < 0:
return self._stream_raw_result_old(res)

# We should also use raw streaming (without keep-alives)
# if we're dealing with a tty-enabled container.
if is_tty:
Expand Down
Loading