1
0

errors.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 ..const import HTTP_TIMEOUT
  14. from .utils import call_silently
  15. from .utils import is_docker_for_mac_installed
  16. from .utils import is_mac
  17. from .utils import is_ubuntu
  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()
  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()
  44. raise ConnectionError()
  45. def log_timeout_error():
  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)." % HTTP_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. if is_mac():
  71. return docker_not_found_mac
  72. if is_ubuntu():
  73. return docker_not_found_ubuntu
  74. return docker_not_found_generic
  75. if is_docker_for_mac_installed():
  76. return conn_error_docker_for_mac
  77. if call_silently(['which', 'docker-machine']) == 0:
  78. return conn_error_docker_machine
  79. return conn_error_generic.format(url=url)
  80. docker_not_found_mac = """
  81. Couldn't connect to Docker daemon. You might need to install Docker:
  82. https://docs.docker.com/engine/installation/mac/
  83. """
  84. docker_not_found_ubuntu = """
  85. Couldn't connect to Docker daemon. You might need to install Docker:
  86. https://docs.docker.com/engine/installation/ubuntulinux/
  87. """
  88. docker_not_found_generic = """
  89. Couldn't connect to Docker daemon. You might need to install Docker:
  90. https://docs.docker.com/engine/installation/
  91. """
  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. """