serialize.py 3.7 KB

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