Exemplo n.º 1
0
Arquivo: vm.py Projeto: cc1-cloud/cc1
    def destroy(vms):
        """
        @parameter{vms}
        @response result
        """
        results = []
        for vm in vms:
            vm = VM.objects.get(pk=vm.id)
            log.debug(vm.user.id, "Killing VM id: %s, state: %s" % (vm.id, vm.state))
            # Check for VM state
            if vm.state in (vm_states['closing'], vm_states['saving']):
                results.append({'status': 'vm_already_closing', 'data': ''})
                continue

            if vm.state in (vm_states['erased'], vm_states['closed']):
                results.append({'status': 'vm_wrong_state', 'data': ''})
                continue

            vm.save_vm = 0

            try:
                vm.save()
                transaction.commit()
                vm.lv_destroy()
            except Exception, e:
                log.exception(vm.user.id, 'error destroying VM: %s' % str(e))
                results.append({'status': 'vm_destroy', 'data': ''})
                message.error(vm.user_id, 'vm_destroy', {'id': vm.id, 'name': vm.name})
                continue

            results.append({'status': 'ok', 'data': ''})
Exemplo n.º 2
0
    def destroy(vms):
        """
        @parameter{vms}
        @response result
        """
        results = []
        for vm in vms:
            vm = VM.objects.get(pk=vm.id)
            log.debug(vm.user.id,
                      "Killing VM id: %s, state: %s" % (vm.id, vm.state))
            # Check for VM state
            if vm.state in (vm_states['closing'], vm_states['saving']):
                results.append({'status': 'vm_already_closing', 'data': ''})
                continue

            if vm.state in (vm_states['erased'], vm_states['closed']):
                results.append({'status': 'vm_wrong_state', 'data': ''})
                continue

            vm.save_vm = 0

            try:
                vm.save()
                transaction.commit()
                vm.lv_destroy()
            except Exception, e:
                log.exception(vm.user.id, 'error destroying VM: %s' % str(e))
                results.append({'status': 'vm_destroy', 'data': ''})
                message.error(vm.user_id, 'vm_destroy', {
                    'id': vm.id,
                    'name': vm.name
                })
                continue

            results.append({'status': 'ok', 'data': ''})
Exemplo n.º 3
0
 def create(self):
     """
     Starts VM's thread.
     -# Gets VM's record from database (basing on vm_id) (if exists).
     -# Copies image chosen for this VM.
     -# Connects to Libvirt and generate template for it.
     -# Creates Libvirt domain.
     -# Sets VM's state as *running*
     -# If VM is element of farm, it sets proper farm state.
     """
     try:
         log.info(
             self.vm.user_id, "Copy image from %s to %s" %
             (self.vm.system_image.path, self.vm.path))
         self.vm.system_image.copy_to_node(self.vm)
     except Exception, e:
         log.exception(self.vm.user_id,
                       'Libvirt error for %d: %s' % (self.vm.id, e))
         self.vm.set_state('failed')
         message.error(self.vm.user_id, 'vm_create', {
             'id': self.vm.id,
             'name': self.vm.name
         })
         self.vm.node.lock()
         self.vm.save(update_fields=['state'])
         return
