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)
def create(self): inst_id = None try: inst_id = utils.get_instance_id_bytenant(DBInstanceType.MASTER) except Exception as e: print e pass self.inst_id = inst_id if not self.inst_id: self.inst_id = utils.create_rds_byclient("HA") utils.check_server_status(self.inst_id, expected_task=utils.tasks.InstanceTasks.NONE, type=DBInstanceType.MASTER, expected_svr_status=utils.ServiceStatuses.RUNNING, deleted=False, timeout=240) self.vip_id = utils.get_vip_id(self.inst_id) _inst = utils.get_instance(self.inst_id, deleted = False) self.group_id = _inst.group_id origin_id = inst_utils.virtual_instid_2_origin_instid(self.inst_id) assert _inst.id == origin_id, (_inst.id, origin_id) self.dbslave_id = utils.get_instance_id(self.group_id, DBInstanceType.STANDBY) LOG.info('master_id:%s slave_id:%s' % (self.inst_id, self.dbslave_id)) self.validate()
def _create_RRs(self, rr_count = 2, attach_same_vip = False): self.master_id = utils.get_instance_id_bytenant(DBInstanceType.MASTER) utils.check_server_status(self.master_id,expected_task=utils.tasks.InstanceTasks.NONE, type=DBInstanceType.MASTER, expected_svr_status=utils.ServiceStatuses.RUNNING, deleted=False,timeout=10) _inst = utils.get_builtin_instance(self.master_id) self.group_id = _inst.group_id rids = self._get_rids() if len(rids) == 0: vinst_id1 = utils.create_rds_byclient("RR", instance_id = self.master_id, attached_vip=None) self._validate(self.master_id, vinst_id1) origin_inst_id1 = inst_utils.virtual_instid_2_origin_instid(vinst_id1, get_deleted = False) vip_id = utils.get_vip_id(vinst_id1, deleted = False) left_to_create = rr_count - 1 while left_to_create > 0: if attach_same_vip: utils.create_rds_byclient("RR", instance_id = self.master_id, attached_vip=vip_id) else: utils.create_rds_byclient("RR", instance_id = self.master_id, attached_vip=None) left_to_create -= 1 rids = self._get_rids() map(lambda x: self._validate(self.master_id, x), rids) if attach_same_vip: utils.check_lbs_vip(vip_id, rids) else: utils.check_lbs_vip(vip_id, [origin_inst_id1]) else: map(lambda x: self._validate(self.master_id, x), rids)
def create(self, req, body, tenant_id): LOG.debug("Creating a Backup for tenant '%s'" % tenant_id) context = req.environ[wsgi.CONTEXT_KEY] data = body['backup'] instance = data.get('instance',None) group = data.get('group',None) name = data['name'] type = data.get("type", "snapshot") #expire_at = data.get("expire_after", 7) desc = data.get('description') parent_id = data.get('parent_id') LOG.info("parent_id:%s", parent_id) if group is None and instance is None: raise exception.BadRequest("you must specify group or instance") instance_id = None if group is not None: try: instance_id = InstanceGroupItem.get_by_gid_type(context, group, DBInstanceType.STANDBY).instance_id except: instance_id = InstanceGroupItem.get_by_gid_type(context, group, DBInstanceType.SINGLE).instance_id if instance_id is None and instance is not None: instance_id = inst_utils.virtual_instid_2_origin_instid(instance) _instance = DBInstance.find_by(context,id=instance_id) _type = _instance.service_type #_image = ServiceImage.find_by(context,service_name=_type) #service_image_id = _image.id ds,ds_version = ds_models.get_datastore_version(_type) service_image_id = ds_version.image_id grp_item = InstanceGroupItem.get_by_instance_id(context, _instance.id) group_id = grp_item.group_id # get this group's autobackup config and set the expire_after default _autobackup = AutoBackup.get_by_gid(context, group_id) expire_after = data.get("expire_after", _autobackup.expire_after) duration = _autobackup.duration expire_at = AutoBackup.calculate_expire_at(expire_after, duration) LOG.info("group_id %s, expire_at :%s", group_id, time.ctime(expire_at)) if grp_item.type == DBInstanceType.MASTER: try: instance_id = InstanceGroupItem.get_by_gid_type(context, group_id, DBInstanceType.STANDBY).instance_id except Exception as e: LOG.error(e) backup = Backup.create(context, instance_id, name, description=desc,group_id=group_id,backup_type=type,expire_at=expire_at,service_image_id=service_image_id,parent_id=parent_id) try: #service = inst_models.ServiceImage.find_by(id=backup.service_image_id) #backup.db_type = service['service_name'] ds,ds_version = ds_patch_models.find_datastore_by_image_id(backup.service_image_id) backup.db_type = ds.name except Exception as ex: backup.db_type = "" LOG.warn("Failed get db type information of backup %s, %s", backup.id, ex) chain = self._get_chain_ids(context, id) LOG.info(_("chain : '%s'") % chain) return wsgi.Result(views.BackupView(backup).data(), 202)
def _get_create_param(self,req,body,instance_type): context = req.environ[wsgi.CONTEXT_KEY] master_id = None extend = None template_config_id = body['instance'].get('template_config_id',None) if 'extend' in body['instance']: extend = body['instance']['extend'] extend['availability_zone'] = body['instance'].get('availability_zone', None) if instance_type==InstanceType.SG or instance_type==InstanceType.HA: service_type = (body['instance'].get('service_type') or CONF.service_type) #service = models.ServiceImage.find_by(service_name=service_type) #image_id = service['image_id'] datastore, datastore_version = ds_models.get_datastore_version(service_type) image_id = datastore_version.image_id name = body['instance'].get('name', None) flavor_ref = body['instance']['flavorRef'] flavor_id = utils.get_id_from_href(flavor_ref) databases = populate_validated_databases( body['instance'].get('databases', [])) users = None try: users = populate_users(body['instance'].get('users', [])) except ValueError as ve: raise exception.BadRequest(msg=ve) if 'volume' in body['instance']: volume_size = int(body['instance']['volume']['size']) else: volume_size = None if 'restorePoint' in body['instance']: backupRef = body['instance']['restorePoint']['backupRef'] backup_id = utils.get_id_from_href(backupRef) else: backup_id = None if instance_type in [InstanceType.RR, InstanceType.STANDBY]: v_master_id = body['instance'].get('instance_id') master_id = instance_utils.virtual_instid_2_origin_instid(v_master_id, get_deleted = False) master_instance = models.BuiltInstance.load(context, id = master_id) service_type = master_instance.service_type #service = models.ServiceImage.find_by(service_name=service_type) #image_id = service['image_id'] datastore, datastore_version = ds_models.get_datastore_version(service_type) image_id = datastore_version.image_id name = body['instance'].get('name', None) if name is None: name = master_instance.name + "_" + instance_type.lower() #flavor_id = master_instance.flavor_id flavor_ref = body['instance']['flavorRef'] flavor_id = utils.get_id_from_href(flavor_ref) databases = [] users = None volume_size = master_instance.volume_size backup_id = None if "1" == body['instance'].get('config_id_from_master', "0"): template_config_id = master_instance.db_info.configuration_id if template_config_id is None: template_config_id = datastore_version.default_configuration_id return context, name, flavor_id,image_id, databases, users,\ service_type, volume_size,backup_id, instance_type,\ extend,master_id,template_config_id
def failover(self, instance_id=None, _strategy=None): self.inst_id = instance_id if not self.inst_id: self.inst_id = utils.get_instance_id_bytenant(DBInstanceType.MASTER) _inst = utils.get_builtin_instance(self.inst_id) self.group_id = _inst.group_id self.dbslave_id = utils.get_instance_id(self.group_id, DBInstanceType.STANDBY) utils.check_server_status(self.inst_id, expected_task=utils.tasks.InstanceTasks.NONE, type=DBInstanceType.MASTER, expected_svr_status=utils.ServiceStatuses.RUNNING, deleted=False, timeout=120) utils.check_server_status(self.dbslave_id, expected_task=utils.tasks.InstanceTasks.NONE, type=DBInstanceType.STANDBY, expected_svr_status=utils.ServiceStatuses.RUNNING, deleted=False, timeout=120) self.vip_id = utils.get_vip_id(self.inst_id) strategy = CONF.ha_failover_strategy virtual_instance_id = None if strategy == 'master': _ret = utils.get_builtin_instance(self.inst_id) nova_instance = _ret.server instance_id = _ret.id type = DBInstanceType.MASTER virtual_instance_id = _ret.virtual_instance_id elif strategy == 'standby': _ret = utils.get_builtin_instance(self.dbslave_id) nova_instance = _ret.server instance_id = _ret.id type = DBInstanceType.STANDBY else: raise Exception("not found ha_failover_strategy %s" % strategy) rancount = random.randint(50, 100) utils.generate_databases(self.inst_id, count = rancount) ip = utils.check_allocate_ip(nova_instance) utils.stop_mysqld(ip, stop_ga=True) utils.check_server_status(instance_id, expected_task=utils.tasks.InstanceTasks.NONE, type=type, expected_svr_status=utils.ServiceStatuses.SHUTDOWN, deleted=False, timeout=120) rpc.call(utils.get_context(), "taskmanager", {"method": "failover", "args": {'instance_id':instance_id}}) if strategy == 'master': origin_inst_id = inst_utils.virtual_instid_2_origin_instid(virtual_instance_id) self.inst_id = origin_inst_id utils.check_server_status(origin_inst_id, expected_task=utils.tasks.InstanceTasks.NONE, type=DBInstanceType.MASTER, expected_svr_status=utils.ServiceStatuses.RUNNING, deleted=False, timeout=120) new_slave_id = utils.get_instance_id(self.group_id, DBInstanceType.STANDBY) utils.check_server_status(new_slave_id, expected_task=utils.tasks.InstanceTasks.NONE, type=DBInstanceType.STANDBY, expected_svr_status=utils.ServiceStatuses.RUNNING, deleted=False, timeout=120) self.dbslave_id = new_slave_id utils.check_generated_databases(self.inst_id, count = rancount) utils.clear_generated_databases(self.dbslave_id, count = rancount) self.validate()
def migrate(self, instance_id=None, _strategy=None): self.inst_id = instance_id if not self.inst_id: self.inst_id = utils.get_instance_id_bytenant(DBInstanceType.MASTER) _master_inst = utils.get_instance(id = self.inst_id, deleted = 0) self.group_id = _master_inst.group_id self.dbslave_id = utils.get_instance_id(self.group_id, DBInstanceType.STANDBY) utils.check_server_status(self.inst_id, expected_task=utils.tasks.InstanceTasks.NONE, type=DBInstanceType.MASTER, expected_svr_status=utils.ServiceStatuses.RUNNING, deleted=False, timeout=120) utils.check_server_status(self.dbslave_id, expected_task=utils.tasks.InstanceTasks.NONE, type=DBInstanceType.STANDBY, expected_svr_status=utils.ServiceStatuses.RUNNING, deleted=False, timeout=120) self.vip_id = utils.get_vip_id(_master_inst.id) virtual_instance_id = None strategy = CONF.migrate_strategy if strategy == 'master': _ret = utils.get_builtin_instance(_master_inst.id) nova_instance = _ret.server instance_id = _master_inst.id type = DBInstanceType.MASTER virtual_instance_id = _ret.virtual_instance_id elif strategy == 'standby': _ret = utils.get_builtin_instance(self.dbslave_id) nova_instance = _ret.server instance_id = self.dbslave_id type = DBInstanceType.STANDBY else: raise Exception("not found migrate_strategy ss%s" % strategy) ran_count = random.randint(50, 100) utils.generate_databases(self.inst_id, count = ran_count) _ret = rpc.call(utils.get_context(), "taskmanager", {"method": "migrate", "args": {'migrate_id':instance_id}}) if strategy == 'master': raw_instance_id = inst_utils.virtual_instid_2_origin_instid(virtual_instance_id) new_server_id = utils.get_builtin_instance(raw_instance_id).server_id utils.check_server_status(raw_instance_id, expected_task=utils.tasks.InstanceTasks.NONE, type=DBInstanceType.MASTER, expected_svr_status=utils.ServiceStatuses.RUNNING, deleted=False, timeout=120) assert new_server_id != nova_instance.id self.inst_id = raw_instance_id elif strategy == "standby": utils.check_server_status(_ret['id'], expected_task=utils.tasks.InstanceTasks.NONE, type = DBInstanceType.STANDBY, expected_svr_status=utils.ServiceStatuses.RUNNING, deleted=False, timeout = 123) self.dbslave_id = _ret['id'] utils.check_generated_databases(self.inst_id, count = ran_count) utils.check_generated_databases(self.dbslave_id, count = ran_count) utils.clear_generated_databases(self.inst_id, count = ran_count) self.validate()