Ejemplo n.º 1
0
 def __init__(self, config: Config):
     self.HOST_NAME = '0.0.0.0'
     self.SERVER_PORT = config.getInteger('queen_port', 4040)
     self.CLIENT_PORT = config.getInteger('client_port', 4041)
     self.KDFS_CONFIG = config
     self.KDFS_START_IP = config.get('nodes_start_ip', '192.168.0.0')
     self.KDFS_END_IP = config.get('nodes_end_ip', '192.168.5.255')
     self.CHUNK_SIZE = config.getInteger('chunk_size', 1024)
     self.MAX_TIMEOUT = config.getInteger('max_timeout', 60)
     # get client ip address
     self.CLIENT_IP = self.KDFS_CONFIG.get('client_ip', '127.0.0.1')
     # check if this node server is queen
     if config.getBoolean('is_queen', False):
         from server.KDFSQueen import KDFSQueen
         self.IS_QUEEN = True
         self.QUEEN = KDFSQueen(config)
         self.MAX_LISTEN = config.getInteger('queen_max_nodes', 1)
     # check for ports is already use
     if ServerUtils.checkPortInUse(
             self.CLIENT_PORT,
             self.CLIENT_IP) or ServerUtils.checkPortInUse(
                 self.SERVER_PORT, self.HOST_NAME):
         KDFSProtocol.echo(
             "One of \"{}:{}\" or \"{}:{}\" ports are already use!".format(
                 self.HOST_NAME, self.SERVER_PORT, self.CLIENT_IP,
                 self.CLIENT_PORT),
             is_err=True)
         return
     # run local socket client in another thread!
     # self.LOCALSERVERTHREAD = threading.Thread(target=self._runLocalServer)
     self.LOCALSERVERTHREAD = multiprocessing.Process(
         target=self._runLocalServer)
     self.LOCALSERVERTHREAD.start()
     # run global socket server
     self._runGlobalServer()
Ejemplo n.º 2
0
    def scanNodes(self, start: str, end: str):
        KDFSProtocol.echo(
            f"validating all KDFS nodes from {start} to {end}...", 'queen')
        # validate old ip addresses of nodes by queen
        IPpool = ServerUtils.getAllOldNodeIPs()
        # iterate all ip addresses in pool
        detected = 0
        for IP in IPpool:
            # find node name by its old IP
            nodeName = ServerUtils.findNodeByIP(IP)
            if self.validateNodeByIP(IP) == 'detect':
                detected += 1
                # update and on state of node
                ServerUtils.updateNodeByName(nodeName, {'state': 'on'})
            # if not detected, then off its state
            else:
                ServerUtils.updateNodeByName(nodeName, {'state': 'off'})

        # KDFSProtocol.echo("(debug) ",detected,len(IPpool))
        # if not detect all nodes by them old IPs and permmit to scan all nodes
        if detected < len(IPpool) and self.GLOBAL_CONFIG.getBoolean(
                'queen_scan_nodes', True):
            # get all IP addresses between start and end IPs
            IPpool = ServerUtils.getIPAddressesPool(start, end)
            # iterate all ip addresses in pool
            for IP in IPpool:
                self.validateNodeByIP(IP)

        print("\n")
Ejemplo n.º 3
0
 def validateNodeByIP(self, IP: str):
     state = 'reject'
     KDFSProtocol.echo("check for {} : ".format(IP), 'queen', end='\t')
     response = ServerUtils.sendAndReceiveServerIdentify(
         self.GLOBAL_CONFIG, IP)
     if response is not None:
         print("ACCEPT", end='\t')
         state = 'accept'
         # print('(debug) node identify:',response)
         # check for verify node
         nodeName = ServerUtils.findNodeByMacAddress(response['macaddr'])
         if nodeName != None:
             print("DETECT [{}]".format(nodeName))
             # update node info
             ServerUtils.updateNodeByName(
                 nodeName, {
                     'ip': IP,
                     'last_updated': strftime("%Y-%m-%d %H:%M:%S",
                                              gmtime()),
                     'hostname': response['hostname']
                 })
             state = 'detect'
             # check for upgrading node
             if int(response['version']) < self.GLOBAL_CONFIG.getInteger(
                     'version', 1):
                 self.upgradeNodeServer(nodeName, IP)
         else:
             print("UNDEFINED")
     else:
         print("REJECT")
         state = 'reject'
     # return state of node
     return state
