Skip to content

Commit

Permalink
Np add firecloud api scripts (#1358)
Browse files Browse the repository at this point in the history
* pin all latest docker version

* add firecloud scripts

* add testing scripts
  • Loading branch information
nikellepetrillo authored Aug 15, 2024
1 parent 27f0e83 commit c350acb
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
53 changes: 53 additions & 0 deletions scripts/firecloud_api/GetWorkflowOutputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import requests
import argparse

def get_workflow_outputs(token, namespace, workspace_name, submission_id, workflow_id, pipeline_name):
# API endpoint to get the workflow outputs
url = f"https://api.firecloud.org/api/workspaces/{namespace}/{workspace_name}/submissions/{submission_id}/workflows/{workflow_id}/outputs"
print(f"Requesting URL: {url}")

# Headers including the authorization token
headers = {
'accept': '*/*',
'Authorization': f'Bearer {token}',
}

# Make the GET request
response = requests.get(url, headers=headers)

# Check if the request was successful
if response.status_code == 200:
json_response = response.json() # parse the JSON response
# extract the outputs section using the task name
outputs = json_response.get('tasks', {}).get(pipeline_name, {}).get('outputs', {})

# Turn the outputs dictionary into a list of values
output_values = list(outputs.values())

return outputs, output_values
else:
print(f"Failed to retrieve workflow outputs. Status code: {response.status_code}")
return None, None

if __name__ == "__main__":
# Define the command-line arguments
parser = argparse.ArgumentParser(description='Fetch workflow outputs from the API.')
parser.add_argument('--token', required=True, help='Authentication token')
parser.add_argument('--namespace', required=True, help='Workspace namespace')
parser.add_argument('--workspace', required=True, help='Workspace name')
parser.add_argument('--submission_id', required=True, help='Submission ID')
parser.add_argument('--workflow_id', required=True, help='Workflow ID')
parser.add_argument('--pipeline_name', required=True, help='Name of the pipeline')

# Parse the arguments
args = parser.parse_args()

# Call the function with the parsed arguments
outputs, output_values = get_workflow_outputs(args.token, args.namespace, args.workspace, args.submission_id, args.workflow_id, args.pipeline_name)

if outputs:
print("Outputs:")
print(outputs)

print("\nOutput Values:")
print(output_values)
90 changes: 90 additions & 0 deletions scripts/firecloud_api/SubmitWorkflowAndGetStatus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import requests
import time
import argparse
import json

def create_submission(token, workspace_namespace, workspace_name, submission_data):
# API endpoint
base_url = f'https://api.firecloud.org/api/workspaces/{workspace_namespace}/{workspace_name}/submissions'

# Headers to make API requests
headers = {
'accept': 'application/json',
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}

# Create the submission
# send an HTTP POST request to the API endpoint
response = requests.post(base_url, headers=headers, json=submission_data)
# convert the response json into a dictionary
submission_response = response.json()
# extract the submission ID from the response dictionary
submission_id = submission_response.get("submissionId")

if not submission_id:
print("Failed to create submission.")
else:
print(f"Submission created with ID: {submission_id}")
return submission_id

def poll_submission_status(token, workspace_namespace, workspace_name, submission_id):

# Status endpoint
status_url = f'https://api.firecloud.org/api/workspaces/{workspace_namespace}/{workspace_name}/submissions/{submission_id}'

# Headers to make API requests
headers = {
'accept': 'application/json',
'Authorization': f'Bearer {token}'
}

# polling the submission status
# create an emptu list to store the previous workflow status
previous_workflow_status = []

# loop until the submission is done
while True:
# send a get request and convert the response json into a dictionary
status_response = requests.get(status_url, headers=headers)
status_data = status_response.json()

# get the submission status
submission_status = status_data.get("status")
# get the workflow status of each workflow in the submission
workflows_status = [workflow.get("status") for workflow in status_data.get("workflows", [])]

# print the workflow status to stdout if it has changed
if workflows_status != previous_workflow_status:
print(f"Workflows Status: {workflows_status}")
previous_workflow_status = workflows_status

# Check if the submission has completed
if submission_status == "Done" and "Failed" in workflows_status:
print("At least one workflow has failed.")
break
elif submission_status == "Done":
break

# Wait for 10 seconds before polling again
time.sleep(10)

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Submit and monitor a job.')
parser.add_argument('--token', required=True, help='API access token')
parser.add_argument('--workspace-namespace', required=True, help='Workspace namespace')
parser.add_argument('--workspace-name', required=True, help='Workspace name')
parser.add_argument('--submission-data-file', required=True, help='Path to the JSON file containing submission data')

args = parser.parse_args()

# load submission data from JSON file
with open(args.submission_data_file, 'r') as file:
submission_data = json.load(file)

# create submission and get submission ID
submission_id = create_submission(args.token, args.workspace_namespace, args.workspace_name, submission_data)

if submission_id:
# Poll submission status
poll_submission_status(args.token, args.workspace_namespace, args.workspace_name, submission_id)

0 comments on commit c350acb

Please sign in to comment.