Exemplo n.º 4
0
    def register_head(vm):
        """
        Head registration process:
        - Creates ssh keys and sets their values for WN;
        - Inserts VMs into the database;
        - Then starts VMThreads which create actual machines.

        Called when registering farms head.

        @parameter{vm,VM} instance of the VM to be registered as head
        """
        log.debug(vm.user_id, "machine %d: registered as head" % vm.id)

        log.debug(vm.user_id, "creating lock for machine %d in farm %d" % (vm.id, vm.farm_id))
        # skip if farm is already configured - reboot head
        if vm.is_head() == True and vm.farm.state == farm_states['running']:
            return

        vms = []
        if vm.farm.state == farm_states['init_head']:
            vm.farm.state = farm_states['running']
            vm.farm.save()

            log.info(vm.user_id, 'generating ssh keys on head %d' % vm.id)

            try:
                r = Command.execute('generate_key', vm.user_id, vm.id)
                r = json.loads(r)
                log.info(vm.user_id, 'generated key: %s for machine %d' % (r, vm.id))
                for wn in vm.farm.vms.all():
                    wn.ssh_username = '******'
                    wn.ssh_key = r
                    wn.save()
                    if not wn.is_head():
                        vms.append(wn)
                ssh_username = '******'
                ssh_key = r
                log.debug(vm.user_id, 'appended %d vms to farm [id:%d]' % (vm.farm.vms.count() - 1, vm.id))  # excluding head

                Command.add_command('add_ssh_key', vm.user_id, vm.id, user=ssh_username, ssh_key=ssh_key)
                Command.add_command('update_hosts', vm.user_id, vm.id, hosts_list=vm.farm.hosts(), user=ssh_username)
                Command.execute('set_hostname', vm.user_id, vm.id, hostname=vm.name.replace(vm.farm.name, 'farm'))

            except Exception:
                log.exception(vm.user_id, '')
                vm.farm.state = farm_states['unconfigured']
                message.error(vm.id, 'farm_create', {'id': vm.farm.id, 'name': vm.farm.name})
        log.info(vm.user_id, 'Head %d registered' % vm.id)
        shared = {"counter": len(vms), "lock": threading.Lock()}
        for vm in vms:
            thread = VMThread(vm, 'create', shared)
            thread.start()
            log.debug(vm.user_id, 'vm thread created [vm id:%d]' % vm.id)
Exemplo n.º 5
0
    def reset(self):
        """
        Restarts VM.

        -# Connects to Libvirt.
        -# Sets VM's state as *restart*.
        -# Restarts it.
        -# Sets VM's state back as *running*.
        """
        log.debug(self.vm.user_id, "VM Reboot")

        self.vm.set_state('restart')
        try:
            self.vm.save()
        except:
            log.exception(self.vm.user_id, 'Cannot set vm state')
            return

        try:
            domain = self.vm.lv_domain()
            domain.reset(0)
            self.vm.libvirt_id = domain.ID()
        except:
            self.vm.node.lock()
            message.error(self.vm.user_id, 'vm_restart', {
                'id': self.vm.id,
                'name': self.vm.name
            })
            self.vm.set_state('failed')
            log.exception(self.vm.user_id, 'Cannot restart machine')

            try:
                self.vm.save()
            except:
                log.exception(self.vm.user_id, 'Cannot update libvirt ID')
                return

            return

        # Update state state
        self.vm.set_state('running')
        try:
            self.vm.save()
        except:
            log.exception(self.vm.user_id, 'Cannot set vm state')
            return
Exemplo n.º 6
0
Arquivo: vm.py Projeto: cc1-cloud/cc1
    def reset(self):
        """
        Restarts VM.

        -# Connects to Libvirt.
        -# Sets VM's state as *restart*.
        -# Restarts it.
        -# Sets VM's state back as *running*.
        """
        log.debug(self.vm.user_id, "VM Reboot")

        self.vm.set_state('restart')
        try:
            self.vm.save()
        except:
            log.exception(self.vm.user_id, 'Cannot set vm state')
            return

        try:
            domain = self.vm.lv_domain()
            domain.reset(0)
            self.vm.libvirt_id = domain.ID()
        except:
            self.vm.node.lock()
            message.error(self.vm.user_id, 'vm_restart', {'id': self.vm.id, 'name': self.vm.name})
            self.vm.set_state('failed')
            log.exception(self.vm.user_id, 'Cannot restart machine')

            try:
                self.vm.save()
            except:
                log.exception(self.vm.user_id, 'Cannot update libvirt ID')
                return

            return

        # Update state state
        self.vm.set_state('running')
        try:
            self.vm.save()
        except:
            log.exception(self.vm.user_id, 'Cannot set vm state')
            return
