1
0

errors.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. 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) as e:
  42. log_timeout_error()
  43. raise ConnectionError()
  44. def log_timeout_error(timeout):
  45. log.error(
  46. "An HTTP request took too long to complete. Retry with --verbose to "
  47. "obtain debug information.\n"
  48. "If you encounter this issue regularly because of slow network "
  49. "conditions, consider setting COMPOSE_HTTP_TIMEOUT to a higher "
  50. "value (current value: %s)." % timeout)
  51. def log_api_error(e, client_version):
  52. if b'client is newer than server' not in e.explanation:
  53. log.error(e.explanation)
  54. return
  55. version = API_VERSION_TO_ENGINE_VERSION.get(client_version)
  56. if not version:
  57. # They've set a custom API version
  58. log.error(e.explanation)
  59. return
  60. log.error(
  61. "The Docker Engine version is less than the minimum required by "
  62. "Compose. Your current project requires a Docker Engine of "
  63. "version {version} or greater.".format(version=version))
  64. def exit_with_error(msg):
  65. log.error(dedent(msg).strip())
  66. raise ConnectionError()
  67. def get_conn_error_message(url):
  68. if call_silently(['which', 'docker']) != 0:
  69. if is_mac():
  70. return docker_not_found_mac
  71. if is_ubuntu():
  72. return docker_not_found_ubuntu
  73. return docker_not_found_generic
  74. if is_docker_for_mac_installed():
  75. return conn_error_docker_for_mac
  76. if call_silently(['which', 'docker-machine']) == 0:
  77. return conn_error_docker_machine
  78. return conn_error_generic.format(url=url)
  79. docker_not_found_mac = """
  80. Couldn't connect to Docker daemon. You might need to install Docker:
  81. https://docs.docker.com/engine/installation/mac/
  82. """
  83. docker_not_found_ubuntu = """
  84. Couldn't connect to Docker daemon. You might need to install Docker:
  85. https://docs.docker.com/engine/installation/ubuntulinux/
  86. """
  87. docker_not_found_generic = """
  88. Couldn't connect to Docker daemon. You might need to install Docker:
  89. https://docs.docker.com/engine/installation/
  90. """
  91. conn_error_docker_machine = """
  92. Couldn't connect to Docker daemon - you might need to run `docker-machine start default`.
  93. """
  94. conn_error_docker_for_mac = """
  95. Couldn't connect to Docker daemon. You might need to start Docker for Mac.
  96. """
  97. conn_error_generic = """
  98. Couldn't connect to Docker daemon at {url} - is it running?
  99. If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
  100. """