def setup_exabgp_env():
    # initialize/tweak ExaBGP config and log internals

    from exabgp.configuration.setup import environment
    environment.application = 'bagpipe-bgp'
    env = environment.setup(None)
    # tell exabgp to parse routes:
    env.log.routes = True

    # we "tweak" the internals of exabgp Logger, so that (a) it does not break
    # oslo_log and (b) it logs through oslo_log
    # decorating the original restart would be better...
    exa_logger.Logger._restart = exa_logger.Logger.restart

    def decorated_restart(f):
        @functools.wraps(f)
        def restart_never_first(self, first):
            # we don't want exabgp to really ever do its first restart stuff
            # that resets the root logger handlers
            return f(self, False)
        return restart_never_first

    exa_logger.Logger.restart = decorated_restart(
        exa_logger.Logger.restart
        )

    exa_logger.Logger._syslog = logging.getLogger(__name__ + ".exabgp").logger

    # prevent exabgp Logger code from adding or removing handlers for
    # this logger
    def noop(handler):
        pass

    exa_logger.Logger._syslog.addHandler = noop
    exa_logger.Logger._syslog.removeHandler = noop

    def patched_format(self, message, source, level, timestamp=None):
        if self.short:
            return message
        return "%-13s %s" % (source, message)

    exa_logger.Logger._format = patched_format

    env.log.enable = True

    if LOG.logger.getEffectiveLevel():
        env.log.level = environment.syslog_value(
            python_logging.getLevelName(LOG.logger.getEffectiveLevel())
            )
    else:
        env.log.level = environment.syslog_value('INFO')
    env.log.all = True
    env.log.packets = True

    # monkey patch exabgp to work around exabgp issue #690
    # only relevant for exabgp 4.0.2
    # ( https://github.com/Exa-Networks/exabgp/issues/690 )
    if hasattr(exa_family.Family, 'size'):
        exa_family.Family.size[(exa_family.AFI.l2vpn,
                                exa_family.SAFI.evpn)] = ((4,), 0)
Ejemplo n.º 2
0
def main ():
	options = docopt.docopt(usage, help=False)

	major = int(sys.version[0])
	minor = int(sys.version[2])

	if major != 2 or minor < 5:
		sys.exit('This program can not work (is not tested) with your python version (< 2.5 or >= 3.0)')

	if options["--version"]:
		print 'ExaBGP : %s' % version
		print 'Python : %s' % sys.version.replace('\n',' ')
		print 'Uname  : %s' % platform.version()
		sys.exit(0)

	if options["--folder"]:
		folder = os.path.realpath(os.path.normpath(options["--folder"]))
	elif os.environ.get('ETC',None):
		folder = os.path.join(os.path.realpath(os.path.normpath(os.environ.get('ETC','etc'))),'exabgp')
	elif sys.argv[0].endswith('/bin/exabgp'):
		folder = sys.argv[0][:-len('/bin/exabgp')] + '/etc/exabgp'
	elif sys.argv[0].endswith('/sbin/exabgp'):
		folder = sys.argv[0][:-len('/sbin/exabgp')] + '/etc/exabgp'
	else:
		folder = '/etc/exabgp'

	if not os.environ.get('ETC',''):
		os.environ['ETC'] = folder

	envfile = 'exabgp.env' if not options["--env"] else options["--env"]
	if not envfile.startswith('/'):
		envfile = '%s/%s' % (folder, envfile)

	from exabgp.configuration.setup import environment

	if options["--decode"]:
		decode = ''.join(options["--decode"]).replace(':','').replace(' ','')
		if not is_hex(decode):
			print usage
			print 'Environment values are:\n' + '\n'.join(' - %s' % _ for _ in environment.default())
			print ""
			print "The BGP message must be an hexadecimal string."
			print ""
			print "All colons or spaces are ignored, for example:"
			print ""
			print "  --decode 001E0200000007900F0003000101"
			print "  --decode 001E:02:0000:0007:900F:0003:0001:01"
			print "  --decode FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF001E0200000007900F0003000101"
			print "  --decode FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:001E:02:0000:0007:900F:0003:0001:01"
			print "  --decode 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 001E02 00000007900F0003000101'"
			sys.exit(1)
	else:
		decode = ''

	try:
		env = environment.setup(envfile)
	except environment.Error,exc:
		print usage
		print '\nconfiguration issue,', str(exc)
		sys.exit(1)
