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