Esempio n. 1
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()

    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)
Esempio n. 2
0
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()
Esempio n. 3
0
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()
Esempio n. 4
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))

        print("Stopping: {}".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)

    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)