Ejemplo n.º 3
0
Archivo: bgp.py Proyecto: xw2060/exabgp
def main ():
	options = docopt.docopt(usage, help=False)

	major = int(sys.version[0])
	minor = int(sys.version[2])

	if major != 2 or minor < 5:
		sys.exit('This program can not work (is not tested) with your python version (< 2.5 or >= 3.0)')

	if options["--version"]:
		print 'ExaBGP : %s' % version
		print 'Python : %s' % sys.version.replace('\n',' ')
		print 'Uname  : %s' % platform.version()
		sys.exit(0)

	if options["--folder"]:
		folder = os.path.realpath(os.path.normpath(options["--folder"]))
	elif os.environ.get('ETC',None):
		folder = os.path.join(os.path.realpath(os.path.normpath(os.environ.get('ETC','etc'))),'exabgp')
	elif sys.argv[0] == '/usr/local/bin/exabgp':
		folder = '/usr/local/etc/exabgp'
	else:
		folder = '/etc/exabgp'

	if not os.environ.get('ETC',''):
		os.environ['ETC'] = folder

	envfile = 'exabgp.env' if not options["--env"] else options["--env"]
	if not envfile.startswith('/'):
		envfile = '%s/%s' % (folder, envfile)

	from exabgp.configuration.setup import environment

	if options["--decode"]:
		decode = ''.join(options["--decode"]).replace(':','').replace(' ','')
		if not is_hex(decode):
			print usage
			print 'Environment values are:\n' + '\n'.join(' - %s' % _ for _ in environment.default())
			print ""
			print "The BGP message must be an hexadecimal string."
			print ""
			print "All colons or spaces are ignored, for example:"
			print ""
			print "  --decode 001E0200000007900F0003000101"
			print "  --decode 001E:02:0000:0007:900F:0003:0001:01"
			print "  --decode FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF001E0200000007900F0003000101"
			print "  --decode FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:001E:02:0000:0007:900F:0003:0001:01"
			print "  --decode 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 001E02 00000007900F0003000101'"
			sys.exit(1)
	else:
		decode = ''

	try:
		env = environment.setup(envfile)
	except environment.Error,exc:
		print usage
		print '\nconfiguration issue,', str(exc)
		sys.exit(1)
Ejemplo n.º 4
0
Archivo: bgp.py Proyecto: aabdnn/exabgp
def get_env (envfile):
	from exabgp.configuration.setup import environment

	try:
		return environment.setup(envfile)
	except environment.Error as exc:
		sys.stdout.write(usage)
		sys.stdout.flush()
		print('\nconfiguration issue,', str(exc))
		sys.exit(1)
Ejemplo n.º 5
0
def get_env(envfile):
    from exabgp.configuration.setup import environment

    try:
        return environment.setup(envfile)
    except environment.Error as exc:
        sys.stdout.write(usage)
        sys.stdout.flush()
        print('\nconfiguration issue,', str(exc))
        sys.exit(1)
Ejemplo n.º 6
0
def main():
    options = docopt.docopt(usage, help=False)

    major = int(sys.version[0])
    minor = int(sys.version[2])

    if major != 2 or minor < 5:
        sys.exit(
            'This program can not work (is not tested) with your python version (< 2.5 or >= 3.0)'
        )

    if options["--version"]:
        print 'ExaBGP : %s' % version
        print 'Python : %s' % sys.version.replace('\n', ' ')
        print 'Uname  : %s' % ' '.join(platform.uname()[:5])
        sys.exit(0)

    if options["--folder"]:
        folder = os.path.realpath(os.path.normpath(options["--folder"]))
    elif sys.argv[0].endswith('/bin/exabgp'):
        folder = sys.argv[0][:-len('/bin/exabgp')] + '/etc/exabgp'
    elif sys.argv[0].endswith('/sbin/exabgp'):
        folder = sys.argv[0][:-len('/sbin/exabgp')] + '/etc/exabgp'
    else:
        folder = '/etc/exabgp'

    os.environ['EXABGP_ETC'] = folder  # This is not most pretty

    if options["--run"]:
        sys.argv = sys.argv[sys.argv.index('--run') + 1:]
        if sys.argv[0] == 'healthcheck':
            from exabgp.application import run_healthcheck
            run_healthcheck()
        elif sys.argv[0] == 'cli':
            from exabgp.application import run_cli
            run_cli()
        else:
            print(usage)
            sys.exit(0)
        return

    envfile = 'exabgp.env' if not options["--env"] else options["--env"]
    if not envfile.startswith('/'):
        envfile = '%s/%s' % (folder, envfile)

    from exabgp.configuration.setup import environment

    try:
        env = environment.setup(envfile)
    except environment.Error, exc:
        print usage
        print '\nconfiguration issue,', str(exc)
        sys.exit(1)
def setup_exabgp_env():
    # initialize/tweak ExaBGP config and log internals

    from exabgp.configuration.setup import environment
    environment.application = 'bagpipe-bgp'
    env = environment.setup(None)
    # tell exabgp to parse routes:
    env.log.routes = True

    # we "tweak" the internals of exabgp Logger, so that (a) it does not break
    # oslo_log and (b) it logs through oslo_log
    # decorating the original restart would be better...
    exa_logger.Logger._restart = exa_logger.Logger.restart

    def decorated_restart(f):
        @six.wraps(f)
        def restart_never_first(self, first):
            # we don't want exabgp to really ever do its first restart stuff
            # that resets the root logger handlers
            return f(self, False)

        return restart_never_first

    exa_logger.Logger.restart = decorated_restart(exa_logger.Logger.restart)

    exa_logger.Logger._syslog = logging.getLogger(__name__ + ".exabgp").logger

    # prevent exabgp Logger code from adding or removing handlers from
    # this logger
    def noop(handler):
        pass

    exa_logger.Logger._syslog.addHandler = noop
    exa_logger.Logger._syslog.removeHandler = noop

    # no need to format all the information twice:
    def patched_format(self, timestamp, level, source, message):
        if self.short:
            return message
        return "%-13s %s" % (source, message)

    exa_logger.Logger._format = patched_format

    env.log.enable = True

    if LOG.logger.getEffectiveLevel():
        env.log.level = environment.syslog_value(
            python_logging.getLevelName(LOG.logger.getEffectiveLevel()))
    else:
        env.log.level = environment.syslog_value('INFO')
    env.log.all = True
    env.log.packets = True
