Commit f2bc8b80 authored by Swann Perarnau's avatar Swann Perarnau
Browse files

[refactor] Only track container inside the CM

The daemon code was maintaining its own container tracker using pids,
instead of using the one in the container manager. This patch removes
this additional tracking, and let the daemon side deal with an actual
namedtuple.
parent 78f63cd4
...@@ -35,7 +35,7 @@ class ContainerManager(object): ...@@ -35,7 +35,7 @@ class ContainerManager(object):
manifest = ImageManifest() manifest = ImageManifest()
if not manifest.load(manifestfile): if not manifest.load(manifestfile):
logger.error("Manifest is invalid") logger.error("Manifest is invalid")
return -1 return None
# ask the resource manager for resources # ask the resource manager for resources
req = resources(int(manifest.app.isolators.container.cpus.value), req = resources(int(manifest.app.isolators.container.cpus.value),
...@@ -71,7 +71,7 @@ class ContainerManager(object): ...@@ -71,7 +71,7 @@ class ContainerManager(object):
c = Container(container_name, manifest, process) c = Container(container_name, manifest, process)
self.pids[process.pid] = c self.pids[process.pid] = c
self.containers[container_name] = c self.containers[container_name] = c
return process return c
def delete(self, uuid): def delete(self, uuid):
"""Delete a container and kill all related processes.""" """Delete a container and kill all related processes."""
......
...@@ -76,7 +76,6 @@ class Application(object): ...@@ -76,7 +76,6 @@ class Application(object):
class Daemon(object): class Daemon(object):
def __init__(self): def __init__(self):
self.applications = {} self.applications = {}
self.containerpids = {}
self.buf = '' self.buf = ''
self.target = 1.0 self.target = 1.0
...@@ -119,24 +118,27 @@ class Daemon(object): ...@@ -119,24 +118,27 @@ class Daemon(object):
self.target = float(msg['limit']) self.target = float(msg['limit'])
logger.info("new target measure: %g", self.target) logger.info("new target measure: %g", self.target)
elif command == 'run': elif command == 'run':
container_uuid = msg['uuid']
if container_uuid in self.container_manager.containers:
logger.info("container already created: %r",
container_uuid)
return
logger.info("new container required: %r", msg) logger.info("new container required: %r", msg)
process = self.container_manager.create(msg) container = self.container_manager.create(msg)
self.containerpids[process.pid] = msg['uuid']
# TODO: obviously we need to send more info than that # TODO: obviously we need to send more info than that
update = {'type': 'container', update = {'type': 'container',
'event': 'start', 'event': 'start',
'uuid': msg['uuid'], 'uuid': container_uuid,
'errno': 0, 'errno': 0 if container else -1,
'pid': process.pid, 'pid': container.process.pid,
} }
self.upstream_pub.send_json(update) self.upstream_pub.send_json(update)
# setup io callbacks # setup io callbacks
process.stdout.read_until_close(partial(self.do_children_io, outcb = partial(self.do_children_io, container_uuid, 'stdout')
msg['uuid'], errcb = partial(self.do_children_io, container_uuid, 'stderr')
'stdout')) container.process.stdout.read_until_close(outcb)
process.stderr.read_until_close(partial(self.do_children_io, container.process.stderr.read_until_close(errcb)
msg['uuid'],
'stderr'))
elif command == 'kill': elif command == 'kill':
logger.info("asked to kill container: %r", msg) logger.info("asked to kill container: %r", msg)
response = self.container_manager.kill(msg['uuid']) response = self.container_manager.kill(msg['uuid'])
...@@ -211,15 +213,15 @@ class Daemon(object): ...@@ -211,15 +213,15 @@ class Daemon(object):
logger.info("child update %d: %r", pid, status) logger.info("child update %d: %r", pid, status)
# check if its a pid we care about # check if its a pid we care about
if pid in self.containerpids: if pid in self.container_manager.pids:
# check if this is an exit # check if this is an exit
if os.WIFEXITED(status) or os.WIFSIGNALED(status): if os.WIFEXITED(status) or os.WIFSIGNALED(status):
uuid = self.containerpids[pid] container = self.container_manager.pids[pid]
self.container_manager.delete(uuid) self.container_manager.delete(container.uuid)
msg = {'type': 'container', msg = {'type': 'container',
'event': 'exit', 'event': 'exit',
'status': status, 'status': status,
'uuid': uuid, 'uuid': container.uuid,
} }
self.upstream_pub.send_json(msg) self.upstream_pub.send_json(msg)
else: else:
......
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