Esempio n. 1
0
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 args.node_names:
        node_names = args.node_names
        node_superset = vnm.get_node_names()
        for node_name in args.node_names:
            try:
                node_name in node_superset
            except KeyError:
                raise CliException(
                    "{} is not a known node name".format(node_name))
    else:
        node_names = vnm.get_node_names()

    # Check expected status of nodes vs what is returned from vnm
    print("NodeName".ljust(15), "Status".ljust(10))
    for node_name in node_names:
        status = vnm.status(node_name)
        if status == "UNKNOWN":
            status = "Not Running"
        print(node_name.ljust(15), status.ljust(10))
Esempio n. 2
0
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 args.node_names:
        node_names = args.node_names
        node_superset = vnm.get_node_names()
        for node_name in args.node_names:
            try:
                node_name in node_superset
            except KeyError:
                raise CliException(
                    "{} is not a known node name".format(node_name))
    else:
        node_names = vnm.get_node_names()

    # Check expected status of nodes vs what is returned from vnm
    print("NodeName".ljust(15), "Status".ljust(10))
    for node_name in node_names:
        status = vnm.status(node_name)
        if status == "UNKNOWN":
            status = "Not Running"
        print(node_name.ljust(15), status.ljust(10))
Esempio n. 3
0
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 range(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))
Esempio n. 4
0
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)

    try:
        existing_nodes = vnm.get_node_names()
    except ManagementError as e:
        raise CliException(str(e))

    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 range(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)

    save_state(state)

    try:
        vnm.update()
    except ManagementError as e:
        raise CliException(str(e))
Esempio n. 5
0
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 args.node_names:
        node_names = args.node_names
    else:
        node_names = vnm.get_node_names()

    # if node_names is empty, stop doesn't get called
    for node_name in node_names:

        node_command_generator.stop(node_name)

    state["DesiredState"] = "Stopped"

    save_state(state)

    print('Stopping validators')

    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 find_still_up(node_names):
        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: killing {}".format(node_name))
        node_controller.kill(node_name)
Esempio n. 6
0
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 args.node_names:
        node_names = args.node_names
    else:
        node_names = vnm.get_node_names()

    # if node_names is empty, stop doesn't get called
    for node_name in node_names:

        node_command_generator.stop(node_name)

    state["DesiredState"] = "Stopped"

    save_state(state)

    print('Stopping validators')

    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 find_still_up(node_names):
        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: killing {}".format(node_name))
        node_controller.kill(node_name)
Esempio n. 7
0
def do_cluster_start(args):
    state = load_state(start=True)

    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 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 range(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 range(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)

        node_command_generator.start(node_args)

    save_state(state)

    print('Starting validators')

    try:
        vnm.update()
    except ManagementError as e:
        raise CliException(str(e))

    subprocess_manage = 'subprocess', 'subprocess-legacy'
    if state["Manage"] in subprocess_manage:
        print('Validators ready')
        try:
            while True:
                time.sleep(128)
        except KeyboardInterrupt:
            print()
            node_names = vnm.get_node_names()
            ns = Namespace(cluster_command='stop',
                           command='cluster',
                           node_names=node_names,
                           verbose=None)
            do_cluster_stop(ns)
Esempio n. 8
0
def do_cluster_start(args):
    state = load_state(start=True)

    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 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 range(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 range(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)

        node_command_generator.start(node_args)

    save_state(state)

    print('Starting validators')

    try:
        vnm.update()
    except ManagementError as e:
        raise CliException(str(e))

    subprocess_manage = 'subprocess', 'subprocess-legacy'
    if state["Manage"] in subprocess_manage:
        print('Validators ready')
        try:
            while True:
                time.sleep(128)
        except KeyboardInterrupt:
            print()
            node_names = vnm.get_node_names()
            ns = Namespace(cluster_command='stop', command='cluster',
                           node_names=node_names, verbose=None)
            do_cluster_stop(ns)
Esempio n. 9
0
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()

    state_nodes = state["Nodes"]

    # if node_names is empty, stop doesn't get called
    for node_name in node_names:
        if node_name not in state_nodes:
            raise CliException("{} is not a known node name".format(node_name))
        if state_nodes[node_name]['Status'] == 'Stopped':
            raise CliException('{} already stopped'.format(node_name))

        node_command_generator.stop(node_name)

        # Update status of Nodes
        node_status = 'Stopped' if node_name in state_nodes else 'Unknown'
        state_nodes[node_name]['Status'] = node_status

    if len(args.node_names) == 0 and len(node_names) == 0:
        for node_name in state_nodes:
            state_nodes[node_name]["Status"] = "Unknown"

    # If none of the nodes are running set overall State to Stopped
    state["DesiredState"] = "Stopped"
    for node in state_nodes:
        if state_nodes[node]["Status"] == "Running":
            state["DesiredState"] = "Running"

    # Update state of nodes
    state["Nodes"] = state_nodes
    save_state(state)

    print('Stopping validators')

    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)