Ejemplo n.º 8
0
def main ():
	options = docopt.docopt(usage, help=False)

	major = int(sys.version[0])
	minor = int(sys.version[2])

	if major != 2 or minor < 5:
		sys.exit('This program can not work (is not tested) with your python version (< 2.5 or >= 3.0)')

	if options["--version"]:
		print 'ExaBGP : %s' % version
		print 'Python : %s' % sys.version.replace('\n',' ')
		print 'Uname  : %s' % ' '.join(platform.uname()[:5])
		sys.exit(0)

	if options["--folder"]:
		folder = os.path.realpath(os.path.normpath(options["--folder"]))
	elif sys.argv[0].endswith('/bin/exabgp'):
		folder = sys.argv[0][:-len('/bin/exabgp')] + '/etc/exabgp'
	elif sys.argv[0].endswith('/sbin/exabgp'):
		folder = sys.argv[0][:-len('/sbin/exabgp')] + '/etc/exabgp'
	else:
		folder = '/etc/exabgp'

	os.environ['EXABGP_ETC'] = folder  # This is not most pretty

	if options["--run"]:
		sys.argv = sys.argv[sys.argv.index('--run')+1:]
		if sys.argv[0] == 'healthcheck':
			from exabgp.application import run_healthcheck
			run_healthcheck()
		elif sys.argv[0] == 'cli':
			from exabgp.application import run_cli
			run_cli()
		else:
			print(usage)
			sys.exit(0)
		return

	envfile = 'exabgp.env' if not options["--env"] else options["--env"]
	if not envfile.startswith('/'):
		envfile = '%s/%s' % (folder, envfile)

	from exabgp.configuration.setup import environment

	try:
		env = environment.setup(envfile)
	except environment.Error,exc:
		print usage
		print '\nconfiguration issue,', str(exc)
		sys.exit(1)
Ejemplo n.º 9
0
def main ():
	options = docopt.docopt(usage, help=False)

	major = int(sys.version[0])
	minor = int(sys.version[2])

	if major != 2 or minor < 5:
		sys.exit('This program can not work (is not tested) with your python version (< 2.5 or >= 3.0)')

	if options["--version"]:
		print 'ExaBGP : %s' % version
		print 'Python : %s' % sys.version.replace('\n',' ')
		print 'Uname  : %s' % platform.version()
		sys.exit(0)

	if options["--folder"]:
		folder = os.path.realpath(os.path.normpath(options["--folder"]))
	# elif os.environ.get('ETC',None):
	# 	folder = os.path.join(os.path.realpath(os.path.normpath(os.environ.get('ETC','etc'))),'exabgp')
	elif sys.argv[0].endswith('/bin/exabgp'):
		folder = sys.argv[0][:-len('/bin/exabgp')] + '/etc/exabgp'
	elif sys.argv[0].endswith('/sbin/exabgp'):
		folder = sys.argv[0][:-len('/sbin/exabgp')] + '/etc/exabgp'
	else:
		folder = '/etc/exabgp'

	# if not os.environ.get('ETC',''):
	# 	os.environ['ETC'] = folder
	os.environ['EXABGP_ETC'] = folder  # This is not most pretty

	envfile = 'exabgp.env' if not options["--env"] else options["--env"]
	if not envfile.startswith('/'):
		envfile = '%s/%s' % (folder, envfile)

	from exabgp.configuration.setup import environment

	try:
		env = environment.setup(envfile)
	except environment.Error,exc:
		print usage
		print '\nconfiguration issue,', str(exc)
		sys.exit(1)
Ejemplo n.º 10
0
def main ():
	options = docopt.docopt(usage, help=False)

	major = int(sys.version[0])
	minor = int(sys.version[2])

	if major != 2 or minor < 5:
		sys.exit('This program can not work (is not tested) with your python version (< 2.5 or >= 3.0)')

	if options["--version"]:
		print 'ExaBGP : %s' % version
		print 'Python : %s' % sys.version.replace('\n',' ')
		print 'Uname  : %s' % platform.version()
		sys.exit(0)

	if options["--folder"]:
		folder = os.path.realpath(os.path.normpath(options["--folder"]))
	elif os.environ.get('ETC',None):
		folder = os.path.join(os.path.realpath(os.path.normpath(os.environ.get('ETC','etc'))),'exabgp')
	elif sys.argv[0].endswith('/bin/exabgp'):
		folder = sys.argv[0][:-len('/bin/exabgp')] + '/etc/exabgp'
	elif sys.argv[0].endswith('/sbin/exabgp'):
		folder = sys.argv[0][:-len('/sbin/exabgp')] + '/etc/exabgp'
	else:
		folder = '/etc/exabgp'

	if not os.environ.get('ETC',''):
		os.environ['ETC'] = folder

	envfile = 'exabgp.env' if not options["--env"] else options["--env"]
	if not envfile.startswith('/'):
		envfile = '%s/%s' % (folder, envfile)

	from exabgp.configuration.setup import environment

	try:
		env = environment.setup(envfile)
	except environment.Error,exc:
		print usage
		print '\nconfiguration issue,', str(exc)
		sys.exit(1)
