Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #782: Invalidate CDN on bundle upload #803

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion cronjobs/src/commands/build_bundles.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import lz4.block
import requests
from google.cloud import storage
from google.cloud import storage, compute_v1

from . import KintoClient, call_parallel, retry_timeout

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

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


def fetch_all_changesets(client):
"""
Expand Down Expand Up @@ -122,6 +127,7 @@ def sync_cloud_storage(
blob = bucket.blob(remote_file_path)
blob.upload_from_filename(filename)
print(f"Uploaded {filename} to gs://{storage_bucket}/{remote_file_path}")
invalidate_cdn_url(remote_file_path)

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


def invalidate_cdn_url(url_to_invalidate):
client = compute_v1.UrlMapsClient()

request = compute_v1.InvalidateCacheUrlMapRequest(
# use default project_id
url_map=CDN_URL_MAP_NAME,
cache_invalidation_rule=compute_v1.CacheInvalidationRule(
path=url_to_invalidate # Example: "/images/logo.png"
)
)
operation = client.invalidate_cache(request=request)
print(f"Invalidation request sent for: {url_to_invalidate}")
return operation


def build_bundles(event, context):
"""
Main command entry point that:
Expand Down
Loading