Make 'version' a string

Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
Aanand Prasad 2016-01-29 15:58:38 +00:00
commit ce0d469c18
27 changed files with 160 additions and 98 deletions

View file

@ -14,6 +14,8 @@ import six
import yaml
from cached_property import cached_property
from ..const import COMPOSEFILE_V1 as V1
from ..const import COMPOSEFILE_V2_0 as V2_0
from ..const import COMPOSEFILE_VERSIONS
from .errors import CircularReference
from .errors import ComposeFileNotFound
@ -129,25 +131,34 @@ class ConfigFile(namedtuple('_ConfigFile', 'filename config')):
@cached_property
def version(self):
version = self.config.get('version', 1)
version = self.config.get('version', V1)
if isinstance(version, dict):
log.warn("Unexpected type for field 'version', in file {} assuming "
"version is the name of a service, and defaulting to "
"Compose file version 1".format(self.filename))
return 1
return V1
if version == '2':
version = V2_0
if version not in COMPOSEFILE_VERSIONS:
raise ConfigurationError(
'Invalid Compose file version: {0}'.format(version))
return version
def get_service(self, name):
return self.get_service_dicts()[name]
def get_service_dicts(self):
return self.config if self.version == 1 else self.config.get('services', {})
return self.config if self.version == V1 else self.config.get('services', {})
def get_volumes(self):
return {} if self.version == 1 else self.config.get('volumes', {})
return {} if self.version == V1 else self.config.get('volumes', {})
def get_networks(self):
return {} if self.version == 1 else self.config.get('networks', {})
return {} if self.version == V1 else self.config.get('networks', {})
class Config(namedtuple('_Config', 'version services volumes networks')):
@ -209,10 +220,6 @@ def validate_config_version(config_files):
next_file.filename,
next_file.version))
if main_file.version not in COMPOSEFILE_VERSIONS:
raise ConfigurationError(
'Invalid Compose file version: {0}'.format(main_file.version))
def get_default_config_files(base_dir):
(candidates, path) = find_candidates_in_parent_dirs(SUPPORTED_FILENAMES, base_dir)
@ -276,7 +283,7 @@ def load(config_details):
main_file,
[file.get_service_dicts() for file in config_details.config_files])
if main_file.version >= 2:
if main_file.version != V1:
for service_dict in service_dicts:
match_named_volumes(service_dict, volumes)
@ -361,7 +368,7 @@ def process_config_file(config_file, service_name=None):
interpolated_config = interpolate_environment_variables(service_dicts, 'service')
if config_file.version == 2:
if config_file.version == V2_0:
processed_config = dict(config_file.config)
processed_config['services'] = services = interpolated_config
processed_config['volumes'] = interpolate_environment_variables(
@ -369,7 +376,7 @@ def process_config_file(config_file, service_name=None):
processed_config['networks'] = interpolate_environment_variables(
config_file.get_networks(), 'network')
if config_file.version == 1:
if config_file.version == V1:
processed_config = services = interpolated_config
config_file = config_file._replace(config=processed_config)
@ -653,7 +660,7 @@ def merge_service_dicts(base, override, version):
if field in base or field in override:
d[field] = override.get(field, base.get(field))
if version == 1:
if version == V1:
legacy_v1_merge_image_or_build(d, base, override)
else:
merge_build(d, base, override)

View file

@ -1,18 +1,18 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"id": "fields_schema_v2.json",
"id": "fields_schema_v2.0.json",
"properties": {
"version": {
"enum": [2]
"type": "string"
},
"services": {
"id": "#/properties/services",
"type": "object",
"patternProperties": {
"^[a-zA-Z0-9._-]+$": {
"$ref": "service_schema_v2.json#/definitions/service"
"$ref": "service_schema_v2.0.json#/definitions/service"
}
},
"additionalProperties": false

View file

@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "service_schema_v2.json",
"id": "service_schema_v2.0.json",
"type": "object",

View file

@ -7,6 +7,7 @@ from __future__ import unicode_literals
import os
from collections import namedtuple
from compose.config.config import V1
from compose.config.errors import ConfigurationError
from compose.const import IS_WINDOWS_PLATFORM
@ -16,7 +17,7 @@ class VolumeFromSpec(namedtuple('_VolumeFromSpec', 'source mode type')):
# TODO: drop service_names arg when v1 is removed
@classmethod
def parse(cls, volume_from_config, service_names, version):
func = cls.parse_v1 if version == 1 else cls.parse_v2
func = cls.parse_v1 if version == V1 else cls.parse_v2
return func(service_names, volume_from_config)
@classmethod

View file

@ -14,9 +14,12 @@ LABEL_PROJECT = 'com.docker.compose.project'
LABEL_SERVICE = 'com.docker.compose.service'
LABEL_VERSION = 'com.docker.compose.version'
LABEL_CONFIG_HASH = 'com.docker.compose.config-hash'
COMPOSEFILE_VERSIONS = (1, 2)
COMPOSEFILE_V1 = '1'
COMPOSEFILE_V2_0 = '2.0'
COMPOSEFILE_VERSIONS = (COMPOSEFILE_V1, COMPOSEFILE_V2_0)
API_VERSIONS = {
1: '1.21',
2: '1.22',
COMPOSEFILE_V1: '1.21',
COMPOSEFILE_V2_0: '1.22',
}

View file

@ -10,6 +10,7 @@ from docker.errors import NotFound
from . import parallel
from .config import ConfigurationError
from .config.config import V1
from .config.sort_services import get_container_name_from_network_mode
from .config.sort_services import get_service_name_from_network_mode
from .const import DEFAULT_TIMEOUT
@ -56,7 +57,7 @@ class Project(object):
"""
Construct a Project from a config.Config object.
"""
use_networking = (config_data.version and config_data.version >= 2)
use_networking = (config_data.version and config_data.version != V1)
project = cls(name, [], client, use_networking=use_networking)
network_config = config_data.networks or {}
@ -94,7 +95,7 @@ class Project(object):
network_mode = project.get_network_mode(service_dict, networks)
volumes_from = get_volumes_from(project, service_dict)
if config_data.version == 2:
if config_data.version != V1:
service_volumes = service_dict.get('volumes', [])
for volume_spec in service_volumes:
if volume_spec.is_named_volume: