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 get_instance_id_bytenant(type): master_id = None _list = group_models.DBInstanceGroupItem.find_all( tenant_id=get_context().tenant, type = type, deleted=False) for _item in _list: _inst = BuiltInstance.load(get_context(), _item.instance_id) if _inst.userStatus == "active": master_id = _inst.id break if master_id is None: raise Exception("not found active %s" % type) return master_id
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 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)