Example #1
0
    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
        '''
Example #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
Example #3
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
    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
        '''
    def __init__(self,
                 port_number=StreamSynthesisConst.SERVER_PORT_NUMBER,
                 timeout=None,
                 handoff_datafile=None):
        self.port_number = port_number
        self.timeout = timeout
        self._handoff_datafile = handoff_datafile
        if self._handoff_datafile:
            self.handoff_data = self._load_handoff_data(self._handoff_datafile)
            self.basevm_list = self.check_basevm(
                self.handoff_data.base_vm_paths,
                self.handoff_data.basevm_sha256_hash)
        else:
            self.handoff_data = None
            self.basevm_list = self.check_basevm_from_db(DBConnector())

        server_address = ("0.0.0.0", self.port_number)
        self.allow_reuse_address = True
        try:
            SocketServer.TCPServer.__init__(self, server_address,
                                            StreamSynthesisHandler)
        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(" - Time out for waiting: %d" % (self.timeout))
        LOG.info(" - Disable Nagle(No TCP delay)  : %s" \
                % str(self.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY)))
        LOG.info("-" * 50)
Example #6
0
    def import_basevm(filename):
        filename = os.path.abspath(filename)
        (base_hashvalue, disk_name, memory_name, diskhash_name, memoryhash_name) = \
                PackagingUtil._get_basevm_attribute(filename)

        # check directory
        base_vm_dir = os.path.join(os.path.dirname(Const.BASE_VM_DIR),
                                   base_hashvalue)
        temp_dir = mkdtemp(prefix="cloudlet-base-")
        disk_tmp_path = os.path.join(temp_dir, disk_name)
        disk_target_path = os.path.join(base_vm_dir, disk_name)
        dbconn, matching_basevm = PackagingUtil._get_matching_basevm(
            disk_target_path)
        if matching_basevm != None:
            LOG.info("Base VM is already exists")
            LOG.info("Delete existing Base VM using command")
            LOG.info("See more 'cloudlet --help'")
            return None
        if not os.path.exists(base_vm_dir):
            LOG.info("create directory for base VM")
            os.makedirs(base_vm_dir)

        # decompress
        LOG.info("Decompressing Base VM to temp directory at %s" % temp_dir)
        zipbase = zipfile.ZipFile(_FileFile("file:///%s" % filename), 'r')
        zipbase.extractall(temp_dir)
        shutil.move(disk_tmp_path, disk_target_path)
        (target_diskhash, target_memory, target_memoryhash) = \
                Const.get_basepath(disk_target_path, check_exist=False)
        path_list = {
            os.path.join(temp_dir, memory_name): target_memory,
            os.path.join(temp_dir, diskhash_name): target_diskhash,
            os.path.join(temp_dir, memoryhash_name): target_memoryhash,
        }

        LOG.info("Place base VM to the right directory")
        for (src, dest) in path_list.iteritems():
            shutil.move(src, dest)

        # add to DB
        dbconn = DBConnector()
        LOG.info("Register New Base to DB")
        LOG.info("ID for the new Base VM: %s" % base_hashvalue)
        new_basevm = BaseVM(disk_target_path, base_hashvalue)
        LOG.info("Success")
        dbconn.add_item(new_basevm)
        return disk_target_path, base_hashvalue
Example #7
0
    def import_basevm(filename):
        filename = os.path.abspath(filename)
        (base_hashvalue, disk_name, memory_name, diskhash_name, memoryhash_name) = \
                PackagingUtil._get_basevm_attribute(filename)

        # check directory
        base_vm_dir = os.path.join(os.path.dirname(Const.BASE_VM_DIR), base_hashvalue)
        temp_dir = mkdtemp(prefix="cloudlet-base-")
        disk_tmp_path = os.path.join(temp_dir, disk_name)
        disk_target_path = os.path.join(base_vm_dir, disk_name)
        dbconn, matching_basevm = PackagingUtil._get_matching_basevm(disk_target_path)
        if matching_basevm != None:
            LOG.info("Base VM is already exists")
            LOG.info("Delete existing Base VM using command")
            LOG.info("See more 'cloudlet --help'")
            return None
        if not os.path.exists(base_vm_dir):
            LOG.info("create directory for base VM")
            os.makedirs(base_vm_dir)

        # decompress
        LOG.info("Decompressing Base VM to temp directory at %s" % temp_dir)
        zipbase = zipfile.ZipFile(_FileFile("file:///%s" % filename), 'r')
        zipbase.extractall(temp_dir)
        shutil.move(disk_tmp_path, disk_target_path)
        (target_diskhash, target_memory, target_memoryhash) = \
                Const.get_basepath(disk_target_path, check_exist=False)
        path_list = {
                os.path.join(temp_dir, memory_name): target_memory,
                os.path.join(temp_dir, diskhash_name): target_diskhash,
                os.path.join(temp_dir, memoryhash_name): target_memoryhash,
                }

        LOG.info("Place base VM to the right directory")
        for (src, dest) in path_list.iteritems():
            shutil.move(src, dest)

        # add to DB
        dbconn = DBConnector()
        LOG.info("Register New Base to DB")
        LOG.info("ID for the new Base VM: %s" % base_hashvalue)
        new_basevm = BaseVM(disk_target_path, base_hashvalue)
        LOG.info("Success")
        dbconn.add_item(new_basevm)
        return disk_target_path, base_hashvalue
    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.write("* Server configuration\n")
        Log.write(" - Open TCP Server at %s\n" % (str(server_address)))
        Log.write(" - Disable Nalge(No TCP delay)  : %s\n" \
                % str(self.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY)))
        Log.write("-"*50)
        Log.write("\n")

        # Start UPnP Server
        try:
            self.upnp_server = UPnPServer()
            self.upnp_server.start()
        except UPnPError as e:
            Log.write(str(e))
            Log.write("[Warning] Cannot start UPnP Server\n")
            self.upnp_server = None
        Log.write("[INFO] Start UPnP Server\n")

        # Start registration client
        if settings.register_server:
            try:
                self.register_client = RegisterThread(
                        settings.register_server,
                        log=Log,
                        update_period=Synthesis_Const.DIRECTORY_UPDATE_PERIOD)
                self.register_client.start()
                Log.write("[INFO] Register to Cloudlet direcory service\n")
            except RegisterError as e:
                Log.write(str(e))
                Log.write("[Warning] Cannot register Cloudlet to central server\n")

        # cloudlet machine monitor
        try:
            self.resource_monitor = ResourceMonitorThread(log=Log) 
            self.resource_monitor.start()
        except ResourceMonitorError as e:
            Log.write(str(e))
            Log.write("[Warning] Cannot register Cloudlet to central server\n")
        Log.flush()
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
Example #10
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