Skip to content
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
2 changes: 2 additions & 0 deletions scripts/checkyamlcompliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
api_ver = False
if "kind" in yamldata.keys():
match yamldata["kind"]:
case "ClusterWorkflowTemplate":
clst_tmpt = True
case "Workflow":
clst_tmpt = True
case "WorkflowTemplate":
Expand Down
2 changes: 2 additions & 0 deletions src/copier_template/scripts/checkyamlcompliance.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ for file in yamllist:
api_ver = False
if "kind" in yamldata.keys():
match yamldata["kind"]:
case "ClusterWorkflowTemplate":
clst_tmpt = True
case "Workflow":
clst_tmpt = True
case _:
Expand Down
6 changes: 3 additions & 3 deletions src/copier_template/src/README.md.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ AUTH=

3. Build the dev container

# Submitting a workflow to Argo
# Submitting a workflow
1. At the start of your workflow definition file, add:

```python
from {{project_name}}.submit_to_argo import submit_workflow_to_argo
from {{project_name}}.submit_to_graphql import submit_workflow
```

2. At the end of a workflow definition file, once your workflow (w) is created, add:
```python
submit_workflow_to_argo(w)
submit_workflow(w)
```

NOTE: Be sure to remove this line upon commiting changes, as all workflow definition files are
Expand Down
24 changes: 0 additions & 24 deletions src/copier_template/src/{{ project_name }}/submit_to_argo.py.jinja

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{% raw %}
import os

import dotenv
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
from hera.workflows import Workflow

from {% endraw %}{{project_name}}{% raw %}.auth.keycloak_checker import set_token_env_variable


def submit_workflow(w: Workflow):
yamlstr = w.to_yaml() # pyright:ignore
dotenv.load_dotenv(dotenv_path="src/.env", override=True)
token: str = set_token_env_variable(True)
host: str = os.environ.get("HOST") # pyright:ignore
visit: str = os.environ.get("VISIT") # pyright:ignore

transport = AIOHTTPTransport(
url=host,
headers={"Authorization": f"Bearer {token}"},
)
client = Client(
transport=transport,
fetch_schema_from_transport=True,
)
mutation = gql("""
mutation Submit($visit: VisitInput!, $manifest: String!) {
submitWorkflow(
visit: $visit
manifest: $manifest
) {
name
}
}
""")
result = client.execute(
mutation,
variable_values={
"visit": {
"proposalCode": str(visit[:2]),
"proposalNumber": int(visit[2:7]),
"number": int(visit[-1]),
},
"manifest": f"""{yamlstr}""",
},
)
name = str(result["submitWorkflow"]["name"])
print(f"Job '{name}' submitted to {visit}")
{% endraw %}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
generateName: hera-example-
name: hera-example
annotations:
workflows.argoproj.io/description: |-
Replicates the functionality of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def to_hdf5(paths: str):


