import boto3 import os import json import time import subprocess import uuid from botocore.config import Config as BotoCoreConfig from dotenv import load_dotenv # Load env file if it exists load_dotenv(verbose=True) ENVIRONMENT = os.environ.get('Environment', 'dev') OUTFILE_DIR = os.environ.get('OUTFILE_DIR') SERVICE_USER = os.environ.get('SERVICE_USER') SERVICE_GROUP = os.environ.get('SERVICE_GROUP') region = os.environ.get('AWS_REGION') boto_config = BotoCoreConfig(read_timeout=70, region_name=region) sf_client = boto3.client('stepfunctions', config=boto_config) while True: response = sf_client.get_activity_task(activityArn=os.environ.get('PREPARE_FILE_SYSTEM_ARN'), workerName=ENVIRONMENT+'_prepare_file_system') if 'taskToken' not in response: print('No Task Token') time.sleep(5) else: print(response['taskToken']) print("===================") activity_token = response['taskToken'] try: generate_uuid = uuid.uuid4() outfile_dir = OUTFILE_DIR + str(generate_uuid) + '/raw' completed_process = subprocess.run(['install', '-v', '-d', '-m', '0775', '-o', SERVICE_USER, '-g', SERVICE_GROUP, outfile_dir], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(completed_process.stdout) partfile_dir = OUTFILE_DIR + str(generate_uuid) + '/file_parts' completed_process = subprocess.run(['install', '-v', '-d', '-m', '0775', '-o', SERVICE_USER, '-g', SERVICE_GROUP, partfile_dir], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(completed_process.stdout) except subprocess.CalledProcessError as exc: sf_client.send_task_failure(taskToken=activity_token, error=str(exc.stderr)) else: sf_client.send_task_success(taskToken=activity_token, output=json.dumps(str(generate_uuid)))