예제 #1
0
    def get_graphic_connection_status(self, graphic_type):
        """"Get graphics connection status for virtual machine.
        
        *Extended function*
        
        :param graphic_type: spice or vnc
        :return: Virtual Machine password
        :rtype: str        
        :raises ClskError: raise :class:`.base.ClskError`  
        """
        if self.is_extended() is False:
            raise NotImplementedError()

        if self.state != 'Running':
            raise ClskError('Virtual machine %s is not running.' % self.name)

        # get hypervisor name
        hostname = self.hostname
        # get instance internal name
        vm_internal_name = self.instancename
        # get instance hypervisor
        hypervisor_type = self.hypervisor

        # KVM hypervisor
        if hypervisor_type == 'KVM':
            try:
                conn = None

                # get connection to qemu server
                virt_server = self.get_hypervisor(hostname).get_conn()
                # create instance of VirtDomain
                virt_domain = VirtDomain(virt_server, name=vm_internal_name)
                # get graphics password
                #deep_data = virt_domain.info()

                # get graphic status
                if graphic_type == 'spice':
                    res = virt_domain.spice_connection_status()
                elif graphic_type == 'vnc':
                    res = virt_domain.vnc_connection_status()
                else:
                    raise VirtDomainError(
                        "%s connection status is not supported" % graphic_type)

                # release libvirt connection
                virt_server.disconnect()
                del virt_server
                del virt_domain

                return res
            except (VirtDomainError) as ex:
                self.logger.error(ex)
                raise ClskError(ex)

        # other hypervisor
        else:
            raise ClskError('Hypervisor %s is not supported' % hypervisor_type)

        self.logger.info('Get %s graphic connection status for instance : %s' %
                         (graphic_type, self.name))
예제 #2
0
    def destroy(self):
        """Destroy virtual machine.
        
        *Async command*
        
        :return: Cloudstack asynchronous job id
        :rtype: str
        :raises ClskError: raise :class:`.base.ClskError`
        """
        # stop virtual machine using hypervisor capabilities
        if self.is_extended() and self.state == 'Running':
            # get hypervisor name
            hostname = self.hostname
            # get instance internal name
            vm_internal_name = self.instancename
            # get instance hypervisor
            hypervisor_type = self.hypervisor

            # KVM hypervisor
            if hypervisor_type == 'KVM':
                try:
                    # get connection to qemu server
                    virt_server = self.get_hypervisor(hostname).get_conn()
                    # create instance of VirtDomain
                    virt_domain = VirtDomain(virt_server,
                                             name=vm_internal_name)
                    virt_domain.shutdown()
                    # release libvirt connection
                    virt_server.disconnect()
                    del virt_server
                    del virt_domain
                except (VirtManagerError, VirtDomainError) as ex:
                    self.logger.error(
                        'Vm %s can not be stopped using libvirt' % self.id)

            # other hypervisor
            else:
                pass

        params = {'command': 'destroyVirtualMachine', 'id': self.id}

        try:
            response = self.send_request(params)
            res = json.loads(response)
            clsk_job_id = res['destroyvirtualmachineresponse']['jobid']
            self.logger.debug(
                'Start job over %s.%s - %s: %s' %
                (self._obj_type, self.name, 'destroyVirtualMachine', res))
            return clsk_job_id
        except KeyError as ex:
            raise ClskError('Error parsing json data: %s' % ex)
        except ApiError as ex:
            raise ClskError(ex)
예제 #3
0
    def configuration(self):
        """Hypervisor virtual machine configuration 
        
        *Extended function*
        
        :raises ClskError: raise :class:`.base.ClskError`
        :raises NotImplementedError: If class extended mode is not active or 
                                     hypervisor is not already supported.
        """
        if self.is_extended() is False:
            raise NotImplementedError()

        if self.state != 'Running':
            raise ClskError('Virtual machine is not running.')

        # get hypervisor name
        hostname = self.hostname
        # get instance internal name
        vm_internal_name = self.instancename
        # get vm hypervisor
        hypervisor_type = self.hypervisor

        # KVM hypervisor
        if hypervisor_type == 'KVM':
            try:
                # get connection to qemu server
                virt_server = self.get_hypervisor(hostname).get_conn()
                # create instance of VirtDomain
                virt_domain = VirtDomain(virt_server, name=vm_internal_name)
                # get domain info
                info = virt_domain.info()
                # release libvirt connection
                virt_server.disconnect()
                del virt_server
                del virt_domain
                return info
            except (VirtManagerError, VirtDomainError) as ex:
                self.logger.error('Error reading libvirt domain configuration')
                raise ClskError(ex)
        # other hypervisor
        else:
            raise NotImplementedError()

        self.logger.info('Get configuration for instance : %s' % self.id)
