Exemplo n.º 1
0
 def _get_matching_basevm(basedisk_path):
     dbconn = DBConnector()
     basedisk_path = os.path.abspath(basedisk_path)
     basevm_list = dbconn.list_item(BaseVM)
     ret_basevm = None
     for item in basevm_list:
         if basedisk_path == item.disk_path:
             ret_basevm = item
             break
     return dbconn, ret_basevm
Exemplo n.º 2
0
 def _get_matching_basevm(basedisk_path):
     dbconn = DBConnector()
     basedisk_path = os.path.abspath(basedisk_path)
     basevm_list = dbconn.list_item(BaseVM)
     ret_basevm = None
     for item in basevm_list:
         if basedisk_path == item.disk_path: 
             ret_basevm = item
             break
     return dbconn, ret_basevm
class SynthesisServer(SocketServer.TCPServer):

    def __init__(self, args):
        settings, args = SynthesisServer.process_command_line(args)
        self.dbconn = DBConnector()
        self.basevm_list = self.check_basevm()

        Synthesis_Const.LOCAL_IPADDRESS = "0.0.0.0"
        server_address = (Synthesis_Const.LOCAL_IPADDRESS, Synthesis_Const.SERVER_PORT_NUMBER)

        self.allow_reuse_address = True
        try:
            SocketServer.TCPServer.__init__(self, server_address, SynthesisHandler)
        except socket.error as e:
            sys.stderr.write(str(e))
            sys.stderr.write("Check IP/Port : %s\n" % (str(server_address)))
            sys.exit(1)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        LOG.info("* Server configuration")
        LOG.info(" - Open TCP Server at %s" % (str(server_address)))
        LOG.info(" - Disable Nagle(No TCP delay)  : %s" \
                % str(self.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY)))
        LOG.info("-"*50)

        # This is cloudlet discovery related part and separated out
        '''
        if settings.register_server:
            try:
                self.register_client = RegisterThread(
                        settings.register_server,
                        update_period=Synthesis_Const.DIRECTORY_UPDATE_PERIOD)
                self.register_client.start()
                LOG.info("[INFO] Register to Cloudlet direcory service")
            except RegisterError as e:
                LOG.info(str(e))
                LOG.info("[Warning] Cannot register Cloudlet to central server")
        try:
            self.rest_server = RESTServer()
            self.rest_server.start()
        except RESTServerError as e:
            LOG.info(str(e))
            LOG.info("[Warning] Cannot start REST API Server")
            self.rest_server = None
        LOG.info("[INFO] Start RESTful API Server")
        try:
            self.resource_monitor = ResourceMonitorThread()
            self.resource_monitor.start()
        except ResourceMonitorError as e:
            LOG.info(str(e))
            LOG.info("[Warning] Cannot register Cloudlet to central server\n")
        '''

    def handle_error(self, request, client_address):
        #SocketServer.TCPServer.handle_error(self, request, client_address)
        #sys.stderr.write("handling error from client %s\n" % (str(client_address)))
        pass

    def expire_all_sessions(self):
        from db.table_def import Session

        LOG.info("Close all running sessions")
        session_list = self.dbconn.list_item(Session)
        for item in session_list:
            if item.status == Session.STATUS_RUNNING:
                item.terminate(Session.STATUS_UNEXPECT_CLOSE)
        self.dbconn.session.commit()


    def terminate(self):
        # expire all existing session
        self.expire_all_sessions()

        # close all thread
        if self.socket != -1:
            self.socket.close()
        if hasattr(self, 'register_client') and self.register_client != None:
            LOG.info("[TERMINATE] Deregister from directory service")
            self.register_client.terminate()
            self.register_client.join()
        if hasattr(self, 'rest_server') and self.rest_server != None:
            LOG.info("[TERMINATE] Terminate REST API monitor")
            self.rest_server.terminate()
            self.rest_server.join()
        if hasattr(self, 'resource_monitor') and self.resource_monitor != None:
            LOG.info("[TERMINATE] Terminate resource monitor")
            self.resource_monitor.terminate()
            self.resource_monitor.join()

        global session_resources
        for (session_id, resource) in session_resources.iteritems():
            try:
                resource.deallocate()
                msg = "Deallocate resources for Session: %s" % str(session_id)
                LOG.info(msg)
            except Exception as e:
                msg = "Failed to deallocate resources for Session : %s" % str(session_id)
                LOG.warning(msg)
        LOG.info("[TERMINATE] Finish synthesis server connection")


    @staticmethod
    def process_command_line(argv):
        global operation_mode
        VERSION = 'VM Synthesis Server: %s' % Cloudlet_Const.VERSION

        parser = OptionParser(usage="usage: %prog " + " [option]",
                version=VERSION)
        parser.add_option(
                '-r', '--register-server', action='store', dest='register_server',
                default=None, help= 'Domain address for registration server.\n \
                        Specify this if you like to register your \
                        Cloudlet to registration server.')
        settings, args = parser.parse_args(argv)
        return settings, args

    def check_basevm(self):
        basevm_list = self.dbconn.list_item(BaseVM)
        ret_list = list()
        LOG.info("-"*50)
        LOG.info("* Base VM Configuration")
        for index, item in enumerate(basevm_list):
            # check file location
            (base_diskmeta, base_mempath, base_memmeta) = \
                    Cloudlet_Const.get_basepath(item.disk_path)
            if not os.path.exists(item.disk_path):
                LOG.warning("disk image (%s) is not exist" % (item.disk_path))
                continue
            if not os.path.exists(base_mempath):
                LOG.warning("memory snapshot (%s) is not exist" % (base_mempath))
                continue

            # add to list
            ret_list.append(item)
            LOG.info(" %d : %s (Disk %d MB, Memory %d MB)" % \
                    (index, item.disk_path, os.path.getsize(item.disk_path)/1024/1024, \
                    os.path.getsize(base_mempath)/1024/1024))
        LOG.info("-"*50)

        if len(ret_list) == 0:
            LOG.error("[Error] NO valid Base VM")
            sys.exit(2)
        return ret_list
