command.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import contextlib
  4. import logging
  5. import os
  6. import re
  7. import six
  8. from requests.exceptions import ConnectionError
  9. from requests.exceptions import SSLError
  10. from . import errors
  11. from . import verbose_proxy
  12. from .. import config
  13. from ..const import API_VERSIONS
  14. from ..project import Project
  15. from .docker_client import docker_client
  16. from .utils import call_silently
  17. from .utils import get_version_info
  18. from .utils import is_mac
  19. from .utils import is_ubuntu
  20. log = logging.getLogger(__name__)
  21. @contextlib.contextmanager
  22. def friendly_error_message():
  23. try:
  24. yield
  25. except SSLError as e:
  26. raise errors.UserError('SSL error: %s' % e)
  27. except ConnectionError:
  28. if call_silently(['which', 'docker']) != 0:
  29. if is_mac():
  30. raise errors.DockerNotFoundMac()
  31. elif is_ubuntu():
  32. raise errors.DockerNotFoundUbuntu()
  33. else:
  34. raise errors.DockerNotFoundGeneric()
  35. elif call_silently(['which', 'docker-machine']) == 0:
  36. raise errors.ConnectionErrorDockerMachine()
  37. else:
  38. raise errors.ConnectionErrorGeneric(get_client().base_url)
  39. def project_from_options(base_dir, options):
  40. return get_project(
  41. base_dir,
  42. get_config_path_from_options(options),
  43. project_name=options.get('--project-name'),
  44. verbose=options.get('--verbose'),
  45. )
  46. def get_config_path_from_options(options):
  47. file_option = options.get('--file')
  48. if file_option:
  49. return file_option
  50. config_file = os.environ.get('COMPOSE_FILE')
  51. return [config_file] if config_file else None
  52. def get_client(verbose=False, version=None):
  53. client = docker_client(version=version)
  54. if verbose:
  55. version_info = six.iteritems(client.version())
  56. log.info(get_version_info('full'))
  57. log.info("Docker base_url: %s", client.base_url)
  58. log.info("Docker version: %s",
  59. ", ".join("%s=%s" % item for item in version_info))
  60. return verbose_proxy.VerboseProxy('docker', client)
  61. return client
  62. def get_project(base_dir, config_path=None, project_name=None, verbose=False):
  63. config_details = config.find(base_dir, config_path)
  64. project_name = get_project_name(config_details.working_dir, project_name)
  65. config_data = config.load(config_details)
  66. api_version = os.environ.get(
  67. 'COMPOSE_API_VERSION',
  68. API_VERSIONS[config_data.version])
  69. client = get_client(verbose=verbose, version=api_version)
  70. return Project.from_config(project_name, config_data, client)
  71. def get_project_name(working_dir, project_name=None):
  72. def normalize_name(name):
  73. return re.sub(r'[^a-z0-9]', '', name.lower())
  74. project_name = project_name or os.environ.get('COMPOSE_PROJECT_NAME')
  75. if project_name is not None:
  76. return normalize_name(project_name)
  77. project = os.path.basename(os.path.abspath(working_dir))
  78. if project:
  79. return normalize_name(project)
  80. return 'default'