Skip to content

Commit

Permalink
Add chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
matbun committed Nov 5, 2024
1 parent e84c8c5 commit dd36593
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
4 changes: 4 additions & 0 deletions ci/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Example of running dagger pipeline of build and local test for torch container
dagger call \
build-torch --context=.. --dockerfile=../env-files/torch/Dockerfile \
test-torch
41 changes: 8 additions & 33 deletions ci/src/main/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
# For example, to import from src/main/main.py:
# >>> from .main import Itwinai as Itwinai

from typing import Annotated
from typing import Annotated, Optional, Self
import dataclasses

import dagger
from dagger import dag, function, object_type, Doc
Expand All @@ -30,7 +31,7 @@
@object_type
class Itwinai:

# torch_container: dagger.Container = None
torch_container: Optional[dagger.Container] = dataclasses.field(default=None, init=False)

@function
def build_torch(
Expand All @@ -43,25 +44,16 @@ def build_torch(
str,
Doc("location of Dockerfile"),
],
) -> dagger.Container:
) -> Self:
"""Build itwinai torch container image from existing Dockerfile"""
return (
self.torch_container = (
dag.container()
.build(context=context, dockerfile=dockerfile)
)
return self

@function
async def test_torch(
self,
context: Annotated[
dagger.Directory,
Doc("location of source directory"),
],
dockerfile: Annotated[
str,
Doc("location of Dockerfile"),
],
) -> str:
async def test_torch(self) -> str:
"""Test itwinai torch container image with pytest on non-HPC environments."""
test_cmd = [
"pytest",
Expand All @@ -71,24 +63,7 @@ async def test_torch(
"tests"
]
return await (
self.build_torch(context=context, dockerfile=dockerfile)
self.torch_container
.with_exec(test_cmd)
.stdout()
)

@function
def container_echo(self, string_arg: str) -> dagger.Container:
"""Returns a container that echoes whatever string argument is provided"""
return dag.container().from_("alpine:latest").with_exec(["echo", string_arg])

@function
async def grep_dir(self, directory_arg: dagger.Directory, pattern: str) -> str:
"""Returns lines that match a pattern in the files of the provided Directory"""
return await (
dag.container()
.from_("alpine:latest")
.with_mounted_directory("/mnt", directory_arg)
.with_workdir("/mnt")
.with_exec(["grep", "-R", pattern, "."])
.stdout()
)

0 comments on commit dd36593

Please sign in to comment.