legacy.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import logging
  2. import re
  3. from .container import get_container_name, Container
  4. log = logging.getLogger(__name__)
  5. # TODO: remove this section when migrate_project_to_labels is removed
  6. NAME_RE = re.compile(r'^([^_]+)_([^_]+)_(run_)?(\d+)$')
  7. ERROR_MESSAGE_FORMAT = """
  8. Compose found the following containers without labels:
  9. {names_list}
  10. As of Compose 1.3.0, containers are identified with labels instead of naming convention. If you want to continue using these containers, run:
  11. $ docker-compose migrate-to-labels
  12. Alternatively, remove them:
  13. $ docker rm -f {rm_args}
  14. """
  15. def check_for_legacy_containers(
  16. client,
  17. project,
  18. services,
  19. stopped=False,
  20. one_off=False):
  21. """Check if there are containers named using the old naming convention
  22. and warn the user that those containers may need to be migrated to
  23. using labels, so that compose can find them.
  24. """
  25. containers = list(get_legacy_containers(
  26. client,
  27. project,
  28. services,
  29. stopped=stopped,
  30. one_off=one_off))
  31. if containers:
  32. raise LegacyContainersError([c.name for c in containers])
  33. class LegacyContainersError(Exception):
  34. def __init__(self, names):
  35. self.names = names
  36. self.msg = ERROR_MESSAGE_FORMAT.format(
  37. names_list="\n".join(" {}".format(name) for name in names),
  38. rm_args=" ".join(names),
  39. )
  40. def __unicode__(self):
  41. return self.msg
  42. __str__ = __unicode__
  43. def add_labels(project, container):
  44. project_name, service_name, one_off, number = NAME_RE.match(container.name).groups()
  45. if project_name != project.name or service_name not in project.service_names:
  46. return
  47. service = project.get_service(service_name)
  48. service.recreate_container(container)
  49. def migrate_project_to_labels(project):
  50. log.info("Running migration to labels for project %s", project.name)
  51. containers = get_legacy_containers(
  52. project.client,
  53. project.name,
  54. project.service_names,
  55. stopped=True,
  56. one_off=False)
  57. for container in containers:
  58. add_labels(project, container)
  59. def get_legacy_containers(
  60. client,
  61. project,
  62. services,
  63. stopped=False,
  64. one_off=False):
  65. containers = client.containers(all=stopped)
  66. for service in services:
  67. for container in containers:
  68. name = get_container_name(container)
  69. if has_container(project, service, name, one_off=one_off):
  70. yield Container.from_ps(client, container)
  71. def has_container(project, service, name, one_off=False):
  72. if not name or not is_valid_name(name, one_off):
  73. return False
  74. container_project, container_service, _container_number = parse_name(name)
  75. return container_project == project and container_service == service
  76. def is_valid_name(name, one_off=False):
  77. match = NAME_RE.match(name)
  78. if match is None:
  79. return False
  80. if one_off:
  81. return match.group(3) == 'run_'
  82. else:
  83. return match.group(3) is None
  84. def parse_name(name):
  85. match = NAME_RE.match(name)
  86. (project, service_name, _, suffix) = match.groups()
  87. return (project, service_name, int(suffix))