Ejemplo n.º 4
0
	def send(self):
		system = platform.system()
		if self.__LINUX in system:
			return self.__send_linux()
		elif self.__WINDOWS in system:
			return self.__send_windows()
		else:
			KDFSProtocol.echo('notifications are not supported for {} system'.format(system)
,'notification',is_err=True)
			return False
Ejemplo n.º 5
0
	def __init__(self, title, description, duration=5, urgency=URGENCY_LOW, icon_path=None):
		if urgency not in [self.URGENCY_LOW, self.URGENCY_NORMAL, self.URGENCY_CRITICAL]:
			KDFSProtocol.echo('invalid urgency was given: {}'.format(urgency),'notification',is_err=True)
			return
		self.__WINDOWS = 'Windows'
		self.__LINUX = 'Linux'
		self.__title = title
		self.__description = description
		self.__duration = duration
		self.__urgency = urgency
		self.__icon_path = icon_path
		self.__is_windows = False
Ejemplo n.º 6
0
    def _broadcastCommand(self,nodesIPs:list,command,params:dict={},recv_file=False):
        # get kdfs config
        finalResponse = {}
        config = self._getConfig()
        port = config.getInteger('queen_port',4040)
        chunk_size = config.getInteger('chunk_size',1024)
        # iterate all selected nodes
        for IP in nodesIPs:
            # get node name by IP
            nodeName = ServerUtils.findNodeByIP(IP)
            KDFSProtocol.echo("Sending \"{}\" command by \"{}\" param(s) to \"{}\" node ...".format(command,','.join(params),nodeName),'command')
            # connect to socket server
            socketi = ServerUtils.socketConnect(IP,port,config.getInteger('max_timeout',60))
            try: 
                # send command with params
                KDFSProtocol.sendMessage(socketi,chunk_size,KDFSProtocol.sendCommandFormatter(command,params,send_queen=True,recv_file=recv_file))
                # get response of command, if exist!
                response = KDFSProtocol.receiveMessage(socketi,chunk_size,is_file=recv_file)
                # print('(debug) node list response:',response)
                # append response to final by node name
                finalResponse[nodeName] = response


            except Exception as e:
                KDFSProtocol.echo("Raise an exception when broadcasting command",'command',err=e,is_err=True)
                # raise
            finally:
                # close socket 
                if socketi is not None:
                    socketi.close()

        return finalResponse
    # ------------------------------
    # ------------------------------
    # ------------------------------
Ejemplo n.º 7
0
	def __send_windows(self):
		try:
			import win10toast
			win10toast.ToastNotifier().show_toast(
				threaded=True,
				title=self.__title,
				msg=self.__description,
				duration=self.__duration,
				icon_path=self.__icon_path
			)
			return True
		except ImportError:
			KDFSProtocol.echo('notifications are not supported, can\'t import necessary library','notification',is_err=True)
			return False
Ejemplo n.º 8
0
	def __send_linux(self):
		import subprocess
		try:
			command = [
				'notify-send', '{}'.format(self.__title),
				'{}'.format(self.__description),
				'-u', self.__urgency,
				'-t', '{}'.format(self.__duration * 1000)
			]
			if self.__icon_path is not None:
				command += ['-i', self.__icon_path]
			subprocess.call(command)
			return True
		except Exception as e:
			KDFSProtocol.echo('notifications are not supported in linux system','notification',err=e)
			return False
Ejemplo n.º 9
0
 def __init__(self, ip: str, port: int, clientsocket: socket.socket,
              config: Config):
     # Thread.__init__(self)
     super(ClientThread, self).__init__()
     self._stop_event = threading.Event()
     self.ip = ip
     self.port = port
     self.socket = clientsocket
     self.config = config
     self.packetNumber = 0
     self.commandData = {}
     self.isSendFile = False
     self.isReceiveFile = False
     self.maxReceivePackets = 1
     self.maxFailedPacketsLimit = 10
     self.FailedPackets = 0
     self.MinimalCommands = MinimalCommands()
     KDFSProtocol.echo(f"New server socket thread started for {ip}",
                       'server')
