errors.py 4.1 KB

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