legacy.py 4.7 KB

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