Skip to content

Commit

Permalink
Use container instead of service to enable terminal calls from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Sep 4, 2024
1 parent 3805296 commit 622eab3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 51 deletions.
30 changes: 5 additions & 25 deletions content/en/docs/03/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ Hints:
{{% details title="show solution" mode-switcher="normalexpertmode" %}}
```python
@function
def backend(self, context: dagger.Directory) -> dagger.Service:
"""Returns a backend service from a container built with the given context, params and service bindings."""
def backend(self, context: dagger.Directory) -> dagger.SContainer:
"""Returns a backend container built with the given context, params and service bindings."""
return (
dag.container()
.with_env_variable("MAX_WORKERS", "1")
Expand All @@ -293,47 +293,27 @@ Hints:
.with_service_binding("meilisearchd", self.meilisearch())
.with_service_binding("redisd", self.redis())
.build(context)
.as_service()
)
```
{{% /details %}}

For convenience, the function returns directly a Service.

And the `frontend`:

{{% details title="show solution" mode-switcher="normalexpertmode" %}}
```python
@function
def frontend(self, context: dagger.Directory) -> dagger.Service:
"""Returns a frontend service from a container built with the given context and params."""
def frontend(self, context: dagger.Directory) -> dagger.Container:
"""Returns a frontend container built with the given context and params."""
return (
dag.container()
.with_env_variable("API_URL", "http://api:8081")
.with_env_variable("REDIS_URL", "redis://redisd:6379/0?decode_responses=True")
.build(context)
.as_service()
)
```
{{% /details %}}

Now the two service bindings in the `proxy` function can be simplified a bit.

Before:

```python
.with_service_binding("frontend", self.build(context.directory("frontend")).as_service())
.with_service_binding("api", self.build(context).as_service())
```

After:

```python
.with_service_binding("frontend", self.frontend(context.directory("frontend")))
.with_service_binding("api", self.backend(context))
```

Now we can finally run ClassQuiz locally:
Now we can run ClassQuiz locally:

