def config(): config_form = ConfigForm() peer_form = BGPPeer() if config_form.validate_on_submit(): bgp_config.update(bgp_config.find_one(), {'$set': { 'local-as': int(config_form.asn.data), 'router-id': config_form.router_id.data, 'local-address': config_form.local_ip.data } }) flash('Config successfully updated.', 'success') return redirect(url_for('config', _anchor='exabgp')) if peer_form.validate_on_submit(): try: # Create the new peer new_peer = { 'ip': peer_form.ip_address.data, 'asn': int(peer_form.asn.data), 'state': 'down', 'enabled': peer_form.enabled.data } bgp_peers.insert_one(new_peer) except: flash('Error adding peer %s.' % peer_form.ip_address.data, 'warning') else: flash('Peer %s added' % peer_form.ip_address.data, 'success') return redirect(url_for('config', _anchor='peers')) else: peers = list(bgp_peers.find()) config = bgp_config.find_one() prefix_counts = {} for peer in bgp_peers.find(): prefix_counts[peer['ip']] = { 'received': len(bgp_peers.distinct('current_prefixes', {'ip': {'$eq': peer['ip']}})), 'advertised': len(adv_routes.distinct('prefix', {'peer': {'$eq': peer['ip']}})) } config_form.asn.data = config['local-as'] config_form.router_id.data = config['router-id'] config_form.local_ip.data = config['local-address'] return render_template('config.html', peers=peers, config=config, config_form=config_form, peer_form=peer_form, prefix_counts=prefix_counts)
def exabgp_status(): current_config = bgp_config.find_one() # Update database if is_exabgp_running(): bgp_config.update(current_config, {'$set': {'state': 'running'}}) else: bgp_config.update(current_config, {'$set': {'state': 'stopped'}}) return jsonify(state=current_config['state'], last_start=current_config['last_start'], last_stop=current_config['last_stop'])
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
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