setup_dns_zone 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/python
  2. import json
  3. import urllib2
  4. import sys
  5. from jinja2 import Template
  6. try:
  7. data = json.load(urllib2.urlopen('http://169.254.169.254/latest/user-data'))
  8. ip = urllib2.urlopen('http://169.254.169.254/latest/meta-data/local-ipv4').read()
  9. except:
  10. print >> sys.stderr, 'Was not able to connect to the Amazon API'
  11. sys.exit(2)
  12. zone_template="""
  13. zone "{{user_data['domain']}}" IN {
  14. type forward;
  15. forward only;
  16. forwarders { {{ipaddr}} port 8600; };
  17. };
  18. """
  19. zt = Template(zone_template)
  20. main_config = open("/etc/bind/zones.consul", "w")
  21. main_config.write(zt.render(user_data=data, ipaddr=ip))
  22. main_config.close()
  23. local_config = open("/etc/bind/named.conf.local", "w")
  24. local_config.write("//Scripted Configure\ninclude \"/etc/bind/zones.consul\";")
  25. local_config.close()
  26. options_template="""
  27. options {
  28. directory "/var/cache/bind";
  29. allow-query { any; };
  30. allow-transfer { localhost; };
  31. recursion yes;
  32. allow-recursion { any; };
  33. forward only;
  34. forwarders {
  35. {% for forwarder in forwarders %}
  36. {{ forwarder }};
  37. {% endfor %}
  38. };
  39. dnssec-validation no;
  40. dnssec-enable no;
  41. auth-nxdomain no; # conform to RFC1035
  42. listen-on-v6 { any; };
  43. };
  44. """
  45. ot = Template(options_template)
  46. if 'upstream_dns' in data:
  47. myfowarders = data['upstream_dns'].split(",")
  48. else:
  49. myfowarders = ['8.8.8.8', '8.8.4.4']
  50. options_config = open("/etc/bind/named.conf.options", "w")
  51. options_config.write(ot.render(forwarders=myfowarders))
  52. options_config.close()