Commit 2f470afb authored by Swann Perarnau's avatar Swann Perarnau

[feature] Add command to list containers

This patch adds a very simple command to list the containers currently
known by the NRM. There's no history or state tracking on the NRM, so
the code is pretty simple.

We expect that some of the container tracking doesn't need to be sent
for such a command, so the listing also filters some of the fields.

This patch also adds an 'event' field to container messages, as it would
probably be needed further for other kind of operations.
parent 848a9755
......@@ -78,6 +78,26 @@ class CommandLineInterface(object):
self.logger.info("container response: %r", msg)
break
def do_list(self, argv):
"""Connect to the NRM and ask to list the containers present on the
system.
The NRM should respond to us on the pub socket with one message listing
all containers."""
command = {'command': 'list',
}
while(True):
self.upstream_pub_socket.send_json(command)
msg = self.upstream_sub_socket.recv_json()
self.logger.info("new message: %r", msg)
# ignore other messages
if isinstance(msg, dict) and msg.get('type') == 'container':
if msg['event'] == 'list':
self.logger.info("list response: %r", msg)
break
def do_setpower(self, argv):
""" Connect to the NRM and ask to change the power limit.
......@@ -117,6 +137,10 @@ class CommandLineInterface(object):
nargs=argparse.REMAINDER)
parser_run.set_defaults(func=self.do_run)
# list containers
parser_list = subparsers.add_parser("list")
parser_list.set_defaults(func=self.do_list)
# setpowerlimit
parser_setpower = subparsers.add_parser("setpower")
parser_setpower.add_argument("-f", "--follow",
......@@ -126,6 +150,7 @@ class CommandLineInterface(object):
help="set new power limit",
type=float)
parser_setpower.set_defaults(func=self.do_setpower)
args = parser.parse_args()
if args.verbose:
self.logger.setLevel(logging.DEBUG)
......
......@@ -92,3 +92,9 @@ class ContainerManager(object):
c = self.containers[uuid]
del self.containers[uuid]
del self.pids[c.pid]
def list(self):
"""List the containers in the system."""
fields = ['uuid', 'pid']
ret = [c._asdict() for c in self.containers.values()]
return [{k: d[k] for k in fields} for d in ret]
......@@ -134,6 +134,14 @@ class Daemon(object):
'errno': pid,
}
self.upstream_pub.send_json(update)
elif command == 'list':
self.logger.info("asked for container list: %r", msg)
response = self.container_manager.list()
update = {'type': 'container',
'event': 'list',
'payload': response,
}
self.upstream_pub.send_json(update)
else:
self.logger.error("invalid command: %r", command)
......
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