```bash
dagger call proxy --context=. --proxy-config=Caddyfile-docker up --ports=8000:8080
Expand Down
14 changes: 6 additions & 8 deletions content/en/docs/03/solution/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
class ClassQuiz:

@function
def frontend(self, context: dagger.Directory) -> dagger.Service:
"""Returns a frontend service from a container built with the given context and params."""
def frontend(self, context: dagger.Directory) -> dagger.Container:
"""Returns a frontend container built with the given context and params."""
return (
dag.container()
.with_env_variable("API_URL", "http://api:8081")
.with_env_variable("REDIS_URL", "redis://redisd:6379/0?decode_responses=True")
.build(context)
.as_service()
)

@function
def backend(self, context: dagger.Directory) -> dagger.Service:
"""Returns a backend service from a container built with the given context, params and service bindings."""
def backend(self, context: dagger.Directory) -> dagger.Container:
"""Returns a backend container built with the given context, params and service bindings."""
return (
dag.container()
.with_env_variable("MAX_WORKERS", "1")
Expand All @@ -37,7 +36,6 @@ def backend(self, context: dagger.Directory) -> dagger.Service:
.with_service_binding("meilisearchd", self.meilisearch())
.with_service_binding("redisd", self.redis())
.build(context)
.as_service()
)

@function
Expand Down Expand Up @@ -79,8 +77,8 @@ def proxy(self, context: dagger.Directory, proxy_config: dagger.File) -> dagger.
return (
dag.container()
.from_("caddy:alpine")
.with_service_binding("frontend", self.frontend(context.directory("frontend")))
.with_service_binding("api", self.backend(context))
.with_service_binding("frontend", self.frontend(context.directory("frontend")).as_service())
.with_service_binding("api", self.backend(context).as_service())
.with_file("/etc/caddy/Caddyfile", proxy_config)
.with_exposed_port(8080)
.as_service()
Expand Down
14 changes: 6 additions & 8 deletions content/en/docs/04/solution/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ async def vulnerability_scan(self, context: dagger.Directory) -> dagger.Director
return directory

@function
def frontend(self, context: dagger.Directory) -> dagger.Service:
"""Returns a frontend service from a container built with the given context and params."""
def frontend(self, context: dagger.Directory) -> dagger.Container:
"""Returns a frontend container built with the given context and params."""
return (
dag.container()
.with_env_variable("API_URL", "http://api:8081")
.with_env_variable("REDIS_URL", "redis://redisd:6379/0?decode_responses=True")
.build(context)
.as_service()
)

@function
def backend(self, context: dagger.Directory) -> dagger.Service:
"""Returns a backend service from a container built with the given context, params and service bindings."""
def backend(self, context: dagger.Directory) -> dagger.Container:
"""Returns a backend container built with the given context, params and service bindings."""
return (
dag.container()
.with_env_variable("MAX_WORKERS", "1")
Expand All @@ -54,7 +53,6 @@ def backend(self, context: dagger.Directory) -> dagger.Service:
.with_service_binding("meilisearchd", self.meilisearch())
.with_service_binding("redisd", self.redis())
.build(context)
.as_service()
)

@function
Expand Down Expand Up @@ -96,8 +94,8 @@ def proxy(self, context: dagger.Directory, proxy_config: dagger.File) -> dagger.
return (
dag.container()
.from_("caddy:alpine")
.with_service_binding("frontend", self.frontend(context.directory("frontend")))
.with_service_binding("api", self.backend(context))
.with_service_binding("frontend", self.frontend(context.directory("frontend")).as_service())
.with_service_binding("api", self.backend(context).as_service())
.with_file("/etc/caddy/Caddyfile", proxy_config)
.with_exposed_port(8080)
.as_service()
Expand Down
3 changes: 2 additions & 1 deletion content/en/docs/05/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ add another one which executes the Python tests:
async def pytest(self, context: dagger.Directory) -> str:
"""Run pytest and return its output."""
return await (
dag.container().build(context)
dag.container()
.with_exec(["pip", "install", "--upgrade", "pip"])
.with_exec(["pip", "install", "--upgrade", "pytest"])
.with_exec(["pytest", "classquiz/tests/", "--ignore=classquiz/tests/test_server.py"])
.build(context)
.stdout()
)
```
Expand Down
17 changes: 8 additions & 9 deletions content/en/docs/05/solution/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ async def ci(self, context: dagger.Directory) -> dagger.Directory:
async def pytest(self, context: dagger.Directory) -> str:
"""Run pytest and return its output."""
return await (
dag.container().build(context)
dag.container()
.with_exec(["pip", "install", "--upgrade", "pip"])
.with_exec(["pip", "install", "--upgrade", "pytest"])
.with_exec(["pytest", "classquiz/tests/", "--ignore=classquiz/tests/test_server.py"])
.build(context)
.stdout()
)

Expand All @@ -39,19 +40,18 @@ async def vulnerability_scan(self, context: dagger.Directory) -> dagger.Director
return directory

@function
def frontend(self, context: dagger.Directory) -> dagger.Service:
"""Returns a frontend service from a container built with the given context and params."""
def frontend(self, context: dagger.Directory) -> dagger.Container:
"""Returns a frontend container built with the given context and params."""
return (
dag.container()
.with_env_variable("API_URL", "http://api:8081")
.with_env_variable("REDIS_URL", "redis://redisd:6379/0?decode_responses=True")
.build(context)
.as_service()
)

@function
def backend(self, context: dagger.Directory) -> dagger.Service:
"""Returns a backend service from a container built with the given context, params and service bindings."""
def backend(self, context: dagger.Directory) -> dagger.Container:
"""Returns a backend container built with the given context, params and service bindings."""
return (
dag.container()
.with_env_variable("MAX_WORKERS", "1")
Expand All @@ -71,7 +71,6 @@ def backend(self, context: dagger.Directory) -> dagger.Service:
.with_service_binding("meilisearchd", self.meilisearch())
.with_service_binding("redisd", self.redis())
.build(context)
.as_service()
)

@function
Expand Down Expand Up @@ -113,8 +112,8 @@ def proxy(self, context: dagger.Directory, proxy_config: dagger.File) -> dagger.
return (
dag.container()
.from_("caddy:alpine")
.with_service_binding("frontend", self.frontend(context.directory("frontend")))
.with_service_binding("api", self.backend(context))
.with_service_binding("frontend", self.frontend(context.directory("frontend")).as_service())
.with_service_binding("api", self.backend(context).as_service())
.with_file("/etc/caddy/Caddyfile", proxy_config)
.with_exposed_port(8080)
.as_service()
Expand Down

0 comments on commit 622eab3

Please sign in to comment.