示例#1
0
    while True:
        event = await subscriber.get()
        if event is None:
            break

        for key, value in event.items():
            print(key,':', value)

        # Demonstrate simple event-driven container mgmt.
        if event['Actor']['ID'] == container._id:
            if event['Action'] == 'start':
                await container.stop()
                print("=> killed {}".format(container._id[:12]))
            elif event['Action'] == 'stop':
                await container.delete(force=True)
                print("=> deleted {}".format(container._id[:12]))
            elif event['Action'] == 'destroy':
                print('=> done with this container!')
                break

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    docker = Docker()
    try:
        # do our stuffs.
        loop.run_until_complete(demo(docker))
    finally:
        loop.run_until_complete(docker.close())
        loop.close()
示例#2
0
    print(f"=> created and started container {container._id[:12]}")

    while True:
        event = await subscriber.get()
        if event is None:
            break
        print(f"event: {event!r}")

        # Demonstrate simple event-driven container mgmt.
        if event['Actor']['ID'] == container._id:
            if event['Action'] == 'start':
                await container.stop()
                print(f"=> killed {container._id[:12]}")
            elif event['Action'] == 'stop':
                await container.delete(force=True)
                print(f"=> deleted {container._id[:12]}")
            elif event['Action'] == 'destroy':
                print('=> done with this container!')
                break


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    docker = Docker()
    try:
        # do our stuffs.
        loop.run_until_complete(demo(docker))
    finally:
        loop.run_until_complete(docker.close())
        loop.close()
示例#3
0
def main(args):
    context = zmq.Context.instance()
    mypid = os.getpid()

    ipc_base_path = Path('/tmp/backend.ai/ipc')
    signal_path = 'ipc://' + str(ipc_base_path / f'stat-start-{mypid}.sock')
    signal_sock = context.socket(zmq.PAIR)
    signal_sock.bind(signal_path)

    stats_sock = context.socket(zmq.PUSH)
    stats_sock.setsockopt(zmq.LINGER, 2000)
    stats_sock.connect(args.sockaddr)
    send_stat = functools.partial(stats_sock.send_serialized,
                                  serialize=lambda v: [msgpack.packb(v)])
    stat = ContainerStat()

    if args.type == 'cgroup':
        with closing(stats_sock), join_cgroup_and_namespace(
                args.cid, stat, send_stat, signal_sock):
            # Agent notification is done inside join_cgroup_and_namespace
            while True:
                new_stat = _collect_stats_sysfs(args.cid)
                stat.update(new_stat)
                msg = {
                    'cid': args.cid,
                    'data': asdict(stat),
                }
                if is_cgroup_running(args.cid) and new_stat is not None:
                    msg['status'] = 'running'
                    send_stat(msg)
                else:
                    msg['status'] = 'terminated'
                    send_stat(msg)
                    break
                time.sleep(1.0)
    elif args.type == 'api':
        loop = asyncio.get_event_loop()
        docker = Docker()
        with closing(stats_sock), closing(loop):
            container = DockerContainer(docker, id=args.cid)
            # Notify the agent to start the container.
            signal_sock.send_multipart([b''])
            # Wait for the container to be actually started.
            signal_sock.recv_multipart()
            while True:
                new_stat = loop.run_until_complete(
                    _collect_stats_api(container))
                stat.update(new_stat)
                msg = {
                    'cid': args.cid,
                    'data': asdict(stat),
                }
                if new_stat is not None:
                    msg['status'] = 'running'
                    send_stat(msg)
                else:
                    msg['status'] = 'terminated'
                    send_stat(msg)
                    break
                time.sleep(1.0)
            loop.run_until_complete(docker.close())

    signal_sock.close()
    sys.exit(0)