Exemplo n.º 4
0
class SynthesisServer(SocketServer.TCPServer):
    def __init__(self, args):
        settings, args = SynthesisServer.process_command_line(args)
        self.dbconn = DBConnector()
        self.basevm_list = self.check_basevm()

        Synthesis_Const.LOCAL_IPADDRESS = "0.0.0.0"
        server_address = (Synthesis_Const.LOCAL_IPADDRESS,
                          Synthesis_Const.SERVER_PORT_NUMBER)

        self.allow_reuse_address = True
        try:
            SocketServer.TCPServer.__init__(self, server_address,
                                            SynthesisHandler)
        except socket.error as e:
            sys.stderr.write(str(e))
            sys.stderr.write("Check IP/Port : %s\n" % (str(server_address)))
            sys.exit(1)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        LOG.info("* Server configuration")
        LOG.info(" - Open TCP Server at %s" % (str(server_address)))
        LOG.info(" - Disable Nagle(No TCP delay)  : %s" \
                % str(self.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY)))
        LOG.info("-" * 50)

        # This is cloudlet discovery related part and separated out
        '''
        if settings.register_server:
            try:
                self.register_client = RegisterThread(
                        settings.register_server,
                        update_period=Synthesis_Const.DIRECTORY_UPDATE_PERIOD)
                self.register_client.start()
                LOG.info("[INFO] Register to Cloudlet direcory service")
            except RegisterError as e:
                LOG.info(str(e))
                LOG.info("[Warning] Cannot register Cloudlet to central server")
        try:
            self.rest_server = RESTServer()
            self.rest_server.start()
        except RESTServerError as e:
            LOG.info(str(e))
            LOG.info("[Warning] Cannot start REST API Server")
            self.rest_server = None
        LOG.info("[INFO] Start RESTful API Server")
        try:
            self.resource_monitor = ResourceMonitorThread()
            self.resource_monitor.start()
        except ResourceMonitorError as e:
            LOG.info(str(e))
            LOG.info("[Warning] Cannot register Cloudlet to central server\n")
        '''

    def handle_error(self, request, client_address):
        #SocketServer.TCPServer.handle_error(self, request, client_address)
        #sys.stderr.write("handling error from client %s\n" % (str(client_address)))
        pass

    def expire_all_sessions(self):
        from db.table_def import Session

        LOG.info("Close all running sessions")
        session_list = self.dbconn.list_item(Session)
        for item in session_list:
            if item.status == Session.STATUS_RUNNING:
                item.terminate(Session.STATUS_UNEXPECT_CLOSE)
        self.dbconn.session.commit()

    def terminate(self):
        # expire all existing session
        self.expire_all_sessions()

        # close all thread
        if self.socket != -1:
            self.socket.close()
        if hasattr(self, 'register_client') and self.register_client != None:
            LOG.info("[TERMINATE] Deregister from directory service")
            self.register_client.terminate()
            self.register_client.join()
        if hasattr(self, 'rest_server') and self.rest_server != None:
            LOG.info("[TERMINATE] Terminate REST API monitor")
            self.rest_server.terminate()
            self.rest_server.join()
        if hasattr(self, 'resource_monitor') and self.resource_monitor != None:
            LOG.info("[TERMINATE] Terminate resource monitor")
            self.resource_monitor.terminate()
            self.resource_monitor.join()

        global session_resources
        for (session_id, resource) in session_resources.iteritems():
            try:
                resource.deallocate()
                msg = "Deallocate resources for Session: %s" % str(session_id)
                LOG.info(msg)
            except Exception as e:
                msg = "Failed to deallocate resources for Session : %s" % str(
                    session_id)
                LOG.warning(msg)
        LOG.info("[TERMINATE] Finish synthesis server connection")

    @staticmethod
    def process_command_line(argv):
        global operation_mode
        VERSION = 'VM Synthesis Server: %s' % Cloudlet_Const.VERSION

        parser = OptionParser(usage="usage: %prog " + " [option]",
                              version=VERSION)
        parser.add_option('-r',
                          '--register-server',
                          action='store',
                          dest='register_server',
                          default=None,
                          help='Domain address for registration server.\n \
                        Specify this if you like to register your \
                        Cloudlet to registration server.')
        settings, args = parser.parse_args(argv)
        return settings, args

    def check_basevm(self):
        basevm_list = self.dbconn.list_item(BaseVM)
        ret_list = list()
        LOG.info("-" * 50)
        LOG.info("* Base VM Configuration")
        for index, item in enumerate(basevm_list):
            # check file location
            (base_diskmeta, base_mempath, base_memmeta) = \
                    Cloudlet_Const.get_basepath(item.disk_path)
            if not os.path.exists(item.disk_path):
                LOG.warning("disk image (%s) is not exist" % (item.disk_path))
                continue
            if not os.path.exists(base_mempath):
                LOG.warning("memory snapshot (%s) is not exist" %
                            (base_mempath))
                continue

            # add to list
            ret_list.append(item)
            LOG.info(" %d : %s (Disk %d MB, Memory %d MB)" % \
                    (index, item.disk_path, os.path.getsize(item.disk_path)/1024/1024, \
                    os.path.getsize(base_mempath)/1024/1024))
        LOG.info("-" * 50)

        if len(ret_list) == 0:
            LOG.error("[Error] NO valid Base VM")
            sys.exit(2)
        return ret_list