serialize.py 6.1 KB

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