generate_config_bird.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import ipaddr
  4. import yaml
  5. import sys
  6. import os
  7. import glob
  8. from jinja2 import Environment, FileSystemLoader
  9. from termcolor import colored
  10. def parse_peers(peer_file, version):
  11. """parse_peers: Just a simple function for peers parsing
  12. :peer_file: The YAML peer file to parse
  13. :returns: Just a return code if the file is correctly parsed or not
  14. """
  15. peering_flat = open(peer_file).read()
  16. ixp = os.path.splitext(os.path.basename(peer_file))[0]
  17. try:
  18. peerings = yaml.safe_load(peering_flat)
  19. except:
  20. print colored('ERROR', 'red') + ": the peers.yaml file could not be parsed.. please check \
  21. your syntax"
  22. sys.exit(2)
  23. for asn in peerings:
  24. for keyword in ['export', 'import', 'description']:
  25. if keyword not in peerings[asn]:
  26. print colored('ERROR', 'red') + ": missing %s statement in stanza %s" % (keyword, asn)
  27. sys.exit(2)
  28. acceptable_exports = ['AS-GITOYEN', 'NOT ANY', 'ANY']
  29. if not peerings[asn]['export'] in acceptable_exports:
  30. print colored('ERROR', 'red') + ": export must be one of the following: %s" \
  31. % " ".join(acceptable_exports)
  32. sys.exit(2)
  33. session = 0
  34. for peer in peerings[asn]['peerings']:
  35. try:
  36. peer_ip = ipaddr.IPAddress(peer)
  37. if type(ipaddr.IPAddress(peer_ip)) is ipaddr.IPv4Address and version == 4:
  38. neighbor_ipv4 = peer_ip
  39. session += 1
  40. elif type(ipaddr.IPAddress(peer_ip)) is ipaddr.IPv6Address and version == 6:
  41. neighbor_ipv6 = peer_ip
  42. session += 1
  43. except ValueError:
  44. print colored('ERROR', 'red') + ": %s in %s is not a valid IP" % (peer, asn)
  45. sys.exit(2)
  46. try:
  47. limit_ipv4 = peerings[asn]['limit_ipv4']
  48. except:
  49. limit_ipv4 = False
  50. try:
  51. limit_ipv6 = peerings[asn]['limit_ipv6']
  52. except:
  53. limit_ipv6 = False
  54. env = Environment(loader=FileSystemLoader('./'))
  55. if version == 6 and 'neighbor_ipv6' in locals() and type(ipaddr.IPAddress(peer_ip)) is ipaddr.IPv6Address:
  56. #Generate IPV6
  57. tpl = env.get_template('templates/bird_v6.j2')
  58. print tpl.render(neighbor_as=asn, description=
  59. peerings[asn]['description'], export_as=peerings[asn]['export'],
  60. import_as=peerings[asn]['import'],
  61. neighbor_ipv6=neighbor_ipv6, ix_name=
  62. ixp , ix_name_strip= ixp.replace('-',''),limit_ipv6=limit_ipv6, session_num=session).encode('utf-8').strip()
  63. else:
  64. if 'neighbor_ipv4' in locals() and type(ipaddr.IPAddress(peer_ip)) is ipaddr.IPv4Address:
  65. #Generate IPV4
  66. tpl = env.get_template('templates/bird_v4.j2')
  67. print tpl.render(neighbor_as=asn, description=
  68. peerings[asn]['description'], export_as=peerings[asn]['export'],
  69. import_as=peerings[asn]['import'], neighbor_ipv4=
  70. neighbor_ipv4,ix_name=ixp, ix_name_strip= ixp.replace('-',''),
  71. limit_ipv4=limit_ipv4, session_num=session).encode('utf-8').strip()
  72. #Basicly check arg
  73. if len(sys.argv) == 2:
  74. for peer_files in glob.glob('peers/*.yml'):
  75. parse_peers(peer_files, int(sys.argv[1]))
  76. else:
  77. print("Invalide argument number, You must specify IP version 4 or 6")