-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runner): add GCS driver and secator threads (#476)
- Loading branch information
Showing
15 changed files
with
353 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from pathlib import Path | ||
from time import time | ||
|
||
from google.cloud import storage | ||
|
||
from secator.config import CONFIG | ||
from secator.runners import Task | ||
from secator.thread import Thread | ||
from secator.utils import debug | ||
|
||
|
||
GCS_BUCKET_NAME = CONFIG.addons.gcs.bucket_name | ||
ITEMS_TO_SEND = { | ||
'url': ['screenshot_path'] | ||
} | ||
|
||
|
||
def process_item(self, item): | ||
if item._type not in ITEMS_TO_SEND.keys(): | ||
return item | ||
if not GCS_BUCKET_NAME: | ||
debug('skipped since addons.gcs.bucket_name is empty.', sub='hooks.gcs') | ||
return item | ||
to_send = ITEMS_TO_SEND[item._type] | ||
for k, v in item.toDict().items(): | ||
if k in to_send and Path(v).exists(): | ||
blob_name = f'{item._uuid}_{k}' | ||
t = Thread(target=upload_blob, args=(GCS_BUCKET_NAME, v, blob_name)) | ||
t.start() | ||
self.threads.append(t) | ||
setattr(item, k, f'gs://{GCS_BUCKET_NAME}/{blob_name}') | ||
return item | ||
|
||
|
||
def upload_blob(bucket_name, source_file_name, destination_blob_name): | ||
"""Uploads a file to the bucket.""" | ||
start_time = time() | ||
storage_client = storage.Client() | ||
bucket = storage_client.bucket(bucket_name) | ||
blob = bucket.blob(destination_blob_name) | ||
blob.upload_from_filename(source_file_name) | ||
end_time = time() | ||
elapsed = end_time - start_time | ||
debug(f'in {elapsed:.4f}s', obj={'blob': 'CREATED', 'blob_name': destination_blob_name, 'bucket': bucket_name}, obj_after=False, sub='hooks.gcs', verbose=True) # noqa: E501 | ||
|
||
|
||
HOOKS = { | ||
Task: {'on_item': [process_item]} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.