예제 #4
0
    def get_graphic_connection(self, graphic_type):
        """"Get graphics connection params for virtual machine.
        
        *Extended function*
        
        :param graphic_type: spice or vnc
        :return: Virtual Machine password
        :rtype: str        
        :raises ClskError: raise :class:`.base.ClskError`  
        """
        if self.is_extended() is False:
            raise NotImplementedError()

        if self.state != 'Running':
            raise ClskError('Virtual machine %s is not running.' % self.name)

        # get hypervisor name
        hostname = self.hostname
        # get instance internal name
        vm_internal_name = self.instancename
        # get vm hypervisor
        hypervisor_type = self.hypervisor

        # KVM hypervisor
        if hypervisor_type == 'KVM':
            try:
                conn = None

                # get connection to qemu server
                virt_server = self.get_hypervisor(hostname).get_conn()
                # create instance of VirtDomain
                virt_domain = VirtDomain(virt_server, name=vm_internal_name)
                # get graphics password
                deep_data = virt_domain.info()

                # get graphic password
                try:
                    # get graphic data
                    graphics = deep_data['devices']['graphics']
                    if type(graphics) is not list:
                        graphics = [graphics]

                    for graphic in graphics:
                        print graphic['type'], graphic_type
                        if graphic['type'] == graphic_type:
                            conn = graphic
                            break
                except Exception as ex:
                    self.logger.error(ex)
                    raise ClskError(ex)

                if conn is None:
                    raise ClskError(
                        '%s connection is not provided for instance %s' %
                        (graphic_type, self.name))

                # release libvirt connection
                virt_server.disconnect()
                del virt_server
                del virt_domain

                return conn
            except (VirtDomainError) as ex:
                self.logger.error(
                    'No graphics device defined for instance %s' % self.name)
                raise ClskError(ex)

        # other hypervisor
        else:
            raise ClskError('Hypervisor %s is not supported' % hypervisor_type)

        self.logger.info('Change password to instance : %s' % self.name)
예제 #5
0
    def change_graphics_password(self, password):
        """"Change graphics password for virtual machine.
        
        *Extended function*
        
        :param devices: litst with additional devices : 
                            spice_graphics, vlc_graphics, 
                            video_cirrus, video_qxl,
                            virtio_serial, usb_redirect, sound_card,
        :raises ClskError: raise :class:`.base.ClskError`
        :raises NotImplementedError: If class extended mode is not active or 
                                     hypervisor is not already supported.        
        """
        if self.is_extended() is False:
            raise NotImplementedError()

        if self.state != 'Running':
            raise ClskError('Virtual machine is not running.')

        # get hypervisor name
        hostname = self.hostname
        # get instance internal name
        vm_internal_name = self.instancename
        # get instance hypervisor
        hypervisor_type = self.hypervisor

        # KVM hypervisor
        if hypervisor_type == 'KVM':
            try:
                # get connection to qemu server
                virt_server = self.get_hypervisor(hostname).get_conn()
                # create instance of VirtDomain
                virt_domain = VirtDomain(virt_server, name=vm_internal_name)
                # change graphics password
                virt_domain.change_graphics_password(password)
                # release libvirt connection
                virt_server.disconnect()
                del virt_server
                del virt_domain
            except (VirtDomainError) as ex:
                self.logger.error('Devices are not appended to instance %s' %
                                  self.id)
                raise ClskError(ex)
            '''
            # save virtual machine devices configuration on db
            try:
                db_session = self._db_server()
                manager = VmManager(db_session)
                # add new virtual machine reference into db and append device 
                try:
                    manager.add_virt_domain(self.id, devices)
                except TransactionError:
                    manager.append_virt_domain_devices(self.id, devices)
                
                #if not manager.get_virt_domain(self.id):
                    
            except (QueryError, TransactionError) as ex:
                self.logger.error('Vm %s devices configuration is not stored on db' % self.id)
                raise ClskError(ex)            
            '''
        # other hypervisor
        else:
            raise NotImplementedError()

        self.logger.info('Change password to instance : %s' % self.id)