bundle.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import json
  4. import logging
  5. import six
  6. from docker.utils import split_command
  7. from docker.utils.ports import split_port
  8. from .cli.errors import UserError
  9. from .config.serialize import denormalize_config
  10. from .network import get_network_defs_for_service
  11. from .service import format_environment
  12. from .service import NoSuchImageError
  13. from .service import parse_repository_tag
  14. log = logging.getLogger(__name__)
  15. SERVICE_KEYS = {
  16. 'working_dir': 'WorkingDir',
  17. 'user': 'User',
  18. 'labels': 'Labels',
  19. }
  20. IGNORED_KEYS = {'build'}
  21. SUPPORTED_KEYS = {
  22. 'image',
  23. 'ports',
  24. 'expose',
  25. 'networks',
  26. 'command',
  27. 'environment',
  28. 'entrypoint',
  29. } | set(SERVICE_KEYS)
  30. VERSION = '0.1'
  31. class NeedsPush(Exception):
  32. def __init__(self, image_name):
  33. self.image_name = image_name
  34. class NeedsPull(Exception):
  35. def __init__(self, image_name):
  36. self.image_name = image_name
  37. class MissingDigests(Exception):
  38. def __init__(self, needs_push, needs_pull):
  39. self.needs_push = needs_push
  40. self.needs_pull = needs_pull
  41. def serialize_bundle(config, image_digests):
  42. return json.dumps(to_bundle(config, image_digests), indent=2, sort_keys=True)
  43. def get_image_digests(project, allow_fetch=False):
  44. digests = {}
  45. needs_push = set()
  46. needs_pull = set()
  47. for service in project.services:
  48. try:
  49. digests[service.name] = get_image_digest(
  50. service,
  51. allow_fetch=allow_fetch,
  52. )
  53. except NeedsPush as e:
  54. needs_push.add(e.image_name)
  55. except NeedsPull as e:
  56. needs_pull.add(e.image_name)
  57. if needs_push or needs_pull:
  58. raise MissingDigests(needs_push, needs_pull)
  59. return digests
  60. def get_image_digest(service, allow_fetch=False):
  61. if 'image' not in service.options:
  62. raise UserError(
  63. "Service '{s.name}' doesn't define an image tag. An image name is "
  64. "required to generate a proper image digest for the bundle. Specify "
  65. "an image repo and tag with the 'image' option.".format(s=service))
  66. _, _, separator = parse_repository_tag(service.options['image'])
  67. # Compose file already uses a digest, no lookup required
  68. if separator == '@':
  69. return service.options['image']
  70. try:
  71. image = service.image()
  72. except NoSuchImageError:
  73. action = 'build' if 'build' in service.options else 'pull'
  74. raise UserError(
  75. "Image not found for service '{service}'. "
  76. "You might need to run `docker-compose {action} {service}`."
  77. .format(service=service.name, action=action))
  78. if image['RepoDigests']:
  79. # TODO: pick a digest based on the image tag if there are multiple
  80. # digests
  81. return image['RepoDigests'][0]
  82. if not allow_fetch:
  83. if 'build' in service.options:
  84. raise NeedsPush(service.image_name)
  85. else:
  86. raise NeedsPull(service.image_name)
  87. return fetch_image_digest(service)
  88. def fetch_image_digest(service):
  89. if 'build' not in service.options:
  90. digest = service.pull()
  91. else:
  92. try:
  93. digest = service.push()
  94. except:
  95. log.error(
  96. "Failed to push image for service '{s.name}'. Please use an "
  97. "image tag that can be pushed to a Docker "
  98. "registry.".format(s=service))
  99. raise
  100. if not digest:
  101. raise ValueError("Failed to get digest for %s" % service.name)
  102. repo, _, _ = parse_repository_tag(service.options['image'])
  103. identifier = '{repo}@{digest}'.format(repo=repo, digest=digest)
  104. # only do this if RepoDigests isn't already populated
  105. image = service.image()
  106. if not image['RepoDigests']:
  107. # Pull by digest so that image['RepoDigests'] is populated for next time
  108. # and we don't have to pull/push again
  109. service.client.pull(identifier)
  110. log.info("Stored digest for {}".format(service.image_name))
  111. return identifier
  112. def to_bundle(config, image_digests):
  113. if config.networks:
  114. log.warn("Unsupported top level key 'networks' - ignoring")
  115. if config.volumes:
  116. log.warn("Unsupported top level key 'volumes' - ignoring")
  117. config = denormalize_config(config)
  118. return {
  119. 'Version': VERSION,
  120. 'Services': {
  121. name: convert_service_to_bundle(
  122. name,
  123. service_dict,
  124. image_digests[name],
  125. )
  126. for name, service_dict in config['services'].items()
  127. },
  128. }
  129. def convert_service_to_bundle(name, service_dict, image_digest):
  130. container_config = {'Image': image_digest}
  131. for key, value in service_dict.items():
  132. if key in IGNORED_KEYS:
  133. continue
  134. if key not in SUPPORTED_KEYS:
  135. log.warn("Unsupported key '{}' in services.{} - ignoring".format(key, name))
  136. continue
  137. if key == 'environment':
  138. container_config['Env'] = format_environment({
  139. envkey: envvalue for envkey, envvalue in value.items()
  140. if envvalue
  141. })
  142. continue
  143. if key in SERVICE_KEYS:
  144. container_config[SERVICE_KEYS[key]] = value
  145. continue
  146. set_command_and_args(
  147. container_config,
  148. service_dict.get('entrypoint', []),
  149. service_dict.get('command', []))
  150. container_config['Networks'] = make_service_networks(name, service_dict)
  151. ports = make_port_specs(service_dict)
  152. if ports:
  153. container_config['Ports'] = ports
  154. return container_config
  155. # See https://github.com/docker/swarmkit/blob//agent/exec/container/container.go#L95
  156. def set_command_and_args(config, entrypoint, command):
  157. if isinstance(entrypoint, six.string_types):
  158. entrypoint = split_command(entrypoint)
  159. if isinstance(command, six.string_types):
  160. command = split_command(command)
  161. if entrypoint:
  162. config['Command'] = entrypoint + command
  163. return
  164. if command:
  165. config['Args'] = command
  166. def make_service_networks(name, service_dict):
  167. networks = []
  168. for network_name, network_def in get_network_defs_for_service(service_dict).items():
  169. for key in network_def.keys():
  170. log.warn(
  171. "Unsupported key '{}' in services.{}.networks.{} - ignoring"
  172. .format(key, name, network_name))
  173. networks.append(network_name)
  174. return networks
  175. def make_port_specs(service_dict):
  176. ports = []
  177. internal_ports = [
  178. internal_port
  179. for port_def in service_dict.get('ports', [])
  180. for internal_port in split_port(port_def)[0]
  181. ]
  182. internal_ports += service_dict.get('expose', [])
  183. for internal_port in internal_ports:
  184. spec = make_port_spec(internal_port)
  185. if spec not in ports:
  186. ports.append(spec)
  187. return ports
  188. def make_port_spec(value):
  189. components = six.text_type(value).partition('/')
  190. return {
  191. 'Protocol': components[2] or 'tcp',
  192. 'Port': int(components[0]),
  193. }