Ejemplo n.º 10
0
    def response(self,res,err=[],just_data=False):
        # check for command permission
        if not self._IsAccessToCommand:
            res = ''
            err = ['Permission denied']

        if just_data: return json.dumps(res)
        
        return KDFSProtocol.sendResponseFormatter(res,err,{
            'type' : self._CommandResponseType
        })
Ejemplo n.º 11
0
 def getClientCommandResponse(self,
                              command: str,
                              params: list = [],
                              ip: str = ''):
     # init vars
     commandClass = None
     # if command is identify
     if command == 'identify':
         (info, err) = MinimalCommands().identifyCommand()
         return KDFSProtocol.sendResponseFormatter(info)
     # check exist such command
     elif os.path.exists("./commands/{}.py".format(command)):
         commandClass = getattr(
             importlib.import_module("commands.{}".format(command)),
             "{}Command".format(command))
         return commandClass(ip, params).response()
     # any other invalid commands
     else:
         return KDFSProtocol.sendResponseFormatter(
             '', ['Not found such command from client!'])
Ejemplo n.º 12
0
 def terminate(self):
     print()
     try:
         # terminate client socket
         if self.CLIENT_SOCKET is not None:
             # self.CLIENT_SOCKET.close()
             self.CLIENT_SOCKET.shutdown()
         self.LOCALSERVERTHREAD.kill()
         KDFSProtocol.echo("KDFS Local Server Shutted down.", 'client')
         # terminate server socket
         if self.SERVER_SOCKET is not None:
             KDFSProtocol.echo("KDFS Server is Shutting down...", 'server')
             # kill all client threads
             try:
                 for t in self.CLIENT_THREADS:
                     t.join()
             except:
                 pass
             # close server connection socket
             self.SERVER_SOCKET.close()
             self.SERVER_SOCKET = None
             # check if logs.log is exist, then remove it
             if os.path.exists(ServerUtils.LOGS_PATH):
                 os.remove(ServerUtils.LOGS_PATH)
             # if tmp directory not exist, create it!
             if os.path.exists(ServerUtils.TMP_PATH):
                 pass  #TODO:
                 # os.remove()
     except Exception as e:
         KDFSProtocol.echo("Raise an Exception when Terminating", err=e)
     finally:
         # exit system
         exit(0)
Ejemplo n.º 13
0
 def sendAndReceiveServerIdentify(config: Config, ip: str):
     socketi = ServerUtils.socketConnect(
         ip, config.getInteger('queen_port', 4040), 2)
     try:
         # send identify command
         chunk_size = config.getInteger('chunk_size', 1024)
         KDFSProtocol.sendMessage(
             socketi, chunk_size,
             KDFSProtocol.sendCommandFormatter('identify', {},
                                               send_queen=True))
         # get response of command, if exist!
         response = KDFSProtocol.receiveMessage(socketi, chunk_size)['data']
         # print('(debug) node identify:',response)
         return response
     except Exception as e:
         return None
         # raise
     finally:
         # close socket
         if socketi is not None:
             socketi.close()
     return None
Ejemplo n.º 14
0
    def copyCommand(self, params: dict):
        # create absolute path with storage path
        absPath = self._getAbsPath(params['path'])
        # print('abspath:',absPath)
        # if type of copy is 'src'
        if params['mode'] == 'src':
            # check for exist directory or file
            if not os.path.exists(absPath):
                return ('', "no such file or directory to retrive")
            # if path is for file
            if os.path.isfile(absPath):
                content = KDFSProtocol.fileCompress(absPath)
                return (content, '')
            # if path is for directory
            elif os.path.isdir(absPath):
                content = KDFSProtocol.directoryCompress(absPath)
                return (content, '')
            else:
                return ('', "undefined path")

        # if type of copy is 'dest'
        elif params['mode'] == 'dest':
            if params['packnumber'] == 1:
                # check for exist parent directory of dest path
                if not os.path.exists(os.path.dirname(absPath)):
                    return ('', "no such parent directory to retrive")
                return ('success', '')
            else:

                # print("data file (copy):",params['data'])
                # save to file
                filename = KDFSProtocol.saveTempCompressedFile(params['data'])
                # extract to min source
                with tarfile.open(filename, 'r:gz') as kdfsTar:
                    kdfsTar.extractall(absPath)

                return ('success', '')
