serialize.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import yaml
  2. from compose.config import types
  3. from compose.const import COMPOSEFILE_V1 as V1
  4. from compose.const import COMPOSEFILE_V2_1 as V2_1
  5. from compose.const import COMPOSEFILE_V2_3 as V2_3
  6. from compose.const import COMPOSEFILE_V3_0 as V3_0
  7. from compose.const import COMPOSEFILE_V3_2 as V3_2
  8. from compose.const import COMPOSEFILE_V3_4 as V3_4
  9. from compose.const import COMPOSEFILE_V3_5 as V3_5
  10. def serialize_config_type(dumper, data):
  11. representer = dumper.represent_str
  12. return representer(data.repr())
  13. def serialize_dict_type(dumper, data):
  14. return dumper.represent_dict(data.repr())
  15. def serialize_string(dumper, data):
  16. """ Ensure boolean-like strings are quoted in the output """
  17. representer = dumper.represent_str
  18. if isinstance(data, bytes):
  19. data = data.decode('utf-8')
  20. if data.lower() in ('y', 'n', 'yes', 'no', 'on', 'off', 'true', 'false'):
  21. # Empirically only y/n appears to be an issue, but this might change
  22. # depending on which PyYaml version is being used. Err on safe side.
  23. return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='"')
  24. return representer(data)
  25. def serialize_string_escape_dollar(dumper, data):
  26. """ Ensure boolean-like strings are quoted in the output and escape $ characters """
  27. data = data.replace('$', '$$')
  28. return serialize_string(dumper, data)
  29. yaml.SafeDumper.add_representer(types.MountSpec, serialize_dict_type)
  30. yaml.SafeDumper.add_representer(types.VolumeFromSpec, serialize_config_type)
  31. yaml.SafeDumper.add_representer(types.VolumeSpec, serialize_config_type)
  32. yaml.SafeDumper.add_representer(types.SecurityOpt, serialize_config_type)
  33. yaml.SafeDumper.add_representer(types.ServiceSecret, serialize_dict_type)
  34. yaml.SafeDumper.add_representer(types.ServiceConfig, serialize_dict_type)
  35. yaml.SafeDumper.add_representer(types.ServicePort, serialize_dict_type)
  36. def denormalize_config(config, image_digests=None):
  37. result = {'version': str(V2_1) if config.version == V1 else str(config.version)}
  38. denormalized_services = [
  39. denormalize_service_dict(
  40. service_dict,
  41. config.version,
  42. image_digests[service_dict['name']] if image_digests else None)
  43. for service_dict in config.services
  44. ]
  45. result['services'] = {
  46. service_dict.pop('name'): service_dict
  47. for service_dict in denormalized_services
  48. }
  49. for key in ('networks', 'volumes', 'secrets', 'configs'):
  50. config_dict = getattr(config, key)
  51. if not config_dict:
  52. continue
  53. result[key] = config_dict.copy()
  54. for name, conf in result[key].items():
  55. if 'external_name' in conf:
  56. del conf['external_name']
  57. if 'name' in conf:
  58. if config.version < V2_1 or (
  59. config.version >= V3_0 and config.version < v3_introduced_name_key(key)):
  60. del conf['name']
  61. elif 'external' in conf:
  62. conf['external'] = bool(conf['external'])
  63. if 'attachable' in conf and config.version < V3_2:
  64. # For compatibility mode, this option is invalid in v2
  65. del conf['attachable']
  66. return result
  67. def v3_introduced_name_key(key):
  68. if key == 'volumes':
  69. return V3_4
  70. return V3_5
  71. def serialize_config(config, image_digests=None, escape_dollar=True):
  72. if escape_dollar:
  73. yaml.SafeDumper.add_representer(str, serialize_string_escape_dollar)
  74. yaml.SafeDumper.add_representer(str, serialize_string_escape_dollar)
  75. else:
  76. yaml.SafeDumper.add_representer(str, serialize_string)
  77. yaml.SafeDumper.add_representer(str, serialize_string)
  78. return yaml.safe_dump(
  79. denormalize_config(config, image_digests),
  80. default_flow_style=False,
  81. indent=2,
  82. width=80,
  83. allow_unicode=True
  84. )
  85. def serialize_ns_time_value(value):
  86. result = (value, 'ns')
  87. table = [
  88. (1000., 'us'),
  89. (1000., 'ms'),
  90. (1000., 's'),
  91. (60., 'm'),
  92. (60., 'h')
  93. ]
  94. for stage in table:
  95. tmp = value / stage[0]
  96. if tmp == int(value / stage[0]):
  97. value = tmp
  98. result = (int(value), stage[1])
  99. else:
  100. break
  101. return '{0}{1}'.format(*result)
  102. def denormalize_service_dict(service_dict, version, image_digest=None):
  103. service_dict = service_dict.copy()
  104. if image_digest:
  105. service_dict['image'] = image_digest
  106. if 'restart' in service_dict:
  107. service_dict['restart'] = types.serialize_restart_spec(
  108. service_dict['restart']
  109. )
  110. if version == V1 and 'network_mode' not in service_dict:
  111. service_dict['network_mode'] = 'bridge'
  112. if 'depends_on' in service_dict and (version < V2_1 or version >= V3_0):
  113. service_dict['depends_on'] = sorted([
  114. svc for svc in service_dict['depends_on'].keys()
  115. ])
  116. if 'healthcheck' in service_dict:
  117. if 'interval' in service_dict['healthcheck']:
  118. service_dict['healthcheck']['interval'] = serialize_ns_time_value(
  119. service_dict['healthcheck']['interval']
  120. )
  121. if 'timeout' in service_dict['healthcheck']:
  122. service_dict['healthcheck']['timeout'] = serialize_ns_time_value(
  123. service_dict['healthcheck']['timeout']
  124. )
  125. if 'start_period' in service_dict['healthcheck']:
  126. service_dict['healthcheck']['start_period'] = serialize_ns_time_value(
  127. service_dict['healthcheck']['start_period']
  128. )
  129. if 'ports' in service_dict:
  130. service_dict['ports'] = [
  131. p.legacy_repr() if p.external_ip or version < V3_2 else p
  132. for p in service_dict['ports']
  133. ]
  134. if 'volumes' in service_dict and (version < V2_3 or (version > V3_0 and version < V3_2)):
  135. service_dict['volumes'] = [
  136. v.legacy_repr() if isinstance(v, types.MountSpec) else v for v in service_dict['volumes']
  137. ]
  138. return service_dict