def _create_pool_member(members, pool): """Creates pool members""" for member in members: ip = Ip.get_by_pk(member['ip']['id']) if member['ip'] else None ipv6 = Ipv6.get_by_pk(member['ipv6']['id']) if member['ipv6'] else None identifier = ip.ip_formated if ip else ipv6.ip_formated pool_member = ServerPoolMember() pool_member.server_pool = pool pool_member.ip = ip pool_member.ipv6 = ipv6 pool_member.identifier = identifier pool_member.weight = member['weight'] pool_member.priority = member['priority'] pool_member.port_real = member['port_real'] pool_member.member_status = member['member_status'] pool_member.limit = member['limit'] pool_member.save() # vip with dsrl3 using pool if pool.dscp: mbs = pool_member.get_spm_by_eqpt_id(pool_member.equipment.id) # check all the pools related to this pool vip request to filter # dscp value related_viprequestports = pool.vips[0].viprequestport_set.all() vippools = [ p.viprequestportpool_set.all()[0].server_pool_id for p in related_viprequestports ] sps = ServerPool.objects.filter(serverpoolmember__in=mbs).exclude( id__in=vippools) dscps = [sp.dscp for sp in sps] mb_name = '{}:{}'.format( (ip.ip_formated if ip else ipv6.ip_formated), member['port_real']) if pool.dscp in dscps: raise ValidationAPIException( 'DRSL3 Restriction: Pool Member {} cannot be insert' ' in Pool {}, because already in other pool'.format( mb_name, pool.identifier)) if pool_member.port_real != pool.default_port: raise ValidationAPIException( 'DRSL3 Restriction: Pool Member {} cannot have different' ' port of Pool {}'.format(mb_name, pool.identifier))
def save_server_pool_member(user, pool, list_server_pool_member): list_pool_member = list() old_priorities_list = list() pool_members_to_be_removed = get_pool_members_to_be_removed( list_server_pool_member) remove_pool_members(pool_members_to_be_removed, pool, user) if list_server_pool_member: apply_new_priorities = False for dic in list_server_pool_member: ip_object, ipv6_object = get_ip_objects(dic) pool_member_id = dic['id_pool_member'] if pool_member_id: pool_member = ServerPoolMember.objects.get(id=pool_member_id) old_member_priority = pool_member.priority old_priorities_list.append(old_member_priority) update_pool_member(pool, pool_member, dic, ip_object, ipv6_object, user) if (old_member_priority != pool_member.priority and pool.pool_created): apply_new_priorities = True else: pool_member = ServerPoolMember() update_pool_member(pool, pool_member, dic, ip_object, ipv6_object, user) pool_member.save() old_priorities_list.append(dic['priority']) # execute script to create real if pool already created # commits transaction. Rolls back if script returns error if pool.pool_created: ip_id = ip_object and ip_object.id or ipv6_object and ipv6_object.id deploy_pool_member_config(ip_id, pool.id, dic['port_real'], pool_member, user) list_pool_member.append(pool_member) # Applies new priority in pool - only 1 script run for all members if (apply_new_priorities): apply_priorities(list_pool_member, old_priorities_list, pool, user) return list_pool_member
def save_server_pool_member(user, pool, list_server_pool_member): list_pool_member = list() old_priorities_list = list() pool_members_to_be_removed = get_pool_members_to_be_removed( list_server_pool_member) remove_pool_members(pool_members_to_be_removed, pool, user) if list_server_pool_member: apply_new_priorities = False for dic in list_server_pool_member: ip_object, ipv6_object = get_ip_objects(dic) pool_member_id = dic['id_pool_member'] if pool_member_id: pool_member = ServerPoolMember.objects.get(id=pool_member_id) old_member_priority = pool_member.priority old_priorities_list.append(old_member_priority) update_pool_member( pool, pool_member, dic, ip_object, ipv6_object, user) if(old_member_priority != pool_member.priority and pool.pool_created): apply_new_priorities = True else: pool_member = ServerPoolMember() update_pool_member( pool, pool_member, dic, ip_object, ipv6_object, user) pool_member.save() old_priorities_list.append(dic['priority']) # execute script to create real if pool already created # commits transaction. Rolls back if script returns error if pool.pool_created: ip_id = ip_object and ip_object.id or ipv6_object and ipv6_object.id deploy_pool_member_config( ip_id, pool.id, dic['port_real'], pool_member, user) list_pool_member.append(pool_member) # Applies new priority in pool - only 1 script run for all members if(apply_new_priorities): apply_priorities(list_pool_member, old_priorities_list, pool, user) return list_pool_member
def update_real_pool(request): """ - update real pool in eqpt - update data pool in db """ pools = request.DATA.get("pools", []) load_balance = {} # valid data for save in DB and apply in eqpt ps, sp = valid_to_save_reals_v2(pools) for pool in pools: ids = [p['id'] for p in pool['server_pool_members'] if p['id']] db_members = ServerPoolMember.objects.filter(id__in=ids) db_members_remove = ServerPoolMember.objects.filter(server_pool__id=pool['server_pool']['id']).exclude(id__in=ids) db_members_id = [str(s.id) for s in db_members] pools_members = list() for pool_member in pool['server_pool_members']: if not pool_member['ipv6']: ip = pool_member['ip']['ip_formated'] else: ip = pool_member['ipv6']['ip_formated'] if pool_member['id']: member = db_members[db_members_id.index(str(pool_member['id']))] if not member.ipv6: ip_db = member.ip.ip_formated else: ip_db = member.ipv6.ip_formated if member.port_real == pool_member['port_real'] and ip_db == ip: pools_members.append({ 'id': pool_member['id'], 'ip': ip, 'port': pool_member['port_real'], 'limit': pool_member['limit'], 'priority': pool_member['priority'], 'weight': pool_member['weight'], }) else: pools_members.append({ 'id': None, 'ip': ip_db, 'port': member.port_real, 'remove': 1 }) pools_members.append({ 'id': pool_member['id'], 'ip': ip, 'port': pool_member['port_real'], 'limit': pool_member['limit'], 'priority': pool_member['priority'], 'weight': pool_member['weight'], 'new': 1 }) else: pools_members.append({ 'id': None, 'ip': ip, 'port': pool_member['port_real'], 'limit': pool_member['limit'], 'priority': pool_member['priority'], 'weight': pool_member['weight'], 'new': 1 }) # members to remove for member in db_members_remove: if not member.ipv6: ip_db = member.ip.ip_formated else: ip_db = member.ipv6.ip_formated pools_members.append({ 'id': member.id, 'ip': ip_db, 'port': member.port_real, 'remove': 1 }) # get eqpts associate with pool equips = EquipamentoAmbiente.objects.filter( ambiente__id=pool['server_pool']['environment']['id'], equipamento__tipo_equipamento__tipo_equipamento=u'Balanceador') equipment_list = [e.equipamento for e in equips] if all_equipments_are_in_maintenance(equipment_list): raise AllEquipmentsAreInMaintenanceException() for e in equips: eqpt_id = str(e.equipamento.id) equipment_access = EquipamentoAcesso.search( equipamento=e.equipamento.id, protocolo="https" ).uniqueResult() equipment = Equipamento.get_by_pk(e.equipamento.id) plugin = PluginFactory.factory(equipment) if not load_balance.get(eqpt_id): load_balance[eqpt_id] = { 'plugin': plugin, 'fqdn': equipment_access.fqdn, 'user': equipment_access.user, 'password': equipment_access.password, 'pools': [], } load_balance[eqpt_id]['pools'].append({ 'id': pool['server_pool']['id'], 'nome': pool['server_pool']['identifier'], 'lb_method': pool['server_pool']['lb_method'], 'healthcheck': pool['server_pool']['healthcheck'], 'action': pool['server_pool']['servicedownaction']['name'], 'pools_members': pools_members }) # get ids from pools created names = [sp[p].id for idx, p in enumerate(ps) if sp[p].pool_created] environments = [sp[p].id for idx, p in enumerate(ps) if sp[p].pool_created] # call plugin to change in load balance for lb in load_balance: lbe = [l for l in load_balance[lb]['pools'] if l['id'] in names if l['id'] in environments] if len(lbe) > 0: json = load_balance[lb] json['pools'] = lbe json['plugin'].updatePool(json) # save pool in DB for idx in sp: idx = str(idx) sp[idx].identifier = ps[idx]['server_pool']['identifier'] sp[idx].environment = Ambiente.objects.get( id=ps[idx]['server_pool']['environment']['id']) sp[idx].default_limit = ps[idx]['server_pool']['default_limit'] sp[idx].default_port = ps[idx]['server_pool']['default_port'] sp[idx].lb_method = ps[idx]['server_pool']['lb_method'] sp[idx].servicedownaction = OptionPool.objects.get( id=ps[idx]['server_pool']['servicedownaction']['id']) is_healthcheck_valid(ps[idx]['server_pool']['healthcheck']) ps[idx]['server_pool']['healthcheck'] = ps[idx]['server_pool']['healthcheck'] sp[idx].healthcheck = get_or_create_healthcheck( request.user, ps[idx]['server_pool']['healthcheck']['healthcheck_expect'], ps[idx]['server_pool']['healthcheck']['healthcheck_type'], ps[idx]['server_pool']['healthcheck']['healthcheck_request'], ps[idx]['server_pool']['healthcheck']['destination'], ps[idx]['server_pool']['healthcheck']['identifier']) sp[idx].save() members_id = [p['id'] for p in pool['server_pool_members'] for pool in pools if p['id']] pms = ServerPoolMember.objects.filter(id__in=members_id) pms_delete = ServerPoolMember.objects.exclude(id__in=members_id).filter(server_pool__id__in=[pool['server_pool']['id'] for pool in pools]) members = dict() for pool in pools: for member in pool['server_pool_members']: if member['id']: members[str(member['id'])] = member # update pool members log.info(pools) for pm in pms: if members.get(str(pm.id)): pm.port_real = members.get(str(pm.id))['port_real'] pm.priority = members.get(str(pm.id))['priority'] pm.weight = members.get(str(pm.id))['weight'] pm.limit = members.get(str(pm.id))['limit'] pm.save() # delete pool members for pm in pms_delete: pm.delete() # create new pool members members = [p for p in pool['server_pool_members'] for pool in pools if not p['id']] for member in members: pm = ServerPoolMember() pm.server_pool_id = member['server_pool']['id'] pm.limit = member['limit'] if member['ip']: pm.ip_id = member['ip']['id'] if member['ipv6']: pm.ipv6_id = member['ipv6']['id'] pm.identifier = member['identifier'] pm.weight = member['weight'] pm.priority = member['priority'] pm.port_real = member['port_real'] pm.save() # Save reals # save_server_pool_member(request.user, sp, list_server_pool_member) return {}
def save_server_pool_member(user, sp, list_server_pool_member): list_pool_member = list() old_priorities_list = list() # Remove empty values from list id_pool_member_noempty = [x['id_pool_member'] for x in list_server_pool_member if x['id_pool_member'] != ''] #exclue server pool member del_smp = sp.serverpoolmember_set.exclude(id__in=id_pool_member_noempty) if del_smp: for obj in del_smp: obj.delete(user) #execute script remove real if pool already created #commit transaction after each successful script call if sp.pool_created: command = settings.POOL_REAL_REMOVE % (obj.server_pool_id, obj.ip_id if obj.ip else obj.ipv6_id, obj.port_real) code, _, _ = exec_script(command) if code != 0: raise exceptions.ScriptCreatePoolException() transaction.commit() if list_server_pool_member: apply_new_priorities = False for dic in list_server_pool_member: # ip_object = None ipv6_object = None if len(dic['ip']) <= 15: ip_object = Ip.get_by_pk(dic['id']) else: ipv6_object = Ipv6.get_by_pk(dic['id']) id_pool = sp.id id_ip = ip_object and ip_object.id or ipv6_object and ipv6_object.id port_ip = dic['port_real'] if dic['id_pool_member']: spm = ServerPoolMember.objects.get(id=dic['id_pool_member']) spm.server_pool = sp spm.identifier = dic['nome_equips'] spm.ip = ip_object spm.ipv6 = ipv6_object spm.weight = dic['weight'] spm.limit = sp.default_limit old_spm_priority = spm.priority old_priorities_list.append(old_spm_priority) spm.priority = dic['priority'] spm.port_real = dic['port_real'] spm.save(user) if(old_spm_priority != spm.priority and sp.pool_created): apply_new_priorities = True else: spm = ServerPoolMember(server_pool=sp, identifier=dic['nome_equips'], ip=ip_object, ipv6=ipv6_object, priority=dic['priority'], weight=dic['weight'], limit=sp.default_limit, port_real=dic['port_real']) spm.save(user) old_priorities_list.append(dic['priority']) #execute script to create real if pool already created #commits transaction. Rolls back if script returns error if sp.pool_created: transaction.commit() #def prepare_and_save(self, server_pool, ip, ip_type, priority, weight, port_real, user, commit=False): #spm.prepare_and_save(sp, ip_object, IP_VERSION.IPv4[1], dic['priority'], dic['weight'], dic['port_real'], user, True) command = settings.POOL_REAL_CREATE % (id_pool, id_ip, port_ip) code, _, _ = exec_script(command) if code != 0: spm.delete(user) transaction.commit() raise exceptions.ScriptCreatePoolException() #if sp.healthcheck_id: # spm.healthcheck = sp.healthcheck list_pool_member.append(spm) #Applies new priority in pool - only 1 script run for all members if(apply_new_priorities): transaction.commit() command = settings.POOL_MEMBER_PRIORITIES % (sp.id) code, _, _ = exec_script(command) if code != 0: for i in len(old_priorities_list): list_pool_member[i].priority = old_priorities_list[i] list_pool_member[i].save(user) transaction.commit() raise exceptions.ScriptAlterPriorityPoolMembersException() return list_pool_member