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.

108 lines
3.4 KiB

4 years ago
  1. #!/home/alpcentaur/ProjektA/PrototypeWebApp/venv/bin/python3.5
  2. # Author: Chris Moyer
  3. #
  4. # cfadmin is similar to sdbadmin for CloudFront, it's a simple
  5. # console utility to perform the most frequent tasks with CloudFront
  6. #
  7. def _print_distributions(dists):
  8. """Internal function to print out all the distributions provided"""
  9. print "%-12s %-50s %s" % ("Status", "Domain Name", "Origin")
  10. print "-"*80
  11. for d in dists:
  12. print "%-12s %-50s %-30s" % (d.status, d.domain_name, d.origin)
  13. for cname in d.cnames:
  14. print " "*12, "CNAME => %s" % cname
  15. print ""
  16. def help(cf, fnc=None):
  17. """Print help message, optionally about a specific function"""
  18. import inspect
  19. self = sys.modules['__main__']
  20. if fnc:
  21. try:
  22. cmd = getattr(self, fnc)
  23. except:
  24. cmd = None
  25. if not inspect.isfunction(cmd):
  26. print "No function named: %s found" % fnc
  27. sys.exit(2)
  28. (args, varargs, varkw, defaults) = inspect.getargspec(cmd)
  29. print cmd.__doc__
  30. print "Usage: %s %s" % (fnc, " ".join([ "[%s]" % a for a in args[1:]]))
  31. else:
  32. print "Usage: cfadmin [command]"
  33. for cname in dir(self):
  34. if not cname.startswith("_"):
  35. cmd = getattr(self, cname)
  36. if inspect.isfunction(cmd):
  37. doc = cmd.__doc__
  38. print "\t%s - %s" % (cname, doc)
  39. sys.exit(1)
  40. def ls(cf):
  41. """List all distributions and streaming distributions"""
  42. print "Standard Distributions"
  43. _print_distributions(cf.get_all_distributions())
  44. print "Streaming Distributions"
  45. _print_distributions(cf.get_all_streaming_distributions())
  46. def invalidate(cf, origin_or_id, *paths):
  47. """Create a cloudfront invalidation request"""
  48. # Allow paths to be passed using stdin
  49. if not paths:
  50. paths = []
  51. for path in sys.stdin.readlines():
  52. path = path.strip()
  53. if path:
  54. paths.append(path)
  55. dist = None
  56. for d in cf.get_all_distributions():
  57. if d.id == origin_or_id or d.origin.dns_name == origin_or_id:
  58. dist = d
  59. break
  60. if not dist:
  61. print "Distribution not found: %s" % origin_or_id
  62. sys.exit(1)
  63. cf.create_invalidation_request(dist.id, paths)
  64. def listinvalidations(cf, origin_or_id):
  65. """List invalidation requests for a given origin"""
  66. dist = None
  67. for d in cf.get_all_distributions():
  68. if d.id == origin_or_id or d.origin.dns_name == origin_or_id:
  69. dist = d
  70. break
  71. if not dist:
  72. print "Distribution not found: %s" % origin_or_id
  73. sys.exit(1)
  74. results = cf.get_invalidation_requests(dist.id)
  75. if results:
  76. for result in results:
  77. if result.status == "InProgress":
  78. result = result.get_invalidation_request()
  79. print result.id, result.status, result.paths
  80. else:
  81. print result.id, result.status
  82. if __name__ == "__main__":
  83. import boto
  84. import sys
  85. cf = boto.connect_cloudfront()
  86. self = sys.modules['__main__']
  87. if len(sys.argv) >= 2:
  88. try:
  89. cmd = getattr(self, sys.argv[1])
  90. except:
  91. cmd = None
  92. args = sys.argv[2:]
  93. else:
  94. cmd = help
  95. args = []
  96. if not cmd:
  97. cmd = help
  98. try:
  99. cmd(cf, *args)
  100. except TypeError as e:
  101. print e
  102. help(cf, cmd.__name__)