command.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from __future__ import unicode_literals
  2. from __future__ import absolute_import
  3. from ..packages.docker import Client
  4. from requests.exceptions import ConnectionError
  5. import errno
  6. import logging
  7. import os
  8. import re
  9. import yaml
  10. import six
  11. from ..project import Project
  12. from ..service import ConfigError
  13. from .docopt_command import DocoptCommand
  14. from .formatter import Formatter
  15. from .utils import cached_property, docker_url, call_silently, is_mac, is_ubuntu
  16. from .errors import UserError
  17. log = logging.getLogger(__name__)
  18. class Command(DocoptCommand):
  19. base_dir = '.'
  20. def dispatch(self, *args, **kwargs):
  21. try:
  22. super(Command, self).dispatch(*args, **kwargs)
  23. except ConnectionError:
  24. if call_silently(['which', 'docker']) != 0:
  25. if is_mac():
  26. raise UserError("""
  27. Couldn't connect to Docker daemon. You might need to install docker-osx:
  28. https://github.com/noplay/docker-osx
  29. """)
  30. elif is_ubuntu():
  31. raise UserError("""
  32. Couldn't connect to Docker daemon. You might need to install Docker:
  33. http://docs.docker.io/en/latest/installation/ubuntulinux/
  34. """)
  35. else:
  36. raise UserError("""
  37. Couldn't connect to Docker daemon. You might need to install Docker:
  38. http://docs.docker.io/en/latest/installation/
  39. """)
  40. elif call_silently(['which', 'docker-osx']) == 0:
  41. raise UserError("Couldn't connect to Docker daemon - you might need to run `docker-osx shell`.")
  42. else:
  43. raise UserError("""
  44. Couldn't connect to Docker daemon at %s - is it running?
  45. If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
  46. """ % self.client.base_url)
  47. @cached_property
  48. def client(self):
  49. return Client(docker_url())
  50. @cached_property
  51. def project(self):
  52. try:
  53. yaml_path = self.check_yaml_filename()
  54. config = yaml.load(open(yaml_path))
  55. except IOError as e:
  56. if e.errno == errno.ENOENT:
  57. log.error("Can't find %s. Are you in the right directory?", os.path.basename(e.filename))
  58. else:
  59. log.error(e)
  60. exit(1)
  61. try:
  62. return Project.from_config(self.project_name, config, self.client)
  63. except ConfigError as e:
  64. raise UserError(six.text_type(e))
  65. @cached_property
  66. def project_name(self):
  67. project = os.path.basename(os.getcwd())
  68. project = re.sub(r'[^a-zA-Z0-9]', '', project)
  69. if not project:
  70. project = 'default'
  71. return project
  72. @cached_property
  73. def formatter(self):
  74. return Formatter()
  75. def check_yaml_filename(self):
  76. if os.path.exists(os.path.join(self.base_dir, 'fig.yaml')):
  77. log.warning("Fig just read the file 'fig.yaml' on startup, rather than 'fig.yml'")
  78. log.warning("Please be aware that fig.yml the expected extension in most cases, and using .yaml can cause compatibility issues in future")
  79. return os.path.join(self.base_dir, 'fig.yaml')
  80. else:
  81. return os.path.join(self.base_dir, 'fig.yml')