class DockerEvents: def __init__(self, docker): self.running = False self.docker = docker self.channel = Channel() def listen(self): return self.channel.listen() def saferun(self): if self.running: return self.running = True asyncio.async(self.run()) @asyncio.coroutine def run(self): self.running = True containers = self.docker.containers response = yield from aiohttp.request( 'GET', self.docker._endpoint('events'), connector=self.docker.connector, ) while True: chunk = yield from response.content.readany() # XXX: WTF. WTF WTF WTF. # WHY AM I NOT GETTING A RETURN ON .READLINE()?! WHY NO NEWLINE # https://github.com/dotcloud/docker/pull/4276 ADDED THEM if chunk == b'': break data = json.loads(chunk.decode('utf-8')) if 'time' in data: data['time'] = dt.datetime.fromtimestamp(data['time']) if 'id' in data and data['status'] in [ "start", "create", ]: data['container'] = yield from containers.get(data['id']) asyncio.async(self.channel.put(data)) response.close() self.running = False
class DockerLog: def __init__(self, docker, container): self.docker = docker self.channel = Channel() self.container = container self.running = False def listen(self): return self.channel.listen() def saferun(self): if self.running: return self.running = True asyncio.async(self.run()) @asyncio.coroutine def run(self): self.running = True containers = self.docker.containers url = self.docker._endpoint( 'containers/{id}/logs'.format(id=self.container._id), follow=True, stdout=True, stderr=True, ) response = yield from aiohttp.request( 'GET', url, connector=self.docker.connector) while True: msg = yield from response.content.readline() asyncio.async(self.channel.put(msg)) if msg == b'': break self.running = False