with Workflow(
generate_name="hera-example-", # when running on graphql this should be name
name="hera-example", # when running on argo this should be generate_name: ...-
entrypoint="workflowentry",
api_version="argoproj.io/v1alpha1",
kind="WorkflowTemplate", # ClusterWorkflowTemplate", when on graphql
Expand Down
30 changes: 20 additions & 10 deletions src/copier_template/tests/test_submit_to_graphql.py.jinja
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
from unittest.mock import MagicMock, patch

from {{project_name}}.submit_to_graphql import submit_to_graphql
from {{project_name}}.submit_workflow import submit_workflow


@patch("{{project_name}}.submit_to_graphql.set_token_env_variable")
@patch("{{project_name}}.submit_to_graphql.Client")
def test_submit_to_graphql(mock_client: MagicMock, mock_key: MagicMock):
mock_key.return_value = "fake_token"
@patch("{{project_name}}.submit_workflow.os.environ.get")
@patch("{{project_name}}.submit_workflow.dotenv.load_dotenv")
@patch("{{project_name}}.submit_workflow.Workflow")
@patch("{{project_name}}.submit_workflow.set_token_env_variable")
@patch("{{project_name}}.submit_workflow.Client")
def test_submit_workflow_to_graphql(
mock_client: MagicMock,
mock_key: MagicMock,
mock_workflow: MagicMock,
mock_load_env: MagicMock,
mock_os_get: MagicMock,
):

mock_instance = MagicMock()
mock_token.return_value = "token"
mock_key.return_value = "token"
mock_client.return_value = mock_instance
mock_instance.execute.return_value = {
"submitWorkflowTemplate": {"name": "workflow123"}
}
submit_to_graphql()
mock_instance.execute.return_value = {"submitWorkflow": {"name": "workflow123"}}
submit_workflow(mock_workflow)
mock_load_env.assert_called_once_with(dotenv_path="src/.env", override=True)
mock_instance.execute.assert_called_once()
mock_workflow.to_yaml.assert_called_once()
mock_os_get.assert_has_calls([call("VISIT"), call("HOST")], any_order=True)
20 changes: 0 additions & 20 deletions src/copier_template/tests/test_submit_workflow_to_argo.jinja

This file was deleted.

24 changes: 0 additions & 24 deletions src/python_interface_to_workflows/submit_to_argo.py

This file was deleted.

38 changes: 0 additions & 38 deletions src/python_interface_to_workflows/submit_to_graphql.py

This file was deleted.

48 changes: 48 additions & 0 deletions src/python_interface_to_workflows/submit_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os

import dotenv
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
from hera.workflows import Workflow

from python_interface_to_workflows.auth.keycloak_checker import set_token_env_variable


def submit_workflow(w: Workflow):
yamlstr = w.to_yaml() # pyright:ignore
dotenv.load_dotenv(dotenv_path="src/.env", override=True)
token: str = set_token_env_variable(True)
host: str = os.environ.get("HOST") # pyright:ignore
visit: str = os.environ.get("VISIT") # pyright:ignore

transport = AIOHTTPTransport(
url=host,
headers={"Authorization": f"Bearer {token}"},
)
client = Client(
transport=transport,
fetch_schema_from_transport=True,
)
mutation = gql("""
mutation Submit($visit: VisitInput!, $manifest: String!) {
submitWorkflow(
visit: $visit
manifest: $manifest
) {
name
}
}
""")
result = client.execute(
mutation,
variable_values={
"visit": {
"proposalCode": str(visit[:2]),
"proposalNumber": int(visit[2:7]),
"number": int(visit[-1]),
},
"manifest": f"""{yamlstr}""",
},
)
name = str(result["submitWorkflow"]["name"])
print(f"Job '{name}' submitted to {visit}")
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
generateName: hera-division-
name: hera-division
annotations:
workflows.argoproj.io/description: |-
Takes a numerical input and returns
Expand Down
2 changes: 1 addition & 1 deletion src/python_interface_to_workflows/templates/example.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
generateName: hera-example-
name: hera-example
annotations:
workflows.argoproj.io/description: |-
Replicates the functionality of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def do_division(a: int, b: int):


with Workflow(
generate_name="hera-division-", # when running on graphql this should be name
name="hera-division", # when running on argo this should be generate_name: ...-
entrypoint="divide",
api_version="argoproj.io/v1alpha1",
kind="WorkflowTemplate", # ClusterWorkflowTemplate", when on graphql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def to_hdf5(paths: str):


with Workflow(
generate_name="hera-example-", # when running on graphql this should be name
name="hera-example", # when running on argo this should be generate_name: ...-
entrypoint="workflowentry",
api_version="argoproj.io/v1alpha1",
kind="WorkflowTemplate", # ClusterWorkflowTemplate", when on graphql
Expand Down
20 changes: 0 additions & 20 deletions tests/test_submit_to_argo.py

This file was deleted.

Loading
Loading