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

Local dagger module wraping external modules #17

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Thumbs.db
.sass-cache
npm-debug.log
node_modules
secret.txt
builds
public
.env
Expand Down
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@

FROM docker.io/nginxinc/nginx-unprivileged:1.27-alpine

LABEL maintainer acend.ch
LABEL org.opencontainers.image.title "puzzle.ch's Dagger Basics Training"
LABEL org.opencontainers.image.description "Container with acend.ch's Dagger Techlab content"
LABEL org.opencontainers.image.authors acend.ch
LABEL org.opencontainers.image.source https://github.com/puzzle/dagger-techlab/
LABEL org.opencontainers.image.licenses CC-BY-SA-4.0
LABEL maintainer=acend.ch
LABEL org.opencontainers.image.title="puzzle.ch's Dagger Basics Training"
LABEL org.opencontainers.image.description="Container with acend.ch's Dagger Techlab content"
LABEL org.opencontainers.image.authors=acend.ch
LABEL org.opencontainers.image.source =ttps://github.com/puzzle/dagger-techlab/

Check warning on line 32 in Dockerfile

View workflow job for this annotation

GitHub Actions / build

Legacy key/value format with whitespace separator should not be used

LegacyKeyValueFormat: "LABEL key=value" should be used instead of legacy "LABEL key value" format More info: https://docs.docker.com/go/dockerfile/rule/legacy-key-value-format/
LABEL org.opencontainers.image.licenses=CC-BY-SA-4.0

EXPOSE 8080

