legacy.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. def is_valid_name(name):
  8. match = NAME_RE.match(name)
  9. return match is not None
  10. def check_for_legacy_containers(
  11. client,
  12. project,
  13. services,
  14. stopped=False,
  15. one_off=False):
  16. """Check if there are containers named using the old naming convention
  17. and warn the user that those containers may need to be migrated to
  18. using labels, so that compose can find them.
  19. """
  20. for container in client.containers(all=stopped):
  21. name = get_container_name(container)
  22. for service in services:
  23. prefix = '%s_%s_%s' % (project, service, 'run_' if one_off else '')
  24. if not name.startswith(prefix):
  25. continue
  26. log.warn(
  27. "Compose found a found a container named %s without any "
  28. "labels. As of compose 1.3.0 containers are identified with "
  29. "labels instead of naming convention. If you'd like compose "
  30. "to use this container, please run "
  31. "`docker-compose migrate-to-labels`" % (name,))
  32. def add_labels(project, container, name):
  33. project_name, service_name, one_off, number = NAME_RE.match(name).groups()
  34. if project_name != project.name or service_name not in project.service_names:
  35. return
  36. service = project.get_service(service_name)
  37. service.recreate_container(container)
  38. def migrate_project_to_labels(project):
  39. log.info("Running migration to labels for project %s", project.name)
  40. client = project.client
  41. for container in client.containers(all=True):
  42. name = get_container_name(container)
  43. if not is_valid_name(name):
  44. continue
  45. add_labels(project, Container.from_ps(client, container), name)