mirror of
https://github.com/docker/compose.git
synced 2025-04-08 17:05:13 +02:00
113 lines
3.4 KiB
Python
113 lines
3.4 KiB
Python
from __future__ import absolute_import
|
|
from __future__ import unicode_literals
|
|
|
|
import six
|
|
import yaml
|
|
|
|
from compose import const
|
|
from compose.config import types
|
|
from compose.const import COMPOSEFILE_V1 as V1
|
|
from compose.const import COMPOSEFILE_V2_1 as V2_1
|
|
|
|
|
|
def serialize_config_type(dumper, data):
|
|
representer = dumper.represent_str if six.PY3 else dumper.represent_unicode
|
|
return representer(data.repr())
|
|
|
|
|
|
def serialize_dict_type(dumper, data):
|
|
return dumper.represent_dict(data.repr())
|
|
|
|
|
|
yaml.SafeDumper.add_representer(types.VolumeFromSpec, serialize_config_type)
|
|
yaml.SafeDumper.add_representer(types.VolumeSpec, serialize_config_type)
|
|
yaml.SafeDumper.add_representer(types.ServiceSecret, serialize_dict_type)
|
|
yaml.SafeDumper.add_representer(types.ServicePort, serialize_dict_type)
|
|
|
|
|
|
def denormalize_config(config):
|
|
result = {'version': V2_1 if config.version == V1 else config.version}
|
|
denormalized_services = [
|
|
denormalize_service_dict(service_dict, config.version)
|
|
for service_dict in config.services
|
|
]
|
|
result['services'] = {
|
|
service_dict.pop('name'): service_dict
|
|
for service_dict in denormalized_services
|
|
}
|
|
result['networks'] = config.networks.copy()
|
|
for net_name, net_conf in result['networks'].items():
|
|
if 'external_name' in net_conf:
|
|
del net_conf['external_name']
|
|
|
|
result['volumes'] = config.volumes.copy()
|
|
for vol_name, vol_conf in result['volumes'].items():
|
|
if 'external_name' in vol_conf:
|
|
del vol_conf['external_name']
|
|
|
|
if config.version in (V3_1,):
|
|
result['secrets'] = config.secrets
|
|
return result
|
|
|
|
|
|
def serialize_config(config):
|
|
return yaml.safe_dump(
|
|
denormalize_config(config),
|
|
default_flow_style=False,
|
|
indent=2,
|
|
width=80)
|
|
|
|
|
|
def serialize_ns_time_value(value):
|
|
result = (value, 'ns')
|
|
table = [
|
|
(1000., 'us'),
|
|
(1000., 'ms'),
|
|
(1000., 's'),
|
|
(60., 'm'),
|
|
(60., 'h')
|
|
]
|
|
for stage in table:
|
|
tmp = value / stage[0]
|
|
if tmp == int(value / stage[0]):
|
|
value = tmp
|
|
result = (int(value), stage[1])
|
|
else:
|
|
break
|
|
return '{0}{1}'.format(*result)
|
|
|
|
|
|
def denormalize_service_dict(service_dict, version):
|
|
service_dict = service_dict.copy()
|
|
|
|
if 'restart' in service_dict:
|
|
service_dict['restart'] = types.serialize_restart_spec(
|
|
service_dict['restart']
|
|
)
|
|
|
|
if version == V1 and 'network_mode' not in service_dict:
|
|
service_dict['network_mode'] = 'bridge'
|
|
|
|
if 'depends_on' in service_dict and version != V2_1:
|
|
service_dict['depends_on'] = sorted([
|
|
svc for svc in service_dict['depends_on'].keys()
|
|
])
|
|
|
|
if 'healthcheck' in service_dict:
|
|
if 'interval' in service_dict['healthcheck']:
|
|
service_dict['healthcheck']['interval'] = serialize_ns_time_value(
|
|
service_dict['healthcheck']['interval']
|
|
)
|
|
if 'timeout' in service_dict['healthcheck']:
|
|
service_dict['healthcheck']['timeout'] = serialize_ns_time_value(
|
|
service_dict['healthcheck']['timeout']
|
|
)
|
|
|
|
if 'ports' in service_dict and version < const.COMPOSEFILE_V3_2:
|
|
service_dict['ports'] = map(
|
|
lambda p: p.legacy_repr() if isinstance(p, types.ServicePort) else p,
|
|
service_dict['ports']
|
|
)
|
|
|
|
return service_dict
|