errors.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import contextlib
  2. import logging
  3. import socket
  4. from distutils.spawn import find_executable
  5. from textwrap import dedent
  6. from docker.errors import APIError
  7. from requests.exceptions import ConnectionError as RequestsConnectionError
  8. from requests.exceptions import ReadTimeout
  9. from requests.exceptions import SSLError
  10. from requests.packages.urllib3.exceptions import ReadTimeoutError
  11. from ..const import API_VERSION_TO_ENGINE_VERSION
  12. from .utils import binarystr_to_unicode
  13. from .utils import is_docker_for_mac_installed
  14. from .utils import is_mac
  15. from .utils import is_ubuntu
  16. from .utils import is_windows
  17. log = logging.getLogger(__name__)
  18. class UserError(Exception):
  19. def __init__(self, msg):
  20. self.msg = dedent(msg).strip()
  21. def __unicode__(self):
  22. return self.msg
  23. __str__ = __unicode__
  24. class ConnectionError(Exception):
  25. pass
  26. @contextlib.contextmanager
  27. def handle_connection_errors(client):
  28. try:
  29. yield
  30. except SSLError as e:
  31. log.error('SSL error: %s' % e)
  32. raise ConnectionError()
  33. except RequestsConnectionError as e:
  34. if e.args and isinstance(e.args[0], ReadTimeoutError):
  35. log_timeout_error(client.timeout)
  36. raise ConnectionError()
  37. exit_with_error(get_conn_error_message(client.base_url))
  38. except APIError as e:
  39. log_api_error(e, client.api_version)
  40. raise ConnectionError()
  41. except (ReadTimeout, socket.timeout):
  42. log_timeout_error(client.timeout)
  43. raise ConnectionError()
  44. except Exception as e:
  45. if is_windows():
  46. import pywintypes
  47. if isinstance(e, pywintypes.error):
  48. log_windows_pipe_error(e)
  49. raise ConnectionError()
  50. raise
  51. def log_windows_pipe_error(exc):
  52. if exc.winerror == 2:
  53. log.error("Couldn't connect to Docker daemon. You might need to start Docker for Windows.")
  54. elif exc.winerror == 232: # https://github.com/docker/compose/issues/5005
  55. log.error(
  56. "The current Compose file version is not compatible with your engine version. "
  57. "Please upgrade your Compose file to a more recent version, or set "
  58. "a COMPOSE_API_VERSION in your environment."
  59. )
  60. else:
  61. log.error(
  62. "Windows named pipe error: {} (code: {})".format(
  63. binarystr_to_unicode(exc.strerror), exc.winerror
  64. )
  65. )
  66. def log_timeout_error(timeout):
  67. log.error(
  68. "An HTTP request took too long to complete. Retry with --verbose to "
  69. "obtain debug information.\n"
  70. "If you encounter this issue regularly because of slow network "
  71. "conditions, consider setting COMPOSE_HTTP_TIMEOUT to a higher "
  72. "value (current value: %s)." % timeout)
  73. def log_api_error(e, client_version):
  74. explanation = binarystr_to_unicode(e.explanation)
  75. if 'client is newer than server' not in explanation:
  76. log.error(explanation)
  77. return
  78. version = API_VERSION_TO_ENGINE_VERSION.get(client_version)
  79. if not version:
  80. # They've set a custom API version
  81. log.error(explanation)
  82. return
  83. log.error(
  84. "The Docker Engine version is less than the minimum required by "
  85. "Compose. Your current project requires a Docker Engine of "
  86. "version {version} or greater.".format(version=version)
  87. )
  88. def exit_with_error(msg):
  89. log.error(dedent(msg).strip())
  90. raise ConnectionError()
  91. def get_conn_error_message(url):
  92. try:
  93. if find_executable('docker') is None:
  94. return docker_not_found_msg("Couldn't connect to Docker daemon.")
  95. if is_docker_for_mac_installed():
  96. return conn_error_docker_for_mac
  97. if find_executable('docker-machine') is not None:
  98. return conn_error_docker_machine
  99. except UnicodeDecodeError:
  100. # https://github.com/docker/compose/issues/5442
  101. # Ignore the error and print the generic message instead.
  102. pass
  103. return conn_error_generic.format(url=url)
  104. def docker_not_found_msg(problem):
  105. return "{} You might need to install Docker:\n\n{}".format(
  106. problem, docker_install_url())
  107. def docker_install_url():
  108. if is_mac():
  109. return docker_install_url_mac
  110. elif is_ubuntu():
  111. return docker_install_url_ubuntu
  112. elif is_windows():
  113. return docker_install_url_windows
  114. else:
  115. return docker_install_url_generic
  116. docker_install_url_mac = "https://docs.docker.com/engine/installation/mac/"
  117. docker_install_url_ubuntu = "https://docs.docker.com/engine/installation/ubuntulinux/"
  118. docker_install_url_windows = "https://docs.docker.com/engine/installation/windows/"
  119. docker_install_url_generic = "https://docs.docker.com/engine/installation/"
  120. conn_error_docker_machine = """
  121. Couldn't connect to Docker daemon - you might need to run `docker-machine start default`.
  122. """
  123. conn_error_docker_for_mac = """
  124. Couldn't connect to Docker daemon. You might need to start Docker for Mac.
  125. """
  126. conn_error_generic = """
  127. Couldn't connect to Docker daemon at {url} - is it running?
  128. If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
  129. """