command.py 3.0 KB

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