Exemplo n.º 7
0
    def copy_to_node(self, vm):
        """
        Copy vm image from storage to node

        @raises{vm_create,CMException}
        """
        r = subprocess.call(['ssh', vm.node.ssh_string, 'chmod a+rw %s' % (vm.system_image.path)])

        log.debug(vm.user.id, "Copy image by ssh")
        log.debug(vm.user.id, str(['ssh', vm.node.ssh_string, 'cp %s /images/%d' % (vm.system_image.path, vm.id)]))

        if subprocess.call(['ssh', vm.node.ssh_string, 'cp %s /images/%d' % (vm.system_image.path, vm.id)]):
            message.error(vm.user_id, 'vm_create', {'id': vm.id, 'name': vm.name})
            raise CMException('vm_create')

        if subprocess.call(['ssh', vm.node.ssh_string, 'chmod a+rw /images/%d' % vm.id]):
            message.error(vm.user_id, 'vm_create', {'id': vm.id, 'name': vm.name})
            raise CMException('vm_create')
        return
Exemplo n.º 8
0
Arquivo: vm.py Projeto: cc1-cloud/cc1
 def create(self):
     """
     Starts VM's thread.
     -# Gets VM's record from database (basing on vm_id) (if exists).
     -# Copies image chosen for this VM.
     -# Connects to Libvirt and generate template for it.
     -# Creates Libvirt domain.
     -# Sets VM's state as *running*
     -# If VM is element of farm, it sets proper farm state.
     """
     try:
         log.info(self.vm.user_id, "Copy image from %s to %s" % (self.vm.system_image.path, self.vm.path))
         self.vm.system_image.copy_to_node(self.vm)
     except Exception, e:
         log.exception(self.vm.user_id, 'Libvirt error for %d: %s' % (self.vm.id, e))
         self.vm.set_state('failed')
         message.error(self.vm.user_id, 'vm_create', {'id': self.vm.id, 'name': self.vm.name})
         self.vm.node.lock()
         self.vm.save(update_fields=['state'])
         return
Exemplo n.º 9
0
class VMThread(threading.Thread):
    def __init__(self, vm, action, shared=None):
        threading.Thread.__init__(self)
        self.vm = vm
        self.action = action
        self.shared = shared

    def terminate(self):
        log.info(0, "Terminate vm %d" % (self.vm.id))
        return self._Thread__stop()

    def create(self):
        """
        Starts VM's thread.
        -# Gets VM's record from database (basing on vm_id) (if exists).
        -# Copies image chosen for this VM.
        -# Connects to Libvirt and generate template for it.
        -# Creates Libvirt domain.
        -# Sets VM's state as *running*
        -# If VM is element of farm, it sets proper farm state.
        """
        try:
            log.info(
                self.vm.user_id, "Copy image from %s to %s" %
                (self.vm.system_image.path, self.vm.path))
            self.vm.system_image.copy_to_node(self.vm)
        except Exception, e:
            log.exception(self.vm.user_id,
                          'Libvirt error for %d: %s' % (self.vm.id, e))
            self.vm.set_state('failed')
            message.error(self.vm.user_id, 'vm_create', {
                'id': self.vm.id,
                'name': self.vm.name
            })
            self.vm.node.lock()
            self.vm.save(update_fields=['state'])
            return

        #TODO: network part
        log.debug(self.vm.user_id, "Attaching network")
        try:
            for lease in self.vm.lease_set.all():
                lease.attach_node()
        except Exception, e:
            log.exception(self.vm.user_id, "Cannot create network")
            self.vm.set_state('failed')
            self.vm.save(update_fields=['state'])
            message.error(self.vm.user_id, 'vm_create', {
                'id': self.vm.id,
                'name': self.vm.name
            })
            self.vm.node.lock()
            self.vm.node.save()
            return
