def delete_adv_route(peer_id, adv_route_id): peer = bgp_peers.find_one({'_id': ObjectId(peer_id)}) adv_route = adv_routes.find_one({'_id': ObjectId(adv_route_id)}) try: withdraw_route(peer, adv_route) except ConnectionError: # Exabgp isn't running, but route won't be advertised when it starts flash('%s is queued to be withdrawn from %s.' % (adv_route['prefix'], peer['ip']), 'success') else: flash('%s has been withdrawn from %s.' % (adv_route['prefix'], peer['ip']), 'success') adv_routes.remove({'_id': ObjectId(adv_route_id)}, 1) return redirect(url_for('peer', peer_id=peer_id))
def peer(peer_id): route_form = AdvertiseRoute() peer_form = BGPPeer() peer = bgp_peers.find_one({'_id': ObjectId(peer_id)}) if route_form.validate_on_submit(): # Check if route is already advertised to this peer if adv_routes.find_one({ 'peer': peer['ip'], 'prefix': route_form.prefix.data, 'next-hop': route_form.next_hop.data }): flash( '%s is already being advertised to %s with a next-hop of %s.' % (route_form.prefix.data, peer.ip, route_form.next_hop.data), 'warning') # Create the advertised route object adv_route = { 'prefix': route_form.prefix.data, 'peer': peer['ip'], 'attributes': { 'origin': route_form.origin.data, 'local-preference': route_form.local_pref.data, 'med': route_form.med.data, 'next-hop': route_form.next_hop.data } } adv_routes.insert_one(adv_route) try: announce_route(peer, adv_route) except ConnectionError: #Exabgp isn't running, but route will be advertised when it starts flash( '%s queued to be announced to %s' % (adv_route['prefix'], peer['ip']), 'success') else: flash( '%s has been announced to %s' % (adv_route['prefix'], peer['ip']), 'success') return redirect(url_for('peer', peer_id=peer_id)) if peer_form.validate_on_submit(): bgp_peers.update(peer, {'$set': {'asn': peer_form.asn.data}}) # If there's a change in Peer's enabled state, edit exabgp config if peer['enabled'] != peer_form.enabled.data: print 'enabled changed from %s to %s' % (peer['enabled'], peer_form.enabled.data) bgp_peers.update(peer, {'$set': { 'enabled': peer_form.enabled.data }}) # Rebuild config file and reload ExaBGP config build_config_file(bgp_config.find_one(), list(bgp_peers.find())) try: # Tear down neighbor connection if not peer_form.enabled.data: send_exabgp_command('neighbor %s teardown 4' % peer['ip']) bgp_peers.update_one( {'ip': peer['ip']}, {'$set': { 'state': 'down', 'current_prefixes': [] }}) exabpg_process('reload') except ConnectionError: # Ignore because ExaBGP will re-read config when it starts pass flash('Changes saved', 'success') return redirect(url_for('peer', peer_id=peer_id)) else: advertised_routes = adv_routes.find({'peer': peer['ip']}) peer_form.ip_address.data = peer['ip'] peer_form.asn.data = peer['asn'] try: peer_form.enabled.data = peer['enabled'] except KeyError: peer_form.enabled.data = False return render_template('peer_info.html', peer=peer, route_form=route_form, peer_form=peer_form, advertised_routes=advertised_routes)
def peer(peer_id): route_form = AdvertiseRoute() peer_form = BGPPeer() peer = bgp_peers.find_one({'_id': ObjectId(peer_id)}) if route_form.validate_on_submit(): # Check if route is already advertised to this peer if adv_routes.find_one( {'peer': peer['ip'], 'prefix': route_form.prefix.data, 'next-hop': route_form.next_hop.data}): flash('%s is already being advertised to %s with a next-hop of %s.' % ( route_form.prefix.data, peer.ip, route_form.next_hop.data), 'warning') # Create the advertised route object adv_route = { 'prefix': route_form.prefix.data, 'peer': peer['ip'], 'attributes': { 'origin': route_form.origin.data, 'local-preference': route_form.local_pref.data, 'med': route_form.med.data, 'next-hop': route_form.next_hop.data } } adv_routes.insert_one(adv_route) try: announce_route(peer, adv_route) except ConnectionError: # Exabgp isn't running, but route will be advertised when it starts flash('%s queued to be announced to %s' % (adv_route['prefix'], peer['ip']), 'success') else: flash('%s has been announced to %s' % (adv_route['prefix'], peer['ip']), 'success') return redirect(url_for('peer', peer_id=peer_id)) if peer_form.validate_on_submit(): bgp_peers.update(peer, {'$set': {'asn': peer_form.asn.data}}) # If there's a change in Peer's enabled state, edit exabgp config if peer['enabled'] != peer_form.enabled.data: print 'enabled changed from %s to %s' % (peer['enabled'], peer_form.enabled.data) bgp_peers.update(peer, {'$set': {'enabled': peer_form.enabled.data}}) # Rebuild config file and reload ExaBGP config build_config_file(bgp_config.find_one(), list(bgp_peers.find())) try: # Tear down neighbor connection if not peer_form.enabled.data: send_exabgp_command('neighbor %s teardown 4' % peer['ip']) bgp_peers.update_one({'ip': peer['ip']}, {'$set': {'state': 'down', 'current_prefixes': []}}) exabpg_process('reload') except ConnectionError: # Ignore because ExaBGP will re-read config when it starts pass flash('Changes saved', 'success') return redirect(url_for('peer', peer_id=peer_id)) else: advertised_routes = adv_routes.find({'peer': peer['ip']}) peer_form.ip_address.data = peer['ip'] peer_form.asn.data = peer['asn'] try: peer_form.enabled.data = peer['enabled'] except KeyError: peer_form.enabled.data = False return render_template('peer_info.html', peer=peer, route_form=route_form, peer_form=peer_form, advertised_routes=advertised_routes)