Expand Down
64 changes: 36 additions & 28 deletions content/en/docs/01/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ It is similar to the [MvnRepository](https://mvnrepository.com/). The MvnReposit
The most common way to call Dagger Functions is using the `dagger` CLI:

```bash
dagger call --mod github.com/shykes/daggerverse/[email protected] hello
dagger call --mod ./mod hello
```

The `dagger` CLI first loads a `hello` module directly from its [GitHub repository](https://github.com/shykes/daggerverse/tree/main/hello) and then executes the `Hello()` function from that module.
The `dagger` CLI first loads the modules dependencies and then executes the local modules `Hello()` function.

{{% alert title="Note" color="primary" %}}
Explanation to the dagger CLI call.\
`dagger call` : execute the dagger CLI `call` command\
`--mod github.com/shykes/daggerverse/[email protected]` : `call` command option to use the `hello` module (load its functions)\
`hello` : execute the `hello` function
`--mod ./mod` : `call` command option to use the specified local module (load its functions)\
`hello` : execute the `Hello()` function
{{% /alert %}}

After a while you should see:
Expand All @@ -80,24 +80,26 @@ hello, world!
```

{{% alert title="Note" color="primary" %}}
Due to Daggers caching mechanism, subsequent calls will be executed much faster!
The first execution will take a considerable amount of time, as the module depends on several other modules
which have to be downloaded.
For this reason and thanks to Daggers caching mechanism, subsequent calls will be executed **much** faster!
{{% /alert %}}


### Exploring Modules and Functions

If you are curious, what other [Functions](https://docs.dagger.io/api/reference/#definition-Function) are available on this [Module](https://docs.dagger.io/api/reference/#definition-Module), you can either have a look at its [source code](https://github.com/shykes/daggerverse/blob/main/hello/main.go)
If you are curious, what other [Functions](https://docs.dagger.io/api/reference/#definition-Function) are available on this module, you can either have a look at its [source code](https://github.com/puzzle/dagger-techlab/blob/main/mod/main.go)
or you can explore its functions using:

```bash
dagger functions --mod github.com/shykes/daggerverse/[email protected]
dagger functions --mod ./mod
```

In this particular case, there aren't any other functions :( - but what about additional arguments of the `Hello()` function?
And what about additional arguments of the `Hello()` function?
Let's find out:

```bash
dagger call --mod github.com/shykes/daggerverse/[email protected] hello --help
dagger call --mod ./mod hello --help
```

{{% alert title="Note" color="primary" %}}
Expand All @@ -116,7 +118,7 @@ Dagger also defines powerful core types: [Container](https://docs.dagger.io/api/
To pass a String argument to a Dagger Function, append the corresponding flag to the dagger call command, followed by the string value:

```bash
dagger call --mod github.com/shykes/daggerverse/[email protected] hello --name=sun
dagger call --mod ./mod hello --name=sun
```


Expand All @@ -131,24 +133,24 @@ True:

```bash
# explicit
dagger call --mod github.com/shykes/daggerverse/[email protected] hello --shout=true
dagger call --mod ./mod hello --shout=true
```

```bash
# implicit
dagger call --mod github.com/shykes/daggerverse/[email protected] hello --shout
dagger call --mod ./mod hello --shout
```

False:

```bash
# explicit
dagger call --mod github.com/shykes/daggerverse/[email protected] hello --shout=false
dagger call --mod ./mod hello --shout=false
```

```bash
# implicit
dagger call --mod github.com/shykes/daggerverse/[email protected] hello
dagger call --mod ./mod hello
```


Expand All @@ -161,12 +163,12 @@ and pass the resulting `Directory` object as argument to the Dagger Function.

Filesystem path:
```bash
dagger call --mod github.com/softwaredevelop/daggerverse/shellcheck@3872d4fb4e5b0e8a2844b2148ea00c076396a53b check --source=/usr/bin
dagger call --mod ./mod ls --dir .
```

Git repository:
```bash
dagger call --mod github.com/softwaredevelop/daggerverse/shellcheck@3872d4fb4e5b0e8a2844b2148ea00c076396a53b check --source=https://github.com/puzzle/action-owasp-dependecy-track-check
dagger call --mod ./mod ls --dir https://github.com/puzzle/action-owasp-dependecy-track-check
```


Expand All @@ -177,11 +179,12 @@ Same as directories, you can pass a Container argument. To do so, add the corres
The CLI will dynamically pull the image, and pass the resulting `Container` object as argument to the Dagger Function.

```bash
dagger call --mod github.com/jpadams/daggerverse/[email protected] scan-container --ctr=alpine:latest
dagger --mod ./mod call os --ctr=alpine:latest
```

{{% alert title="Note" color="primary" %}}
It is important to know that in Dagger, a `Container` object is not merely a string referencing an image on a remote registry.
It is the **actual state of a container**, managed by the Dagger Engine, and passed to a Dagger Function's code as if it were just another variable!
It is the **actual state of a container**, managed by the Dagger Engine, and passed to a Dagger Functions code as if it were just another variable!
{{% /alert %}}


Expand All @@ -193,16 +196,22 @@ writing them into the filesystem of containers you're building, or inserting the
To pass a Secret to a Dagger Function, source it from a host environment variable `env:`, the host filesystem `file:`, or a host command `cmd:`.

Here is an example of passing a GitHub access token from an environment variable named `GITHUB_TOKEN` to a Dagger Function.

The Dagger Function uses the token to query the GitHub CLI for a list of issues in the Dagger open-source repository:

```bash
dagger call --mod github.com/aweris/daggerverse/[email protected] run --token=env:GITHUB_TOKEN --cmd="issue list --repo=dagger/dagger"
```

{{% alert title="Note" color="primary" %}}
This is only an example, you don't have to make it run.
{{% /alert %}}


## Task {{% param sectionnumber %}}.1: Explore a module

Explore the `github.com/purpleclay/daggerverse/[email protected]` module.

Make it return the phrase `Dagger puts a smile on my face!`.

{{% details title="show hint" mode-switcher="normalexpertmode" %}}
Expand All @@ -226,28 +235,27 @@ dagger call --mod github.com/purpleclay/daggerverse/[email protected] say --msg="Da

## Task {{% param sectionnumber %}}.2: Make use of multiple arguments

Call the `Hello()` function of `github.com/shykes/daggerverse/[email protected]` so that it returns the phrase `Welcome, sunshine!` in ASCII-art.
Call the `Hello()` function so that it returns the phrase `Welcome, sunshine!` in ASCII-art (giant letters).

{{% details title="show solution" mode-switcher="normalexpertmode" %}}
```bash
dagger call --mod github.com/shykes/daggerverse/[email protected] hello --giant --greeting=Welcome --name=sunshine
dagger call --mod ./mod hello --giant --greeting=Welcome --name=sunshine
```
{{% /details %}}


## Task {{% param sectionnumber %}}.3: Pass a secret

Set and replace the `--token` value in the following call with a secret using an environment variable
Set the `--password` value in the following call with a secret, using an environment variable, containing the password "MySuperSecret".

```bash
dagger call --mod github.com/aweris/daggerverse/[email protected] run --token=visible --cmd="issue list --repo=dagger/dagger"
dagger call --mod ./mod unlock --password=visible
```


{{% details title="show solution" mode-switcher="normalexpertmode" %}}
```bash
export SECRET=invisible
dagger call --mod github.com/aweris/daggerverse/[email protected] run --token=env:SECRET --cmd="issue list --repo=dagger/dagger"
export SECRET=MySuperSecret
dagger call --mod ./mod unlock --password env:SECRET
```
{{% /details %}}

Expand All @@ -256,18 +264,18 @@ or using a file
{{% details title="show solution" mode-switcher="normalexpertmode" %}}
```bash
echo $SECRET > secret.txt
dagger call --mod github.com/aweris/daggerverse/[email protected] run --token=file:./secret.txt --cmd="issue list --repo=dagger/dagger"
dagger call --mod ./mod unlock --password file:./secret.txt
```
{{% /details %}}

or using a command

{{% details title="show solution" mode-switcher="normalexpertmode" %}}
```bash
dagger call --mod github.com/aweris/daggerverse/[email protected] run --token=cmd:"head -c10 /dev/random | base64" --cmd="issue list --repo=dagger/dagger"
dagger call --mod ./mod unlock --password cmd:"echo $SECRET"
```
{{% /details %}}

{{% alert title="Note" color="primary" %}}
Unless you provide a working token, the function execution will fail with an `HTTP 401` error.
Unless you provide the right password, the function execution will fail with an error.
{{% /alert %}}
45 changes: 25 additions & 20 deletions content/en/docs/02/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,33 @@ are all objects. They each define various functions for interacting with their r
Let's explore them step by step:

```bash
dagger call --mod github.com/sagikazarmark/daggerverse/[email protected] --help
dagger call --mod ./mod --help
```

{{% details title="show available 'module' functions" mode-switcher="normalexpertmode" %}}
```
USAGE
dagger call [options] [arguments] <function>

FUNCTIONS
container
host-keys Returns the SSH host keys.
known-hosts Return a formatted SSH known_hosts file.
---> service Return a service that runs the OpenSSH server.
with-authorized-key Authorize a public key.
with-config Mount a custom SSH configuration file (with .conf extension).
FUNCTIONS
hello Say hello to the world!
lint Lint a Python codebase
ls Returns the files of the directory
os Returns the operating system of the container
---> ssh-service Returns a service that runs an OpenSSH server
unlock Returns the answer to everything when the password is right
wolfi Build a Wolfi Linux container
```
{{% /details %}}

```bash
dagger call --mod github.com/sagikazarmark/daggerverse/[email protected] service --help
dagger call --mod ./mod ssh-service --help
```

{{% details title="show available 'service' object functions" mode-switcher="normalexpertmode" %}}
```
USAGE
dagger call service [arguments] <function>
dagger call ssh-service [arguments] <function>

FUNCTIONS
endpoint Retrieves an endpoint that clients can use to reach this container.
Expand All @@ -55,13 +56,13 @@ dagger call --mod github.com/sagikazarmark/daggerverse/[email protected] ser
{{% /details %}}

```bash
dagger call --mod github.com/sagikazarmark/daggerverse/[email protected] service up --help
dagger call --mod ./mod ssh-service up --help
```

{{% details title="show available 'up' function arguments" mode-switcher="normalexpertmode" %}}
```
USAGE
dagger call service up [arguments]
dagger call ssh-service up [arguments]

ARGUMENTS
---> --ports PortForward List of frontend/backend port mappings to forward.
Expand All @@ -72,44 +73,48 @@ dagger call --mod github.com/sagikazarmark/daggerverse/[email protected] ser
{{% /details %}}

Now that we have got all the pieces together, let's expose a Service returned by a Dagger Function on a specified host port,
by chaining a call to the `Service` object's `Up()` function:
by chaining a call to the `Service` objects `Up()` function:

{{% alert title="Note" color="primary" %}}
The `Service` object is returned by the modules `SshService()` function.
{{% /alert %}}

```bash
dagger call --mod github.com/sagikazarmark/daggerverse/[email protected] service up --ports=22022:22
dagger call --mod ./mod ssh-service up --ports=22022:22
```

Here we print the contents of a File returned by a Dagger Function, by chaining a call to the `File` object's `Contents()` function:
Here we print the contents of a File returned by a Dagger Function, by chaining a call to the `File` objects `Contents()` function:

```bash
dagger call --mod github.com/dagger/dagger/dev/ruff@a29dadbb5d9968784847a15fccc5629daf2985ae lint --source=https://github.com/puzzle/puzzle-radicale-auth-ldap report contents
dagger call --mod ./mod lint --source=https://github.com/puzzle/puzzle-radicale-auth-ldap report contents
```


### Task {{% param sectionnumber %}}.1: Chain calls

Display and return the contents of the `/etc/os-release` file in a container, by chaining additional calls to the `Container`
object of the `github.com/shykes/daggerverse/[email protected]` module:
object, returned by the modules `Wolfi()` function:

{{% details title="show hint" mode-switcher="normalexpertmode" %}}
Have a look at the [WithExec()](https://docs.dagger.io/api/reference/#Container-withExec) and [Stout()](https://docs.dagger.io/api/reference/#Container-stdout) functions.
{{% /details %}}

{{% details title="show solution" mode-switcher="normalexpertmode" %}}
```bash
dagger call --mod github.com/shykes/daggerverse/[email protected] container with-exec --args="cat","/etc/os-release" stdout
dagger call --mod ./mod wolfi with-exec --args="cat","/etc/os-release" stdout
```
{{% /details %}}

Try an alternative approach using [File()](https://docs.dagger.io/api/reference/#definition-File) instead.

{{% details title="show hint" mode-switcher="normalexpertmode" %}}
```bash
dagger call --mod github.com/shykes/daggerverse/[email protected] container file --help
dagger call --mod ./mod wolfi file --help
```
{{% /details %}}

{{% details title="show solution" mode-switcher="normalexpertmode" %}}
```bash
dagger call --mod github.com/shykes/daggerverse/[email protected] container file --path=/etc/os-release contents
dagger call --mod ./mod wolfi file --path=/etc/os-release contents
```
{{% /details %}}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ github.com/acend/docsy-plus v1.0.0 h1:Ag2xQv15gwqPnsvWSBP8GKAnRrctVkADwaG3Qymt5w
github.com/acend/docsy-plus v1.0.0/go.mod h1:YDHqf+DCZcx5HvKGzaBluPmLfgHQ2GKkYjggvF98jR4=
github.com/acend/docsy-plus v1.1.0 h1:MgHPR3YRPrJSWtMS3eQKJivdCEwFHCIKD0jChsZS3SM=
github.com/acend/docsy-plus v1.1.0/go.mod h1:LPbI0Ljrhzt0YHUg8qozWVUXjrMVI1cFVPn3TyQxbcY=
github.com/acend/docsy-plus v1.2.0 h1:MJaMdkqXU6ws7A+6Lzhx4qGvncifund3NF44Tzs7iVM=
github.com/acend/docsy-plus v1.2.0/go.mod h1:LPbI0Ljrhzt0YHUg8qozWVUXjrMVI1cFVPn3TyQxbcY=
github.com/google/docsy v0.4.0 h1:Eyt2aiDC1fnw/Qq/9xnIqUU5n5Yyk4c8gX3nBDdTv/4=
github.com/google/docsy v0.4.0/go.mod h1:vJjGkHNaw9bO42gpFTWwAUzHZWZEVlK46Kx7ikY5c7Y=
Expand Down
4 changes: 4 additions & 0 deletions mod/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/dagger.gen.go linguist-generated
/internal/dagger/** linguist-generated
/internal/querybuilder/** linguist-generated
/internal/telemetry/** linguist-generated
4 changes: 4 additions & 0 deletions mod/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/dagger.gen.go
/internal/dagger
/internal/querybuilder
/internal/telemetry
4 changes: 4 additions & 0 deletions mod/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Dagger Module fot the Dagger techlab

Helper module for the [Dagger Techlab](https://dagger-techlab.puzzle.ch/)

24 changes: 24 additions & 0 deletions mod/dagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "mod",
"sdk": "go",
"dependencies": [
{
"name": "hello",
"source": "github.com/shykes/daggerverse/hello@54d86c6002d954167796e41886a47c47d95a626d"
},
{
"name": "openssh-server",
"source": "github.com/sagikazarmark/daggerverse/openssh-server@c767a220a5f507555fd415e9c9729234589fd95c"
},
{
"name": "ruff",
"source": "github.com/dagger/dagger/dev/ruff@d9a15262229f1dba0aa53dbd0216908438331995"
},
{
"name": "wolfi",
"source": "github.com/shykes/daggerverse/wolfi@3338120927f8e291c4780de691ef63a7c9d825c0"
}
],
"source": ".",
"engineVersion": "v0.13.1"
}
Loading