Esempio n. 1
0
    def _create(self, req, body, tenant_id, instance_type):
        """return a SimpleInstance"""
        LOG.info(_("Creating database instance for tenant '%s'") % tenant_id)
        LOG.info(_("req : '%s'\n\n") % req)
        LOG.info(_("body : '%s'\n\n") % body)
        
 
        context, name, flavor_id,image_id, databases, users,\
            service_type, volume_size,backup_id, instance_type,\
            extend,master_id,template_config_id = self._get_create_param(req, body, instance_type)
            
        
#             @classmethod
#             def create(cls, context, name, flavor_id, image_id, databases, 
#                        users, service_type, volume_size, backup_id, instance_type, 
#                        extend=None, master_id=None):
        
        instance = pacth_inst_models.KSC_Instance.create(context, name, flavor_id,
                                          image_id, databases, users,
                                          service_type, volume_size,
                                          backup_id, instance_type,
                                          template_config_id,
                                          extend=extend, master_id=master_id)
        
        if instance_type in [InstanceType.HA, InstanceType.SG, InstanceType.RR]:
            vinst_id = utils.generate_uuid()
            InstanceVip.fake_allocate(context, instance.id, attached_virtual_instid = vinst_id)
        
        return instance   
 def test_allocate_vip_with_vip_address(self):
     instance_id = "instance-id"
     vip_address = "vip_address"
     
     instance = fake()
     server =fake()
     server.networks = {"private": [ "10.1.0.13"] }
     instance.server =  server
     when(BuiltInstance).load(any(), any()).thenReturn(instance)
     
     when(instance_vip).create_member(any(),any(),any(),any(),any()).thenReturn('member_id')
     
     vip = fake()
     vip.id = "vip_id"
     vip.lb_id = "lb_id"
     when(models.DBVips).find_by(any(),vip=any(),deleted=any()).thenReturn(vip)
     
     when(models.DBInstanceVip).create(instance_id=any(),rip=any(),rport=any(),vip_id=any(),mid=any()).thenReturn(None)
     
     vip_1= vip_service.allocate(self.context,instance_id,vip_address=vip_address)
     self.assertEqual(vip_address,vip_1)
     
     verify(instance_vip,times=0).create_pool(any(),any())
     verify(instance_vip,times=0).create_vip(any(),any(),any(),any())
     verify(models.DBVips,times=0).create(vip=any(),vport=any(),lb_id =any())
     verify(instance_vip).create_member(any(),any(),any(),any(),any())
     verify(models.DBVips).find_by(any(),vip=any(),deleted=any())
Esempio n. 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)
Esempio n. 4
0
    def attach_vip(cls, context, instance_id, vip):
        """
        1.check vip args
        2.expect not allocated;
        3.get rip,rport
        4.add into args.vip as a member
        :param context:
        :param instance_id:
        :param vip:
        :return:
        """
        instance = BuiltInstance.load(context, instance_id)
        LOG.info("attach_vip instance:%s", instance_id)
        # check vip args
        if not vip:
            msg = "Failed attach_vip to instance:%s, need vip" % instance.id
            LOG.error(msg)
            raise Exception(msg)

        # expect not allocated
        try:
            instance_vip = DBInstanceVip.find_by(context, instance_id=instance.id, deleted=False)
        except Exception as ex:
            pass
        else:
            if instance_vip and instance_vip.vip_id is not None:
                msg = "Failed attach_vip to instance:%s, instance already attached with vip_id:%s." \
                      % (instance.id, instance_vip.vip_id)
                LOG.error(msg)
                raise Exception(msg)

        # try get rip,rport
        try:
            rip, rport = VipManager._get_instance_rip_rport(instance)
        except Exception as ex:
            msg = "Failed attach_vip to instance:%s, exception:%s" % (instance.id, ex)
            LOG.error(msg)
            raise Exception(msg)

        virtual_instance_id = instance_vip.virtual_instance_id
        if virtual_instance_id is None:
            raise Exception("virtual_instance_id not found, instance_id: %s" % instance_id)

        # add into args.vip as a member
        vport = instance.port
        LOG.info("attach_vip, instance:%s, rip:%s, rport:%s, vip:%s, vport:%s", instance.id, rip,
                 rport, vip, vport)
        vip = InstanceVip.allocate(context, instance_id=instance.id, rip=rip, vip_address=vip,
                                   vport=vport, rport=rport, attached_virtual_instid=virtual_instance_id)

        LOG.info("attach_vip ok, instance:%s, vip:%s", instance, vip)
 def test_deallocate(self):
     instance_id = "instance_id"
     
     vip = fake()
     vip.id = "vip_id"
     vip.lb_id = "lb_id"
     vip.mid = "mid"
     def delete():
         pass
     vip.delete = delete
     when(models.DBInstanceVip).find_by(any(),instance_id=any(),deleted=any()).thenReturn(vip)
     when(instance_vip).delete_member(any(),any()).thenReturn(None)
     
     self.assertEqual(None, vip_service.deallocate(self.context, instance_id))
