errors.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import contextlib
  4. import logging
  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 ..const import API_VERSION_TO_ENGINE_VERSION
  11. from ..const import HTTP_TIMEOUT
  12. from .utils import call_silently
  13. from .utils import is_mac
  14. from .utils import is_ubuntu
  15. log = logging.getLogger(__name__)
  16. class UserError(Exception):
  17. def __init__(self, msg):
  18. self.msg = dedent(msg).strip()
  19. def __unicode__(self):
  20. return self.msg
  21. __str__ = __unicode__
  22. class ConnectionError(Exception):
  23. pass
  24. @contextlib.contextmanager
  25. def handle_connection_errors(client):
  26. try:
  27. yield
  28. except SSLError as e:
  29. log.error('SSL error: %s' % e)
  30. raise ConnectionError()
  31. except RequestsConnectionError:
  32. if call_silently(['which', 'docker']) != 0:
  33. if is_mac():
  34. exit_with_error(docker_not_found_mac)
  35. if is_ubuntu():
  36. exit_with_error(docker_not_found_ubuntu)
  37. exit_with_error(docker_not_found_generic)
  38. if call_silently(['which', 'docker-machine']) == 0:
  39. exit_with_error(conn_error_docker_machine)
  40. exit_with_error(conn_error_generic.format(url=client.base_url))
  41. except APIError as e:
  42. log_api_error(e, client.api_version)
  43. raise ConnectionError()
  44. except ReadTimeout as e:
  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)." % HTTP_TIMEOUT)
  51. raise ConnectionError()
  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. docker_not_found_mac = """
  69. Couldn't connect to Docker daemon. You might need to install Docker:
  70. https://docs.docker.com/engine/installation/mac/
  71. """
  72. docker_not_found_ubuntu = """
  73. Couldn't connect to Docker daemon. You might need to install Docker:
  74. https://docs.docker.com/engine/installation/ubuntulinux/
  75. """
  76. docker_not_found_generic = """
  77. Couldn't connect to Docker daemon. You might need to install Docker:
  78. https://docs.docker.com/engine/installation/
  79. """
  80. conn_error_docker_machine = """
  81. Couldn't connect to Docker daemon - you might need to run `docker-machine start default`.
  82. """
  83. conn_error_generic = """
  84. Couldn't connect to Docker daemon at {url} - is it running?
  85. If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
  86. """