Skip to content

Commit 9588bc5

Browse files
committed
Fix #782: Invalidate CDN on bundle upload
1 parent b549c9f commit 9588bc5

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

cronjobs/src/commands/build_bundles.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import lz4.block
1616
import requests
17-
from google.cloud import storage
17+
from google.cloud import storage, compute_v1
1818

1919
from . import KintoClient, call_parallel, retry_timeout
2020

@@ -31,6 +31,11 @@
3131
BUILD_ALL = os.getenv("BUILD_ALL", "0") in "1yY"
3232
SKIP_UPLOAD = os.getenv("SKIP_UPLOAD", "0") in "1yY"
3333

34+
# URL map name is "{application}-{realm}-{environment}-{cdn_name}-cdn"
35+
# See https://github.com/mozilla-it/webservices-infra/blob/1eafab1688d164a0f3e2f8dfcfb6ad1647837b67/remote-settings/tf/modules/backend_bucket_cdn/main.tf#L2
36+
# and https://github.com/mozilla-it/webservices-infra/blob/1eafab1688d164a0f3e2f8dfcfb6ad1647837b67/remote-settings/tf/modules/backend_bucket_cdn/main.tf#L72
37+
CDN_URL_MAP_NAME = f"remote-settings-{REALM}-{ENVIRONMENT}-attachments-cdn"
38+
3439

3540
def fetch_all_changesets(client):
3641
"""
@@ -122,6 +127,7 @@ def sync_cloud_storage(
122127
blob = bucket.blob(remote_file_path)
123128
blob.upload_from_filename(filename)
124129
print(f"Uploaded {filename} to gs://{storage_bucket}/{remote_file_path}")
130+
invalidate_cdn_url(remote_file_path)
125131

126132
to_delete = {os.path.join(remote_folder, f) for f in to_delete}
127133
blobs = bucket.list_blobs(prefix=remote_folder)
@@ -131,6 +137,21 @@ def sync_cloud_storage(
131137
print(f"Deleted gs://{storage_bucket}/{blob.name}")
132138

133139

140+
def invalidate_cdn_url(url_to_invalidate):
141+
client = compute_v1.UrlMapsClient()
142+
143+
request = compute_v1.InvalidateCacheUrlMapRequest(
144+
# use default project_id
145+
url_map=CDN_URL_MAP_NAME,
146+
cache_invalidation_rule=compute_v1.CacheInvalidationRule(
147+
path=url_to_invalidate # Example: "/images/logo.png"
148+
)
149+
)
150+
operation = client.invalidate_cache(request=request)
151+
print(f"Invalidation request sent for: {url_to_invalidate}")
152+
return operation
153+
154+
134155
def build_bundles(event, context):
135156
"""
136157
Main command entry point that:

0 commit comments

Comments
 (0)