legacy.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import logging
  2. import re
  3. from .const import LABEL_VERSION
  4. from .container import get_container_name, Container
  5. log = logging.getLogger(__name__)
  6. # TODO: remove this section when migrate_project_to_labels is removed
  7. NAME_RE = re.compile(r'^([^_]+)_([^_]+)_(run_)?(\d+)$')
  8. ERROR_MESSAGE_FORMAT = """
  9. Compose found the following containers without labels:
  10. {names_list}
  11. As of Compose 1.3.0, containers are identified with labels instead of naming convention. If you want to continue using these containers, run:
  12. $ docker-compose migrate-to-labels
  13. Alternatively, remove them:
  14. $ docker rm -f {rm_args}
  15. """
  16. ONE_OFF_ADDENDUM_FORMAT = """
  17. You should also remove your one-off containers:
  18. $ docker rm -f {rm_args}
  19. """
  20. ONE_OFF_ERROR_MESSAGE_FORMAT = """
  21. Compose found the following containers without labels:
  22. {names_list}
  23. As of Compose 1.3.0, containers are identified with labels instead of naming convention.
  24. Remove them before continuing:
  25. $ docker rm -f {rm_args}
  26. """
  27. def check_for_legacy_containers(
  28. client,
  29. project,
  30. services,
  31. allow_one_off=True):
  32. """Check if there are containers named using the old naming convention
  33. and warn the user that those containers may need to be migrated to
  34. using labels, so that compose can find them.
  35. """
  36. containers = get_legacy_containers(client, project, services, one_off=False)
  37. if containers:
  38. one_off_containers = get_legacy_containers(client, project, services, one_off=True)
  39. raise LegacyContainersError(
  40. [c.name for c in containers],
  41. [c.name for c in one_off_containers],
  42. )
  43. if not allow_one_off:
  44. one_off_containers = get_legacy_containers(client, project, services, one_off=True)
  45. if one_off_containers:
  46. raise LegacyOneOffContainersError(
  47. [c.name for c in one_off_containers],
  48. )
  49. class LegacyError(Exception):
  50. def __unicode__(self):
  51. return self.msg
  52. __str__ = __unicode__
  53. class LegacyContainersError(LegacyError):
  54. def __init__(self, names, one_off_names):
  55. self.names = names
  56. self.one_off_names = one_off_names
  57. self.msg = ERROR_MESSAGE_FORMAT.format(
  58. names_list="\n".join(" {}".format(name) for name in names),
  59. rm_args=" ".join(names),
  60. )
  61. if one_off_names:
  62. self.msg += ONE_OFF_ADDENDUM_FORMAT.format(rm_args=" ".join(one_off_names))
  63. class LegacyOneOffContainersError(LegacyError):
  64. def __init__(self, one_off_names):
  65. self.one_off_names = one_off_names
  66. self.msg = ONE_OFF_ERROR_MESSAGE_FORMAT.format(
  67. names_list="\n".join(" {}".format(name) for name in one_off_names),
  68. rm_args=" ".join(one_off_names),
  69. )
  70. def add_labels(project, container):
  71. project_name, service_name, one_off, number = NAME_RE.match(container.name).groups()
  72. if project_name != project.name or service_name not in project.service_names:
  73. return
  74. service = project.get_service(service_name)
  75. service.recreate_container(container)
  76. def migrate_project_to_labels(project):
  77. log.info("Running migration to labels for project %s", project.name)
  78. containers = get_legacy_containers(
  79. project.client,
  80. project.name,
  81. project.service_names,
  82. one_off=False,
  83. )
  84. for container in containers:
  85. add_labels(project, container)
  86. def get_legacy_containers(
  87. client,
  88. project,
  89. services,
  90. one_off=False):
  91. return list(_get_legacy_containers_iter(
  92. client,
  93. project,
  94. services,
  95. one_off=one_off,
  96. ))
  97. def _get_legacy_containers_iter(
  98. client,
  99. project,
  100. services,
  101. one_off=False):
  102. containers = client.containers(all=True)
  103. for service in services:
  104. for container in containers:
  105. if LABEL_VERSION in (container.get('Labels') or {}):
  106. continue
  107. name = get_container_name(container)
  108. if has_container(project, service, name, one_off=one_off):
  109. yield Container.from_ps(client, container)
  110. def has_container(project, service, name, one_off=False):
  111. if not name or not is_valid_name(name, one_off):
  112. return False
  113. container_project, container_service, _container_number = parse_name(name)
  114. return container_project == project and container_service == service
  115. def is_valid_name(name, one_off=False):
  116. match = NAME_RE.match(name)
  117. if match is None:
  118. return False
  119. if one_off:
  120. return match.group(3) == 'run_'
  121. else:
  122. return match.group(3) is None
  123. def parse_name(name):
  124. match = NAME_RE.match(name)
  125. (project, service_name, _, suffix) = match.groups()
  126. return (project, service_name, int(suffix))