Esempio n. 6
0
    def allocate_vip(cls, context, instance_id):
        """
        1.expect not allocated;
        2.get rip,rport;
        3.allocate vip;
        :param context:
        :param instance_id:
        :return vip:
        """
        instance = BuiltInstance.load(context, instance_id)
        LOG.info("allocate_vip instance:%s", instance_id)

        # expect not allocated
        try:
            instance_vip = DBInstanceVip.find_by(context, instance_id=instance.id, deleted=False)

        except Exception as ex:
            pass
        else:
            if instance_vip and instance_vip.vip_id is not None:
                msg = "Failed allocate_vip to instance:%s, instance already allocated with vip_id:%s." \
                      % (instance.id, instance_vip.vip_id)
                LOG.error(msg)
                raise Exception(msg)

        # try get rip,rport
        try:
            rip, rport = VipManager._get_instance_rip_rport(instance)
        except Exception as ex:
            msg = "Failed allocate_vip to instance:%s, exception:%s" % (instance.id, ex)
            LOG.error(msg)
            raise Exception(msg)

        virtual_instance_id = instance_vip.virtual_instance_id
        if virtual_instance_id is None:
            raise Exception("virtual_instance_id not found, instance_id: %s" % instance_id)

        # allocate
        vport = instance.port
        LOG.info("allocate_vip, instance:%s, rip:%s, rport:%s, vport:%s", instance, rip, rport, vport)
        vip = InstanceVip.allocate(context, instance_id=instance.id, rip=rip, vport=vport, rport=rport,
                                   attached_virtual_instid=virtual_instance_id)

        LOG.info("allocate_vip ok, instance:%s, vip:%s", instance, vip)
        return vip