Ejemplo n.º 15
0
    def _runGlobalServer(self):
        # create a socket object
        if self.SERVER_SOCKET is None:
            # AF_INET == ipv4
            # SOCK_STREAM == TCP
            self.SERVER_SOCKET = socket.socket(socket.AF_INET,
                                               socket.SOCK_STREAM)
        # bind to the port
        try:
            self.SERVER_SOCKET.bind((self.HOST_NAME, self.SERVER_PORT))
            self.SERVER_SOCKET.listen(self.MAX_LISTEN)
        except:
            self.SERVER_SOCKET.close()
            KDFSProtocol.echo("Can not bind server on {} port".format(
                self.SERVER_PORT),
                              'server',
                              is_err=True)
            return
        # self.SERVER_SOCKET.settimeout(1000)
        KDFSProtocol.echo(
            "KDFS Server Socket listenning on {}:{}".format(
                self.HOST_NAME, self.SERVER_PORT), 'server')
        # listen on any request
        while True:
            try:
                if self.SERVER_SOCKET is None: break
                # accept connections from outside
                (clientsocket, (ip, port)) = self.SERVER_SOCKET.accept()
                KDFSProtocol.echo(
                    f"Connection from {ip} has been established.", 'server')

                newthread = ClientThread(ip, port, clientsocket,
                                         self.KDFS_CONFIG)
                self.CLIENT_THREADS.append(newthread)
                newthread.start()

            except KeyboardInterrupt:
                break
            except Exception as e:
                KDFSProtocol.echo("raise an exception", 'server', e)
                continue

        # Clean up the connection
        self.terminate()
Ejemplo n.º 16
0
    def upgradeCommand(self, params: dict):
        # if packet number is 1,then just return 'yes' or 'no'
        if params['packnumber'] == 1:
            # check if server can accept upgrades or not
            if self.config.getBoolean('accept_upgrades', True):
                KDFSProtocol.echo(
                    "Accept kdfs version {} upgrading...".format(
                        params['version']), 'upgrade')
                # check if upgrades folder is not exsit
                if not os.path.exists(ServerUtils.UPGRADE_PATH):
                    os.makedirs(ServerUtils.UPGRADE_PATH)
                # resturn accept response
                return ('yes', '')
            else:
                KDFSProtocol.echo("Refuse any upgrading", 'upgrade')
                # return refuse response
                return ('no', '')
        # if get file in next packet number, save it
        else:
            try:
                # save to file
                with open(ServerUtils.UPGRADE_ZIP_PATH.format(
                        params['version']),
                          mode='wb') as f:
                    f.write(bytearray(params['data']))
                # extract to min source
                with tarfile.open(
                        ServerUtils.UPGRADE_ZIP_PATH.format(params['version']),
                        'r:gz') as kdfsTar:
                    kdfsTar.extractall("./")
                # update kdfs version number
                self.config.updateItem('version', params['version'])
                KDFSProtocol.echo(
                    "saved kdfs version {} upgraded file in local".format(
                        params['version']), 'upgrade')
                # if os is linux, then run bash script
                if ServerUtils.detectOS() == 'linux':
                    os.system("sh ./upgrade.sh &")
                else:
                    pass
                    # TODO:

                return ('success', '')

            except Exception as e:
                return (e, '')
Ejemplo n.º 17
0
 def getQueenCommandResponse(self,
                             command: str,
                             params: dict = {},
                             data=None,
                             packnumber=1,
                             sendfile=False):
     response = ''
     error = ''
     # append more data to params of command
     params['data'] = data
     params['packnumber'] = packnumber
     # try to calling command of minimal class
     try:
         (response, error) = getattr(self.MinimalCommands,
                                     "{}Command".format(command))(params)
     except (AttributeError):
         error = 'Not found such command!'
     # return repsonse with error of command
     if sendfile:
         if error == '':
             return response
         return error
     else:
         return KDFSProtocol.sendResponseFormatter(response, [error])