Exemplo n.º 10
0
    def copy_to_node(self, vm):
        """
        Copy vm image from storage to node

        @raises{vm_create,CMException}
        """
        r = subprocess.call([
            'ssh', vm.node.ssh_string,
            'chmod a+rw %s' % (vm.system_image.path)
        ])

        log.debug(vm.user.id, "Copy image by ssh")
        log.debug(
            vm.user.id,
            str([
                'ssh', vm.node.ssh_string,
                'cp %s /images/%d' % (vm.system_image.path, vm.id)
            ]))

        if subprocess.call([
                'ssh', vm.node.ssh_string,
                'cp %s /images/%d' % (vm.system_image.path, vm.id)
        ]):
            message.error(vm.user_id, 'vm_create', {
                'id': vm.id,
                'name': vm.name
            })
            raise CMException('vm_create')

        if subprocess.call(
            ['ssh', vm.node.ssh_string,
             'chmod a+rw /images/%d' % vm.id]):
            message.error(vm.user_id, 'vm_create', {
                'id': vm.id,
                'name': vm.name
            })
            raise CMException('vm_create')
        return
Exemplo n.º 11
0
Arquivo: vm.py Projeto: cc1-cloud/cc1
            self.vm.set_state('failed')
            self.vm.save(update_fields=['state'])
            message.error(self.vm.user_id, 'vm_create', {'id': self.vm.id, 'name': self.vm.name})
            self.vm.node.lock()
            self.vm.node.save()
            return

        log.info(self.vm.user_id, "Connecting libvirt and generating template")
        try:
            conn = libvirt.open(self.vm.node.conn_string)
            tmpl = self.vm.libvirt_template()
            log.debug(self.vm.user_id, "Create from template: %s" % tmpl)
        except Exception, e:
            log.exception(self.vm.user_id, "Cannot connect to libvirt")
            self.vm.set_state('failed')
            message.error(self.vm.user_id, 'vm_create', {'id': self.vm.id, 'name': self.vm.name})
            self.vm.node.lock()
            self.vm.save(update_fields=['state'])
            return

        log.info(self.vm.user_id, "Creating libvirt domain")
        try:
            domain = conn.createXML(tmpl, 0)
            self.vm.libvirt_id = domain.ID()
            self.vm.save(update_fields=['libvirt_id'])
            log.debug(self.vm.user_id, "New domain id: %d" % domain.ID())
        except Exception, e:
            log.exception(self.vm.user_id, 'Libvirt error: %s' % e)
            self.vm.set_state('failed')
            message.error(self.vm.user_id, 'vm_create', {'id': self.vm.id, 'name': self.vm.name})
            self.vm.node.lock()
Exemplo n.º 12
0
Arquivo: vm.py Projeto: cc1-cloud/cc1
            return

        img = SystemImage.create(name=(self.name + "_autosave" if self.save_vm == 1 else self.name),
                                 description=self.description, user=self.user, platform=self.system_image.platform,
                                 disk_controller=self.system_image.disk_controller,
                                 network_device=self.system_image.network_device,
                                 video_device=self.system_image.video_device)
        img.size = self.system_image.size
        img.save()

        try:
            img.copy_to_storage(self, img)
        except Exception, e:
            self.set_state('saving failed')
            self.node.lock()
            message.error(self.user.id, 'vm_save', {'id': self.id, 'name': self.name})
            try:
                img.delete()
                transaction.commit()
            except Exception, e:
                log.exception(self.user.id, "Cannot commit changes: %s" % e)
            log.exception(self.user.id, "Cannot move image - error code: %s" % e)
            return

        img.state = image_states['ok']
        try:
            img.save()
        except Exception, e:
            log.error(self.user.id, "Cannot commit changes: %s" % e)
            message.error(self.user.id, 'vm_save', {'id': self.id, 'name': self.name})
