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_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 get_node_controller(state, args): # pylint: disable=redefined-variable-type # Get base controller: node_controller = None if state['Manage'] == 'subprocess': node_controller = SubprocessNodeController() elif state['Manage'] == 'docker': node_controller = DockerNodeController() elif state['Manage'] == 'daemon': node_controller = DaemonNodeController() else: raise CliException('invalid management type:' ' {}'.format(state['Manage'])) # Optionally decorate with WrappedNodeController args_wrap = False if not hasattr(args, 'wrap') else args.wrap if 'Wrap' not in state.keys(): # if wrap has not been set in state, set it state['Wrap'] = args_wrap else: # state already knows about a wrapper if args_wrap is not False and args_wrap != state['Wrap']: raise CliException("Already wrapped to %s." % state["Wrap"]) if state['Wrap'] is not False: if not isinstance(node_controller, SubprocessNodeController): raise CliException("--wrap currently only implemented for " "'subprocess' management type") # either args or state have indicated a WrappedNodeController if 'ManageWrap' not in state.keys(): state['ManageWrap'] = None node_controller = WrappedNodeController( node_controller, data_dir=state['Wrap'], clean_data_dir=state['ManageWrap']) if state['Wrap'] is None: state['Wrap'] = node_controller.get_data_dir() state['ManageWrap'] = True print('{} wrapped to {}'.format(args.cluster_command, state['Wrap'])) # Return out construction: return node_controller
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))