2014-01-06 03:26:32 +01:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from __future__ import absolute_import
|
2014-01-16 13:56:36 +01:00
|
|
|
from ..packages.docker import Client
|
2014-01-16 17:36:01 +01:00
|
|
|
from requests.exceptions import ConnectionError
|
2014-01-03 12:58:49 +01:00
|
|
|
import errno
|
2013-12-11 15:25:32 +01:00
|
|
|
import logging
|
|
|
|
import os
|
2013-12-19 16:32:24 +01:00
|
|
|
import re
|
2013-12-11 15:25:32 +01:00
|
|
|
import yaml
|
2014-02-28 20:16:32 +01:00
|
|
|
from ..packages import six
|
2014-02-19 23:42:21 +01:00
|
|
|
import sys
|
2013-12-11 15:25:32 +01:00
|
|
|
|
2013-12-19 17:55:12 +01:00
|
|
|
from ..project import Project
|
2014-02-05 01:33:29 +01:00
|
|
|
from ..service import ConfigError
|
2013-12-11 15:25:32 +01:00
|
|
|
from .docopt_command import DocoptCommand
|
|
|
|
from .formatter import Formatter
|
2014-02-05 00:19:50 +01:00
|
|
|
from .utils import cached_property, docker_url, call_silently, is_mac, is_ubuntu
|
2014-02-26 16:31:14 +01:00
|
|
|
from . import errors
|
2013-12-11 15:25:32 +01:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class Command(DocoptCommand):
|
2014-01-09 16:30:36 +01:00
|
|
|
base_dir = '.'
|
|
|
|
|
2014-02-26 16:45:14 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.yaml_path = os.environ.get('FIG_FILE', None)
|
|
|
|
|
2014-01-16 17:36:01 +01:00
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
super(Command, self).dispatch(*args, **kwargs)
|
|
|
|
except ConnectionError:
|
2014-02-05 00:19:50 +01:00
|
|
|
if call_silently(['which', 'docker']) != 0:
|
|
|
|
if is_mac():
|
2014-02-26 16:31:14 +01:00
|
|
|
raise errors.DockerNotFoundMac()
|
2014-02-05 00:19:50 +01:00
|
|
|
elif is_ubuntu():
|
2014-02-26 16:31:14 +01:00
|
|
|
raise errors.DockerNotFoundUbuntu()
|
2014-02-05 00:19:50 +01:00
|
|
|
else:
|
2014-02-26 16:31:14 +01:00
|
|
|
raise errors.DockerNotFoundGeneric()
|
2014-02-05 00:19:50 +01:00
|
|
|
elif call_silently(['which', 'docker-osx']) == 0:
|
2014-02-26 16:31:14 +01:00
|
|
|
raise errors.ConnectionErrorDockerOSX()
|
2014-02-04 23:42:55 +01:00
|
|
|
else:
|
2014-02-26 16:31:14 +01:00
|
|
|
raise errors.ConnectionErrorGeneric(self.client.base_url)
|
2014-01-16 17:36:01 +01:00
|
|
|
|
2014-02-26 16:45:14 +01:00
|
|
|
def perform_command(self, options, *args, **kwargs):
|
|
|
|
if options['--file'] is not None:
|
|
|
|
self.yaml_path = os.path.join(self.base_dir, options['--file'])
|
|
|
|
return super(Command, self).perform_command(options, *args, **kwargs)
|
|
|
|
|
2013-12-11 15:25:32 +01:00
|
|
|
@cached_property
|
|
|
|
def client(self):
|
2013-12-31 13:37:17 +01:00
|
|
|
return Client(docker_url())
|
2013-12-11 15:25:32 +01:00
|
|
|
|
|
|
|
@cached_property
|
2013-12-19 17:55:12 +01:00
|
|
|
def project(self):
|
2014-01-03 12:58:49 +01:00
|
|
|
try:
|
2014-02-26 16:45:14 +01:00
|
|
|
yaml_path = self.yaml_path
|
|
|
|
if yaml_path is None:
|
|
|
|
yaml_path = self.check_yaml_filename()
|
2014-01-09 16:30:36 +01:00
|
|
|
config = yaml.load(open(yaml_path))
|
2014-01-06 03:26:32 +01:00
|
|
|
except IOError as e:
|
2014-01-03 12:58:49 +01:00
|
|
|
if e.errno == errno.ENOENT:
|
2014-02-26 16:44:06 +01:00
|
|
|
raise errors.FigFileNotFound(os.path.basename(e.filename))
|
|
|
|
raise errors.UserError(six.text_type(e))
|
2014-01-03 12:58:49 +01:00
|
|
|
|
2014-02-05 01:33:29 +01:00
|
|
|
try:
|
|
|
|
return Project.from_config(self.project_name, config, self.client)
|
|
|
|
except ConfigError as e:
|
2014-02-26 16:31:14 +01:00
|
|
|
raise errors.UserError(six.text_type(e))
|
2013-12-19 16:32:24 +01:00
|
|
|
|
|
|
|
@cached_property
|
2013-12-19 17:55:12 +01:00
|
|
|
def project_name(self):
|
2013-12-19 16:32:24 +01:00
|
|
|
project = os.path.basename(os.getcwd())
|
|
|
|
project = re.sub(r'[^a-zA-Z0-9]', '', project)
|
|
|
|
if not project:
|
|
|
|
project = 'default'
|
|
|
|
return project
|
2013-12-11 15:25:32 +01:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def formatter(self):
|
|
|
|
return Formatter()
|
|
|
|
|
2014-01-28 01:43:23 +01:00
|
|
|
def check_yaml_filename(self):
|
|
|
|
if os.path.exists(os.path.join(self.base_dir, 'fig.yaml')):
|
|
|
|
|
|
|
|
log.warning("Fig just read the file 'fig.yaml' on startup, rather than 'fig.yml'")
|
|
|
|
log.warning("Please be aware that fig.yml the expected extension in most cases, and using .yaml can cause compatibility issues in future")
|
|
|
|
|
|
|
|
return os.path.join(self.base_dir, 'fig.yaml')
|
|
|
|
else:
|
2014-01-28 10:51:33 +01:00
|
|
|
return os.path.join(self.base_dir, 'fig.yml')
|