Exemplo n.º 1
0
    def process_request(self, command, data):
        logger.debug("[Master] [{0}] [Request-R]: '{1}'.".format(self.name, command))


        if command == 'echo-c':  # Echo
            return 'ok-c ', data.decode()
        elif command == 'sync_i_c_m_p':
            result = self.manager.get_client_status(client_id=self.name, key='sync_integrity_free')
            return 'ack', str(result)
        elif command == 'sync_ai_c_mp':
            return 'ack', str(self.manager.get_client_status(client_id=self.name, key='sync_agentinfo_free'))
        elif command == 'sync_ev_c_mp':
            return 'ack', str(self.manager.get_client_status(client_id=self.name, key='sync_extravalid_free'))
        elif command == 'sync_i_c_m':  # Client syncs integrity
            data = data.decode()
            pci_thread = ProcessClientIntegrity(manager=self.manager, manager_handler=self, filename=data, stopper=self.stopper)
            pci_thread.start()
            # data will contain the filename
            return 'ack', self.set_worker(command, pci_thread, data)
        elif command == 'sync_ai_c_m':
            data = data.decode()
            mcf_thread = ProcessClientFiles(manager_handler=self, filename=data, stopper=self.stopper)
            mcf_thread.start()
            # data will contain the filename
            return 'ack', self.set_worker(command, mcf_thread, data)
        elif command == 'sync_ev_c_m':
            data = data.decode()
            mcf_thread = ProcessExtraValidFiles(manager_handler=self, filename=data, stopper=self.stopper)
            mcf_thread.start()
            return 'ack', self.set_worker(command, mcf_thread, data)
        elif command == 'get_nodes':
            data = data.decode()
            response = {name:data['info'] for name,data in self.server.get_connected_clients().items()}
            cluster_config = read_config()
            response.update({cluster_config['node_name']:{"name": cluster_config['node_name'], "ip": cluster_config['nodes'][0],  "type": "master",  "version": __version__}})
            serialized_response = ['ok', json.dumps(response)]
            return serialized_response
        elif command == 'get_health':
            filter_nodes = data.decode()
            response = self.manager.get_healthcheck(filter_nodes)
            serialized_response = ['ok', json.dumps(response)]
            return serialized_response
        elif command == 'get_agents':
            data = data.decode()
            split_data = data.split('%--%', 5)
            filter_status = split_data[0] if split_data[0] != 'None' else None
            filter_nodes = split_data[1] if split_data[1] != 'None' else None
            offset = split_data[2] if split_data[2] != 'None' else None
            limit = split_data[3] if split_data[3] != 'None' else None
            sort = split_data[4] if split_data[4] != 'None' else None
            search = split_data[5] if split_data[5] != 'None' else None
            response = get_agents_status(filter_status, filter_nodes, offset, limit, sort, search)
            serialized_response = ['ok', json.dumps(response)]
            return serialized_response
        else:  # Non-master requests
            return ServerHandler.process_request(self, command, data)
Exemplo n.º 2
0
    def process_response(self, response):
        # FixMe: Move this line to communications
        answer, payload = self.split_data(response)

        logger.debug("[Master] [{0}] [Response-R]: '{1}'.".format(self.name, answer))

        if answer == 'ok-m':  # test
            response_data = '[response_only_for_master] Client answered: {}.'.format(payload)
        else:
            response_data = ServerHandler.process_response(self, response)

        return response_data
Exemplo n.º 3
0
    def process_request(self, command, data):
        logger.debug("[Master ] [{0}] [Request-R    ]: '{1}'.".format(
            self.name, command))

        if command == 'echo-c':  # Echo
            self.process_keep_alive_from_worker()
            return 'ok-c ', data.decode()
        elif command == 'sync_i_c_m_p':
            result = self.manager.get_worker_status(worker_id=self.name,
                                                    key='sync_integrity_free')
            return 'ack', str(result)
        elif command == 'sync_ai_c_mp':
            return 'ack', str(
                self.manager.get_worker_status(worker_id=self.name,
                                               key='sync_agentinfo_free'))
        elif command == 'sync_ev_c_mp':
            return 'ack', str(
                self.manager.get_worker_status(worker_id=self.name,
                                               key='sync_extravalid_free'))
        elif command == 'sync_i_c_m':  # Worker syncs integrity
            data = data.decode()
            pci_thread = ProcessWorkerIntegrity(manager=self.manager,
                                                manager_handler=self,
                                                filename=data,
                                                stopper=self.stopper)
            pci_thread.start()
            # data will contain the filename
            return 'ack', self.set_worker_thread(command, pci_thread, data)
        elif command == 'sync_ai_c_m':
            data = data.decode()
            mcf_thread = ProcessWorkerFiles(manager_handler=self,
                                            filename=data,
                                            stopper=self.stopper)
            mcf_thread.start()
            # data will contain the filename
            return 'ack', self.set_worker_thread(command, mcf_thread, data)
        elif command == 'sync_ev_c_m':
            data = data.decode()
            mcf_thread = ProcessExtraValidFiles(manager_handler=self,
                                                filename=data,
                                                stopper=self.stopper)
            mcf_thread.start()
            return 'ack', self.set_worker_thread(command, mcf_thread, data)
        elif command == 'get_nodes':
            data = data.decode()
            response = {
                name: data['info']
                for name, data in self.server.get_connected_workers().items()
            }
            cluster_config = read_config()
            response.update({
                cluster_config['node_name']: {
                    "name": cluster_config['node_name'],
                    "ip": cluster_config['nodes'][0],
                    "type": "master",
                    "version": __version__
                }
            })
            serialized_response = ['json', json.dumps(response)]
            return serialized_response
        elif command == 'get_health':
            _, filter_nodes = data.decode().split(' ', 1)
            response = self.manager.get_healthcheck(
                filter_nodes if filter_nodes != 'None' else None)
            serialized_response = ['json', json.dumps(response)]
            return serialized_response
        elif command == 'get_config':
            response = self.manager.get_configuration()
            serialized_response = ['ok', json.dumps(response)]
            return serialized_response
        elif command == 'string':
            string_sender_thread = FragmentedStringReceiverMaster(
                manager_handler=self, stopper=self.stopper)
            string_sender_thread.start()
            return 'ack', self.set_worker_thread(command, string_sender_thread)
        elif command == 'dapi':
            self.server.add_api_request(self.name + ' ' + data.decode())
            return 'ack', "Request is being processed"
        elif command == "dapi_res":
            string_receiver = FragmentedAPIResponseReceiver(
                manager_handler=self,
                stopper=self.stopper,
                worker_id=data.decode())
            string_receiver.start()
            return 'ack', self.set_worker_thread(command, string_receiver)
        elif command == 'err-is':
            logger.debug("{} Internal socket error received: {}".format(
                self.tag, data.decode()))
            return 'ack', 'thanks'
        else:  # Non-master requests
            return ServerHandler.process_request(self, command, data)
Exemplo n.º 4
0
 def __init__(self, sock, server, asyncore_map, addr=None):
     ServerHandler.__init__(self, sock, server, asyncore_map, addr)
     self.manager = server