예제 #1
0
    def admin_user(self, req, tenant_id, id):
        """Return admin user name for the specified instance."""
        LOG.info(_("req : '%s'\n\n") % req)
        LOG.info(_("Get admin user name for instance or group '%s'")%id)

        if False==uuid.is_uuid_like(id):
            msg = (_("Wrong id of instance or group."))
            LOG.error(msg)
            raise exception.BadRequest(msg)

        group_id = None
        context = req.environ[wsgi.CONTEXT_KEY]
        try:
            db_info = models.get_db_info(context, id)
            group_id = db_info.group_id
        except exception.NotFound:
            LOG.debug("Not instance id %s"%id)
            pass

        if group_id==None:
            try:
                InstanceGroup.get_by_groupid(context, id)
                group_id = id
            except exception.NotFound:
                msg = (_("Without find instance or group of the id"))
                LOG.error(msg)
                raise exception.BadRequest(msg)    

        group = InstanceGroup.get_by_groupid(context, group_id)
        ret = {'admin_user': {"admin_user": group.admin_name}}
        return wsgi.Result(ret, 202)
예제 #2
0
    def test_create(self):
        db_info = InstanceGroup.create(self.context,self.tenant_id)
        group_id = db_info.id

        db_record = InstanceGroup.get_by_groupid(self.context,group_id)
        print db_record.readonly,db_record.lock
        self.assertTrue(db_record)
예제 #3
0
 def group_update(self, group_id,notify_when_delete=False):
     LOG.debug("Making async send group_info %s to %s" % (group_id,self._get_routing_key()))
     
     item_json = dict()
     try:
         group = InstanceGroup.get_by_groupid(self.context, group_id)
     except exception.ModelNotFoundError:
         if notify_when_delete:
             group = InstanceGroup.get_by_groupid(self.context, group_id,deleted=True)
     tenant_id = group.tenant_id
     
     item_list = InstanceGroupItem.list_by_gid(self.context, group_id)
     for item in item_list:
         _id = item.instance_id
         if self._get_running_instance(_id):
             if item.type ==DBInstanceType.SINGLE or item.type == DBInstanceType.MASTER:
                 db_type = DBInstanceType.MASTER
             else:
                 db_type = item.type
             _data = {'id':item.instance_id,'role':item.type,'group_id':item.group_id}
             if item.type == DBInstanceType.READ_REPLI:
                 _list = item_json.get(item.type,[])
                 _list.append(_data)
                 item_json[db_type] = _list
             else:
                 item_json[db_type] = _data
     if item_json or notify_when_delete:
         ret_json = {"id":group_id}
         ret_json.update({'tenant_id':tenant_id})
         ret_json.update(item_json)
         self._cast("group_update",{"value":ret_json})
         
예제 #4
0
    def delete_async(self):
        LOG.debug("prepare delete instance %s" % self.id)
        deleted_at = utils.utcnow()
        # Delete guest queue.
        _item = InstanceGroupItem.get_by_instance_id(self.context,self.id)
        group_id = _item.group_id

        if _item.type == DBInstanceType.MASTER:
            standby = None
            try:
                standby = InstanceGroupItem.get_by_gid_type(self.context, group_id,DBInstanceType.STANDBY)
            except exception.ModelNotFoundError:
                pass
            if standby is not None:

                standby.delete()
                standby_inst_id = standby.instance_id
                standby_tasks = self.load(self.context,standby_inst_id)
                standby_tasks.update_db(deleted=True, deleted_at=deleted_at,
                                        task_status=InstanceTasks.NONE)
                standby_tasks.set_servicestatus_deleted()
                standby_tasks._delete_instance_config()

        item_list = InstanceGroupItem.list_by_gid(self.context,group_id)
        if len(item_list)==1:
            # Delete associated security group
            if CONF.trove_security_groups_support:
                try:
                    SecurityGroup.delete_for_group(self.group_id,
                                                   self.context)
                except Exception as e:
                    LOG.error(utils.get_traceback_stack())

            LOG.info("send notify to monitor when delete instance %s" % self.id)
            group_rpcapi.API(self.context).group_update(group_id,notify_when_delete=True)

            LOG.info("Delete group %s" % group_id)
            InstanceGroup.delete(self.context,group_id)

        if _item.type in [DBInstanceType.MASTER,DBInstanceType.SINGLE]:
            try:
                LOG.info("Delete autobackup_setting of group_id %s" % group_id)
                AutoBackup.delete(self.context,group_id)
            except:
                LOG.error(utils.get_traceback_stack())


        _item.delete()
        self.update_db(deleted=True, deleted_at=deleted_at,
                       task_status=InstanceTasks.NONE)
        self.set_servicestatus_deleted()