Ejemplo n.º 11
0
parsing.py

Created by Thomas Mangin on 2009-09-06.
Copyright (c) 2009-2015 Exa Networks. All rights reserved.
"""

import unittest

import os
import glob

from exabgp.configuration.ancient import Configuration
from exabgp.configuration.check import check_neighbor

from exabgp.configuration.setup import environment
env = environment.setup('')
env.log.enable = True
env.log.all = False
env.log.configuration = False
env.log.parser = False


class TestControl(unittest.TestCase):
    def setUp(self):
        location = os.path.abspath(
            os.path.join(os.path.dirname(__file__), '..', 'conf', '*.conf'))
        self.files = glob.glob(location)

    # These files contains invalid attribute we can not parse
    skip = 'attributes.conf'
Ejemplo n.º 12
0
def main ():
	options = docopt.docopt(usage, help=False)

	major = int(sys.version[0])
	minor = int(sys.version[2])

	if major != 2 or minor < 5:
		sys.exit('This program can not work (is not tested) with your python version (< 2.5 or >= 3.0)')

	if options["--version"]:
		print('ExaBGP : %s' % version)
		print('Python : %s' % sys.version.replace('\n',' '))
		print('Uname  : %s' % ' '.join(platform.uname()[:5]))
		sys.exit(0)

	if options["--folder"]:
		folder = os.path.realpath(os.path.normpath(options["--folder"]))
	elif sys.argv[0].endswith('/bin/exabgp'):
		folder = sys.argv[0][:-len('/bin/exabgp')] + '/etc/exabgp'
	elif sys.argv[0].endswith('/sbin/exabgp'):
		folder = sys.argv[0][:-len('/sbin/exabgp')] + '/etc/exabgp'
	else:
		folder = '/etc/exabgp'

	os.environ['EXABGP_ETC'] = folder  # This is not most pretty

	if options["--run"]:
		sys.argv = sys.argv[sys.argv.index('--run')+1:]
		if sys.argv[0] == 'healthcheck':
			from exabgp.application import run_healthcheck
			run_healthcheck()
		elif sys.argv[0] == 'cli':
			from exabgp.application import run_cli
			run_cli()
		else:
			print(usage)
			sys.exit(0)
		return

	envfile = 'exabgp.env' if not options["--env"] else options["--env"]
	if not envfile.startswith('/'):
		envfile = '%s/%s' % (folder, envfile)

	from exabgp.configuration.setup import environment

	try:
		env = environment.setup(envfile)
	except environment.Error as exc:
		print(usage)
		print('\nconfiguration issue,', str(exc))
		sys.exit(1)

	# Must be done before setting the logger as it modify its behaviour

	if options["--debug"]:
		env.log.all = True
		env.log.level = syslog.LOG_DEBUG

	logger = Logger()

	named_pipe = os.environ.get('NAMED_PIPE','')
	if named_pipe:
		from exabgp.application.control import main as control
		control(named_pipe)
		sys.exit(0)

	if options["--decode"]:
		decode = ''.join(options["--decode"]).replace(':','').replace(' ','')
		if not is_bgp(decode):
			print(usage)
			print('Environment values are:\n' + '\n'.join(' - %s' % _ for _ in environment.default()))
			print("")
			print("The BGP message must be an hexadecimal string.")
			print("")
			print("All colons or spaces are ignored, for example:")
			print("")
			print("  --decode 001E0200000007900F0003000101")
			print("  --decode 001E:02:0000:0007:900F:0003:0001:01")
			print("  --decode FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF001E0200000007900F0003000101")
			print("  --decode FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:001E:02:0000:0007:900F:0003:0001:01")
			print("  --decode 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 001E02 00000007900F0003000101'")
			sys.exit(1)
	else:
		decode = ''

	# Make sure our child has a named pipe name
	if env.api.file:
		os.environ['NAMED_PIPE'] = env.api.file

	duration = options["--signal"]
	if duration and duration.isdigit():
		pid = os.fork()
		if pid:
			import time
			import signal
			try:
				time.sleep(int(duration))
				os.kill(pid,signal.SIGUSR1)
			except KeyboardInterrupt:
				pass
			try:
				pid,code = os.wait()
				sys.exit(code)
			except KeyboardInterrupt:
				try:
					pid,code = os.wait()
					sys.exit(code)
				except Exception:
					sys.exit(0)

	if options["--help"]:
		print(usage)
		print('Environment values are:\n' + '\n'.join(' - %s' % _ for _ in environment.default()))
		sys.exit(0)

	if options["--decode"]:
		env.log.parser = True
		env.debug.route = decode
		env.tcp.bind = ''

	if options["--profile"]:
		env.profile.enable = True
		if options["--profile"].lower() in ['1','true']:
			env.profile.file = True
		elif options["--profile"].lower() in ['0','false']:
			env.profile.file = False
		else:
			env.profile.file = options["--profile"]

	if envfile and not os.path.isfile(envfile):
		comment = 'environment file missing\ngenerate it using "exabgp --fi > %s"' % envfile
	else:
		comment = ''

	if options["--full-ini"] or options["--fi"]:
		for line in environment.iter_ini():
			print(line)
		sys.exit(0)

	if options["--full-env"] or options["--fe"]:
		print()
		for line in environment.iter_env():
			print(line)
		sys.exit(0)

	if options["--diff-ini"] or options["--di"]:
		for line in environment.iter_ini(True):
			print(line)
		sys.exit(0)

	if options["--diff-env"] or options["--de"]:
		for line in environment.iter_env(True):
			print(line)
		sys.exit(0)

	if options["--once"]:
		env.tcp.once = True

	if options["--pdb"]:
		# The following may fail on old version of python (but is required for debug.py)
		os.environ['PDB'] = 'true'
		env.debug.pdb = True

	if options["--test"]:
		env.debug.selfcheck = True
		env.log.parser = True

	if options["--memory"]:
		env.debug.memory = True

	configurations = []
	# check the file only once that we have parsed all the command line options and allowed them to run
	if options["<configuration>"]:
		for f in options["<configuration>"]:
			normalised = os.path.realpath(os.path.normpath(f))
			if os.path.isfile(normalised):
				configurations.append(normalised)
				continue
			if f.startswith('etc/exabgp'):
				normalised = os.path.join(folder,f[11:])
				if os.path.isfile(normalised):
					configurations.append(normalised)
					continue

			logger.configuration('one of the arguments passed as configuration is not a file (%s)' % f,'error')
			sys.exit(1)

	else:
		print(usage)
		print('Environment values are:\n' + '\n'.join(' - %s' % _ for _ in environment.default()))
		print('\nno configuration file provided')
		sys.exit(1)

	from exabgp.bgp.message.update.attribute import Attribute
	Attribute.caching = env.cache.attributes

	if env.debug.rotate or len(configurations) == 1:
		run(env,comment,configurations)

	if not (env.log.destination in ('syslog','stdout','stderr') or env.log.destination.startswith('host:')):
		logger.configuration('can not log to files when running multiple configuration (as we fork)','error')
		sys.exit(1)

	try:
		# run each configuration in its own process
		pids = []
		for configuration in configurations:
			pid = os.fork()
			if pid == 0:
				run(env,comment,[configuration],os.getpid())
			else:
				pids.append(pid)

		# If we get a ^C / SIGTERM, ignore just continue waiting for our child process
		import signal
		signal.signal(signal.SIGINT, signal.SIG_IGN)

		# wait for the forked processes
		for pid in pids:
			os.waitpid(pid,0)
	except OSError as exc:
		logger.reactor('Can not fork, errno %d : %s' % (exc.errno,exc.strerror),'critical')
		sys.exit(1)
Ejemplo n.º 13
0
#!/usr/bin/env python
# encoding: utf-8
"""
open.py