Ejemplo n.º 18
0
    def _runLocalServer(self):
        # create a socket object
        if self.CLIENT_SOCKET is None:
            # AF_INET == ipv4
            # SOCK_STREAM == TCP
            self.CLIENT_SOCKET = socket.socket(socket.AF_INET,
                                               socket.SOCK_STREAM)
        # bind to the port
        try:
            self.CLIENT_SOCKET.bind((self.CLIENT_IP, self.CLIENT_PORT))
            self.CLIENT_SOCKET.listen(1)
        except:
            KDFSProtocol.echo("Can not bind local server on {} port".format(
                self.CLIENT_PORT),
                              'client',
                              is_err=True)
            return
        # self.SERVER_SOCKET.settimeout(1000)
        KDFSProtocol.echo(
            "KDFS Client Socket listenning on {}:{}".format(
                self.CLIENT_IP, self.CLIENT_PORT), 'client')
        # listen on any request
        while True:
            try:
                if self.CLIENT_SOCKET is None: break
                # accept connections from outside
                (clientsocket, (ip, port)) = self.CLIENT_SOCKET.accept()
                KDFSProtocol.echo("Connection has been established.", 'client')
                # Receive the data in small chunks and retransmit it
                while True:
                    try:
                        #=>get a command with parameters as json
                        chunk_size = self.KDFS_CONFIG.getInteger(
                            'chunk_size', 1024)
                        command = KDFSProtocol.receiveMessage(
                            clientsocket, chunk_size)
                        # check for empty command
                        if command == '' or type(
                                command
                        ) is not dict or command['command'] is None:
                            KDFSProtocol.echo(
                                "No command received. shutting down socket...",
                                'client')
                            break
                        KDFSProtocol.echo(
                            'Command Request received : {}'.format(
                                command['command']), 'client')
                        # get queen ip address
                        if self.QUEEN_IP == '':
                            KDFSProtocol.echo(
                                "Searching for queen server on local network...",
                                'client')
                            self.findQueenIP()
                        # if not found queen server
                        if self.QUEEN_IP == '':
                            KDFSProtocol.echo("Not Found Queen Server!",
                                              'client')
                            break
                        else:
                            KDFSProtocol.echo(
                                "Queen Server IP is {}".format(self.QUEEN_IP),
                                'client')
                        # send command to queen server
                        KDFSProtocol.echo(
                            "Sending client command to Queen Server...",
                            'client')
                        queensocket = ServerUtils.socketConnect(
                            self.QUEEN_IP, self.SERVER_PORT, self.MAX_TIMEOUT)
                        try:
                            # send client command to queen server
                            KDFSProtocol.sendMessage(queensocket, chunk_size,
                                                     json.dumps(command))
                            # get command response as json
                            response = KDFSProtocol.receiveMessage(
                                queensocket, chunk_size)
                            # close queen socket
                            queensocket.close()
                            # send response to client program
                            KDFSProtocol.sendMessage(clientsocket, chunk_size,
                                                     json.dumps(response))
                        except Exception as e:
                            KDFSProtocol.echo("connection closed by queen",
                                              'client', e)
                            break

                    except Exception as e:
                        KDFSProtocol.echo("connection closed by client",
                                          'client', e)
                        # raise
                        break
            except KeyboardInterrupt:
                break
            except Exception as e:
                KDFSProtocol.echo("raise an exception", 'client', e)
                continue

        # Clean up the connection
        self.CLIENT_SOCKET.close()