예제 #5
0
    def errorlogs(self, req, tenant_id, id):
        """Return all group's slowquery log path"""
        LOG.info(_("req : '%s'\n\n") % req)
        LOG.info("List group %s slowquery list" % id)
        context = req.environ[wsgi.CONTEXT_KEY]
        search_ops = {}
        search_ops.update(req.GET)
        ids = search_ops.get("ids", None)
        ## For the log path is according to tenant id and group id
        ## In case give invalid/malicious tenant_id/group_id, we must check this
        
        origin_ids = []
        for vid in ids.split(','):
            oid = instance_utils.virtual_instid_2_origin_instid(vid, get_deleted = False)
            origin_ids.append(oid)
              
        new_ids = None      
        if len(origin_ids) > 0:
            new_ids = ",".join(origin_ids)
        
        try:
            group = InstanceGroup.get_by_groupid(context, id)
        except exception.NotFound:
            msg = "The group %s no found" % id
            LOG.error(msg)
            raise exception.BadRequest(msg)

        if group.tenant_id != tenant_id:
            msg = "The tenant %s forbid access group %s" % (tenant_id, id)
            LOG.error(msg)
            raise exception.BadRequest(msg)
        
        relative_path = models.Instance.list_errorlog(context, id, ids=new_ids)
        ret = {"errorlog_path": relative_path}
        return wsgi.Result(ret, 202)
예제 #6
0
    def delete_async(self, fake):
        LOG.debug("prepare delete instance %s, fake: %s " % (self.id, fake))
        modified_group_id = self.group_id
        self._delete_resources(fake)
        # Delete guest queue.
        _item = InstanceGroupItem.get_by_instance_id(self.context, self.id)
        group_id = _item.group_id
        del_instance_type = _item.type

        # if size of item_list equal 1,then we will delete last instance in group
        item_list = InstanceGroupItem.list_by_gid(self.context, modified_group_id)
        if len(item_list) == 1:
            if CONF.trove_security_groups_support:
                if fake is True:
                    LOG.debug("fake is True, %s skip delete secgroup rules", self.group_id)
                else:
                    # Delete associated security group
                    self.update_db(task_status=InstanceTasks.DELETEING_SECURITY_GROUP)
                    try:
                        SecurityGroup.delete_for_group(modified_group_id, self.context)
                    except Exception as e:
                        LOG.error(utils.get_traceback_stack())

        self.set_servicestatus_deleted()

        # zs: configuration is needed for restore deleted instance, DO NOT DELETE!
        # self._delete_instance_config()

        LOG.info("Delete instance_group_item for instance %s" % self.id)
        _type = self.type
        InstanceGroupItem.delete(self.context, self.id)
        deleted_at = utils.utcnow()
        if fake is True and _type == DBInstanceType.MASTER:
            LOG.debug("fake is True, %s is MASTER, set task_status :%s ", self.id, InstanceTasks.FAKE_DELETED)
            self.update_db(deleted=True, deleted_at=deleted_at, task_status=InstanceTasks.FAKE_DELETED)
        else:
            self.update_db(deleted=True, deleted_at=deleted_at, task_status=InstanceTasks.NONE)

        LOG.info("send notify to monitor when delete instance %s" % self.id)
        group_rpcapi.API(self.context).group_update(group_id, notify_when_delete=True)

        if len(item_list) == 1:
            LOG.info("Delete group %s" % group_id)
            InstanceGroup.delete(self.context, group_id)

        self._send_usage_event(self.server, utils.utcnow())
예제 #7
0
 def test_update_readonly_lock(self):
     _group = InstanceGroup.create(self.context,self.tenant_id)
     InstanceGroup.set_readonly(self.context,_group.id,1)
     InstanceGroup.set_lock(self.context,_group.id,1)
     db_record = InstanceGroup.get_by_groupid(self.context,_group.id)
     self.assertEqual(1, db_record.readonly)
     self.assertEqual(1, db_record.lock)