Created by Thomas Morin, Orange on 2015-07-10.
Copyright (c) 2009-2015 Orange. All rights reserved.
"""
import unittest

from exabgp.configuration.setup import environment
environment.setup('')

from exabgp.reactor.protocol import AFI, SAFI

from exabgp.bgp.message.update import Attributes

from exabgp.bgp.message.update.attribute.localpref import LocalPreference
from exabgp.bgp.message.update.attribute.community.extended.communities \
    import ExtendedCommunities
from exabgp.bgp.message.update.attribute.community.extended \
    import RouteTargetASN2Number as RouteTarget
from exabgp.bgp.message.update.attribute.community.extended.encapsulation \
    import Encapsulation

from exabgp.bgp.message.update.nlri.mpls import MPLSVPN
from exabgp.bgp.message.update.nlri.evpn.mac import MAC as EVPNMAC
from exabgp.bgp.message.update.nlri.qualifier.rd import RouteDistinguisher
from exabgp.bgp.message.update.nlri.qualifier.labels import Labels
from exabgp.bgp.message.update.nlri.qualifier.esi import ESI
from exabgp.bgp.message.update.nlri.qualifier.etag import EthernetTag
Ejemplo n.º 14
0
Created by Thomas Mangin on 2009-09-06.
Copyright (c) 2009-2015 Exa Networks. All rights reserved.
"""

import unittest

import os
import sys
import glob

from exabgp.configuration.current import Configuration
from exabgp.configuration.check import check_neighbor

from exabgp.configuration.setup import environment
env = environment.setup('')
env.log.enable = True
env.log.all = False
env.log.configuration = False
env.log.parser = False

class TestControl (unittest.TestCase):
	def setUp (self):
		location = os.path.abspath(os.path.join(os.path.dirname(__file__),'..','conf','*.conf'))
		self.files = glob.glob(location)

	# These files contains invalid attribute we can not parse
	skip = 'attributes.conf'

	def test_all_configuration (self):
		for filename in self.files:
