|
@@ -1,11 +1,13 @@
|
|
|
from __future__ import unicode_literals
|
|
|
from __future__ import absolute_import
|
|
|
from collections import namedtuple
|
|
|
-from docker.errors import APIError
|
|
|
import logging
|
|
|
import re
|
|
|
import os
|
|
|
import sys
|
|
|
+
|
|
|
+from docker.errors import APIError
|
|
|
+
|
|
|
from .container import Container
|
|
|
from .progress_stream import stream_output, StreamOutputError
|
|
|
|
|
@@ -43,6 +45,9 @@ class ConfigError(ValueError):
|
|
|
VolumeSpec = namedtuple('VolumeSpec', 'external internal mode')
|
|
|
|
|
|
|
|
|
+ServiceName = namedtuple('ServiceName', 'project service number')
|
|
|
+
|
|
|
+
|
|
|
class Service(object):
|
|
|
def __init__(self, name, client=None, project='default', links=None, volumes_from=None, **options):
|
|
|
if not re.match('^%s+$' % VALID_NAME_CHARS, name):
|
|
@@ -185,8 +190,8 @@ class Service(object):
|
|
|
"""
|
|
|
containers = self.containers(stopped=True)
|
|
|
|
|
|
- if len(containers) == 0:
|
|
|
- log.info("Creating %s..." % self.next_container_name())
|
|
|
+ if not containers:
|
|
|
+ log.info("Creating %s..." % self._next_container_name(containers))
|
|
|
container = self.create_container(**override_options)
|
|
|
self.start_container(container)
|
|
|
return [(None, container)]
|
|
@@ -264,7 +269,7 @@ class Service(object):
|
|
|
containers = self.containers(stopped=True)
|
|
|
|
|
|
if not containers:
|
|
|
- log.info("Creating %s..." % self.next_container_name())
|
|
|
+ log.info("Creating %s..." % self._next_container_name(containers))
|
|
|
new_container = self.create_container()
|
|
|
return [self.start_container(new_container)]
|
|
|
else:
|
|
@@ -273,19 +278,15 @@ class Service(object):
|
|
|
def get_linked_names(self):
|
|
|
return [s.name for (s, _) in self.links]
|
|
|
|
|
|
- def next_container_name(self, one_off=False):
|
|
|
+ def _next_container_name(self, all_containers, one_off=False):
|
|
|
bits = [self.project, self.name]
|
|
|
if one_off:
|
|
|
bits.append('run')
|
|
|
- return '_'.join(bits + [str(self.next_container_number(one_off=one_off))])
|
|
|
+ return '_'.join(bits + [str(self._next_container_number(all_containers))])
|
|
|
|
|
|
- def next_container_number(self, one_off=False):
|
|
|
- numbers = [parse_name(c.name)[2] for c in self.containers(stopped=True, one_off=one_off)]
|
|
|
-
|
|
|
- if len(numbers) == 0:
|
|
|
- return 1
|
|
|
- else:
|
|
|
- return max(numbers) + 1
|
|
|
+ def _next_container_number(self, all_containers):
|
|
|
+ numbers = [parse_name(c.name).number for c in all_containers]
|
|
|
+ return 1 if not numbers else max(numbers) + 1
|
|
|
|
|
|
def _get_links(self, link_to_self):
|
|
|
links = []
|
|
@@ -319,7 +320,9 @@ class Service(object):
|
|
|
container_options = dict((k, self.options[k]) for k in DOCKER_CONFIG_KEYS if k in self.options)
|
|
|
container_options.update(override_options)
|
|
|
|
|
|
- container_options['name'] = self.next_container_name(one_off)
|
|
|
+ container_options['name'] = self._next_container_name(
|
|
|
+ self.containers(stopped=True, one_off=one_off),
|
|
|
+ one_off)
|
|
|
|
|
|
# If a qualified hostname was given, split it into an
|
|
|
# unqualified hostname and a domainname unless domainname
|
|
@@ -424,10 +427,10 @@ def is_valid_name(name, one_off=False):
|
|
|
return match.group(3) is None
|
|
|
|
|
|
|
|
|
-def parse_name(name, one_off=False):
|
|
|
+def parse_name(name):
|
|
|
match = NAME_RE.match(name)
|
|
|
(project, service_name, _, suffix) = match.groups()
|
|
|
- return (project, service_name, int(suffix))
|
|
|
+ return ServiceName(project, service_name, int(suffix))
|
|
|
|
|
|
|
|
|
def get_container_name(container):
|