serialize.py 6.0 KB

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