Ejemplo n.º 19
0
    def run(self):
        # KDFSProtocol.echo("(debug) run client ...")
        while True:
            try:
                if not self.socket: break
                #=>get a data with parameters as json
                chunk_size = self.config.getInteger('chunk_size', 1024)
                data = KDFSProtocol.receiveMessage(self.socket,
                                                   chunk_size,
                                                   is_file=self.isReceiveFile)
                # check if data is empty
                # if data is None:
                #     self.FailedPackets += 1
                #     # if failed packets is less than max limit, ifnore it!
                #     if self.FailedPackets < self.maxFailedPacketsLimit:
                #         KDFSProtocol.echo("failed to receive a packet ({}/{})".format(self.FailedPackets,self.maxFailedPacketsLimit),'server',is_err=True)
                #         continue
                # KDFSProtocol.echo(f'data get:{data},recv_file:{self.isReceiveFile},packnumber:{self.packetNumber}','debug')
                # increase packet number
                self.packetNumber += 1
                command = {}
                # get data as command
                if self.packetNumber == 1 and data is not None:
                    command = data
                    self.commandData = command
                    # if receive next packet is file
                    self.isReceiveFile = command['send_file']
                    # if send next packet is file
                    self.isSendFile = command['receive_file']
                    #  how many packets to receive
                    self.maxReceivePackets = command['max_packets']
                else:
                    command = self.commandData
                    self.commandData = ''
                # check for empty command
                if command == '' or command == {} or self.packetNumber > self.maxReceivePackets:
                    KDFSProtocol.echo(
                        "No command received. shutting down socket for \"{}\"..."
                        .format(self.ip), 'server')
                    self.socket.close()
                    break
                KDFSProtocol.echo(
                    'Command Request received : {} (request #{}/{})'.format(
                        command['command'], self.packetNumber,
                        self.maxReceivePackets), 'server')
                # print("command get:",command,self.packetNumber)
                response = ''
                # get queen command response as json
                if command['send_by'] == 'queen':
                    response = self.getQueenCommandResponse(
                        command['command'],
                        command['params'],
                        data,
                        self.packetNumber,
                        sendfile=self.isSendFile)
                    KDFSProtocol.echo(
                        "Sending response for \"{}\" node...".format(self.ip),
                        'server')
                    # print('client response:',response,self.isSendFile)

                # get client command response as json (for queen!)
                elif self.config.getBoolean('is_queen', False):
                    response = self.getClientCommandResponse(
                        command['command'], command['params'], self.ip)
                    KDFSProtocol.echo(
                        "Sending response for \"{}\" node...".format(self.ip),
                        'queen')
                    # KDFSProtocol.echo("(debug) response:",response)
                # send response of command
                KDFSProtocol.sendMessage(self.socket,
                                         chunk_size,
                                         response,
                                         send_file=self.isSendFile)

            except Exception as e:
                KDFSProtocol.echo("Connection closed by client.(1)", 'server',
                                  e)
                # raise
                break
Ejemplo n.º 20
0
 params = sys.argv[2:]
 # print('argvs:',command,params)
 # check for arguments
 if command == 'help':
     help()
 else:
     # start time of process command
     startTime = currentMilliseconds()
     # set loading
     print('Please Wait...',end="\r")
     try:
         # create a socket object
         socketi = ServerUtils.socketConnect(KDFSConfig.get('client_ip','127.0.0.1'),KDFSConfig.getInteger('client_port',4041),KDFSConfig.getInteger('max_timeout',60))
         # send command to server
         chunk_size = KDFSConfig.getInteger('chunk_size',1024)
         KDFSProtocol.sendMessage(socketi,chunk_size,KDFSProtocol.sendCommandFormatter(command,params))
         # print("(debug):",socketi,chunk_size,command,params)
         # get response of command
         response = KDFSProtocol.receiveMessage(socketi,chunk_size)
         # print("(debug) receive response : ",response)
         # end time of process command
         endTime = currentMilliseconds()
         # show command with params on output
         print("\r({}) >> [{} sec] {} {}\n\n".format(platform.node(),(endTime-startTime)/1000, command,' '.join(params)))
         # check for errors
         if response is None or response['data'] is None:
             raiseError("Can not retrive response from server!",True)
             exit(1)
         if len(response['errors']) > 0:
             for err in response['errors']:
                 raiseError(err,True)
