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

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