def do_cluster_status(args): state = load_state() node_controller = get_node_controller(state, args) node_command_generator = SimpleNodeCommandGenerator() vnm = ValidatorNetworkManager( node_controller=node_controller, node_command_generator=node_command_generator) if len(args.node_names) > 0: node_names = args.node_names else: node_names = vnm.get_node_names() # Check expected status of nodes vs what is returned from vnm print("NodeName Expected Current") nodes = state["Nodes"] for node_name in nodes: if node_name not in node_names and \ (nodes[node_name]["Status"] == "Running" or nodes[node_name]["Status"] == "No Response"): print("{} {} {}".format(node_name, nodes[node_name]["Status"], "Not Running")) else: status = vnm.status(node_name) if status == "UNKNOWN" and \ nodes[node_name]["Status"] == "Stopped": print("{} {} {}".format(node_name, nodes[node_name]["Status"], status)) else: print("{} {} {}".format(node_name, nodes[node_name]["Status"], status))
def do_cluster_stop(args): state = load_state() node_controller = get_node_controller(state, args) node_command_generator = SimpleNodeCommandGenerator() vnm = ValidatorNetworkManager( node_controller=node_controller, node_command_generator=node_command_generator) if len(args.node_names) > 0: node_names = args.node_names else: node_names = vnm.get_node_names() nodes = state["Nodes"] for node_name in node_names: if node_name not in nodes: raise CliException("{} is not a known node name".format(node_name)) if nodes[node_name]['Status'] == 'Stopped': raise CliException('{} already stopped'.format(node_name)) print("Stopping: {}".format(node_name)) node_command_generator.stop(node_name) # Update status of Nodes if node_name in nodes: nodes[node_name]["Status"] = "Stopped" else: nodes[node_name] = {"Status": "Unknown"} if len(args.node_names) == 0 and len(node_names) == 0: for node_name in nodes: nodes[node_name]["Status"] = "Unknown" # If none of the nodes are running set overall State to Stopped state["DesiredState"] = "Stopped" for node in nodes: if nodes[node]["Status"] == "Running": state["DesiredState"] = "Running" # Update state of nodes state["Nodes"] = nodes save_state(state) vnm.update() # Wait up to 16 seconds for our targeted nodes to gracefully shut down def find_still_up(targeted_nodes): return set(vnm.get_node_names()).intersection(set(targeted_nodes)) timeout = 16 mark = time.time() while len(find_still_up(node_names)) > 0: if time.time() - mark > timeout: break time.sleep(1) # Force kill any targeted nodes that are still up for node_name in find_still_up(node_names): print("Node name still up: killling {}".format(node_name)) node_controller.kill(node_name)
def do_cluster_stats(args): # pylint: disable=redefined-variable-type file_name = \ os.path.join(os.path.expanduser("~"), '.sawtooth', 'cluster', "state.yaml") # Get current expected state if os.path.isfile(file_name): with open(file_name, 'r') as state_file: state = yaml.load(state_file) else: raise CliException("Missing state file") node_controller = get_node_controller(state, args) node_command_generator = SimpleNodeCommandGenerator() vnm = ValidatorNetworkManager( node_controller=node_controller, node_command_generator=node_command_generator) nodes = state["Nodes"] for node_name in nodes: try: node_ip = vnm.get_ip(node_name) node_name_stats = node_name break except ManagementError as e: raise CliException(str(e)) node_url = "http://" + node_ip.strip(' \t\n\r') + ":" + \ nodes[node_name_stats]["HttpPort"] run_stats(node_url)
def do_cluster_stop(args): # pylint: disable=redefined-variable-type file_name = \ os.path.join(os.path.expanduser("~"), '.sawtooth', 'cluster', "state.yaml") # Get current state of Nodes if os.path.isfile(file_name): with open(file_name, 'r') as state_file: state = yaml.load(state_file) else: raise CliException("Missing state file") if state['Manage'] is None or state['Manage'] == 'docker': node_controller = DockerNodeController() elif state['Manage'] == 'daemon': node_controller = DaemonNodeController() else: raise CliException('invalid management' ' type: {}'.format(state['Manage'])) node_command_generator = SimpleNodeCommandGenerator() vnm = ValidatorNetworkManager( node_controller=node_controller, node_command_generator=node_command_generator) if len(args.node_names) > 0: node_names = args.node_names else: node_names = vnm.get_node_names() nodes = state["Nodes"] for node_name in node_names: print "Stopping: {}".format(node_name) node_command_generator.stop(node_name) # Update status of Nodes if node_name in nodes: nodes[node_name]["Status"] = "Stopped" else: nodes[node_name] = {"Status": "Unknown"} if len(args.node_names) == 0 and len(node_names) == 0: for node_name in nodes: nodes[node_name]["Status"] = "Unknown" # If none of the nodes are running set overall State to Stopped state["DesiredState"] = "Stopped" for node in nodes: if nodes[node]["Status"] == "Running": state["DesiredState"] = "Running" # Update state of nodes state["Nodes"] = nodes with open(file_name, 'w') as state_file: yaml.dump(state, state_file, default_flow_style=False) vnm.update()
def do_cluster_extend(args): state = load_state() node_controller = get_node_controller(state, args) node_command_generator = SimpleNodeCommandGenerator() vnm = ValidatorNetworkManager( node_controller=node_controller, node_command_generator=node_command_generator) existing_nodes = state["Nodes"] desired_stated = state["DesiredState"] if desired_stated != "Running": raise CliException( "You must have a running network.\n" + "Use the cluster start command to start a validator network.") print("Extending network by {} nodes.".format(args.count)) index_offset = len(existing_nodes) for i in xrange(0, args.count): j = i + index_offset node_name = "validator-{:0>3}".format(j) if node_name in existing_nodes and vnm.is_running(node_name): print("Already running: {}".format(node_name)) continue # genesis is true for the first node genesis = (j == 0) gossip_port = 5500 + j http_port = 8800 + j node_args = NodeArguments(node_name, http_port=http_port, gossip_port=gossip_port, genesis=genesis) node_command_generator.start(node_args) state["Nodes"][node_name] = { "Status": "Running", "Index": i, "HttpPort": str(http_port), "GossipPort": str(gossip_port) } save_state(state) try: vnm.update() except ManagementError as e: raise CliException(str(e))
def do_cluster_status(args): # pylint: disable=redefined-variable-type file_name = \ os.path.join(os.path.expanduser("~"), '.sawtooth', 'cluster', "state.yaml") # Get current expected state if os.path.isfile(file_name): with open(file_name, 'r') as state_file: state = yaml.load(state_file) else: raise CliException("Missing state file") if state['Manage'] is None or state['Manage'] == 'docker': node_controller = DockerNodeController() elif state['Manage'] == 'daemon': node_controller = DaemonNodeController() else: raise CliException('invalid management' ' type: {}'.format(state['Manage'])) node_command_generator = SimpleNodeCommandGenerator() vnm = ValidatorNetworkManager( node_controller=node_controller, node_command_generator=node_command_generator) if len(args.node_names) > 0: node_names = args.node_names else: node_names = vnm.get_node_names() # Check expected status of nodes vs what is returned from vnm print "NodeName Expected Current" nodes = state["Nodes"] for node_name in nodes: if node_name not in node_names and \ (nodes[node_name]["Status"] == "Running" or nodes[node_name]["Status"] == "No Response"): print "{} {} {}".format(node_name, nodes[node_name]["Status"], "Not Running") else: status = vnm.status(node_name) if status == "UNKNOWN" and \ nodes[node_name]["Status"] == "Stopped": print "{} {} {}".format(node_name, nodes[node_name]["Status"], status) else: print "{} {} {}".format(node_name, nodes[node_name]["Status"], status)
def do_cluster_status(args): state = load_state() node_controller = get_node_controller(state, args) node_command_generator = SimpleNodeCommandGenerator() vnm = ValidatorNetworkManager( node_controller=node_controller, node_command_generator=node_command_generator) if len(args.node_names) > 0: node_names = args.node_names node_superset = state['Nodes'] nodes = {} for node_name in args.node_names: try: nodes[node_name] = node_superset[node_name] except KeyError: raise CliException( "{} is not a known node name".format(node_name)) else: node_names = vnm.get_node_names() nodes = state['Nodes'] # Check expected status of nodes vs what is returned from vnm print("NodeName Expected Current") for node_name in nodes: if node_name not in node_names and \ (nodes[node_name]["Status"] == "Running" or nodes[node_name]["Status"] == "No Response"): print("{} {} {}".format(node_name, nodes[node_name]["Status"], "Not Running")) else: status = vnm.status(node_name) if status == "UNKNOWN" and \ nodes[node_name]["Status"] == "Stopped": print("{} {} {}".format(node_name, nodes[node_name]["Status"], status)) else: print("{} {} {}".format(node_name, nodes[node_name]["Status"], status))
def do_cluster_stats(args): state = load_state() node_controller = get_node_controller(state, args) node_command_generator = SimpleNodeCommandGenerator() vnm = ValidatorNetworkManager( node_controller=node_controller, node_command_generator=node_command_generator) nodes = state["Nodes"] for node_name in nodes: try: node_ip = vnm.get_ip(node_name) node_name_stats = node_name break except ManagementError as e: raise CliException(str(e)) node_url = "http://" + node_ip.strip(' \t\n\r') + ":" + \ nodes[node_name_stats]["HttpPort"] run_stats(node_url)
def do_cluster_start(args): # pylint: disable=redefined-variable-type file_name = \ os.path.join(os.path.expanduser("~"), '.sawtooth', 'cluster', "state.yaml") # Check for existing state.yaml and get state. If not create state dict. if os.path.isfile(file_name): with open(file_name, 'r') as state_file: state = yaml.load(state_file) else: state = dict() state["DesiredState"] = "Stopped" # Check State for Running validators, if stopped clear out nodes. if state["DesiredState"] == "Stopped": state["Nodes"] = {} if "Manage" not in state or state["DesiredState"] == "Stopped": if args.manage == "docker" or args.manage is None: state["Manage"] = "docker" elif args.manage == "daemon": state["Manage"] = "daemon" elif args.manage is not None and state['Manage'] != args.manage\ and state["DesiredState"] == "Running": raise CliException('Cannot use two different Manage types.' ' Already running {}'.format(state["Manage"])) state["DesiredState"] = "Running" if state["Manage"] == 'docker': node_controller = DockerNodeController() elif state["Manage"] == 'daemon': node_controller = DaemonNodeController() else: raise CliException('invalid management type:' ' {}'.format(state["Manage"])) node_command_generator = SimpleNodeCommandGenerator() vnm = ValidatorNetworkManager( node_controller=node_controller, node_command_generator=node_command_generator) try: existing_nodes = vnm.get_node_names() except ManagementError as e: raise CliException(str(e)) for i in xrange(0, args.count): node_name = "validator-{:0>3}".format(i) if node_name in existing_nodes and vnm.is_running(node_name): print "Already running: {}".format(node_name) continue # genesis is true for the first node genesis = (i == 0) gossip_port = 5500 + i http_port = 8800 + i print "Starting: {}".format(node_name) node_command_generator.start(node_name, http_port=http_port, gossip_port=gossip_port, genesis=genesis) state["Nodes"][node_name] = {"Status": "Running", "Index": i} # Write file to default directory with current state Nodes with open(file_name, 'w') as state_file: yaml.dump(state, state_file, default_flow_style=False) try: vnm.update() except ManagementError as e: raise CliException(str(e))
def do_cluster_extend(args): # pylint: disable=redefined-variable-type file_name = \ os.path.join(os.path.expanduser("~"), '.sawtooth', 'cluster', "state.yaml") # Get current state of Nodes if os.path.isfile(file_name): with open(file_name, 'r') as state_file: state = yaml.load(state_file) else: raise CliException("Missing state file") if state['Manage'] is None or state['Manage'] == 'docker': node_controller = DockerNodeController() elif state['Manage'] == 'daemon': node_controller = DaemonNodeController() else: raise CliException('invalid management' ' type: {}'.format(state['Manage'])) node_command_generator = SimpleNodeCommandGenerator() vnm = ValidatorNetworkManager( node_controller=node_controller, node_command_generator=node_command_generator) existing_nodes = state["Nodes"] desired_stated = state["DesiredState"] if desired_stated != "Running": raise CliException( "You must have a running network.\n" + "Use the cluster start command to start a validator network.") print "Extending network by {} nodes.".format(args.count) index_offset = len(existing_nodes) for i in xrange(0, args.count): j = i + index_offset node_name = "validator-{:0>3}".format(j) if node_name in existing_nodes and vnm.is_running(node_name): print "Already running: {}".format(node_name) continue # genesis is true for the first node genesis = (j == 0) gossip_port = 5500 + j http_port = 8800 + j print "Starting: {}".format(node_name) node_config = NodeConfig(node_name, http_port=http_port, gossip_port=gossip_port, genesis=genesis) node_command_generator.start(node_config) state["Nodes"][node_name] = {"Status": "Running", "Index": j} # Write file to default directory with current state Nodes with open(file_name, 'w') as state_file: yaml.dump(state, state_file, default_flow_style=False) try: vnm.update() except ManagementError as e: raise CliException(str(e))
def do_cluster_start(args): state = load_state(start=True) # Check State for Running validators, if stopped clear out nodes. if state["DesiredState"] == "Stopped": state["Nodes"] = {} manage_type = DEFAULT_MANAGE if args.manage is None else args.manage if "Manage" not in state or state["DesiredState"] == "Stopped": state['Manage'] = manage_type elif args.manage is not None and state['Manage'] != args.manage\ and state["DesiredState"] == "Running": raise CliException('Cannot use two different Manage types.' ' Already running {}'.format(state["Manage"])) state["DesiredState"] = "Running" if state["Manage"] in TNG_MANAGE: if args.processors is None: raise CliException("Use -P to specify one or more processors") state['Processors'] = args.processors node_controller = get_node_controller(state, args) node_command_generator = SimpleNodeCommandGenerator() vnm = ValidatorNetworkManager( node_controller=node_controller, node_command_generator=node_command_generator) try: existing_nodes = vnm.get_node_names() except ManagementError as e: raise CliException(str(e)) # Check for runnings nodes. If found, raise exception with message to use # sawtooth cluster extend command to add nodes to running network. for i in xrange(0, args.count): node_name = "validator-{:0>3}".format(i) if node_name in existing_nodes and vnm.is_running(node_name): print("Already running: {}".format(node_name)) raise CliException("Please use 'sawtooth cluster extend'\ to add more nodes.") for i in xrange(0, args.count): node_name = "validator-{:0>3}".format(i) if node_name in existing_nodes and vnm.is_running(node_name): print("Already running: {}".format(node_name)) continue # genesis is true for the first node genesis = (i == 0) gossip_port = 5500 + i http_port = 8800 + i node_args = NodeArguments(node_name, http_port=http_port, gossip_port=gossip_port, genesis=genesis) if node_args.genesis is True: node_controller.create_genesis_block(node_args) print("Starting: {}".format(node_name)) node_command_generator.start(node_args) state["Nodes"][node_name] = { "Status": "Running", "Index": i, "HttpPort": str(http_port), "GossipPort": str(gossip_port) } save_state(state) try: vnm.update() except ManagementError as e: raise CliException(str(e)) node_names = state['Nodes'].keys() if state["Manage"] in SUBPROCESS_MANAGE: try: while True: time.sleep(128) except KeyboardInterrupt: print() ns = Namespace(cluster_command='stop', command='cluster', node_names=node_names, verbose=None) do_cluster_stop(ns)
def do_cluster_start(args): state = load_state(start=True) # Check State for Running validators, if stopped clear out nodes. if state["DesiredState"] == "Stopped": state["Nodes"] = {} if "Manage" not in state or state["DesiredState"] == "Stopped": if args.manage == "subprocess" or args.manage is None: state["Manage"] = "subprocess" elif args.manage == "docker": state["Manage"] = "docker" elif args.manage == "daemon": state["Manage"] = "daemon" elif args.manage == "docker-tng": state["Manage"] = "docker-tng" elif args.manage is not None and state['Manage'] != args.manage\ and state["DesiredState"] == "Running": raise CliException('Cannot use two different Manage types.' ' Already running {}'.format(state["Manage"])) state["DesiredState"] = "Running" if state["Manage"] == 'docker-tng': state['Processors'] = args.processors node_controller = get_node_controller(state, args) node_command_generator = SimpleNodeCommandGenerator() vnm = ValidatorNetworkManager( node_controller=node_controller, node_command_generator=node_command_generator) try: existing_nodes = vnm.get_node_names() except ManagementError as e: raise CliException(str(e)) for i in xrange(0, args.count): node_name = "validator-{:0>3}".format(i) if node_name in existing_nodes and vnm.is_running(node_name): print "Already running: {}".format(node_name) continue # genesis is true for the first node genesis = (i == 0) gossip_port = 5500 + i http_port = 8800 + i node_args = NodeArguments(node_name, http_port=http_port, gossip_port=gossip_port, genesis=genesis) if node_args.genesis is True: node_controller.create_genesis_block(node_args) print "Starting: {}".format(node_name) node_command_generator.start(node_args) state["Nodes"][node_name] = {"Status": "Running", "Index": i} save_state(state) try: vnm.update() except ManagementError as e: raise CliException(str(e)) if state["Manage"] == 'subprocess': try: while True: time.sleep(128) except KeyboardInterrupt: print ns = Namespace(cluster_command='stop', command='cluster', node_names=[], verbose=None) do_cluster_stop(ns)