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

feat: offswitch #8541

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
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
44 changes: 29 additions & 15 deletions ietf/doc/storage_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ def _get_storage(kind: str):


def exists_in_storage(kind: str, name: str) -> bool:
store = _get_storage(kind)
return store.exists_in_storage(kind, name)
if settings.ENABLE_BLOBSTORAGE:
store = _get_storage(kind)
return store.exists_in_storage(kind, name)
else:
return False


def remove_from_storage(kind: str, name: str, warn_if_missing: bool = True) -> None:
store = _get_storage(kind)
store.remove_from_storage(kind, name, warn_if_missing)
if settings.ENABLE_BLOBSTORAGE:
store = _get_storage(kind)
store.remove_from_storage(kind, name, warn_if_missing)
return None


Expand All @@ -41,8 +45,9 @@ def store_file(
doc_rev: Optional[str] = None,
) -> None:
# debug.show('f"asked to store {name} into {kind}"')
store = _get_storage(kind)
store.store_file(kind, name, file, allow_overwrite, doc_name, doc_rev)
if settings.ENABLE_BLOBSTORAGE:
store = _get_storage(kind)
store.store_file(kind, name, file, allow_overwrite, doc_name, doc_rev)
return None


Expand All @@ -54,7 +59,9 @@ def store_bytes(
doc_name: Optional[str] = None,
doc_rev: Optional[str] = None,
) -> None:
return store_file(kind, name, ContentFile(content), allow_overwrite)
if settings.ENABLE_BLOBSTORAGE:
store_file(kind, name, ContentFile(content), allow_overwrite)
return None


def store_str(
Expand All @@ -65,18 +72,25 @@ def store_str(
doc_name: Optional[str] = None,
doc_rev: Optional[str] = None,
) -> None:
content_bytes = content.encode("utf-8")
return store_bytes(kind, name, content_bytes, allow_overwrite)
if settings.ENABLE_BLOBSTORAGE:
content_bytes = content.encode("utf-8")
store_bytes(kind, name, content_bytes, allow_overwrite)
return None


def retrieve_bytes(kind: str, name: str) -> bytes:
store = _get_storage(kind)
with store.open(name) as f:
content = f.read()
content = b""
if settings.ENABLE_BLOBSTORAGE:
store = _get_storage(kind)
with store.open(name) as f:
content = f.read()
return content


def retrieve_str(kind: str, name: str) -> str:
content_bytes = retrieve_bytes(kind, name)
# TODO: try to decode all the different ways doc.text() does
return content_bytes.decode("utf-8")
content = ""
if settings.ENABLE_BLOBSTORAGE:
content_bytes = retrieve_bytes(kind, name)
# TODO-BLOBSTORE: try to decode all the different ways doc.text() does
content = content_bytes.decode("utf-8")
return content
2 changes: 2 additions & 0 deletions ietf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@
# Server-side static.ietf.org URL (used in pdfized)
STATIC_IETF_ORG_INTERNAL = STATIC_IETF_ORG

ENABLE_BLOBSTORAGE = True

WSGI_APPLICATION = "ietf.wsgi.application"

AUTHENTICATION_BACKENDS = ( 'ietf.ietfauth.backends.CaseInsensitiveModelBackend', )
Expand Down
16 changes: 9 additions & 7 deletions ietf/utils/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Django Storage classes"""
from pathlib import Path

from django.conf import settings
from django.core.files.storage import FileSystemStorage
from ietf.doc.storage_utils import store_file
from .log import log
Expand Down Expand Up @@ -39,13 +40,14 @@ def save(self, name, content, max_length=None):
# Write content to the filesystem - this deals with chunks, etc...
saved_name = super().save(name, content, max_length)

# Retrieve the content and write to the blob store
blob_name = Path(saved_name).name # strips path
try:
with self.open(saved_name, "rb") as f:
store_file(self.kind, blob_name, f, allow_overwrite=True)
except Exception as err:
log(f"Failed to shadow {saved_name} at {self.kind}:{blob_name}: {err}")
if settings.ENABLE_BLOBSTORAGE:
# Retrieve the content and write to the blob store
blob_name = Path(saved_name).name # strips path
try:
with self.open(saved_name, "rb") as f:
store_file(self.kind, blob_name, f, allow_overwrite=True)
except Exception as err:
log(f"Failed to shadow {saved_name} at {self.kind}:{blob_name}: {err}")
return saved_name # includes the path!

def deconstruct(self):
Expand Down