errors.py 4.0 KB

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