def undeploy(self, pool_id, user_id): msg = { 'object_type': 'pool', 'action': 'undeploy', 'object_id': pool_id } self.update_state( state='PROGRESS', meta=msg ) pool_obj = facade.get_pool_by_id(pool_id) pool_serializer = serializers.PoolV3Serializer(pool_obj) locks_list = create_lock([pool_id], LOCK_POOL) user = Usuario.objects.get(id=user_id) try: facade_pool_deploy.delete_real_pool([pool_serializer.data], user) except Exception, exception: msg['message'] = 'Pool {} was not undeployed.'.format(pool_obj) msg['reason'] = str(exception) raise Exception(msg)
def redeploy(self, pool_dict, user_id): pool_id = pool_dict.get('id') msg = { 'object_type': 'pool', 'action': 'redeploy', 'object_id': pool_id } self.update_state( state='PROGRESS', meta=msg ) pool_obj = facade.get_pool_by_id(pool_id) locks_list = create_lock([pool_id], LOCK_POOL) user = Usuario.objects.get(id=user_id) try: facade_pool_deploy.update_real_pool([pool_dict], user) except Exception, exception: msg['message'] = 'Pool {} was not redeployed.'.format(pool_obj) msg['reason'] = str(exception) raise Exception(msg)
def undeploy(self, pool_id, user_id): msg = {'object_type': 'pool', 'action': 'undeploy', 'object_id': pool_id} self.update_state(state='PROGRESS', meta=msg) pool_obj = facade.get_pool_by_id(pool_id) pool_serializer = serializers.PoolV3Serializer(pool_obj) locks_list = create_lock([pool_id], LOCK_POOL) user = Usuario.objects.get(id=user_id) try: facade_pool_deploy.delete_real_pool([pool_serializer.data], user) except Exception, exception: msg['message'] = 'Pool {} was not undeployed.'.format(pool_obj) msg['reason'] = str(exception) raise Exception(msg)
def redeploy(self, pool_dict, user_id): pool_id = pool_dict.get('id') msg = {'object_type': 'pool', 'action': 'redeploy', 'object_id': pool_id} self.update_state(state='PROGRESS', meta=msg) pool_obj = facade.get_pool_by_id(pool_id) locks_list = create_lock([pool_id], LOCK_POOL) user = Usuario.objects.get(id=user_id) try: facade_pool_deploy.update_real_pool([pool_dict], user) except Exception, exception: msg['message'] = 'Pool {} was not redeployed.'.format(pool_obj) msg['reason'] = str(exception) raise Exception(msg)
def prepare_apply(load_balance, vip, created=True, user=None): vip_request = copy.deepcopy(vip) id_vip = str(vip_request.get('id')) equips, conf, cluster_unit = _validate_vip_to_apply( vip_request, created, user) cache_group = OptionVip.objects.get( id=vip_request.get('options').get('cache_group')) traffic_return = OptionVip.objects.get( id=vip_request.get('options').get('traffic_return')) timeout = OptionVip.objects.get( id=vip_request.get('options').get('timeout')) persistence = OptionVip.objects.get( id=vip_request.get('options').get('persistence')) if vip_request['ipv4']: ipv4 = Ip.get_by_pk(vip_request['ipv4']) if vip_request[ 'ipv4'] else None vip_request['ipv4'] = { 'id': ipv4.id, 'ip_formated': ipv4.ip_formated } if vip_request['ipv6']: ipv6 = Ipv6.get_by_pk(vip_request['ipv6']) if vip_request[ 'ipv6'] else None vip_request['ipv6'] = { 'id': ipv6.id, 'ip_formated': ipv6.ip_formated } if conf: conf = json.loads(conf) vip_request['options'] = dict() vip_request['options']['cache_group'] = { 'id': cache_group.id, 'nome_opcao_txt': cache_group.nome_opcao_txt } vip_request['options']['traffic_return'] = { 'id': traffic_return.id, 'nome_opcao_txt': traffic_return.nome_opcao_txt } vip_request['options']['timeout'] = { 'id': timeout.id, 'nome_opcao_txt': timeout.nome_opcao_txt } vip_request['options']['persistence'] = { 'id': persistence.id, 'nome_opcao_txt': persistence.nome_opcao_txt } vip_request['options']['cluster_unit'] = cluster_unit try: vip_request['options']['dscp'] = VipRequestDSCP.objects.get( vip_request=vip_request['id'] ).dscp except: vip_request['options']['dscp'] = None pass for idx, port in enumerate(vip_request['ports']): for i, pl in enumerate(port['pools']): pool = get_pool_by_id(pl['server_pool']) pool_serializer = pool_slz.PoolV3Serializer(pool) l7_rule = OptionVip.objects.get( id=pl['l7_rule']).nome_opcao_txt healthcheck = pool_serializer.data['healthcheck'] healthcheck['identifier'] = reserve_name_healthcheck( pool_serializer.data['identifier']) healthcheck['new'] = True vip_request['ports'][idx]['pools'][i]['server_pool'] = { 'id': pool_serializer.data['id'], 'nome': pool_serializer.data['identifier'], 'lb_method': pool_serializer.data['lb_method'], 'healthcheck': healthcheck, 'action': pool_serializer.data['servicedownaction']['name'], 'pool_created': pool_serializer.data['pool_created'], 'pools_members': [{ 'id': pool_member['id'], 'identifier': pool_member['identifier'], 'ip': pool_member['ip']['ip_formated'] if pool_member['ip'] else pool_member['ipv6']['ip_formated'], 'port': pool_member['port_real'], 'member_status': pool_member['member_status'], 'limit': pool_member['limit'], 'priority': pool_member['priority'], 'weight': pool_member['weight'] } for pool_member in pool_serializer.data['server_pool_members']] } vip_request['ports'][idx]['pools'][i]['l7_rule'] = l7_rule l7_protocol = OptionVip.objects.get( id=port['options']['l7_protocol']) l4_protocol = OptionVip.objects.get( id=port['options']['l4_protocol']) vip_request['ports'][idx]['options'] = dict() vip_request['ports'][idx]['options']['l7_protocol'] = { 'id': l7_protocol.id, 'nome_opcao_txt': l7_protocol.nome_opcao_txt } vip_request['ports'][idx]['options']['l4_protocol'] = { 'id': l4_protocol.id, 'nome_opcao_txt': l4_protocol.nome_opcao_txt } vip_request['conf'] = conf if conf: for idx, layer in enumerate(conf['conf']['layers']): requiments = layer.get('requiments') if requiments: # validate for port for idx_port, port in enumerate(vip['ports']): for requiment in requiments: condicionals = requiment.get('condicionals') for condicional in condicionals: validated = True validations = condicional.get('validations') for validation in validations: if validation.get('type') == 'optionvip': validated &= valid_expression( validation.get('operator'), int(vip['options'][ validation.get('variable')]), int(validation.get('value')) ) if validation.get('type') == 'portoptionvip': validated &= valid_expression( validation.get('operator'), int(port['options'][ validation.get('variable')]), int(validation.get('value')) ) if validation.get('type') == 'field' and validation.get('variable') == 'cluster_unit': validated &= valid_expression( validation.get('operator'), cluster_unit, validation.get('value') ) if validated: use = condicional.get('use') for item in use: definitions = item.get('definitions') eqpts = item.get('eqpts') if eqpts: eqpts = Equipamento.objects.filter( id__in=eqpts, maintenance=0, tipo_equipamento__tipo_equipamento=u'Balanceador').distinct() if facade_eqpt.all_equipments_are_in_maintenance(equips): raise exceptions_eqpt.AllEquipmentsAreInMaintenanceException() if user: if not facade_eqpt.all_equipments_can_update_config(equips, user): raise exceptions_eqpt.UserDoesNotHavePermInAllEqptException( 'User does not have permission to update conf in eqpt. \ Verify the permissions of user group with equipment group. Vip:{}'.format( vip_request['id'])) for eqpt in eqpts: eqpt_id = str(eqpt.id) if not load_balance.get(eqpt_id): equipment_access = EquipamentoAcesso.search( equipamento=eqpt.id ) plugin = PluginFactory.factory( eqpt) load_balance[eqpt_id] = { 'plugin': plugin, 'access': equipment_access, 'vips': [], 'layers': {}, } idx_layer = str(idx) idx_port_str = str(port['port']) if not load_balance[eqpt_id]['layers'].get(id_vip): load_balance[eqpt_id][ 'layers'][id_vip] = dict() if load_balance[eqpt_id]['layers'][id_vip].get(idx_layer): if load_balance[eqpt_id]['layers'][id_vip].get(idx_layer).get('definitions').get(idx_port_str): load_balance[eqpt_id]['layers'][id_vip][idx_layer][ 'definitions'][idx_port_str] += definitions else: load_balance[eqpt_id]['layers'][id_vip][idx_layer][ 'definitions'][idx_port_str] = definitions else: load_balance[eqpt_id]['layers'][id_vip][idx_layer] = { 'vip_request': vip_request, 'definitions': { idx_port_str: definitions } } # In first validated==True stops conditionals. # Removing this break will add a wrong # conditional. break for e in equips: eqpt_id = str(e.id) if not load_balance.get(eqpt_id): equipment_access = EquipamentoAcesso.search( equipamento=e.id ) plugin = PluginFactory.factory(e) load_balance[eqpt_id] = { 'plugin': plugin, 'access': equipment_access, 'vips': [], 'layers': {}, } load_balance[eqpt_id]['vips'].append({'vip_request': vip_request}) return load_balance
def prepare_apply(load_balance, vip, created=True, user=None): vip_request = copy.deepcopy(vip) id_vip = str(vip_request.get('id')) equips, conf, cluster_unit = _validate_vip_to_apply( vip_request, created, user) cache_group = OptionVip.objects.get( id=vip_request.get('options').get('cache_group')) traffic_return = OptionVip.objects.get( id=vip_request.get('options').get('traffic_return')) timeout = OptionVip.objects.get( id=vip_request.get('options').get('timeout')) persistence = OptionVip.objects.get( id=vip_request.get('options').get('persistence')) if vip_request['ipv4']: ipv4 = Ip.get_by_pk( vip_request['ipv4']) if vip_request['ipv4'] else None vip_request['ipv4'] = {'id': ipv4.id, 'ip_formated': ipv4.ip_formated} if vip_request['ipv6']: ipv6 = Ipv6.get_by_pk( vip_request['ipv6']) if vip_request['ipv6'] else None vip_request['ipv6'] = {'id': ipv6.id, 'ip_formated': ipv6.ip_formated} if conf: conf = json.loads(conf) vip_request['options'] = dict() vip_request['options']['cache_group'] = { 'id': cache_group.id, 'nome_opcao_txt': cache_group.nome_opcao_txt } vip_request['options']['traffic_return'] = { 'id': traffic_return.id, 'nome_opcao_txt': traffic_return.nome_opcao_txt } vip_request['options']['timeout'] = { 'id': timeout.id, 'nome_opcao_txt': timeout.nome_opcao_txt } vip_request['options']['persistence'] = { 'id': persistence.id, 'nome_opcao_txt': persistence.nome_opcao_txt } vip_request['options']['cluster_unit'] = cluster_unit try: vip_request['options']['dscp'] = VipRequestDSCP.objects.get( vip_request=vip_request['id']).dscp except: vip_request['options']['dscp'] = None pass for idx, port in enumerate(vip_request['ports']): for i, pl in enumerate(port['pools']): pool = get_pool_by_id(pl['server_pool']) pool_serializer = pool_slz.PoolV3Serializer(pool) l7_rule = OptionVip.objects.get(id=pl['l7_rule']).nome_opcao_txt healthcheck = pool_serializer.data['healthcheck'] healthcheck['identifier'] = reserve_name_healthcheck( pool_serializer.data['identifier']) healthcheck['new'] = True vip_request['ports'][idx]['pools'][i]['server_pool'] = { 'id': pool_serializer.data['id'], 'nome': pool_serializer.data['identifier'], 'lb_method': pool_serializer.data['lb_method'], 'healthcheck': healthcheck, 'action': pool_serializer.data['servicedownaction']['name'], 'pool_created': pool_serializer.data['pool_created'], 'pools_members': [{ 'id': pool_member['id'], 'identifier': pool_member['identifier'], 'ip': pool_member['ip']['ip_formated'] if pool_member['ip'] else pool_member['ipv6']['ip_formated'], 'port': pool_member['port_real'], 'member_status': pool_member['member_status'], 'limit': pool_member['limit'], 'priority': pool_member['priority'], 'weight': pool_member['weight'] } for pool_member in pool_serializer.data['server_pool_members'] ] } vip_request['ports'][idx]['pools'][i]['l7_rule'] = l7_rule l7_protocol = OptionVip.objects.get(id=port['options']['l7_protocol']) l4_protocol = OptionVip.objects.get(id=port['options']['l4_protocol']) vip_request['ports'][idx]['options'] = dict() vip_request['ports'][idx]['options']['l7_protocol'] = { 'id': l7_protocol.id, 'nome_opcao_txt': l7_protocol.nome_opcao_txt } vip_request['ports'][idx]['options']['l4_protocol'] = { 'id': l4_protocol.id, 'nome_opcao_txt': l4_protocol.nome_opcao_txt } vip_request['conf'] = conf if conf: for idx, layer in enumerate(conf['conf']['layers']): requiments = layer.get('requiments') if requiments: # validate for port for idx_port, port in enumerate(vip['ports']): for requiment in requiments: condicionals = requiment.get('condicionals') for condicional in condicionals: validated = True validations = condicional.get('validations') for validation in validations: if validation.get('type') == 'optionvip': validated &= valid_expression( validation.get('operator'), int(vip['options'][validation.get( 'variable')]), int(validation.get('value'))) if validation.get('type') == 'portoptionvip': validated &= valid_expression( validation.get('operator'), int(port['options'][validation.get( 'variable')]), int(validation.get('value'))) if validation.get( 'type') == 'field' and validation.get( 'variable') == 'cluster_unit': validated &= valid_expression( validation.get('operator'), cluster_unit, validation.get('value')) if validated: use = condicional.get('use') for item in use: definitions = item.get('definitions') eqpts = item.get('eqpts') if eqpts: eqpts = Equipamento.objects.filter( id__in=eqpts, maintenance=0, tipo_equipamento__tipo_equipamento= u'Balanceador').distinct() if facade_eqpt.all_equipments_are_in_maintenance( equips): raise exceptions_eqpt.AllEquipmentsAreInMaintenanceException( ) if user: if not facade_eqpt.all_equipments_can_update_config( equips, user): raise exceptions_eqpt.UserDoesNotHavePermInAllEqptException( 'User does not have permission to update conf in eqpt. \ Verify the permissions of user group with equipment group. Vip:{}' .format(vip_request['id'])) for eqpt in eqpts: eqpt_id = str(eqpt.id) if not load_balance.get(eqpt_id): equipment_access = EquipamentoAcesso.search( equipamento=eqpt.id) plugin = PluginFactory.factory( eqpt) load_balance[eqpt_id] = { 'plugin': plugin, 'access': equipment_access, 'vips': [], 'layers': {}, } idx_layer = str(idx) idx_port_str = str(port['port']) if not load_balance[eqpt_id][ 'layers'].get(id_vip): load_balance[eqpt_id][ 'layers'][id_vip] = dict() if load_balance[eqpt_id]['layers'][ id_vip].get(idx_layer): if load_balance[eqpt_id][ 'layers'][id_vip].get( idx_layer).get( 'definitions' ).get( idx_port_str): load_balance[eqpt_id][ 'layers'][id_vip][ idx_layer][ 'definitions'][ idx_port_str] += definitions else: load_balance[eqpt_id][ 'layers'][id_vip][ idx_layer][ 'definitions'][ idx_port_str] = definitions else: load_balance[eqpt_id][ 'layers'][id_vip][ idx_layer] = { 'vip_request': vip_request, 'definitions': { idx_port_str: definitions } } # In first validated==True stops conditionals. # Removing this break will add a wrong # conditional. break for e in equips: eqpt_id = str(e.id) if not load_balance.get(eqpt_id): equipment_access = EquipamentoAcesso.search(equipamento=e.id) plugin = PluginFactory.factory(e) load_balance[eqpt_id] = { 'plugin': plugin, 'access': equipment_access, 'vips': [], 'layers': {}, } load_balance[eqpt_id]['vips'].append({'vip_request': vip_request}) return load_balance
def update_real_pool(pools, user): """Update real pool in Load Balancer and DB.""" load_balance = dict() keys = list() for pool in pools: pool_obj = facade_v3.get_pool_by_id(pool['id']) healthcheck_old = serializers.HealthcheckV3Serializer( pool_obj.healthcheck).data db_members = pool_obj.serverpoolmember_set.all() member_ids = [ spm['id'] for spm in pool['server_pool_members'] if spm['id'] ] db_members_remove = list(db_members.exclude(id__in=member_ids)) db_members_id = [str(s.id) for s in db_members] pool_obj.update_v3(pool, user, permit_created=True) pools_members = list() for pool_member in pool['server_pool_members']: ip = pool_member['ip']['ip_formated'] if pool_member[ 'ip'] else pool_member['ipv6']['ip_formated'] if pool_member.get('id', None) is not None: member = db_members[db_members_id.index(str( pool_member['id']))] ip_db = member.ip.ip_formated \ if member.ip else member.ipv6.ip_formated if member.port_real == pool_member['port_real'] \ and ip_db == ip: # update info of member pools_members.append({ 'id': pool_member['id'], 'identifier': member.identifier, 'ip': ip, 'port': pool_member['port_real'], 'limit': pool_member['limit'], 'priority': pool_member['priority'], 'member_status': pool_member['member_status'], 'weight': pool_member['weight'], }) else: # delete member with old port and old ip pools_members.append({ 'id': None, 'identifier': member.identifier, 'ip': ip_db, 'port': member.port_real, 'remove': 1 }) # create member with new port and new ip pools_members.append({ 'id': pool_member['id'], 'identifier': ip, 'ip': ip, 'port': pool_member['port_real'], 'limit': pool_member['limit'], 'priority': pool_member['priority'], 'weight': pool_member['weight'], 'member_status': pool_member['member_status'], 'new': 1 }) else: # create member pools_members.append({ 'id': None, 'identifier': ip, 'ip': ip, 'port': pool_member['port_real'], 'limit': pool_member['limit'], 'priority': pool_member['priority'], 'weight': pool_member['weight'], 'member_status': pool_member['member_status'], 'new': 1 }) # delete members for member in db_members_remove: pools_members.append({ 'id': member.id, 'identifier': member.identifier, 'ip': member.ip.ip_formated if member.ip else member.ipv6.ip_formated, 'port': member.port_real, 'remove': 1 }) # get eqpts associate with pool equips = _validate_pool_to_apply(pool, update=True, user=user) keys.append(sorted([str(eqpt.id) for eqpt in equips])) healthcheck = copy.deepcopy(pool['healthcheck']) healthcheck['new'] = False if json_delta.diff(healthcheck_old, pool['healthcheck']): healthcheck['identifier'] = facade_v3.reserve_name_healthcheck( pool['identifier']) healthcheck['new'] = True for e in equips: eqpt_id = str(e.id) equipment_access = EquipamentoAcesso.search(equipamento=e.id) plugin = PluginFactory.factory(e) if not load_balance.get(eqpt_id): load_balance[eqpt_id] = { 'plugin': plugin, 'access': equipment_access, 'pools': [], } vips_requests = ServerPool().get_vips_related(pool['id']) serializer_vips = serializers_vip.VipRequestV3Serializer( vips_requests, many=True, include=( 'ipv4__details', 'ipv6__details', 'ports__identifier', 'ports__pools__server_pool__basic__lb_method', )) vips = serializer_vips.data load_balance[eqpt_id]['pools'].append({ 'id': pool['id'], 'nome': pool['identifier'], 'lb_method': pool['lb_method'], 'healthcheck': healthcheck, 'action': pool['servicedownaction']['name'], 'vips': vips, 'pools_members': pools_members }) # pools are in differents load balancers keys = [','.join(key) for key in keys] if len(list(set(keys))) > 1: raise Exception('Pools are in differents load balancers') for lb in load_balance: load_balance[lb]['plugin'].update_pool(load_balance[lb]) return {}
def update_real_pool(pools, user): """Update real pool in Load Balancer and DB.""" load_balance = dict() keys = list() for pool in pools: pool_obj = facade_v3.get_pool_by_id(pool['id']) healthcheck_old = serializers.HealthcheckV3Serializer( pool_obj.healthcheck).data db_members = pool_obj.serverpoolmember_set.all() member_ids = [spm['id'] for spm in pool['server_pool_members'] if spm['id']] db_members_remove = list(db_members.exclude(id__in=member_ids)) db_members_id = [str(s.id) for s in db_members] pool_obj.update_v3(pool, user, permit_created=True) pools_members = list() for pool_member in pool['server_pool_members']: ip = pool_member['ip']['ip_formated'] if pool_member[ 'ip'] else pool_member['ipv6']['ip_formated'] if pool_member.get('id', None) is not None: member = db_members[ db_members_id.index(str(pool_member['id']))] ip_db = member.ip.ip_formated \ if member.ip else member.ipv6.ip_formated if member.port_real == pool_member['port_real'] \ and ip_db == ip: # update info of member pools_members.append({ 'id': pool_member['id'], 'identifier': member.identifier, 'ip': ip, 'port': pool_member['port_real'], 'limit': pool_member['limit'], 'priority': pool_member['priority'], 'member_status': pool_member['member_status'], 'weight': pool_member['weight'], }) else: # delete member with old port and old ip pools_members.append({ 'id': None, 'identifier': member.identifier, 'ip': ip_db, 'port': member.port_real, 'remove': 1 }) # create member with new port and new ip pools_members.append({ 'id': pool_member['id'], 'identifier': ip, 'ip': ip, 'port': pool_member['port_real'], 'limit': pool_member['limit'], 'priority': pool_member['priority'], 'weight': pool_member['weight'], 'member_status': pool_member['member_status'], 'new': 1 }) else: # create member pools_members.append({ 'id': None, 'identifier': ip, 'ip': ip, 'port': pool_member['port_real'], 'limit': pool_member['limit'], 'priority': pool_member['priority'], 'weight': pool_member['weight'], 'member_status': pool_member['member_status'], 'new': 1 }) # delete members for member in db_members_remove: pools_members.append({ 'id': member.id, 'identifier': member.identifier, 'ip': member.ip.ip_formated if member.ip else member.ipv6.ip_formated, 'port': member.port_real, 'remove': 1 }) # get eqpts associate with pool equips = _validate_pool_to_apply(pool, update=True, user=user) keys.append(sorted([str(eqpt.id) for eqpt in equips])) healthcheck = copy.deepcopy(pool['healthcheck']) healthcheck['new'] = False if json_delta.diff(healthcheck_old, pool['healthcheck']): healthcheck['identifier'] = facade_v3.reserve_name_healthcheck( pool['identifier']) healthcheck['new'] = True for e in equips: eqpt_id = str(e.id) equipment_access = EquipamentoAcesso.search( equipamento=e.id ) plugin = PluginFactory.factory(e) if not load_balance.get(eqpt_id): load_balance[eqpt_id] = { 'plugin': plugin, 'access': equipment_access, 'pools': [], } vips_requests = ServerPool().get_vips_related(pool['id']) serializer_vips = serializers_vip.VipRequestV3Serializer( vips_requests, many=True, include=( 'ipv4__details', 'ipv6__details', 'ports__identifier', 'ports__pools__server_pool__basic__lb_method', ) ) vips = serializer_vips.data load_balance[eqpt_id]['pools'].append({ 'id': pool['id'], 'nome': pool['identifier'], 'lb_method': pool['lb_method'], 'healthcheck': healthcheck, 'action': pool['servicedownaction']['name'], 'vips': vips, 'pools_members': pools_members }) # pools are in differents load balancers keys = [','.join(key) for key in keys] if len(list(set(keys))) > 1: raise Exception('Pools are in differents load balancers') for lb in load_balance: load_balance[lb]['plugin'].update_pool(load_balance[lb]) return {}