You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
8.9 KiB

4 years ago
  1. #!/home/alpcentaur/ProjektA/PrototypeWebApp/venv/bin/python3.5
  2. # Author: Chris Moyer
  3. #
  4. # route53 is similar to sdbadmin for Route53, it's a simple
  5. # console utility to perform the most frequent tasks with Route53
  6. #
  7. # Example usage. Use route53 get after each command to see how the
  8. # zone changes.
  9. #
  10. # Add a non-weighted record, change its value, then delete. Default TTL:
  11. #
  12. # route53 add_record ZPO9LGHZ43QB9 rr.example.com A 4.3.2.1
  13. # route53 change_record ZPO9LGHZ43QB9 rr.example.com A 9.8.7.6
  14. # route53 del_record ZPO9LGHZ43QB9 rr.example.com A 9.8.7.6
  15. #
  16. # Add a weighted record with two different weights. Note that the TTL
  17. # must be specified as route53 uses positional parameters rather than
  18. # option flags:
  19. #
  20. # route53 add_record ZPO9LGHZ43QB9 wrr.example.com A 1.2.3.4 600 foo9 10
  21. # route53 add_record ZPO9LGHZ43QB9 wrr.example.com A 4.3.2.1 600 foo8 10
  22. #
  23. # route53 change_record ZPO9LGHZ43QB9 wrr.example.com A 9.9.9.9 600 foo8 10
  24. #
  25. # route53 del_record ZPO9LGHZ43QB9 wrr.example.com A 1.2.3.4 600 foo9 10
  26. # route53 del_record ZPO9LGHZ43QB9 wrr.example.com A 9.9.9.9 600 foo8 10
  27. #
  28. # Add a non-weighted alias, change its value, then delete. Alaises inherit
  29. # their TTLs from the backing ELB:
  30. #
  31. # route53 add_alias ZPO9LGHZ43QB9 alias.example.com A Z3DZXE0Q79N41H lb-1218761514.us-east-1.elb.amazonaws.com.
  32. # route53 change_alias ZPO9LGHZ43QB9 alias.example.com. A Z3DZXE0Q79N41H lb2-1218761514.us-east-1.elb.amazonaws.com.
  33. # route53 delete_alias ZPO9LGHZ43QB9 alias.example.com. A Z3DZXE0Q79N41H lb2-1218761514.us-east-1.elb.amazonaws.com.
  34. def _print_zone_info(zoneinfo):
  35. print "="*80
  36. print "| ID: %s" % zoneinfo['Id'].split("/")[-1]
  37. print "| Name: %s" % zoneinfo['Name']
  38. print "| Ref: %s" % zoneinfo['CallerReference']
  39. print "="*80
  40. print zoneinfo['Config']
  41. print
  42. def create(conn, hostname, caller_reference=None, comment=''):
  43. """Create a hosted zone, returning the nameservers"""
  44. response = conn.create_hosted_zone(hostname, caller_reference, comment)
  45. print "Pending, please add the following Name Servers:"
  46. for ns in response.NameServers:
  47. print "\t", ns
  48. def delete_zone(conn, hosted_zone_id):
  49. """Delete a hosted zone by ID"""
  50. response = conn.delete_hosted_zone(hosted_zone_id)
  51. print response
  52. def ls(conn):
  53. """List all hosted zones"""
  54. response = conn.get_all_hosted_zones()
  55. for zoneinfo in response['ListHostedZonesResponse']['HostedZones']:
  56. _print_zone_info(zoneinfo)
  57. def get(conn, hosted_zone_id, type=None, name=None, maxitems=None):
  58. """Get all the records for a single zone"""
  59. response = conn.get_all_rrsets(hosted_zone_id, type, name, maxitems=maxitems)
  60. # If a maximum number of items was set, we limit to that number
  61. # by turning the response into an actual list (copying it)
  62. # instead of allowing it to page
  63. if maxitems:
  64. response = response[:]
  65. print '%-40s %-5s %-20s %s' % ("Name", "Type", "TTL", "Value(s)")
  66. for record in response:
  67. print '%-40s %-5s %-20s %s' % (record.name, record.type, record.ttl, record.to_print())
  68. def _add_del(conn, hosted_zone_id, change, name, type, identifier, weight, values, ttl, comment):
  69. from boto.route53.record import ResourceRecordSets
  70. changes = ResourceRecordSets(conn, hosted_zone_id, comment)
  71. change = changes.add_change(change, name, type, ttl,
  72. identifier=identifier, weight=weight)
  73. for value in values.split(','):
  74. change.add_value(value)
  75. print changes.commit()
  76. def _add_del_alias(conn, hosted_zone_id, change, name, type, identifier, weight, alias_hosted_zone_id, alias_dns_name, comment):
  77. from boto.route53.record import ResourceRecordSets
  78. changes = ResourceRecordSets(conn, hosted_zone_id, comment)
  79. change = changes.add_change(change, name, type,
  80. identifier=identifier, weight=weight)
  81. change.set_alias(alias_hosted_zone_id, alias_dns_name)
  82. print changes.commit()
  83. def add_record(conn, hosted_zone_id, name, type, values, ttl=600,
  84. identifier=None, weight=None, comment=""):
  85. """Add a new record to a zone. identifier and weight are optional."""
  86. _add_del(conn, hosted_zone_id, "CREATE", name, type, identifier,
  87. weight, values, ttl, comment)
  88. def del_record(conn, hosted_zone_id, name, type, values, ttl=600,
  89. identifier=None, weight=None, comment=""):
  90. """Delete a record from a zone: name, type, ttl, identifier, and weight must match."""
  91. _add_del(conn, hosted_zone_id, "DELETE", name, type, identifier,
  92. weight, values, ttl, comment)
  93. def add_alias(conn, hosted_zone_id, name, type, alias_hosted_zone_id,
  94. alias_dns_name, identifier=None, weight=None, comment=""):
  95. """Add a new alias to a zone. identifier and weight are optional."""
  96. _add_del_alias(conn, hosted_zone_id, "CREATE", name, type, identifier,
  97. weight, alias_hosted_zone_id, alias_dns_name, comment)
  98. def del_alias(conn, hosted_zone_id, name, type, alias_hosted_zone_id,
  99. alias_dns_name, identifier=None, weight=None, comment=""):
  100. """Delete an alias from a zone: name, type, alias_hosted_zone_id, alias_dns_name, weight and identifier must match."""
  101. _add_del_alias(conn, hosted_zone_id, "DELETE", name, type, identifier,
  102. weight, alias_hosted_zone_id, alias_dns_name, comment)
  103. def change_record(conn, hosted_zone_id, name, type, newvalues, ttl=600,
  104. identifier=None, weight=None, comment=""):
  105. """Delete and then add a record to a zone. identifier and weight are optional."""
  106. from boto.route53.record import ResourceRecordSets
  107. changes = ResourceRecordSets(conn, hosted_zone_id, comment)
  108. # Assume there are not more than 10 WRRs for a given (name, type)
  109. responses = conn.get_all_rrsets(hosted_zone_id, type, name, maxitems=10)
  110. for response in responses:
  111. if response.name != name or response.type != type:
  112. continue
  113. if response.identifier != identifier or response.weight != weight:
  114. continue
  115. change1 = changes.add_change("DELETE", name, type, response.ttl,
  116. identifier=response.identifier,
  117. weight=response.weight)
  118. for old_value in response.resource_records:
  119. change1.add_value(old_value)
  120. change2 = changes.add_change("UPSERT", name, type, ttl,
  121. identifier=identifier, weight=weight)
  122. for new_value in newvalues.split(','):
  123. change2.add_value(new_value)
  124. print changes.commit()
  125. def change_alias(conn, hosted_zone_id, name, type, new_alias_hosted_zone_id, new_alias_dns_name, identifier=None, weight=None, comment=""):
  126. """Delete and then add an alias to a zone. identifier and weight are optional."""
  127. from boto.route53.record import ResourceRecordSets
  128. changes = ResourceRecordSets(conn, hosted_zone_id, comment)
  129. # Assume there are not more than 10 WRRs for a given (name, type)
  130. responses = conn.get_all_rrsets(hosted_zone_id, type, name, maxitems=10)
  131. for response in responses:
  132. if response.name != name or response.type != type:
  133. continue
  134. if response.identifier != identifier or response.weight != weight:
  135. continue
  136. change1 = changes.add_change("DELETE", name, type,
  137. identifier=response.identifier,
  138. weight=response.weight)
  139. change1.set_alias(response.alias_hosted_zone_id, response.alias_dns_name)
  140. change2 = changes.add_change("UPSERT", name, type, identifier=identifier, weight=weight)
  141. change2.set_alias(new_alias_hosted_zone_id, new_alias_dns_name)
  142. print changes.commit()
  143. def help(conn, fnc=None):
  144. """Prints this help message"""
  145. import inspect
  146. self = sys.modules['__main__']
  147. if fnc:
  148. try:
  149. cmd = getattr(self, fnc)
  150. except:
  151. cmd = None
  152. if not inspect.isfunction(cmd):
  153. print "No function named: %s found" % fnc
  154. sys.exit(2)
  155. (args, varargs, varkw, defaults) = inspect.getargspec(cmd)
  156. print cmd.__doc__
  157. print "Usage: %s %s" % (fnc, " ".join([ "[%s]" % a for a in args[1:]]))
  158. else:
  159. print "Usage: route53 [command]"
  160. for cname in dir(self):
  161. if not cname.startswith("_"):
  162. cmd = getattr(self, cname)
  163. if inspect.isfunction(cmd):
  164. doc = cmd.__doc__
  165. print "\t%-20s %s" % (cname, doc)
  166. sys.exit(1)
  167. if __name__ == "__main__":
  168. import boto
  169. import sys
  170. conn = boto.connect_route53()
  171. self = sys.modules['__main__']
  172. if len(sys.argv) >= 2:
  173. try:
  174. cmd = getattr(self, sys.argv[1])
  175. except:
  176. cmd = None
  177. args = sys.argv[2:]
  178. else:
  179. cmd = help
  180. args = []
  181. if not cmd:
  182. cmd = help
  183. try:
  184. cmd(conn, *args)
  185. except TypeError as e:
  186. print e
  187. help(conn, cmd.__name__)