On new project creation, use the backend storage set in config
This commit is contained in:
@@ -17,6 +17,10 @@ from . import gcs
|
|||||||
|
|
||||||
|
|
||||||
def default_storage_backend(name: str) -> Bucket:
|
def default_storage_backend(name: str) -> Bucket:
|
||||||
|
"""Returns an instance of a Bucket, based on the default backend.
|
||||||
|
|
||||||
|
Depending on the backend this may actually create the bucket.
|
||||||
|
"""
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
|
|
||||||
backend_name = current_app.config['STORAGE_BACKEND']
|
backend_name = current_app.config['STORAGE_BACKEND']
|
||||||
|
@@ -6,7 +6,7 @@ import typing
|
|||||||
from bson import ObjectId
|
from bson import ObjectId
|
||||||
from gcloud.storage.client import Client
|
from gcloud.storage.client import Client
|
||||||
import gcloud.storage.blob
|
import gcloud.storage.blob
|
||||||
from gcloud.exceptions import NotFound
|
import gcloud.exceptions as gcloud_exc
|
||||||
from flask import current_app, g
|
from flask import current_app, g
|
||||||
from werkzeug.local import LocalProxy
|
from werkzeug.local import LocalProxy
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ class GoogleCloudStorageBucket(Bucket):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
self._gcs_bucket = gcs.get_bucket(name)
|
self._gcs_bucket = gcs.get_bucket(name)
|
||||||
except NotFound:
|
except gcloud_exc.NotFound:
|
||||||
self._gcs_bucket = gcs.bucket(name)
|
self._gcs_bucket = gcs.bucket(name)
|
||||||
# Hardcode the bucket location to EU
|
# Hardcode the bucket location to EU
|
||||||
self._gcs_bucket.location = 'EU'
|
self._gcs_bucket.location = 'EU'
|
||||||
@@ -72,6 +72,7 @@ class GoogleCloudStorageBucket(Bucket):
|
|||||||
# }
|
# }
|
||||||
# ]
|
# ]
|
||||||
self._gcs_bucket.create()
|
self._gcs_bucket.create()
|
||||||
|
log.info('Created GCS instance for project %s', name)
|
||||||
|
|
||||||
self.subdir = subdir
|
self.subdir = subdir
|
||||||
|
|
||||||
@@ -115,7 +116,7 @@ class GoogleCloudStorageBucket(Bucket):
|
|||||||
try:
|
try:
|
||||||
gblob.delete()
|
gblob.delete()
|
||||||
return True
|
return True
|
||||||
except NotFound:
|
except gcloud_exc.NotFound:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def copy_blob(self, blob: Blob, to_bucket: Bucket):
|
def copy_blob(self, blob: Blob, to_bucket: Bucket):
|
||||||
@@ -184,7 +185,7 @@ class GoogleCloudStorageBlob(Blob):
|
|||||||
# Reload to get the actual file properties from Google.
|
# Reload to get the actual file properties from Google.
|
||||||
try:
|
try:
|
||||||
self.gblob.reload()
|
self.gblob.reload()
|
||||||
except NotFound:
|
except gcloud_exc.NotFound:
|
||||||
return False
|
return False
|
||||||
return self.gblob.exists()
|
return self.gblob.exists()
|
||||||
|
|
||||||
|
@@ -9,6 +9,7 @@ from pillar.api.node_types.comment import node_type_comment
|
|||||||
from pillar.api.node_types.group import node_type_group
|
from pillar.api.node_types.group import node_type_group
|
||||||
from pillar.api.node_types.group_texture import node_type_group_texture
|
from pillar.api.node_types.group_texture import node_type_group_texture
|
||||||
from pillar.api.node_types.texture import node_type_texture
|
from pillar.api.node_types.texture import node_type_texture
|
||||||
|
from pillar.api.file_storage_backends import default_storage_backend
|
||||||
from pillar.api.file_storage_backends.gcs import GoogleCloudStorageBucket
|
from pillar.api.file_storage_backends.gcs import GoogleCloudStorageBucket
|
||||||
from pillar.api.utils import authorization, authentication
|
from pillar.api.utils import authorization, authentication
|
||||||
from pillar.api.utils import remove_private_keys
|
from pillar.api.utils import remove_private_keys
|
||||||
@@ -167,19 +168,8 @@ def after_inserting_project(project, db_user):
|
|||||||
else:
|
else:
|
||||||
project['url'] = "p-{!s}".format(project_id)
|
project['url'] = "p-{!s}".format(project_id)
|
||||||
|
|
||||||
# Initialize storage page (defaults to GCS)
|
# Initialize storage using the default specified in STORAGE_BACKEND
|
||||||
if current_app.config.get('TESTING'):
|
default_storage_backend(str(project_id))
|
||||||
log.warning('Not creating Google Cloud Storage bucket while running unit tests!')
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
gcs_storage = GoogleCloudStorageBucket(str(project_id))
|
|
||||||
# FIXME: don't use internal property, but use our bucket/blob API.
|
|
||||||
if gcs_storage._gcs_bucket.exists():
|
|
||||||
log.info('Created GCS instance for project %s', project_id)
|
|
||||||
else:
|
|
||||||
log.warning('Unable to create GCS instance for project %s', project_id)
|
|
||||||
except gcs_exceptions.Forbidden as ex:
|
|
||||||
log.warning('GCS forbids me to create CGS instance for project %s: %s', project_id, ex)
|
|
||||||
|
|
||||||
# Commit the changes directly to the MongoDB; a PUT is not allowed yet,
|
# Commit the changes directly to the MongoDB; a PUT is not allowed yet,
|
||||||
# as the project doesn't have a valid permission structure.
|
# as the project doesn't have a valid permission structure.
|
||||||
|
Reference in New Issue
Block a user