serialize.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import six
  4. import yaml
  5. from compose import const
  6. from compose.config import types
  7. from compose.const import COMPOSEFILE_V1 as V1
  8. from compose.const import COMPOSEFILE_V2_1 as V2_1
  9. def serialize_config_type(dumper, data):
  10. representer = dumper.represent_str if six.PY3 else dumper.represent_unicode
  11. return representer(data.repr())
  12. def serialize_dict_type(dumper, data):
  13. return dumper.represent_dict(data.repr())
  14. yaml.SafeDumper.add_representer(types.VolumeFromSpec, serialize_config_type)
  15. yaml.SafeDumper.add_representer(types.VolumeSpec, serialize_config_type)
  16. yaml.SafeDumper.add_representer(types.ServiceSecret, serialize_dict_type)
  17. yaml.SafeDumper.add_representer(types.ServicePort, serialize_dict_type)
  18. def denormalize_config(config):
  19. result = {'version': V2_1 if config.version == V1 else config.version}
  20. denormalized_services = [
  21. denormalize_service_dict(service_dict, config.version)
  22. for service_dict in config.services
  23. ]
  24. result['services'] = {
  25. service_dict.pop('name'): service_dict
  26. for service_dict in denormalized_services
  27. }
  28. result['networks'] = config.networks.copy()
  29. for net_name, net_conf in result['networks'].items():
  30. if 'external_name' in net_conf:
  31. del net_conf['external_name']
  32. result['volumes'] = config.volumes.copy()
  33. for vol_name, vol_conf in result['volumes'].items():
  34. if 'external_name' in vol_conf:
  35. del vol_conf['external_name']
  36. if config.version in (V3_1,):
  37. result['secrets'] = config.secrets
  38. return result
  39. def serialize_config(config):
  40. return yaml.safe_dump(
  41. denormalize_config(config),
  42. default_flow_style=False,
  43. indent=2,
  44. width=80)
  45. def serialize_ns_time_value(value):
  46. result = (value, 'ns')
  47. table = [
  48. (1000., 'us'),
  49. (1000., 'ms'),
  50. (1000., 's'),
  51. (60., 'm'),
  52. (60., 'h')
  53. ]
  54. for stage in table:
  55. tmp = value / stage[0]
  56. if tmp == int(value / stage[0]):
  57. value = tmp
  58. result = (int(value), stage[1])
  59. else:
  60. break
  61. return '{0}{1}'.format(*result)
  62. def denormalize_service_dict(service_dict, version):
  63. service_dict = service_dict.copy()
  64. if 'restart' in service_dict:
  65. service_dict['restart'] = types.serialize_restart_spec(
  66. service_dict['restart']
  67. )
  68. if version == V1 and 'network_mode' not in service_dict:
  69. service_dict['network_mode'] = 'bridge'
  70. if 'depends_on' in service_dict and version != V2_1:
  71. service_dict['depends_on'] = sorted([
  72. svc for svc in service_dict['depends_on'].keys()
  73. ])
  74. if 'healthcheck' in service_dict:
  75. if 'interval' in service_dict['healthcheck']:
  76. service_dict['healthcheck']['interval'] = serialize_ns_time_value(
  77. service_dict['healthcheck']['interval']
  78. )
  79. if 'timeout' in service_dict['healthcheck']:
  80. service_dict['healthcheck']['timeout'] = serialize_ns_time_value(
  81. service_dict['healthcheck']['timeout']
  82. )
  83. if 'ports' in service_dict and version < const.COMPOSEFILE_V3_2:
  84. service_dict['ports'] = map(
  85. lambda p: p.legacy_repr() if isinstance(p, types.ServicePort) else p,
  86. service_dict['ports']
  87. )
  88. return service_dict