From 9e17cff0efaff14f2b03d5d0ba70559661733b26 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Tue, 12 Jan 2016 18:12:53 +0000 Subject: [PATCH] Refactor API version switching logic Signed-off-by: Aanand Prasad --- compose/cli/command.py | 6 +++++- compose/cli/docker_client.py | 7 ++----- compose/const.py | 7 +++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/compose/cli/command.py b/compose/cli/command.py index f14388c6a..c1681ffc7 100644 --- a/compose/cli/command.py +++ b/compose/cli/command.py @@ -13,6 +13,7 @@ from requests.exceptions import SSLError from . import errors from . import verbose_proxy from .. import config +from ..const import API_VERSIONS from ..project import Project from .docker_client import docker_client from .utils import call_silently @@ -77,7 +78,10 @@ def get_project(base_dir, config_path=None, project_name=None, verbose=False): config_details = config.find(base_dir, config_path) project_name = get_project_name(config_details.working_dir, project_name) config_data = config.load(config_details) - api_version = '1.21' if config_data.version < 2 else None + + api_version = os.environ.get( + 'COMPOSE_API_VERSION', + API_VERSIONS[config_data.version]) client = get_client(verbose=verbose, version=api_version) return Project.from_config(project_name, config_data, client) diff --git a/compose/cli/docker_client.py b/compose/cli/docker_client.py index 48ba97bda..611997dfa 100644 --- a/compose/cli/docker_client.py +++ b/compose/cli/docker_client.py @@ -11,8 +11,6 @@ from ..const import HTTP_TIMEOUT log = logging.getLogger(__name__) -DEFAULT_API_VERSION = '1.21' - def docker_client(version=None): """ @@ -23,8 +21,7 @@ def docker_client(version=None): log.warn('The DOCKER_CLIENT_TIMEOUT environment variable is deprecated. Please use COMPOSE_HTTP_TIMEOUT instead.') kwargs = kwargs_from_env(assert_hostname=False) - kwargs['version'] = version or os.environ.get( - 'COMPOSE_API_VERSION', - DEFAULT_API_VERSION) + if version: + kwargs['version'] = version kwargs['timeout'] = HTTP_TIMEOUT return Client(**kwargs) diff --git a/compose/const.py b/compose/const.py index 84a5057a4..331895b10 100644 --- a/compose/const.py +++ b/compose/const.py @@ -15,3 +15,10 @@ LABEL_SERVICE = 'com.docker.compose.service' LABEL_VERSION = 'com.docker.compose.version' LABEL_CONFIG_HASH = 'com.docker.compose.config-hash' COMPOSEFILE_VERSIONS = (1, 2) + +API_VERSIONS = { + 1: '1.21', + + # TODO: update to 1.22 when there's a Docker 1.10 build to test against + 2: '1.21', +}