Esempio n. 7
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
Esempio n. 8
0
    def create_instance(self, flavor, image_id, databases, users,
                        service_type, volume_size, security_groups,
                        backup_id, instance_type, ignore_hosts=None,
                        master_id=None, extend=None):

        if instance_type==DBInstanceType.STANDBY or instance_type==DBInstanceType.READ_REPLI:
            if master_id is None:
                raise Exception("when instance_type is STANDBY or RR, The master_id can't none")

        availability_zone=None
        overrides = {}
        if extend != None:
            availability_zone=extend.get('availability_zone', None)
            ds_version_id = extend.get('datastore_version_id', None)
            overrides = extend.get('overrides', {})

        self.update_db(task_status=InstanceTasks.BUILDING_SERVER)
        try:
            server, volume_info = self._create_server_volume_individually(
                flavor,
                image_id,
                security_groups,
                service_type,
                ignore_hosts, availability_zone)
        except Exception as e:
            self.set_servicestatus(ServiceStatuses.UNKNOWN)
            raise e

        try:
            configuration_id =self.db_info.configuration_id
            LOG.debug("Prepare task  instance id = %s,  configuration id =%s" % (self.id, configuration_id))
            overrides = KSC_Configuration.get_configuration_overrides(self.context, configuration_id)
        except Exception:
            pass

        self.update_db(task_status=InstanceTasks.GUEST_PREPARE)
        LOG.info("======= > groupid %s" % self.group_id)
        group = InstanceGroup.get_by_groupid(self.context, self.group_id)
        sys_variables = {"port" : group.db_port, "read_only" : "OFF" if instance_type in (DBInstanceType.MASTER, DBInstanceType.SINGLE) else "ON"}
        overrides.update(sys_variables)
        config = self._render_config(flavor)
        overrides_config = self._render_override_config(flavor, overrides)

        self._guest_prepare(server, flavor['ram'], volume_info,
                            databases, users, backup_id,
                            config.config_contents,
                            overrides_contents = overrides_config.config_contents)

        try:
            utils.poll_until(self._service_is_active,
                             sleep_time=USAGE_SLEEP_TIME,
                             time_out=USAGE_TIMEOUT)
        except Exception as e:
            self.set_servicestatus(ServiceStatuses.UNKNOWN)
            raise e

        group_item = InstanceGroupItem.get_by_instance_id(self.context, self.id)
        group_id = group_item.group_id

        self.update_db(task_status=InstanceTasks.CONFIG_MYSQL)
        if instance_type==DBInstanceType.STANDBY or instance_type==DBInstanceType.READ_REPLI:
            if instance_type==DBInstanceType.STANDBY:
                self._update_instance_type(master_id, DBInstanceType.MASTER)

            self._update_instance_type(self.id, instance_type)
            self._relocate_master(master_id,self.id,backup_id)

            #when upgrade single to ha
            if instance_type == DBInstanceType.STANDBY:
                master_group_item = InstanceGroupItem.get_by_instance_id(self.context,master_id)
                InstanceGroupItem.update_type(self.context, item_id=master_group_item.id, type=DBInstanceType.MASTER)

        if instance_type==DBInstanceType.SINGLE or instance_type==DBInstanceType.MASTER:
            self._create_master_user(instance_id=self.id, user=extend.get('admin_user'), \
                                     password=extend.get('admin_password'))

            self._update_instance_type(self.id, instance_type)


        if instance_type==DBInstanceType.SINGLE or instance_type==DBInstanceType.MASTER:
            try:
                self.update_db(task_status=InstanceTasks.BACKUPING)
                expiretime = AutoBackup.get_autobackup_expiretime(self.context, group_id)
                backup_name = self._backup_name(instance_id=self.id)


                # Fore. 2014/07/02 Get Service Image ID from datastore version 
                #                 _type = self.service_type
                #                 _image = ServiceImage.find_by(self.context,service_name=_type)
                #ds, ds_version = ds_models.get_datastore_version(type = None, version = ds_version_id)
                #service_image_id = image_id

                desc = 'Init backup for new instance'
                Backup.create(context=self.context, instance=self.id,
                              name=backup_name, description=desc, group_id=group_id,
                              backup_type=BackupType.AUTOBACKUP, expire_at=expiretime,init=True,service_image_id=image_id)
            except Exception as e:
                msg = "Error creating backup for instance %s %s" % (self.id,utils.get_traceback_stack())
                LOG.error(msg)

        self.update_db(task_status=InstanceTasks.SETIOTUNE)
        LOG.debug("Set block iotune for instance %s ." % self.id)

        self._set_blkiotune(flavor)

        if instance_type!=DBInstanceType.STANDBY:
            if CONF.trove_vip_support:
                self.update_db(task_status=InstanceTasks.ALLOCATE_VIP)
                try:
                    rip = self._get_instance_ip(self.id)
                    vip = InstanceVip.allocate(self.context,instance_id=self.id,rip = rip)
                    LOG.debug("Allocated vip %s for instance %s"%(vip, self.id))
                except Exception as e:
                    self.set_servicestatus(ServiceStatuses.UNKNOWN)
                    raise e

        self.update_db(deleted=False,task_status=InstanceTasks.NONE)

        LOG.info("create instance_id:%s,notify monitor,autobackup" % self.id)
        group_rpcapi.API(self.context).group_update(group_id)
        self.send_usage_event('create', instance_size=flavor['ram'])