Refactor service to add a container object
This commit is contained in:
parent
5e1e4a71e0
commit
a5fc880d10
6 changed files with 169 additions and 57 deletions
|
|
@ -78,7 +78,7 @@ class TopLevelCommand(Command):
|
|||
Usage: ps
|
||||
"""
|
||||
for container in self._get_containers(all=False):
|
||||
print get_container_name(container)
|
||||
print container.name
|
||||
|
||||
def run(self, options):
|
||||
"""
|
||||
|
|
@ -126,4 +126,4 @@ class TopLevelCommand(Command):
|
|||
LogPrinter(client=self.client).attach(containers)
|
||||
|
||||
def _get_containers(self, all):
|
||||
return [c for s in self.service_collection for c in s.get_containers(all=all)]
|
||||
return [c for s in self.service_collection for c in s.containers(all=all)]
|
||||
|
|
|
|||
86
plum/container.py
Normal file
86
plum/container.py
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
|
||||
|
||||
class Container(object):
|
||||
"""
|
||||
Represents a Docker container, constructed from the output of
|
||||
GET /containers/:id:/json.
|
||||
"""
|
||||
def __init__(self, client, dictionary, has_been_inspected=False):
|
||||
self.client = client
|
||||
self.dictionary = dictionary
|
||||
self.has_been_inspected = has_been_inspected
|
||||
|
||||
@classmethod
|
||||
def from_ps(cls, client, dictionary, **kwargs):
|
||||
"""
|
||||
Construct a container object from the output of GET /containers/json.
|
||||
"""
|
||||
new_dictionary = {
|
||||
'ID': dictionary['Id'],
|
||||
'Image': dictionary['Image'],
|
||||
}
|
||||
for name in dictionary.get('Names', []):
|
||||
if len(name.split('/')) == 2:
|
||||
new_dictionary['Name'] = name
|
||||
return cls(client, new_dictionary, **kwargs)
|
||||
|
||||
@classmethod
|
||||
def from_id(cls, client, id):
|
||||
return cls(client, client.inspect_container(id))
|
||||
|
||||
@classmethod
|
||||
def create(cls, client, **options):
|
||||
response = client.create_container(**options)
|
||||
return cls.from_id(client, response['Id'])
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self.dictionary['ID']
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self.dictionary['Name']
|
||||
|
||||
@property
|
||||
def environment(self):
|
||||
self.inspect_if_not_inspected()
|
||||
out = {}
|
||||
for var in self.dictionary.get('Config', {}).get('Env', []):
|
||||
k, v = var.split('=', 1)
|
||||
out[k] = v
|
||||
return out
|
||||
|
||||
def start(self, **options):
|
||||
return self.client.start(self.id, **options)
|
||||
|
||||
def stop(self):
|
||||
return self.client.stop(self.id)
|
||||
|
||||
def kill(self):
|
||||
return self.client.kill(self.id)
|
||||
|
||||
def remove(self):
|
||||
return self.client.remove_container(self.id)
|
||||
|
||||
def inspect_if_not_inspected(self):
|
||||
if not self.has_been_inspected:
|
||||
self.inspect()
|
||||
|
||||
def wait(self):
|
||||
return self.client.wait(self.id)
|
||||
|
||||
def logs(self, *args, **kwargs):
|
||||
return self.client.logs(self.id, *args, **kwargs)
|
||||
|
||||
def inspect(self):
|
||||
self.dictionary = self.client.inspect_container(self.id)
|
||||
return self.dictionary
|
||||
|
||||
def links(self):
|
||||
links = []
|
||||
for container in self.client.containers():
|
||||
for name in container['Names']:
|
||||
bits = name.split('/')
|
||||
if len(bits) > 2 and bits[1] == self.name[1:]:
|
||||
links.append(bits[2])
|
||||
return links
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
from docker.client import APIError
|
||||
import logging
|
||||
import re
|
||||
from .container import Container
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -21,28 +22,26 @@ class Service(object):
|
|||
self.links = links or []
|
||||
self.options = options
|
||||
|
||||
@property
|
||||
def containers(self):
|
||||
return list(self.get_containers(all=True))
|
||||
|
||||
def get_containers(self, all):
|
||||
def containers(self, all=False):
|
||||
l = []
|
||||
for container in self.client.containers(all=all):
|
||||
name = get_container_name(container)
|
||||
if is_valid_name(name) and parse_name(name)[0] == self.name:
|
||||
yield container
|
||||
l.append(Container.from_ps(self.client, container))
|
||||
return l
|
||||
|
||||
def start(self):
|
||||
if len(self.containers) == 0:
|
||||
if len(self.containers()) == 0:
|
||||
return self.start_container()
|
||||
|
||||
def stop(self):
|
||||
self.scale(0)
|
||||
|
||||
def scale(self, num):
|
||||
while len(self.containers) < num:
|
||||
while len(self.containers()) < num:
|
||||
self.start_container()
|
||||
|
||||
while len(self.containers) > num:
|
||||
while len(self.containers()) > num:
|
||||
self.stop_container()
|
||||
|
||||
def create_container(self, **override_options):
|
||||
|
|
@ -52,12 +51,12 @@ class Service(object):
|
|||
"""
|
||||
container_options = self._get_container_options(override_options)
|
||||
try:
|
||||
return self.client.create_container(**container_options)
|
||||
return Container.create(self.client, **container_options)
|
||||
except APIError, e:
|
||||
if e.response.status_code == 404 and e.explanation and 'No such image' in e.explanation:
|
||||
log.info('Pulling image %s...' % container_options['image'])
|
||||
self.client.pull(container_options['image'])
|
||||
return self.client.create_container(**container_options)
|
||||
return Container.create(self.client, **container_options)
|
||||
raise
|
||||
|
||||
def start_container(self, container=None, **override_options):
|
||||
|
|
@ -71,39 +70,32 @@ class Service(object):
|
|||
port_bindings[int(internal_port)] = int(external_port)
|
||||
else:
|
||||
port_bindings[int(port)] = None
|
||||
log.info("Starting %s..." % container['Id'])
|
||||
self.client.start(
|
||||
container['Id'],
|
||||
log.info("Starting %s..." % container.name)
|
||||
container.start(
|
||||
links=self._get_links(),
|
||||
port_bindings=port_bindings,
|
||||
)
|
||||
return container
|
||||
|
||||
def stop_container(self):
|
||||
container = self.containers[-1]
|
||||
log.info("Stopping and removing %s..." % get_container_name(container))
|
||||
self.client.kill(container)
|
||||
self.client.remove_container(container)
|
||||
container = self.containers()[-1]
|
||||
log.info("Stopping and removing %s..." % container.name)
|
||||
container.kill()
|
||||
container.remove()
|
||||
|
||||
def next_container_number(self):
|
||||
numbers = [parse_name(get_container_name(c))[1] for c in self.containers]
|
||||
numbers = [parse_name(c.name)[1] for c in self.containers(all=True)]
|
||||
|
||||
if len(numbers) == 0:
|
||||
return 1
|
||||
else:
|
||||
return max(numbers) + 1
|
||||
|
||||
def get_names(self):
|
||||
return [get_container_name(c) for c in self.containers]
|
||||
|
||||
def inspect(self):
|
||||
return [self.client.inspect_container(c['Id']) for c in self.containers]
|
||||
|
||||
def _get_links(self):
|
||||
links = {}
|
||||
for service in self.links:
|
||||
for name in service.get_names():
|
||||
links[name] = name
|
||||
for container in service.containers():
|
||||
links[container.name[1:]] = container.name[1:]
|
||||
return links
|
||||
|
||||
def _get_container_options(self, override_options):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue