-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement check for gp execution
- Loading branch information
1 parent
9459307
commit bbf2288
Showing
7 changed files
with
47 additions
and
21 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
54 changes: 40 additions & 14 deletions
54
research_datastream/terraform/lambda_functions/checker/lambda_function.py
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 |
---|---|---|
@@ -1,30 +1,56 @@ | ||
import boto3 | ||
import time | ||
import re | ||
|
||
client_s3 = boto3.client('s3') | ||
|
||
def wait_for_object_existence(bucket_name,object_key): | ||
def wait_for_object_existence(bucket_name,prefix): | ||
|
||
ii_obj_found = False | ||
while not ii_obj_found: | ||
try: | ||
client_s3.head_object(Bucket=bucket_name, Key=object_key) | ||
print(f"Key: '{object_key}' found!") | ||
ii_obj_found = True | ||
except: | ||
while True: | ||
response = client_s3.list_objects_v2(Bucket=bucket_name, Prefix=prefix) | ||
if 'Contents' in response: | ||
print(f"Objects exists in bucket {bucket_name} at prefix {prefix}") | ||
return | ||
else: | ||
time.sleep(1) | ||
if not ii_obj_found: raise Exception(f'{object_key} does not exist in {bucket_name} ') | ||
|
||
def lambda_handler(event, context): | ||
|
||
bucket = None | ||
prefix = None | ||
if event["run_options"]['ii_check_s3']: | ||
if not 'datastream_command_options' in event: raise Exception(f'The lambda only knows how to check s3 object for datastream_command_options with s3_bucket and object_prefix set') | ||
bucket = event['datastream_command_options']['s3_bucket'] | ||
obj_key = event['datastream_command_options']['object_prefix'] + '/ngen-run.tar.gz' | ||
print(f'Checking if {obj_key} exists in {bucket}') | ||
wait_for_object_existence(bucket, obj_key) | ||
if not 'datastream_command_options' in event: | ||
for jcmd in event["commands"]: | ||
bucket_pattern = r"--s3_bucket[=\s']+([^\s']+)" | ||
match = re.search(bucket_pattern, jcmd) | ||
if match: | ||
bucket = match.group(1) | ||
prefix_pattern = r"--s3_prefix[=\s']+([^\s']+)" | ||
match = re.search(prefix_pattern, jcmd) | ||
if match: | ||
prefix = match.group(1) | ||
else: | ||
bucket = event['datastream_command_options']['s3_bucket'] | ||
prefix = event['datastream_command_options']['s3_prefix'] | ||
if bucket is None or prefix is None: | ||
raise Exception(f'User specified ii_check_s3, but no s3_bucket or s3_prefix were not found in commands') | ||
print(f'Checking if any objects with prefix {prefix} exists in {bucket}') | ||
wait_for_object_existence(bucket, prefix) | ||
else: | ||
print(f'No s3 object check was performed.') | ||
|
||
return event | ||
|
||
if __name__ == "__main__": | ||
|
||
import argparse, json | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--execution', dest="execution", type=str, help="",default = None) | ||
args = parser.parse_args() | ||
|
||
with open(args.execution,'r') as fp: | ||
execution = json.load(fp) | ||
|
||
lambda_handler(execution,"") | ||
|
||
|
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