Ejemplo n.º 15
0
Archivo: bgp.py Proyecto: qoke/exabgp
def main():
    options = docopt.docopt(usage, help=False)

    major = int(sys.version[0])
    minor = int(sys.version[2])

    if major != 2 or minor < 5:
        sys.exit(
            'This program can not work (is not tested) with your python version (< 2.5 or >= 3.0)'
        )

    if options["--version"]:
        print('ExaBGP : %s' % version)
        print('Python : %s' % sys.version.replace('\n', ' '))
        print('Uname  : %s' % ' '.join(platform.uname()[:5]))
        sys.exit(0)

    if options["--folder"]:
        folder = os.path.realpath(os.path.normpath(options["--folder"]))
    elif sys.argv[0].endswith('/bin/exabgp'):
        folder = sys.argv[0][:-len('/bin/exabgp')] + '/etc/exabgp'
    elif sys.argv[0].endswith('/sbin/exabgp'):
        folder = sys.argv[0][:-len('/sbin/exabgp')] + '/etc/exabgp'
    else:
        folder = '/etc/exabgp'

    os.environ['EXABGP_ETC'] = folder  # This is not most pretty

    if options["--run"]:
        sys.argv = sys.argv[sys.argv.index('--run') + 1:]
        if sys.argv[0] == 'healthcheck':
            from exabgp.application import run_healthcheck
            run_healthcheck()
        elif sys.argv[0] == 'cli':
            from exabgp.application import run_cli
            run_cli()
        else:
            print(usage)
            sys.exit(0)
        return

    envfile = 'exabgp.env' if not options["--env"] else options["--env"]
    if not envfile.startswith('/'):
        envfile = '%s/%s' % (folder, envfile)

    from exabgp.configuration.setup import environment

    try:
        env = environment.setup(envfile)
    except environment.Error as exc:
        print(usage)
        print('\nconfiguration issue,', str(exc))
        sys.exit(1)

    # Must be done before setting the logger as it modify its behaviour

    if options["--debug"]:
        env.log.all = True
        env.log.level = syslog.LOG_DEBUG

    logger = Logger()

    named_pipe = os.environ.get('NAMED_PIPE', '')
    if named_pipe:
        from exabgp.application.control import main as control
        control(named_pipe)
        sys.exit(0)

    if options["--decode"]:
        decode = ''.join(options["--decode"]).replace(':', '').replace(' ', '')
        if not is_bgp(decode):
            print(usage)
            print('Environment values are:\n' +
                  '\n'.join(' - %s' % _ for _ in environment.default()))
            print("")
            print("The BGP message must be an hexadecimal string.")
            print("")
            print("All colons or spaces are ignored, for example:")
            print("")
            print("  --decode 001E0200000007900F0003000101")
            print("  --decode 001E:02:0000:0007:900F:0003:0001:01")
            print(
                "  --decode FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF001E0200000007900F0003000101"
            )
            print(
                "  --decode FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:001E:02:0000:0007:900F:0003:0001:01"
            )
            print(
                "  --decode 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 001E02 00000007900F0003000101'"
            )
            sys.exit(1)
    else:
        decode = ''

    # Make sure our child has a named pipe name
    if env.api.file:
        os.environ['NAMED_PIPE'] = env.api.file

    duration = options["--signal"]
    if duration and duration.isdigit():
        pid = os.fork()
        if pid:
            import time
            import signal
            try:
                time.sleep(int(duration))
                os.kill(pid, signal.SIGUSR1)
            except KeyboardInterrupt:
                pass
            try:
                pid, code = os.wait()
                sys.exit(code)
            except KeyboardInterrupt:
                try:
                    pid, code = os.wait()
                    sys.exit(code)
                except Exception:
                    sys.exit(0)

    if options["--help"]:
        print(usage)
        print('Environment values are:\n' +
              '\n'.join(' - %s' % _ for _ in environment.default()))
        sys.exit(0)

    if options["--decode"]:
        env.log.parser = True
        env.debug.route = decode
        env.tcp.bind = ''

    if options["--profile"]:
        env.profile.enable = True
        if options["--profile"].lower() in ['1', 'true']:
            env.profile.file = True
        elif options["--profile"].lower() in ['0', 'false']:
            env.profile.file = False
        else:
            env.profile.file = options["--profile"]

    if envfile and not os.path.isfile(envfile):
        comment = 'environment file missing\ngenerate it using "exabgp --fi > %s"' % envfile
    else:
        comment = ''

    if options["--full-ini"] or options["--fi"]:
        for line in environment.iter_ini():
            print(line)
        sys.exit(0)

    if options["--full-env"] or options["--fe"]:
        print()
        for line in environment.iter_env():
            print(line)
        sys.exit(0)

    if options["--diff-ini"] or options["--di"]:
        for line in environment.iter_ini(True):
            print(line)
        sys.exit(0)

    if options["--diff-env"] or options["--de"]:
        for line in environment.iter_env(True):
            print(line)
        sys.exit(0)

    if options["--once"]:
        env.tcp.once = True

    if options["--pdb"]:
        # The following may fail on old version of python (but is required for debug.py)
        os.environ['PDB'] = 'true'
        env.debug.pdb = True

    if options["--test"]:
        env.debug.selfcheck = True
        env.log.parser = True

    if options["--memory"]:
        env.debug.memory = True

    configurations = []
    # check the file only once that we have parsed all the command line options and allowed them to run
    if options["<configuration>"]:
        for f in options["<configuration>"]:
            normalised = os.path.realpath(os.path.normpath(f))
            if os.path.isfile(normalised):
                configurations.append(normalised)
                continue
            if f.startswith('etc/exabgp'):
                normalised = os.path.join(folder, f[11:])
                if os.path.isfile(normalised):
                    configurations.append(normalised)
                    continue

            logger.configuration(
                'one of the arguments passed as configuration is not a file (%s)'
                % f, 'error')
            sys.exit(1)

    else:
        print(usage)
        print('Environment values are:\n' +
              '\n'.join(' - %s' % _ for _ in environment.default()))
        print('\nno configuration file provided')
        sys.exit(1)

    from exabgp.bgp.message.update.attribute import Attribute
    Attribute.caching = env.cache.attributes

    if env.debug.rotate or len(configurations) == 1:
        run(env, comment, configurations)

    if not (env.log.destination in ('syslog', 'stdout', 'stderr')
            or env.log.destination.startswith('host:')):
        logger.configuration(
            'can not log to files when running multiple configuration (as we fork)',
            'error')
        sys.exit(1)

    try:
        # run each configuration in its own process
        pids = []
        for configuration in configurations:
            pid = os.fork()
            if pid == 0:
                run(env, comment, [configuration], os.getpid())
            else:
                pids.append(pid)

        # If we get a ^C / SIGTERM, ignore just continue waiting for our child process
        import signal
        signal.signal(signal.SIGINT, signal.SIG_IGN)

        # wait for the forked processes
        for pid in pids:
            os.waitpid(pid, 0)
    except OSError as exc:
        logger.reactor(
            'Can not fork, errno %d : %s' % (exc.errno, exc.strerror),
            'critical')
        sys.exit(1)
