legacy.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. names = list(get_legacy_container_names(
  26. client,
  27. project,
  28. services,
  29. stopped=stopped,
  30. one_off=one_off))
  31. if names:
  32. raise LegacyContainersError(names)
  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, name):
  44. project_name, service_name, one_off, number = NAME_RE.match(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. client = project.client
  52. for container in client.containers(all=True):
  53. name = get_container_name(container)
  54. if not is_valid_name(name):
  55. continue
  56. add_labels(project, Container.from_ps(client, container), name)
  57. def get_legacy_container_names(
  58. client,
  59. project,
  60. services,
  61. stopped=False,
  62. one_off=False):
  63. for container in client.containers(all=stopped):
  64. name = get_container_name(container)
  65. for service in services:
  66. if has_container(project, service, name, one_off=one_off):
  67. yield name
  68. def has_container(project, service, name, one_off=False):
  69. if not name or not is_valid_name(name, one_off):
  70. return False
  71. container_project, container_service, _container_number = parse_name(name)
  72. return container_project == project and container_service == service
  73. def is_valid_name(name, one_off=False):
  74. match = NAME_RE.match(name)
  75. if match is None:
  76. return False
  77. if one_off:
  78. return match.group(3) == 'run_'
  79. else:
  80. return match.group(3) is None
  81. def parse_name(name):
  82. match = NAME_RE.match(name)
  83. (project, service_name, _, suffix) = match.groups()
  84. return (project, service_name, int(suffix))