def http(self, io_handler, isolate=None): """ Prints the port(s) to access the isolates HTTP service """ try: # Prepare the targets group, peers = self.__compute_targets(isolate) except KeyError as ex: io_handler.write_line("{0}", ex) return # Prepare a count down event if group is not None: nb_peers = len(self._directory.get_peers_for_group(group)) if nb_peers == 0: io_handler.write_line("No peer found in group '{0}'", group) return event = CountdownEvent(nb_peers) else: event = CountdownEvent(len(peers)) # Prepare callback and errback succeeded = {} failed = set() def on_error(herald_svc, exception): """ Failed to send a message """ self.__on_error(herald_svc, exception, failed, event) def on_success(herald_svc, reply): """ Got a reply for a message """ # Reply content is a dictionary, extract PID step_up = reply.sender not in succeeded succeeded[reply.sender] = reply.content['http.port'] if step_up: event.step() # Send the message message = beans.Message(SUBJECT_GET_HTTP) if group is not None: self._herald.post_group(group, message, on_success, on_error) else: for uid in peers: self._herald.post(uid, message, on_success, on_error) # Wait for results (5 seconds max) event.wait(5) # Forget about the message self._herald.forget(message.uid) # Setup the headers headers = ('Name', 'UID', 'Node Name', 'Node UID', 'HTTP') # Compute the table content table = [] for uid, http_port in succeeded.items(): # Add the line to the table try: peer = self._directory.get_peer(uid) except KeyError: # Unknown peer line = ("<unknown>", uid, "<unknown>", "<unknown>", http_port) else: # Print known information line = (peer.name, uid, peer.node_name, peer.node_uid, http_port) table.append(line) if table: # Sort values table.sort() # Print the table io_handler.write_line(self._utils.make_table(headers, table)) if failed: io_handler.write_line("These isolates didn't respond:") for uid in failed: try: name = self._directory.get_peer(uid).name except KeyError: name = "<unknown>" io_handler.write_line("{0} - {1}", name, uid)
def shells(self, io_handler, isolate=None, kind=None): """ Prints the port(s) to access the isolate remote shell(s) """ try: # Prepare the targets group, peers = self.__compute_targets(isolate) except KeyError as ex: io_handler.write_line("{0}", ex) return # Prepare a count down event if group is not None: nb_peers = len(self._directory.get_peers_for_group(group)) if nb_peers == 0: io_handler.write_line("No peer found in group '{0}'", group) return event = CountdownEvent(nb_peers) else: event = CountdownEvent(len(peers)) # Prepare callback and errback succeeded = {} failed = set() def on_error(herald_svc, exception): """ Failed to send a message """ self.__on_error(herald_svc, exception, failed, event) def on_success(herald_svc, reply): """ Got a reply for a message """ try: # Already known peer (multiple answers) succeeded[reply.sender].update(reply.content) except KeyError: # A new peer answered succeeded[reply.sender] = reply.content.copy() event.step() # Send the message message = beans.Message(SUBJECT_GET_SHELLS) if group is not None: self._herald.post_group(group, message, on_success, on_error) else: for uid in peers: self._herald.post(uid, message, on_success, on_error) # Wait for results (5 seconds max) event.wait(5) # Forget about the message self._herald.forget(message.uid) # Compute the shell names shell_names = set() for uid, shells in succeeded.items(): shell_names.update(shell_name for shell_name in shells) # Sort shell names, using a list shell_names = list(shell_names) shell_names.sort() if kind: # Filter on shell names given if kind in shell_names: shell_names = [kind] else: io_handler.write_line("No isolate with shell: {0}", kind) return # Compute the table content table = [] for uid, shells in succeeded.items(): # First columns: Name, UID, Node Name, Node UID try: # Known peer peer = self._directory.get_peer(uid) line = [peer.name, uid, peer.node_name, peer.node_uid] except KeyError: # Unknown peer line = ["<unknown>", uid, "<unknown>", "<unknown>"] shell_ports = {} # Find the shell ports for shell_name in shell_names: try: shell_ports[shell_name] = shells[shell_name] except KeyError: pass # Make the lines for shell_name in shell_names: line.append(shell_ports.get(shell_name, 'n/a')) # Add the line to the table table.append(line) if table: # Setup the headers headers = ['Name', 'UID', 'Node Name', 'Node UID'] + shell_names # Sort values table.sort() # Print the table io_handler.write_line(self._utils.make_table(headers, table)) if failed: io_handler.write_line("These isolates didn't respond:") for uid in failed: io_handler.write_line("{0} - {1}", self._directory.get_peer(uid).name, uid)