Example #1
0
    def allow_instance_attach_2_vip(cls, inst_id, vip):
        '''
        
        :param cls:
        :param inst_id:
        :param vip: vip could be the very vip or vip_id 
        '''
        # Relations: VIP 1 ----------- 1,* Instance 
        allow = False
        vip_info = None

        try:
            vip_info = DBVips.get_by(vip = vip, deleted = False)
            if vip_info == None:
                vip_info = DBVips.get_by(id = vip, deleted = False)
        except ModelNotFoundError, e:
            LOG.error(e)
            pass
Example #2
0
    def data(self):
        security_group_id = None
        try:
            association = SecurityGroupGroupAssociation.find_by(group_id=self.instance.db_info.group_id,
                                                                deleted=False)
            security_group_id = association.security_group_id
        except ModelNotFoundError as ex:
            pass
        rport = None
        rip = None
        virtual_instance_id = None
        vip = None
        try:
            instance_vip = DBInstanceVip.find_by(self.context, instance_id=self.instance.id, deleted=False)
            rport = instance_vip.rport
            rip = instance_vip.rip
            virtual_instance_id = instance_vip.virtual_instance_id
            vip_info = DBVips.find_by(self.context, id=instance_vip.vip_id, deleted=False)
            vip = vip_info.vip
        except ModelNotFoundError as ex:
            pass

        instance_dict = {
            "id": self.instance.id,
            "name": self.instance.name,
            #"status": self.instance.userStatus,
            "links": self._build_links(),
            "flavor": self.instance.flavor_id,
            "group_id": self.instance.db_info.group_id,
            #"type": self.instance.type,
            "vip": vip,
            "tenant_id": self.instance.tenant_id,
            "task_status": self.instance.task_status_ext,
            # "server_status": self.instance.db_info.server_status,
            # "service_status": self.instance.service_status_ext,
            "created": self.instance.created,
            "updated": self.instance.updated,
            #"admin_user": self.instance.admin_user,
            "port": rport,
            "db_type": self.instance.service_type,
            "security_group": security_group_id,
            "compute_instance_id": self.instance.db_info.compute_instance_id,
            "configuration_id":self.instance.db_info.configuration_id,
            "addresses": rip,
            "virtual_id" : virtual_instance_id,
            "origin_id" : self.instance.id,
            "deleted_at" : self.instance.db_info.deleted_at,
        }
        if CONF.trove_volume_support:
            instance_dict['volume'] = {'size': self.instance.volume_size}

        if instance_dict.get("virtual_id") != None:
            instance_dict['id'] = instance_dict.get('virtual_id')

        LOG.debug(instance_dict)
        return {"instance": instance_dict}
Example #3
0
    def dettach_vip(cls, context, instance_id):
        """
        1.dettach_vip;
        2.release_vip if the vip not bind any instance;
        :param context:
        :param instance_id:
        :return:
        """
        instance = BuiltInstance.load(context, instance_id)
        LOG.info("dettach_vip instance:%s", instance_id)
        # try dettach_vip
        try:
            instance_vip = DBInstanceVip.find_by(context, instance_id=instance.id, deleted=False)
            vip_id = instance_vip.vip_id
            InstanceVip.deallocate(context, instance.id, purge=False)
        except Exception as e:
            msg = "Failed dettach_vip from instance:%s, exception:%s" % (instance.id, e)
            LOG.error(msg)
            raise Exception(msg)

        # try release_vip if the vip not bind any instance
        try:
            vips = DBVips.find_by(context, id=vip_id, deleted=False)
            vip = vips.vip
        except Exception as e:
            LOG.warn("get vips error, vip_id:%s, exception:%s", vip_id, e)
            return

        try:
            inst_list = DBInstanceVip.find_all(vip_id=vip_id, deleted=False)
            LOG.debug("dettach_vip find_all by vip_id:%s, inst_list:%s", vip_id, inst_list)
            has = False
            for inst in inst_list:
                LOG.debug("inst:%s, vip_id:%s", inst.id, inst.vip_id)
                has = True
            if not has:
                InstanceVip.release_vip(context, vip)
        except Exception as e:
            msg = "Failed release_vip with vip:%s, exception:%s" % (vip, e)
            LOG.error(msg)
            raise Exception(msg)

        LOG.info("dettach_vip ok, instance:%s", instance)
