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
|
|
|
|
|
2013-12-19 17:55:12 +01:00
|
|
|
from ..project import Project
|
2013-12-11 15:25:32 +01:00
|
|
|
from .docopt_command import DocoptCommand
|
|
|
|
from .formatter import Formatter
|
2013-12-31 13:37:17 +01:00
|
|
|
from .utils import cached_property, docker_url
|
2014-01-16 17:36:01 +01:00
|
|
|
from .errors import UserError
|
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-01-16 17:36:01 +01:00
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
super(Command, self).dispatch(*args, **kwargs)
|
|
|
|
except ConnectionError:
|
|
|
|
raise UserError("""
|
|
|
|
Couldn't connect to Docker daemon at %s - is it running?
|
|
|
|
|
|
|
|
If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
|
|
|
|
""" % self.client.base_url)
|
|
|
|
|
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-01-09 16:30:36 +01:00
|
|
|
yaml_path = os.path.join(self.base_dir, 'fig.yml')
|
|
|
|
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-01-09 16:30:36 +01:00
|
|
|
log.error("Can't find %s. Are you in the right directory?", os.path.basename(e.filename))
|
2014-01-03 12:58:49 +01:00
|
|
|
else:
|
|
|
|
log.error(e)
|
|
|
|
|
|
|
|
exit(1)
|
|
|
|
|
2013-12-19 17:55:12 +01:00
|
|
|
return Project.from_config(self.project_name, config, self.client)
|
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()
|
|
|
|
|