def stop_broker(self, cur_message):

        """ 
        Default handler for stopping a broker on a remote node

        --- Please Read --- 
        
        Setup Notes:

            For now this is using ssh to invoke a command on the remote host

            Please install the ssh keys that will allow password-less access to a user that has credentials to perform a command:
            
                kill -9 <Broker PID>

            Eventually this could be using a Queue for control messages or post using HTTP to a rest control service deployed on the node

        """
        
        results             = build_dict("FAILED", "Stop Broker Not Started", {
                                            "Nodes"     : [],
                                            "Errors"    : []
                                    })

        try:

            self.m_cur_count    += 1

            self.lg("Handling - Stopping a Broker(" + str(json.dumps(cur_message)) + ")", 6)

            ssh_host            = str(cur_message["Host"])
            ssh_user            = str(cur_message["User"])

            commands_to_run     = cur_message["Commands"]
            count_idx           = 0

            values              = {
                                    "PID"   : None,
                                }

            all_success         = False
            err_msg             = "Stop Broker Failed"

            for ssh_command in commands_to_run:
                
                valid_command   = True

                # Allow for edits before running:
                remote_command  = str(ssh_command)

                if count_idx == 1:
                    
                    if str(values["PID"]) != "" and str(values["PID"]).lower() != "none":
                        remote_command  = remote_command.replace("%i", str(values["PID"]))

                        if values["PID"] != "":
                            self.lg("Stopping Remote Broker(" + str(ssh_host) + ") with PID(" + str(values["PID"]) + ") Command(" + str(remote_command) + ")", 6)
                        else:
                            valid_command   = False

                    else:
                        err_msg     = "Failed to find a valid Broker PID for Stopping Remote Broker(" + str(ssh_host) + ") with PID(" + str(values["PID"]) + ") Command(" + str(remote_command) + ")"
                        self.elg(err_msg, True)
                        results     = build_dict("FAILED", str(err_msg), { "Nodes" : [], "Errors" : [] })
                        return results

                # end of prescreening for kill command

                if valid_command:
                    proc            = subprocess.Popen(remote_command, shell=True, stdout=subprocess.PIPE)
                    std_rows        = proc.communicate()[0].split("\n")

                    # On Docker containers the stop action hook handles this in one command
                    if len(commands_to_run) == 1:
                        self.lg("Stopped Broker", 6)
                        all_success = True
                        break

                    # For killing the broker the first command gets the PID
                    else:
                        if len(std_rows) == 1:

                            if count_idx == 0:
                                err_msg     = "Failed to Stop Broker with Error(" + str(std_rows) + ")"
                                self.elg(err_msg, True)
                                results     = build_dict("FAILED", str(err_msg), { "Nodes" : [], "Errors" : [] })
                                return results

                            else:
                                all_success = True

                        else:
                            if count_idx == 0:
                                values["PID"]   = str(std_rows[0].replace("   ", " ").replace("  ", " ").split(" ")[1]).strip().lstrip()
                                if values["PID"] != "":
                                    self.lg("Stopping Remote Broker(" + str(ssh_host) + ") with PID(" + str(values["PID"]) + ")", 6)
                # end of valid command

                count_idx       += 1

            # end of for all commands
            
            if all_success:
                
                cluster_nodes   = self.return_all_cluster_node_details()

                if "Expected" in cur_message:

                    self.lg("Checking Cluster", 6)
                    # Confirm the cluster is as expected after stopping it:
                    all_valid       = False

                    if cluster_nodes["Status"] == "SUCCESS":
                    
                        self.lg("Validing Expected(" + str(len(cur_message["Expected"])) + ") Cluster(" + str(len(cluster_nodes["Record"]["Nodes"])) + ")", 6)

                        errors      = []

                        if len(cur_message["Expected"]) == len(cluster_nodes["Record"]["Nodes"]):

                            # This is looks like it should be refactored...

                            for expected_node in cur_messages["Expected"]["Messages"]:

                                found_node          = False

                                for cluster_node in cluster_nodes["Record"]["Nodes"]:
                                    if str(cluster_node["Name"]).lower().strip().lstrip() == str(expected_node["Name"]).lower().strip().lstrip():
                                        found_node  = True
                                        for key in expected_node:
                                            if key not in cluster_node:
                                                errors.append("Missing Expected(" + str(expected_node["Name"]) + ") with a Cluster Key(" + str(key) + ")")
                                            # if it is missing
                                            else:
                                                if cluster_node[key].strip().lstrip() != expected_node[key].lstrip().strip():
                                                    errors.append("Cluster Node(" + str(cluster_node["Name"]) + ") Key(" + str(key) + ") Value(" + str(cluster_node[key]) + ") Does not match Expected(" + str(expected_node["Name"]) + ") Value(" + str(expected_node[key]) + ")")
                                        # for each expected key on the node
                                    # if the cluster's name matches the expected

                                # end for all cluster nodes

                            # end for all expecteds
                        else:
                            errors.append("Cluster Node Mismatch Expected(" + str(len(cur_message["Expected"])) + ") Cluster(" + str(len(cluster_nodes["Record"]["Nodes"])) + ")")
                        # if there are expectations on the cluster's new status, perform the validation and build the errors

                        if len(errors) == 0:
                            results = build_dict("SUCCESS", "", { "Nodes" : cluster_nodes["Record"]["Nodes"], "Errors" : [] })
                        else:
                            results = build_dict("FAILED",  "", { "Nodes" : cluster_nodes["Record"]["Nodes"], "Errors" : [] })
                            
                    # end of if cluster_nodes["Status"] == "SUCCESS"
                else:
                    results         = build_dict("SUCCESS", "", { "Nodes" : cluster_nodes["Record"]["Nodes"], "Errors" : [] })
                # if success

            # all success 
            else:
                results             = build_dict("FAILED", str(err_msg), { "Nodes" : [], "Errors" : [] })
            # end of if/else

        except Exception,k:
            err_msg     = "Failed Stop Broker Number(" + str(self.m_message_number) + ") Message(" + str(cur_message) + ") with Ex(" + str(k) + ")"
            self.elg(err_msg, True)
            results     = build_dict("FAILED", str(err_msg), { "Nodes" : [], "Errors" : [] })

            self.m_message_number   += 1
        # end of try/ex

        return results
    # end of stop_broker
dataset/ETHPy150Open GetLevvel/message-simulator/src/rabbit_message_publisher.py/RabbitMessagePublisher.stop_broker/original