command.py 3.3 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. from ..packages import six
  11. import sys
  12. from ..project import Project
  13. from ..service import ConfigError
  14. from .docopt_command import DocoptCommand
  15. from .formatter import Formatter
  16. from .utils import cached_property, docker_url, call_silently, is_mac, is_ubuntu
  17. from . import errors
  18. log = logging.getLogger(__name__)
  19. class Command(DocoptCommand):
  20. base_dir = '.'
  21. def __init__(self):
  22. self._yaml_path = os.environ.get('FIG_FILE', None)
  23. self.explicit_project_name = None
  24. def dispatch(self, *args, **kwargs):
  25. try:
  26. super(Command, self).dispatch(*args, **kwargs)
  27. except ConnectionError:
  28. if call_silently(['which', 'docker']) != 0:
  29. if is_mac():
  30. raise errors.DockerNotFoundMac()
  31. elif is_ubuntu():
  32. raise errors.DockerNotFoundUbuntu()
  33. else:
  34. raise errors.DockerNotFoundGeneric()
  35. elif call_silently(['which', 'docker-osx']) == 0:
  36. raise errors.ConnectionErrorDockerOSX()
  37. else:
  38. raise errors.ConnectionErrorGeneric(self.client.base_url)
  39. def perform_command(self, options, *args, **kwargs):
  40. if options['--file'] is not None:
  41. self.yaml_path = os.path.join(self.base_dir, options['--file'])
  42. if options['--project-name'] is not None:
  43. self.explicit_project_name = options['--project-name']
  44. return super(Command, self).perform_command(options, *args, **kwargs)
  45. @cached_property
  46. def client(self):
  47. return Client(docker_url())
  48. @cached_property
  49. def project(self):
  50. try:
  51. config = yaml.safe_load(open(self.yaml_path))
  52. except IOError as e:
  53. if e.errno == errno.ENOENT:
  54. raise errors.FigFileNotFound(os.path.basename(e.filename))
  55. raise errors.UserError(six.text_type(e))
  56. try:
  57. return Project.from_config(self.project_name, config, self.client)
  58. except ConfigError as e:
  59. raise errors.UserError(six.text_type(e))
  60. @cached_property
  61. def project_name(self):
  62. project = os.path.basename(os.path.dirname(os.path.abspath(self.yaml_path)))
  63. if self.explicit_project_name is not None:
  64. project = self.explicit_project_name
  65. project = re.sub(r'[^a-zA-Z0-9]', '', project)
  66. if not project:
  67. project = 'default'
  68. return project
  69. @cached_property
  70. def formatter(self):
  71. return Formatter()
  72. @cached_property
  73. def yaml_path(self):
  74. if self._yaml_path is not None:
  75. return self._yaml_path
  76. elif 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')
  82. @yaml_path.setter
  83. def yaml_path(self, value):
  84. self._yaml_path = value