Example #4
0
    def fake_deleted_instance_delete(cls, context, instance_id):
        base_msg = " instance_id: %s " % instance_id
        success = True
        msg = " fake_deleted_instance_delete %s " % base_msg

        deleted_at = utils.utcnow()
        db_info = None
        try:
            db_info = DBInstance.find_by(context=context, id=instance_id, task_id=InstanceTasks.FAKE_DELETED.code,
                                         deleted=True)
            db_info.update(task_status=InstanceTasks.DELETING)
            LOG.debug("fake_deleted_instance_delete, load instance ok, %s " % base_msg)
        except Exception:
            LOG.debug("fake_deleted_instance_delete failed, deleted instance not found, %s " % base_msg)
        if db_info is None:
            success = False
            msg = " fake_deleted_instance_delete failed, load instance error %s " % base_msg
            return success, msg

        try:
            server = load_server(context, db_info.id, db_info.compute_instance_id)
            LOG.debug("fake_deleted_instance_delete, load server: %s ok, %s ", db_info.compute_instance_id, base_msg)
            nova_client = create_nova_client(context)

            def server_is_finished():
                try:
                    server_id = db_info.compute_instance_id
                    _server = nova_client.servers.get(server_id)
                    if _server.status not in ['SHUTDOWN', 'ACTIVE']:
                        _msg = "Server %s got into %s status during delete " \
                               "of instance %s!" % (server.id, server.status, instance_id)
                        LOG.error(_msg)
                    return False
                except nova_exceptions.NotFound:
                    return True

            try:
                LOG.debug("Delete compute server %s" % server.id)
                server.delete()
                poll_until(server_is_finished, sleep_time=1, time_out=CONF.server_delete_time_out)
                guest = create_guest_client(context, db_info.id)
                guest.delete_queue()
                LOG.debug("fake_deleted_instance_delete, delete server: %s ok, %s ", db_info.compute_instance_id,
                          base_msg)
            except Exception as ex:
                LOG.error(utils.get_traceback_stack())
                success = False
                msg += " ,deleted server error, compute_instance_id: %s, ex:%s, %s " \
                       % (db_info.compute_instance_id, str(ex), base_msg)
        except Exception as ex:
            LOG.error("COMPUTE ID = %s" % db_info.compute_instance_id)
            success = False
            msg += " ,load server error, compute_instance_id: %s, %s " % (db_info.compute_instance_id, base_msg)

        if CONF.trove_vip_support:
            try:
                db_info.update(task_status=InstanceTasks.RELEASE_VIP)
                instance_vip = DBInstanceVip.find_by(context, instance_id=instance_id, deleted=False)
                vip_info = DBVips.find_by(context, id=instance_vip.vip_id, deleted=False)
                InstanceVip.release_vip(context, vip_info.vip)
                LOG.debug("fake_deleted_instance_delete, release_vip: %s ok, %s " % (vip_info.vip, base_msg))
            except Exception as ex:
                LOG.error(utils.get_traceback_stack())
                success = False
                msg += " ,release_vip error, ex:%s, %s " % (str(ex), base_msg)

        if CONF.trove_security_groups_support:
            db_info.update(task_status=InstanceTasks.DELETEING_SECURITY_GROUP)
            try:
                SecurityGroup.delete_for_group(db_info.group_id, context)
                LOG.debug(
                    "fake_deleted_instance_delete, delete SecurityGroup: %s ok, %s " % (db_info.group_id, base_msg))
            except Exception as ex:
                LOG.error(utils.get_traceback_stack())
                success = False
                msg += " ,delete SecurityGroup error, ex:%s, %s " % (str(ex), base_msg)

        db_info.update(deleted_at=deleted_at, task_status=InstanceTasks.NONE)

        if success is True:
            msg = "fake_deleted_instance_delete finished, %s " % base_msg

        return success, msg