Ejemplo n.º 16
0
"""

import os
import sys
import time
import signal
import thread
import subprocess
from collections import deque

from exabgp.reactor.api.transcoder import Transcoder
from exabgp.reactor.api.processes import preexec_helper

from exabgp.configuration.setup import environment

environment.setup('')

# test = """\
# { "exabgp": "3.5.0", "time": 1430238962.74, "host" : "mangin.local", "pid" : 37912, "ppid" : 37903, "type": "state", "neighbor": { "address": { "local": "82.219.212.34", "peer": "127.0.0.1" }, "asn": { "local": "65534", "peer": "65533" }, "state": "down", "reason": "out loop, peer reset, message [closing connection] error[the TCP connection was closed by the remote end]"} }
# { "exabgp": "3.5.0", "time": 1430238928.75, "host" : "mangin.local", "pid" : 37912, "ppid" : 37903, "type": "state", "neighbor": { "address": { "local": "82.219.212.34", "peer": "127.0.0.1" }, "asn": { "local": "65534", "peer": "65533" }, "state": "up"} }
# { "exabgp": "3.5.0", "time": 1430293452.31, "host" : "mangin.local", "pid" : 57788, "ppid" : 57779, "type": "open", "neighbor": { "address": { "local": "172.20.10.6", "peer": "127.0.0.1" }, "asn": { "local": "65534", "peer": "65533" }, "direction": "send", "message": { "category": 1, "header": "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00AF01", "body": "04FFFE00B4800000009202060104000100010206010400010002020601040001000402060104000100800206010400010085020601040001008602060104000200010206010400020080020601040002008502060104000200860206010400190041020641040000FFFE0230402E84B00001018000010280000104800001808000018580000186800002018000028080000285800002868000194180" } } }
# { "exabgp": "3.5.0", "time": 1430293452.32, "host" : "mangin.local", "pid" : 57788, "ppid" : 57779, "type": "open", "neighbor": { "address": { "local": "172.20.10.6", "peer": "127.0.0.1" }, "asn": { "local": "65534", "peer": "65533" }, "direction": "receive", "message": { "category": 1, "header": "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00AF01", "body": "04FFFD00B47F0000009202060104000100010206010400010002020601040001000402060104000100800206010400010085020601040001008602060104000200010206010400020080020601040002008502060104000200860206010400190041020641040000FFFD0230402E84B00001018000010280000104800001808000018580000186800002018000028080000285800002868000194180" } } }
# { "exabgp": "3.5.0", "time": 1430238928.75, "host" : "mangin.local", "pid" : 37912, "ppid" : 37903, "type": "update", "neighbor": { "address": { "local": "82.219.212.34", "peer": "127.0.0.1" }, "asn": { "local": "65534", "peer": "65533" }, "direction": "receive", "message": { "category": 2, "header": "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF005002", "body": "0000002740010100400212020400000001000000020000000300000004400304010101018004040000006408630858084D08420837082C08210816080B" } } }
# { "exabgp": "3.5.0", "time": 1430238928.76, "host" : "mangin.local", "pid" : 37912, "ppid" : 37903, "type": "update", "neighbor": { "address": { "local": "82.219.212.34", "peer": "127.0.0.1" }, "asn": { "local": "65534", "peer": "65533" }, "direction": "receive", "message": { "category": 2, "header": "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF001E02", "body": "00000007900F0003001941" } } }
# { "exabgp": "3.5.0", "time": 1430238928.76, "host" : "mangin.local", "pid" : 37912, "ppid" : 37903, "type": "update", "neighbor": { "address": { "local": "82.219.212.34", "peer": "127.0.0.1" }, "asn": { "local": "65534", "peer": "65533" }, "direction": "receive", "message": { "category": 2, "header": "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF001E02", "body": "00000007900F0003000286" } } }
# { "exabgp": "3.5.0", "time": 1430238928.76, "host" : "mangin.local", "pid" : 37912, "ppid" : 37903, "type": "update", "neighbor": { "address": { "local": "82.219.212.34", "peer": "127.0.0.1" }, "asn": { "local": "65534", "peer": "65533" }, "direction": "receive", "message": { "category": 2, "header": "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF001E02", "body": "00000007900F0003000285" } } }
# """.split('\n')


