def data(self): """ :return: { "vips": [{ "id": "xxx", "vip": "10.147.0.150", "vport": "3306", "members": [{ "id": "xxx", "rip": "10.142.1.129", "rport": "3306", "mid": "xxx", "instance_id": "xxx", "type": "MASTER" }] }] } """ vips = [] for v in self.vips: members = DBInstanceVip.list_by_vip_id(v.id) if self.type_in_members(members, self.instance_type): vips.append(VipView(self.context, v, members, self.instance_type).data()["vip"]) return {"vips": vips}
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}
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)
def wrapper(*args, **kwargs): callargs = getcallargs(func, *args, **kwargs) argspec = getargspec(func) # print argspec args_list = argspec.args varargs = argspec.varargs varkw = argspec.keywords #print "orgin callargs: %s, args_list: %s, varargs: %s, varkw: %s" % (callargs, args_list, varargs, varkw) found_args = filter(lambda x: x in converted_keys, callargs.keys()) #print "found_args: %s" % (found_args) if found_args != None and len(found_args) > 0: final_args = list(args) for k in found_args: try: if show_deleted: instance_vip = DBInstanceVip.query().filter_by(virtual_instance_id = callargs[k]).\ order_by("created DESC").first() else: instance_vip = DBInstanceVip.get_by(virtual_instance_id = callargs[k], deleted = 0) #print "instance_vip: %s" % (instance_vip) if instance_vip != None: raw_id = instance_vip.instance_id #print "raw_id: %s" % (raw_id) #print "!! k: %s, kwargs.keys: %s, arg_names: %s" % (k, kwargs.keys(), args_list) if kwargs and k in kwargs.keys(): kwargs.pop(k) kwargs[k] = raw_id #print "!! kwarg_name: %s, new_kwarg_value: %s" % (k, kwargs[k]) elif args_list and k in args_list: idx = args_list.index(k) final_args[idx] = raw_id #print "!! arg_name: %s, new_arg_value: %s" % (k, final_args[idx]) except Exception, e: LOG.error(e) continue
def virtual_instid_2_origin_instid(virtual_inst_id, get_deleted = False): raw_id = virtual_inst_id try: instance_vip = DBInstanceVip.get_by(virtual_instance_id = virtual_inst_id, deleted = get_deleted) LOG.info("virtual_instance_id: %s, instnace_vip: %s" % (virtual_inst_id, instance_vip)) if instance_vip != None: raw_id = instance_vip.instance_id except Exception, e: LOG.error(e)
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 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
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
def allocate(cls, context, instance_id,rip,vip_address= None, rport=3306,vport=3306,weight=1,lb_method="ROUND_ROBIN",connection_limit=1000,deleted=False, attached_virtual_instid = None): if not rip: raise GetRipFailed(**locals()) vip = None vip_id = None mid = None pool_id = None if not vip_address: try: rds_tenant_id = CONF.lb_tenant reuqest_tenant_id = context.tenant LOG.debug("default reuqest_tenant_id: %s ", reuqest_tenant_id) private_net = None try: if reuqest_tenant_id is not None: private_net = DBPrivateNet.find_by(context, tenant_id=reuqest_tenant_id, deleted=False) else: LOG.error("reuqest_tenant_id is None, %s " % reuqest_tenant_id) except Exception as ex: LOG.debug("private_net not found! reuqest_tenant_id:%s, ex:%s" % (reuqest_tenant_id, ex)) if private_net: rds_tenant_id = private_net.tenant_id LOG.debug("use private_net, rds_tenant_id: %s ", rds_tenant_id) networks = list_networks(context) LOG.debug("list_networks: %s" % networks) network_list = filter( lambda x: x is not None and x['tenant_id'] == rds_tenant_id \ and x['shared'] is False and x['type'] == 'private', networks['networks']) LOG.debug("filtered networks by tenant_id, rds_tenant_id: %s, network_list: %s" % (rds_tenant_id, network_list)) use_network_type = False if network_list and len(network_list) > 0: subnets = list_subnets(context) LOG.debug("list_subnets: %s" % subnets) network_ids = map(lambda x: x['id'], network_list) LOG.debug("network_ids: %s" % network_ids) subnets_list = filter(lambda x: x['network_id'] in network_ids, subnets['subnets']) LOG.debug("filtered subnets by network_ids, subnets_list: %s, network_ids: %s" % (subnets_list, network_ids)) if subnets_list and len(subnets_list) > 0: for subnet in subnets_list: try: subnet_id = subnet["id"] LOG.debug("create_pool, subnet:%s, subnet_id:%s" % (subnet, subnet_id)) pool_id = create_pool(context, subnet_id=subnet_id) if pool_id: LOG.debug("create_pool finished, subnet:%s, pool_id:%s" % (subnet, pool_id)) break except Exception as ex: LOG.error("create_pool failed! subnet:%s, ex:%s" % (subnet, ex.message)) continue else: use_network_type = True else: use_network_type = True if use_network_type: # to compitable the region did not subed a single network to rds network_type = CONF.lb_subnet_name LOG.warn("filtered networks by tenant_id failed, use network_type: %s," " rds_tenant_id: %s, network_list: %s" % (network_type, rds_tenant_id, network_list)) pool_id = create_pool(context, network_type=network_type) if not pool_id: msg = "create_pool failed! network_list:%s, subnets_list:%s" % (network_list, subnets_list) LOG.error(msg) raise Exception(msg) LOG.debug("pool_id: %s" % pool_id) vip,lb_id = create_vip(context, pool_id, vport, connection_limit) mid = create_member(context,rip, lb_id, instance_id,rport, weight) vip_id = models.DBVips.create(vip=vip,vport=vport,lb_id =lb_id).id except Exception as e: raise Exception(stackinfo()) else: try: vip_info = models.DBVips.find_by(context,vip=vip_address,deleted=deleted) vip_id =vip_info.id lb_id = vip_info.lb_id mid = create_member(context,rip, lb_id, instance_id, rport, weight) vip = vip_address except Exception as e: raise Exception(stackinfo()) if attached_virtual_instid != None: try: inst_vip_dbinfo = DBInstanceVip.find_by(context, instance_id=instance_id, virtual_instance_id = attached_virtual_instid, deleted = 0) inst_vip_dbinfo.rip = rip inst_vip_dbinfo.rport = rport inst_vip_dbinfo.vip_id = vip_id inst_vip_dbinfo.mid = mid inst_vip_dbinfo.save() except Exception, e: models.DBInstanceVip.create(instance_id=instance_id,rip=rip,rport=rport, vip_id=vip_id,mid = mid, virtual_instance_id = attached_virtual_instid)