Ejemplo n.º 21
0
    def _broadcastFile(self,nodeIPs:list,command,params:dict={},file_content=b''):
        # get kdfs config
        finalResponse = {}
        config = self._getConfig()
        port = config.getInteger('queen_port',4040)
        chunk_size = config.getInteger('chunk_size',1024)
        socket_list = {}
        # create socket connections by IP nodes
        for IP in nodeIPs:
            # get node name by IP
            nodeName = ServerUtils.findNodeByIP(IP)
            # connect to socket server
            socketi = ServerUtils.socketConnect(IP,port,config.getInteger('max_timeout',60))
            try:   
                # send command with params
                KDFSProtocol.echo("Sending \"{}\" command by \"{}\" param(s) to \"{}\" node ...".format(command,','.join(params),nodeName),'command')

                KDFSProtocol.sendMessage(socketi,chunk_size,KDFSProtocol.sendCommandFormatter(command,params,send_queen=True,send_file=True,max_send_packets=2))
                # get response of command, if exist!
                response = KDFSProtocol.receiveMessage(socketi,chunk_size)
                # KDFSProtocol.echo(f'response data(accept):{response}','debug')
                # get error of response,if exist
                if response['data'] is None or (response['data'] != 'success' and response['data'] != 'yes'):
                    return ('','failed to retrive accept response for send file')
                
                # if file to send, send file
                KDFSProtocol.echo("Sending file for \"{}\" command to \"{}\" node ...".format(command,nodeName),'command')

                # KDFSProtocol.echo(f'data file:{params}','debug')
                KDFSProtocol.sendMessage(socketi,chunk_size,file_content,send_file=True) 
                # get response of file, if exist!
                response = KDFSProtocol.receiveMessage(socketi,chunk_size)
                # get error of response,if exist
                if response['errors'] is not None and len(response['errors'])>1:
                    return ('',response['errors'][0])
                

            except Exception as e:
                KDFSProtocol.echo("Raise an exception when broadcasting file",'command',err=e,is_err=True)
                # raise
            finally:
                # close socket 
                if socketi is not None:
                    socketi.close()
        # return success response to send file successfully
        return ('success','')
Ejemplo n.º 22
0
    def upgradeNodeServer(self, name: str, ip: str):
        version = self.GLOBAL_CONFIG.getInteger('version', 1)
        zip_path = ServerUtils.UPGRADE_ZIP_PATH.format(version)
        KDFSProtocol.echo(
            "Upgrading \"{}\" node to verison {}...".format(name, version),
            'queen')
        # create zip file of all files to need upgrade, if not exist!
        if not os.path.exists(zip_path):
            # list of files that must upgrade for any node
            files = [
                "server/__init__.py",
                "server/KDFSProtocol.py",
                "server/KDFSServer.py",
                "server/ServerUtils.py",
                "server/ClientThread.py",
                "kdfs.py",
                "KDFSHelp",
                "kdfs.conf.sample",
                "server.py",
                "libs/__init__.py",
                "libs/Config.py",
                "libs/Daemon.py",
                "libs/tabulate.py",
                "libs/Notification.py",
                "libs/termcolor.py",
                "commands/__init__.py",
                "commands/minimal.py",
                "commands/minimalUitls.py",
            ]
            # if os is linux, then run bash script
            if ServerUtils.detectOS() == 'linux':
                files.append("upgrade.sh")
                files.append("kdfs_server.sh")
            # TODO:
            # compress all files in tar.gz file
            with tarfile.open(zip_path, 'w:gz') as kdfsTar:
                for f in files:
                    kdfsTar.add(f)

        # connect to node and send upgrade file for it
        try:
            # read new kdfs zip file
            filecontent = b''
            with open(zip_path, mode='rb') as f:
                filecontent = f.read()
            base = baseCommand(ip, 'upgrade', [])
            (response, err) = base._broadcastFile([ip], 'upgrade',
                                                  {'version': version},
                                                  filecontent)
            if response == 'success':
                KDFSProtocol.echo(
                    "Success upgrading of \"{}\" node server! Reconnect later (about 3 sec later)"
                    .format(name), 'queen')
            else:
                KDFSProtocol.echo(
                    "Seems that Failed upgrading of \"{}\" node server".format(
                        name),
                    'queen',
                    err=err)

            return True
        except (Exception, KeyboardInterrupt) as e:
            KDFSProtocol.echo("Failed upgrading by Exception", 'queen', e)
            return False