# XXX: We do not accept input from the forked application
Ejemplo n.º 17
0
from exabgp.bgp.message.open.asn import ASN
from exabgp.bgp.message.open.holdtime import HoldTime

from exabgp.protocol.family import known_families
from exabgp.bgp.message.update import Update
from exabgp.bgp.message.open import Open
from exabgp.bgp.message.open.capability import Capabilities
from exabgp.bgp.message.open.capability import Capability
from exabgp.bgp.message.open.capability.negotiated import Negotiated

# from exabgp.bgp.message.notification import Notify


from exabgp.configuration.setup import environment

env = environment.setup("")

from exabgp.logger import Logger

logger = Logger()


class Neighbor:
    description = "a test neighbor"
    router_id = RouterID("127.0.0.1")
    local_address = IPv4("127.0.0.1")
    peer_address = IPv4("127.0.0.1")
    peer_as = ASN("65500")
    local_as = ASN("65500")
    hold_time = HoldTime(180)
    asn4 = False
Ejemplo n.º 18
0
def exabgp(localip, routerid, asn, peerip, peerport, peeras):
    from exabgp.reactor.api.command.command import Command
    from exabgp.reactor.api.command.limit import match_neighbors
    from exabgp.reactor.api.command.limit import extract_neighbors
    from exabgp.reactor.loop import Reactor

    # Extend ExaBGP to handle a "send-raw-data [[neighbor IP] ...] filename" command to cram raw
    # data down the peers throat as fast as possible (limited by kernel syscall).
    @Command.register('text', 'send-raw-data')
    def send_raw_data(self, reactor, service, line):
        def callback():
            try:
                descriptions, command = extract_neighbors(line)
                peers = match_neighbors(reactor.peers, descriptions)
                if not peers:
                    self.log_failure(
                        'no neighbor matching the command : %s' % command)
                    reactor.processes.answer(service, 'error')
                    yield True
                    return

                with open(command, "rb") as datafile:
                    rawdata = datafile.read()

                assert rawdata
                for peer in peers:
                    log.info("send-raw-data: %d bytes to %s", len(rawdata),
                             str(peer))
                    peer.proto.connection.writer(rawdata)

                reactor.processes.answer_done(service)
            except Exception as ex:
                self.log_failure('issue with send-raw-data: ' + str(ex))
                reactor.processes.answer(service, 'error')
                yield True

        reactor.asynchronous.schedule(service, line, callback())

    # from exabgp.pplication.bgp import main
    from exabgp.application.bgp import __exit
    from exabgp.debug import setup_report
    setup_report()
    from exabgp.configuration.setup import environment
    env = environment.setup("/nofile")

    with tempfile.NamedTemporaryFile(mode='w') as scriptfile:
        scriptfile.write("""#!/bin/bash
while read nchange; do
    echo $nchange >> /tmp/bgp-script-out.txt
    if [[ $(echo $nchange | sed '/neighbor {} up/!d') ]]; then
        echo send-raw-data foobar.raw
    fi
done
""".format(peerip))
        scriptfile.flush()
        os.system("chmod 755 " + scriptfile.name)

        with tempfile.NamedTemporaryFile(mode='w') as conffile:
            conffile.write("""
    process senddata {{
        run bash {};
        encoder text;
    }}

    neighbor {} {{                 # Remote neighbor to peer with
        router-id {};              # Our local router-id
        local-address {};          # Our local update-source
        local-as {};               # Our local AS
        peer-as {};                # Peer's AS

        api {{
            processes [senddata];
            neighbor-changes;
        }}
    }}
        """.format(scriptfile.name, peerip, routerid, localip, asn, peeras))
            conffile.flush()
            root = os.getcwd()
            exit_code = Reactor([conffile.name]).run(False, root)
            __exit(env.debug.memory, exit_code)
Ejemplo n.º 19
0
from exabgp.bgp.message import Update
from exabgp.bgp.message import Open
from exabgp.bgp.message.open import Version
from exabgp.bgp.message.open import ASN
from exabgp.bgp.message.open import RouterID
from exabgp.bgp.message.open import HoldTime
from exabgp.bgp.message.open.capability import Capabilities
from exabgp.bgp.message.open.capability import Capability
from exabgp.bgp.message.open.capability import Negotiated
from exabgp.bgp.message.update.nlri import NLRI


from exabgp.configuration.setup import environment
from exabgp.logger import Logger

environment.setup("")
logger = Logger()


bodies = []

body = [
    0x0,
    0x0,  # len withdrawn routes
    # No routes to remove
    # Attributes
    0x0,
    0x30,  # len attributes (48)
    0x40,  # Flag Transitive
    0x1,  # Code : Attribute ID Origin
    0x1,  # len