test_peering_relations.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import ipaddr
  4. import yaml
  5. import sys
  6. import glob
  7. from termcolor import colored
  8. def parse_peers(peer_file):
  9. """parse_peers: Just a simple function for peers parsing
  10. :peer_file: The YAML peer file to parse
  11. :returns: Just a return code if the file is correctly parsed or not
  12. """
  13. peering_flat = open(peer_file).read()
  14. try:
  15. peerings = yaml.safe_load(peering_flat)
  16. except:
  17. print colored('ERROR', 'red') + ": the peers.yaml file could not be parsed... please check \
  18. your syntax"
  19. sys.exit(2)
  20. # IXPs Gitoyen is connected to
  21. connected_ixps = {
  22. "amsix": [ipaddr.IPNetwork('80.249.210.195/21'),
  23. ipaddr.IPNetwork('2001:7f8:1::a502:766:1/64')],
  24. "franceix": [ipaddr.IPNetwork('37.49.236.190/23'),
  25. ipaddr.IPNetwork('2001:7f8:54::190/64')],
  26. "equinix": [ipaddr.IPNetwork('195.42.145.64/23'),
  27. ipaddr.IPNetwork('2001:7f8:43::2:766:1/64')],
  28. "sfinx": [ipaddr.IPNetwork('194.68.129.186/24'),
  29. ipaddr.IPNetwork('2001:7f8:4e:2::186/64')]}
  30. for asn in peerings:
  31. for keyword in ['export', 'import', 'description', 'peerings']:
  32. if keyword not in peerings[asn]:
  33. print colored('ERROR', 'red') + ": missing %s statement in stanza %s" % (keyword, asn)
  34. sys.exit(2)
  35. for peer in peerings[asn]['peerings']:
  36. try:
  37. peer_ip = ipaddr.IPAddress(peer)
  38. except ValueError:
  39. print colored('ERROR', 'red') + ": %s in %s is not a valid IP" % (peer, asn)
  40. sys.exit(2)
  41. # search if we can reach the peer
  42. found = False
  43. for ixp in connected_ixps:
  44. for subnet in connected_ixps[ixp]:
  45. if ipaddr.IPAddress(peer) in subnet:
  46. print colored('OK', 'green') + ": found %s (%s) in %s" % (peer, asn, ixp)
  47. found = True
  48. if not found:
  49. print colored('ERROR', 'red') + ": AS 20766 cannot reach %s through %s, either a typo \
  50. or we are not connected to the same internet exchange" \
  51. % (peer, " ".join(connected_ixps))
  52. sys.exit(2)
  53. acceptable_exports = ['AS-GITOYEN', 'NOT ANY', 'ANY']
  54. if not peerings[asn]['export'] in acceptable_exports:
  55. print colored('ERROR', 'red') + ": export must be one of the following: %s" \
  56. % " ".join(acceptable_exports)
  57. sys.exit(2)
  58. print colored('HOORAY', 'yellow') + ": Ready for production!!!"
  59. for peer_files in glob.glob('peers/*.yml'):
  60. parse_peers(peer_files)