1 #!/usr/bin/env python 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 17 def help(cf, fnc=None): 18 """Print help message, optionally about a specific function""" 19 import inspect 20 self = sys.modules['__main__'] 21 if fnc: 22 try: 23 cmd = getattr(self, fnc) 24 except: 25 cmd = None 26 if not inspect.isfunction(cmd): 27 print "No function named: %s found" % fnc 28 sys.exit(2) 29 (args, varargs, varkw, defaults) = inspect.getargspec(cmd) 30 print cmd.__doc__ 31 print "Usage: %s %s" % (fnc, " ".join([ "[%s]" % a for a in args[1:]])) 32 else: 33 print "Usage: cfadmin [command]" 34 for cname in dir(self): 35 if not cname.startswith("_"): 36 cmd = getattr(self, cname) 37 if inspect.isfunction(cmd): 38 doc = cmd.__doc__ 39 print "\t%s - %s" % (cname, doc) 40 sys.exit(1) 41 42 def ls(cf): 43 """List all distributions and streaming distributions""" 44 print "Standard Distributions" 45 _print_distributions(cf.get_all_distributions()) 46 print "Streaming Distributions" 47 _print_distributions(cf.get_all_streaming_distributions()) 48 49 def invalidate(cf, origin_or_id, *paths): 50 """Create a cloudfront invalidation request""" 51 # Allow paths to be passed using stdin 52 if not paths: 53 paths = [] 54 for path in sys.stdin.readlines(): 55 path = path.strip() 56 if path: 57 paths.append(path) 58 dist = None 59 for d in cf.get_all_distributions(): 60 if d.id == origin_or_id or d.origin.dns_name == origin_or_id: 61 dist = d 62 break 63 if not dist: 64 print "Distribution not found: %s" % origin_or_id 65 sys.exit(1) 66 cf.create_invalidation_request(dist.id, paths) 67 68 def listinvalidations(cf, origin_or_id): 69 """List invalidation requests for a given origin""" 70 dist = None 71 for d in cf.get_all_distributions(): 72 if d.id == origin_or_id or d.origin.dns_name == origin_or_id: 73 dist = d 74 break 75 if not dist: 76 print "Distribution not found: %s" % origin_or_id 77 sys.exit(1) 78 results = cf.get_invalidation_requests(dist.id) 79 if results: 80 for result in results: 81 if result.status == "InProgress": 82 result = result.get_invalidation_request() 83 print result.id, result.status, result.paths 84 else: 85 print result.id, result.status 86 87 88 if __name__ == "__main__": 89 import boto 90 import sys 91 cf = boto.connect_cloudfront() 92 self = sys.modules['__main__'] 93 if len(sys.argv) >= 2: 94 try: 95 cmd = getattr(self, sys.argv[1]) 96 except: 97 cmd = None 98 args = sys.argv[2:] 99 else: 100 cmd = help 101 args = [] 102 if not cmd: 103 cmd = help 104 try: 105 cmd(cf, *args) 106 except TypeError, e: 107 print e 108 help(cf, cmd.__name__) 109