Beispiel #1
0
def config_action(action):

    current_config = bgp_config.find_one()
    peers = list(bgp_peers.find())

    #Rebuild config file before reload action
    if action == 'reload':
        build_config_file(current_config, peers)

    # Send action control to ExaBGP
    result = exabpg_process(action)

    # Update ExaBGP state and last_start
    current_config = bgp_config.find_one()
    if action == 'stop':
        bgp_config.update(
            current_config,
            {'$set': {
                'state': 'stopped',
                'last_stop': datetime.now()
            }})
    elif action == 'restart':
        bgp_config.update(
            current_config, {
                '$set': {
                    'state': 'running',
                    'last_start': datetime.now(),
                    'last_stop': datetime.now()
                }
            })
    elif action == 'start':
        bgp_config.update(
            current_config,
            {'$set': {
                'state': 'running',
                'last_start': datetime.now()
            }})

    return jsonify(result=result), 200
Beispiel #2
0
def config_action(action):
    current_config = bgp_config.find_one()
    peers = list(bgp_peers.find())

    # Rebuild config file before reload action
    if action == 'reload':
        build_config_file(current_config, peers)

    # Send action control to ExaBGP
    result = exabpg_process(action)

    # Update ExaBGP state and last_start
    current_config = bgp_config.find_one()
    if action == 'stop':
        bgp_config.update(current_config, {'$set': {'state': 'stopped', 'last_stop': datetime.now()}})
    elif action == 'restart':
        bgp_config.update(current_config,
                          {'$set': {'state': 'running', 'last_start': datetime.now(), 'last_stop': datetime.now()}})
    elif action == 'start':
        bgp_config.update(current_config, {'$set': {'state': 'running', 'last_start': datetime.now()}})

    return jsonify(result=result), 200
Beispiel #3
0
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)
Beispiel #4
0
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)