Browse code

When using default index, invalidate path

Josep del Rio authored on 2012/04/19 05:20:12
Showing 3 changed files
... ...
@@ -423,7 +423,26 @@ class CloudFront(object):
423 423
                                      body = request_body, headers = headers)
424 424
         return response
425 425
 
426
-    def InvalidateObjects(self, uri, paths):
426
+    def InvalidateObjects(self, uri, paths, default_index_file, invalidate_default_index_on_cf, invalidate_default_index_root_on_cf):
427
+        # joseprio: if the user doesn't want to invalidate the default index
428
+        # path, or if the user wants to invalidate the root of the default
429
+        # index, we need to process those paths
430
+        if not invalidate_default_index_on_cf or invalidate_default_index_root_on_cf:
431
+            new_paths = []
432
+            default_index_suffix = '/' + default_index_file
433
+            for path in paths:
434
+                if path.endswith(default_index_suffix) or path ==  default_index_file:
435
+                    if invalidate_default_index_on_cf:
436
+                        new_paths.append(path)
437
+                        output('Appending ' + path)
438
+                    if invalidate_default_index_root_on_cf:
439
+                        new_paths.append(path[:-len(default_index_file)])
440
+                        output('Appending ' + path[:-len(default_index_file)])
441
+                else:
442
+                    new_paths.append(path)
443
+                    output('Appending ' + path)
444
+            paths = new_paths
445
+        
427 446
         # uri could be either cf:// or s3:// uri
428 447
         cfuri = self.get_dist_name_for_bucket(uri)
429 448
         if len(paths) > 999:
... ...
@@ -80,6 +80,9 @@ class Config(object):
80 80
     follow_symlinks = False
81 81
     socket_timeout = 300
82 82
     invalidate_on_cf = False
83
+    # joseprio: new flags for default index invalidation
84
+    invalidate_default_index_on_cf = False
85
+    invalidate_default_index_root_on_cf = True
83 86
     website_index = "index.html"
84 87
     website_error = ""
85 88
     website_endpoint = "http://%(bucket)s.s3-website-%(location)s.amazonaws.com/"
... ...
@@ -952,7 +952,18 @@ def cmd_sync_local2remote(args):
952 952
         else:
953 953
             # 'uri' from the last iteration is still valid at this point
954 954
             cf = CloudFront(cfg)
955
-            result = cf.InvalidateObjects(uri, uploaded_objects_list)
955
+            # joseprio: obtain default index file if we need it for invalidating
956
+            # the files
957
+            default_index_file = None
958
+            if cfg.invalidate_default_index_on_cf or cfg.invalidate_default_index_root_on_cf:
959
+                info_response = s3.website_info(destination_base_uri, cfg.bucket_location)
960
+                if info_response:
961
+                  default_index_file = info_response['index_document']
962
+                  if len(default_index_file) < 1:
963
+                      default_index_file = None
964
+            # joseprio: pass configuration information for invalidating default
965
+            # index file
966
+            result = cf.InvalidateObjects(uri, uploaded_objects_list, default_index_file, cfg.invalidate_default_index_on_cf, cfg.invalidate_default_index_root_on_cf)
956 967
             if result['status'] == 201:
957 968
                 output("Created invalidation request for %d paths" % len(uploaded_objects_list))
958 969
                 output("Check progress with: s3cmd cfinvalinfo cf://%s/%s" % (result['dist_id'], result['request_id']))
... ...
@@ -1540,6 +1551,10 @@ def main():
1540 1540
     optparser.add_option(      "--enable", dest="enable", action="store_true", help="Enable given CloudFront distribution (only for [cfmodify] command)")
1541 1541
     optparser.add_option(      "--disable", dest="enable", action="store_false", help="Enable given CloudFront distribution (only for [cfmodify] command)")
1542 1542
     optparser.add_option(      "--cf-invalidate", dest="invalidate_on_cf", action="store_true", help="Invalidate the uploaded filed in CloudFront. Also see [cfinval] command.")
1543
+    # joseprio: adding options to invalidate the default index and the default
1544
+    # index root
1545
+    optparser.add_option(      "--cf-invalidate-default-index", dest="invalidate_default_index_on_cf", action="store_false", help="When using Custom Origin and S3 static website, setting it to true will invalidate the default index file.")
1546
+    optparser.add_option(      "--cf-invalidate-default-index-root", dest="invalidate_default_index_root_on_cf", action="store_true", help="When using Custom Origin and S3 static website, setting it to true will invalidate the path of the default index file.")
1543 1547
     optparser.add_option(      "--cf-add-cname", dest="cf_cnames_add", action="append", metavar="CNAME", help="Add given CNAME to a CloudFront distribution (only for [cfcreate] and [cfmodify] commands)")
1544 1548
     optparser.add_option(      "--cf-remove-cname", dest="cf_cnames_remove", action="append", metavar="CNAME", help="Remove given CNAME from a CloudFront distribution (only for [cfmodify] command)")
1545 1549
     optparser.add_option(      "--cf-comment", dest="cf_comment", action="store", metavar="COMMENT", help="Set COMMENT for a given CloudFront distribution (only for [cfcreate] and [cfmodify] commands)")