Commit 0519387e authored by André Anjos's avatar André Anjos 💬

[async] More utility functions

parent ae451cdd
......@@ -72,7 +72,7 @@ class Host(object):
kwargs['base_url'] = "http://%s:%s" % (host, port)
self.kwargs = kwargs
self.beat_environments = {}
self.environments = {}
def setup(self):
......@@ -80,7 +80,7 @@ class Host(object):
if self.use_machine is not None:
# requires a bootstrapping machine to run
if not self.machine.status(self.use_machine):
logger.info("Starting docker machine `%s'...", self.use_machine)
logger.debug("[docker-machine] start %s", self.use_machine)
self.machine.start(self.use_machine)
self.started_machine = True
......@@ -90,7 +90,28 @@ class Host(object):
else:
self.client = docker.Client(**self.kwargs)
self.beat_environments = self._discover_environments(raise_on_errors=True)
self.environments = self._discover_environments(raise_on_errors=True)
def __contains__(self, key):
return key in self.environments
def __str__(self):
if self.machine is not None:
return 'virtualbox@%s' % \
self.machine.config(machine=self.use_machine)['base_url']
else:
return self.kwargs['base_url']
def env2docker(self, key):
'''Returns a nice docker image name given a BEAT environment key'''
attrs = self.environments[key]
if attrs['tag'] is not None: return attrs['tag']
return attrs['short_id']
def teardown(self):
......@@ -98,13 +119,22 @@ class Host(object):
for container in self.containers: self.rm(container)
if self.use_machine is not None and self.started_machine:
logger.info("Stopping docker machine `%s'...", self.use_machine)
logger.debug("[docker-machine] start %s", self.use_machine)
self.machine.stop(self.use_machine)
self.started_machine = False
self.client = None
def __enter__(self):
self.setup()
return self
def __exit__(self, *exc):
self.teardown()
def _discover_environments(self, raise_on_errors=True):
'''Returns a dictionary containing information about docker environments
......@@ -199,19 +229,17 @@ class Host(object):
"""
if image in self.beat_environments: #replace by a real image name
attrs = self.beat_environments[image]
if attrs['tag'] is not None: image = attrs['tag']
else: image = attrs['short_id']
if image in self: #replace by a real image name
image = self.env2docker(image)
logger.info("[docker] create_container %s %s", image, ' '.join(command))
logger.debug("[docker] create_container %s %s", image, ' '.join(command))
container = self.client.create_container(image=image, command=command,
**args)
self.containers.append(container)
if tmp_archive is not None:
# Place the tarball into the container
logger.info("[docker] archive -> %s@/tmp", container['Id'][:12])
logger.debug("[docker] archive -> %s@/tmp", container['Id'][:12])
self.client.put_archive(container, '/tmp', tmp_archive)
return container
......@@ -220,7 +248,7 @@ class Host(object):
def start(self, container):
'''Starts a given container'''
logger.info("[docker] start %s", container['Id'][:12])
logger.debug("[docker] start %s", container['Id'][:12])
self.client.start(container)
......@@ -230,7 +258,7 @@ class Host(object):
'''
try:
logger.info("[docker] inspect %s", container['Id'][:12])
logger.debug("[docker] inspect %s", container['Id'][:12])
z = self.client.inspect_container(container)
return z['State']['Status']
except Exception as e:
......@@ -246,10 +274,9 @@ class Host(object):
logger.warn("Killing container `%s' which is on state `%s'",
container['Id'], status)
self.client.kill(container)
else:
logger.info("[docker] rm %s", container['Id'][:12])
self.client.remove_container(container)
logger.debug("[docker] rm %s", container['Id'][:12])
self.client.remove_container(container)
self.containers.remove(container)
......@@ -384,6 +411,11 @@ class Popen:
self.rm()
@property
def pid(self):
return self.container['Id'][:12]
def wait(self, timeout=None):
'''Reads stdout and stderr until the underlying processes finishes
......
......@@ -255,8 +255,9 @@ class AsyncTest(unittest.TestCase):
percent = stats['cpu']['percent']
assert percent < (1.05*max_cpu_percent), "%.2f%% is more than 5%% off the expected ceiling at %.2f%%!" % (percent, max_cpu_percent)
# make sure nothing is there anymore
self.assertEqual(p.wait(), 137)
# make sure nothing is there anymore
p.kill()
self.assertEqual(p.wait(), 137)
assert not self.host.containers #all containers are gone
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment