generate_config_bird.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. session += 1
  38. if type(ipaddr.IPAddress(peer_ip)) is ipaddr.IPv4Address:
  39. neighbor_ipv4 = peer_ip
  40. elif type(ipaddr.IPAddress(peer_ip)) is ipaddr.IPv6Address:
  41. neighbor_ipv6 = peer_ip
  42. except ValueError:
  43. print colored('ERROR', 'red') + ": %s in %s is not a valid IP" % (peer, asn)
  44. sys.exit(2)
  45. try:
  46. limit_ipv4 = peerings[asn]['limit_ipv4']
  47. except:
  48. limit_ipv4 = False
  49. try:
  50. limit_ipv6 = peerings[asn]['limit_ipv6']
  51. except:
  52. limit_ipv6 = False
  53. env = Environment(loader=FileSystemLoader('./'))
  54. if version == 6 and 'neighbor_ipv6' in locals():
  55. #Generate IPV6
  56. tpl = env.get_template('templates/bird_v6.j2')
  57. print tpl.render(neighbor_as=asn, description=
  58. peerings[asn]['description'], export_as=peerings[asn]['export'],
  59. import_as=peerings[asn]['import'],
  60. neighbor_ipv6=neighbor_ipv6, ix_name=
  61. ixp, limit_ipv6=limit_ipv6, session_num=session)
  62. else:
  63. if 'neighbor_ipv4' in locals():
  64. #Generate IPV4
  65. tpl = env.get_template('templates/bird_v4.j2')
  66. print tpl.render(neighbor_as=asn, description=
  67. peerings[asn]['description'], export_as=peerings[asn]['export'],
  68. import_as=peerings[asn]['import'], neighbor_ipv4=
  69. neighbor_ipv4,ix_name=ixp,
  70. limit_ipv4=limit_ipv4, session_num=session)
  71. #Basicly check arg
  72. if len(sys.argv) == 2:
  73. for peer_files in glob.glob('peers/*.yml'):
  74. parse_peers(peer_files, int(sys.argv[1]))
  75. else:
  76. print("Invalide argument number, You must specify IP version 4 or 6")