Exemplo n.º 13
0
    def register_head(vm):
        """
        Head registration process:
        - Creates ssh keys and sets their values for WN;
        - Inserts VMs into the database;
        - Then starts VMThreads which create actual machines.

        Called when registering farms head.

        @parameter{vm,VM} instance of the VM to be registered as head
        """
        log.debug(vm.user_id, "machine %d: registered as head" % vm.id)

        log.debug(
            vm.user_id,
            "creating lock for machine %d in farm %d" % (vm.id, vm.farm_id))
        # skip if farm is already configured - reboot head
        if vm.is_head() == True and vm.farm.state == farm_states['running']:
            return

        vms = []
        if vm.farm.state == farm_states['init_head']:
            vm.farm.state = farm_states['running']
            vm.farm.save()

            log.info(vm.user_id, 'generating ssh keys on head %d' % vm.id)

            try:
                r = Command.execute('generate_key', vm.user_id, vm.id)
                r = json.loads(r)
                log.info(vm.user_id,
                         'generated key: %s for machine %d' % (r, vm.id))
                for wn in vm.farm.vms.all():
                    wn.ssh_username = '******'
                    wn.ssh_key = r
                    wn.save()
                    if not wn.is_head():
                        vms.append(wn)
                ssh_username = '******'
                ssh_key = r
                log.debug(vm.user_id, 'appended %d vms to farm [id:%d]' %
                          (vm.farm.vms.count() - 1, vm.id))  # excluding head

                Command.add_command('add_ssh_key',
                                    vm.user_id,
                                    vm.id,
                                    user=ssh_username,
                                    ssh_key=ssh_key)
                Command.add_command('update_hosts',
                                    vm.user_id,
                                    vm.id,
                                    hosts_list=vm.farm.hosts(),
                                    user=ssh_username)
                Command.execute('set_hostname',
                                vm.user_id,
                                vm.id,
                                hostname=vm.name.replace(vm.farm.name, 'farm'))

            except Exception:
                log.exception(vm.user_id, '')
                vm.farm.state = farm_states['unconfigured']
                message.error(vm.id, 'farm_create', {
                    'id': vm.farm.id,
                    'name': vm.farm.name
                })
        log.info(vm.user_id, 'Head %d registered' % vm.id)
        shared = {"counter": len(vms), "lock": threading.Lock()}
        for vm in vms:
            thread = VMThread(vm, 'create', shared)
            thread.start()
            log.debug(vm.user_id, 'vm thread created [vm id:%d]' % vm.id)
Exemplo n.º 14
0
                'name': self.vm.name
            })
            self.vm.node.lock()
            self.vm.node.save()
            return

        log.info(self.vm.user_id, "Connecting libvirt and generating template")
        try:
            conn = libvirt.open(self.vm.node.conn_string)
            tmpl = self.vm.libvirt_template()
            log.debug(self.vm.user_id, "Create from template: %s" % tmpl)
        except Exception, e:
            log.exception(self.vm.user_id, "Cannot connect to libvirt")
            self.vm.set_state('failed')
            message.error(self.vm.user_id, 'vm_create', {
                'id': self.vm.id,
                'name': self.vm.name
            })
            self.vm.node.lock()
            self.vm.save(update_fields=['state'])
            return

        log.info(self.vm.user_id, "Creating libvirt domain")
        try:
            domain = conn.createXML(tmpl, 0)
            self.vm.libvirt_id = domain.ID()
            self.vm.save(update_fields=['libvirt_id'])
            log.debug(self.vm.user_id, "New domain id: %d" % domain.ID())
        except Exception, e:
            log.exception(self.vm.user_id, 'Libvirt error: %s' % e)
            self.vm.set_state('failed')
            message.error(self.vm.user_id, 'vm_create', {
Exemplo n.º 15
0
            description=self.description,
            user=self.user,
            platform=self.system_image.platform,
            disk_controller=self.system_image.disk_controller,
            network_device=self.system_image.network_device,
            video_device=self.system_image.video_device)
        img.size = self.system_image.size
        img.save()

        try:
            img.copy_to_storage(self, img)
        except Exception, e:
            self.set_state('saving failed')
            self.node.lock()
            message.error(self.user.id, 'vm_save', {
                'id': self.id,
                'name': self.name
            })
            try:
                img.delete()
                transaction.commit()
            except Exception, e:
                log.exception(self.user.id, "Cannot commit changes: %s" % e)
            log.exception(self.user.id,
                          "Cannot move image - error code: %s" % e)
            return

        img.state = image_states['ok']
        try:
            img.save()
        except Exception, e:
            log.error(self.user.id, "Cannot commit changes: %s" % e)