Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more exception reporting to automate run failures #101

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/deploy-lambda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ jobs:
pip install --upgrade setuptools
pip list
- name: Check for vulnerabilities in libraries
# Safety is a tool that checks your installed dependencies for known security vulnerabilities.
# The free version stopped working with 3.0
run: |
pip install safety
pip install safety==2.3.5
pip freeze | safety check

- name: Test with pytest
Expand Down
104 changes: 58 additions & 46 deletions aws/globus_automate_flow.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import json
from typing import Mapping, Any, Optional, List

from globus_sdk import FlowsClient

from globus_sdk import FlowsClient, GlobusAPIError
import mdf_toolbox

from flow_action import FlowAction
from globus_auth_manager import GlobusAuthManager


class GlobusAutomateFlowDef:
def __init__(self,
flow_definition: Mapping[str, Any],
title: str,
subtitle: Optional[str] = None,
description: Optional[str] = None,
keywords: List[str] = [],
visible_to: List[str] = [],
runnable_by: List[str] = [],
administered_by: List[str] = [],
input_schema: Optional[Mapping[str, Any]] = None):
def __init__(
self,
flow_definition: Mapping[str, Any],
title: str,
subtitle: Optional[str] = None,
description: Optional[str] = None,
keywords: List[str] = [],
visible_to: List[str] = [],
runnable_by: List[str] = [],
administered_by: List[str] = [],
input_schema: Optional[Mapping[str, Any]] = None,
):
self.flow_definition = flow_definition
self.title = title
self.subtitle = subtitle
Expand All @@ -41,19 +42,25 @@ def __init__(self, client: FlowsClient, globus_auth: GlobusAuthManager = None):
self.globus_auth = globus_auth

@classmethod
def from_flow_def(cls, client: FlowsClient,
flow_def: GlobusAutomateFlowDef,
globus_auth: GlobusAuthManager = None):
def from_flow_def(
cls,
client: FlowsClient,
flow_def: GlobusAutomateFlowDef,
globus_auth: GlobusAuthManager = None,
):
result = GlobusAutomateFlow(client, globus_auth)
result._deploy_mdf_flow(flow_def)
return result

@classmethod
def from_existing_flow(cls, path: str = None,
flow_id: str = None,
flow_scope: str = None,
client: FlowsClient = None,
globus_auth: GlobusAuthManager = None):
def from_existing_flow(
cls,
path: str = None,
flow_id: str = None,
flow_scope: str = None,
client: FlowsClient = None,
globus_auth: GlobusAuthManager = None,
):
"""
Create a GlobusAutomateFlow object from an existing flow. The flow-id and
flow-scope can either come out of a json file, or be provided directly.
Expand All @@ -79,24 +86,22 @@ def url(self):
return "https://flows.globus.org/flows/" + self.flow_id

def __str__(self):
return f'Globus Automate Flow: id={self.flow_id}, scope={self.flow_scope}'
return f"Globus Automate Flow: id={self.flow_id}, scope={self.flow_scope}"

def get_runas_auth(self):
return mdf_toolbox.login(
services=[self.flow_scope],
make_clients=False)[self.flow_scope]
return mdf_toolbox.login(services=[self.flow_scope], make_clients=False)[
self.flow_scope
]

def get_status(self, action_id: str):
return self.flows_client.flow_action_status(
self.flow_id,
self.flow_scope,
action_id).data
self.flow_id, self.flow_scope, action_id
).data

def get_flow_logs(self, action_id: str):
return self.flows_client.flow_action_log(
self.flow_id, self.flow_scope,
action_id,
limit=100).data
self.flow_id, self.flow_scope, action_id, limit=100
).data

def update_flow(self, flow_def: GlobusAutomateFlowDef):
flow_deploy_res = self.flows_client.update_flow(
Expand All @@ -109,12 +114,12 @@ def update_flow(self, flow_def: GlobusAutomateFlowDef):
flow_starters=flow_def.runnable_by,
flow_administrators=flow_def.administered_by,
# TODO: Make rough schema outline into JSONSchema
input_schema=flow_def.input_schema
input_schema=flow_def.input_schema,
)
self.flow_id = flow_deploy_res["id"]
self.flow_scope = flow_deploy_res["globus_auth_scope"]
self.saved_flow = self.flows_client.get_flow(self.flow_id).data
self.runAsScopes = self.saved_flow['globus_auth_scopes_by_RunAs']
self.runAsScopes = self.saved_flow["globus_auth_scopes_by_RunAs"]
print(self.runAsScopes)

def _deploy_mdf_flow(self, mdf_flow_def: GlobusAutomateFlowDef):
Expand All @@ -127,35 +132,42 @@ def _deploy_mdf_flow(self, mdf_flow_def: GlobusAutomateFlowDef):
flow_starters=mdf_flow_def.runnable_by,
flow_administrators=mdf_flow_def.administered_by,
# TODO: Make rough schema outline into JSONSchema
input_schema=mdf_flow_def.input_schema
input_schema=mdf_flow_def.input_schema,
)
self.flow_id = flow_deploy_res["id"]
self.flow_scope = flow_deploy_res["globus_auth_scope"]
self.saved_flow = self.flows_client.get_flow(self.flow_id).data
self.runAsScopes = self.saved_flow['globus_auth_scopes_by_RunAs']
self.runAsScopes = self.saved_flow["globus_auth_scopes_by_RunAs"]
print(self.runAsScopes)

def run_flow(self, flow_input: dict, monitor_by: list = None, label=None):
flow_res = self.flows_client.run_flow(self.flow_id, self.flow_scope,
flow_input, monitor_by=monitor_by,
label=label)
return FlowAction(self, flow_res.data['action_id'])

try:
flow_res = self.flows_client.run_flow(
self.flow_id,
self.flow_scope,
flow_input,
monitor_by=monitor_by,
label=label,
)
except GlobusAPIError as e:
print(e.raw_json)
raise

return FlowAction(self, flow_res.data["action_id"])

def save_flow(self, path):
# Save Flow ID/scope for future use
with open(path, 'w') as f:
flow_info = {
"flow_id": self.flow_id,
"flow_scope": self.flow_scope
}
with open(path, "w") as f:
flow_info = {"flow_id": self.flow_id, "flow_scope": self.flow_scope}
json.dump(flow_info, f)

def read_flow(self, path):
# Save Flow ID/scope for future use
with open(path, 'r') as f:
with open(path, "r") as f:
flow_info = json.load(f)
self.flow_id = flow_info['flow_id']
self.flow_scope = flow_info['flow_scope']
self.flow_id = flow_info["flow_id"]
self.flow_scope = flow_info["flow_scope"]

def get_scope_id_for_runAs_role(self, rolename):
return self.globus_auth.scope_id_from_uri(self.runAsScopes[rolename])
Expand Down
Loading