예제 #8
0
    def create(self, req, body, tenant_id):
        LOG.debug("Creating a Security Group Rule for tenant '%s'" % tenant_id)

        context = req.environ[wsgi.CONTEXT_KEY]
        self._validate_create_body(body)

        sec_group_id = body['security_group_rule']['group_id']
        sgia = models.SecurityGroupGroupAssociation
        try:
            inst_group_id = sgia.get_group_id_by_security_group_id(sec_group_id)
            sec_group = models.SecurityGroup.find_by(id=sec_group_id,
                                                 tenant_id=tenant_id,
                                                 deleted=False)
        except exception.NotFound:
            # if cann't get inst_group_id from sec_group_id, then the sec_group_id must be int_group_id
            inst_group_id = sec_group_id
            sec_group = sgia.get_security_group_by_group_id(inst_group_id)

        inst_group = InstanceGroup.get_by_groupid(context, inst_group_id)
        db_port = inst_group.db_port
        from_port = body['security_group_rule'].get('from_port', db_port)
        to_port = body['security_group_rule'].get('to_port', db_port)
        protocol = body['security_group_rule'].get('protocol', CONF.trove_security_group_rule_protocol)
        if protocol not in self.VAILD_PROTOCOL:
            raise exception.SecurityGroupRuleCreationError(
                "The protocol of security_group_rule must in %s" % self.VAILD_PROTOCOL)

        sec_group_rule = models.SecurityGroupRule.create_sec_group_rule(
            sec_group,
            protocol,
            int(from_port), int(to_port),
            #int(db_port),int(db_port),
            body['security_group_rule']['cidr'],
            context)

        resultView = views.SecurityGroupRulesView(sec_group_rule,
                                                  req,
                                                  tenant_id).create()
        return wsgi.Result(resultView, 201)
예제 #9
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'])
예제 #10
0
 def _create_group(self):
     group = InstanceGroup.create(self.context,self.tenant_id)
     return group.id
예제 #11
0
    def backups(self, req, tenant_id, id):
        """Return all backups for the specified instance."""
        LOG.info(_("req : '%s'\n\n") % req)
        LOG.info(_("Indexing backups for instance or group '%s'")%id)

        if False==uuid.is_uuid_like(id):
            msg = (_("Wrong id of instance or group."))
            LOG.error(msg)
            raise exception.BadRequest(msg)

        group_id = None
        context = req.environ[wsgi.CONTEXT_KEY]
        try:
            db_info = models.get_db_info(context, id)
            group_id = db_info.group_id
        except exception.NotFound:
            LOG.debug("Not instance id %s"%id)
            pass

        if group_id==None:
            try:
                InstanceGroup.get_by_groupid(context, id)
                group_id = id
            except exception.NotFound:
                msg = (_("Without find instance or group of the id"))
                LOG.error(msg)
                raise exception.BadRequest(msg)    
        else:
            try:
                item = InstanceGroupItem.get_by_instance_id(context, id)
                if item.type==DBInstanceType.READ_REPLI or item.type==DBInstanceType.STANDBY:
                    msg = (_("Without backups for the instance.")) 
                    LOG.error(msg)
                    raise exception.BadRequest(msg)
            except exception.NotFound:
                msg = (_("Without find instance group item information."))
                LOG.error(msg)
                raise exception.BadRequest(msg)   
 
        backups = backup_model.list_autobackup(context, group_id)
        backups += backup_model.list_snapshot(context, group_id)
        bks = []
        #for backup in backups:
        #    instance = models.FreshInstance.load(context, backup.instance_id)
        #    backup.db_type = instance.service_type
        #    bks.append(backup)
        #TODO(ksc-need-discuss) backup.db_type didn't used in backup view, really need it?
        tmp = dict()
        for backup in backups:
            ## autobackup's service_image_id is NULL
            if backup.service_image_id:
                if backup.service_image_id not in tmp:
                    #service_image = models.ServiceImage.find_by(id=backup.service_image_id)
                    #backup.db_type = service_image.service_name
                    #tmp[backup.service_image_id] = service_image.service_name
                    try:
                        #image_id may be is not exists in datastaore
                        datastore, datastore_version = ds_path_models.find_datastore_by_image_id(backup.service_image_id)
                        tmp[backup.service_image_id] = datastore.name
                        backup.db_type = datastore.name
                    except Exception:
                        backup.db_type = ""
                else:
                    backup.db_type = tmp[backup.service_image_id]
            else:
                backup.db_type = ""
            bks.append(backup)
        backups = bks
        LOG.debug('Getted backups %s'%backups)
        return wsgi.Result(backup_views.BackupViews(backups).data(), 200)
예제 #12
0
 def test_list(self):
     InstanceGroup.create(self.context,self.tenant_id)
     db_list = InstanceGroup.list(self.context)
     self.assertEqual(1, db_list.count())
예제 #13
0
 def test_delete(self):
     db_info = InstanceGroup.create(self.context,self.tenant_id)
     InstanceGroup.delete(self.context,db_info.id)
     self.assertRaises(exception.NotFound,InstanceGroup.get_by_groupid,self.context, db_info.id)