Skip to content

Commit

Permalink
Merge pull request #17 from puzzle/dagger-module-reusing-external-module
Browse files Browse the repository at this point in the history
Local dagger module wraping external modules
  • Loading branch information
chrira authored Nov 12, 2024
2 parents 8e371b2 + b620dc2 commit abade41
Show file tree
Hide file tree
Showing 12 changed files with 428 additions and 60 deletions.
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 @@ RUN wkhtmltopdf --enable-internal-links --enable-local-file-access \

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=puzzle.ch
LABEL org.opencontainers.image.title="puzzle.ch's Dagger Basics Training"
LABEL org.opencontainers.image.description="Container with puzzle.ch's Dagger Techlab content"
LABEL org.opencontainers.image.authors=puzzle.ch
LABEL org.opencontainers.image.source=https://github.com/puzzle/dagger-techlab/
LABEL org.opencontainers.image.licenses=CC-BY-SA-4.0

EXPOSE 8080

Expand Down
124 changes: 90 additions & 34 deletions content/en/docs/01/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,22 @@ 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 github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
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 `dagger-techlab-module` module directly from its [GitHub repository](https://github.com/puzzle/dagger-techlab/tree/main/mod) and then executes the `Hello()` function from that module.

{{% 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
Explanation to the dagger CLI call:

* `dagger call`:
* execute the dagger CLI `call` command
* `--mod github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63`:
* `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 +86,29 @@ 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 github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63
```

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 github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
hello --help
```

{{% alert title="Note" color="primary" %}}
Expand All @@ -116,7 +127,9 @@ 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 github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
hello --name=sun
```


Expand All @@ -131,24 +144,32 @@ True:

```bash
# explicit
dagger call --mod github.com/shykes/daggerverse/[email protected] hello --shout=true
dagger call \
--mod github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
hello --shout=true
```

```bash
# implicit
dagger call --mod github.com/shykes/daggerverse/[email protected] hello --shout
dagger call \
--mod github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
hello --shout
```

False:

```bash
# explicit
dagger call --mod github.com/shykes/daggerverse/[email protected] hello --shout=false
dagger call \
--mod github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
hello --shout=false
```

```bash
# implicit
dagger call --mod github.com/shykes/daggerverse/[email protected] hello
dagger call \
--mod github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
hello
```


Expand All @@ -161,12 +182,16 @@ 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 github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
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 github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
ls --dir https://github.com/puzzle/action-owasp-dependecy-track-check
```


Expand All @@ -177,11 +202,14 @@ 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 github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
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,61 +221,85 @@ 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"
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" %}}
```bash
dagger functions --mod github.com/purpleclay/daggerverse/[email protected]
dagger functions \
--mod github.com/purpleclay/daggerverse/[email protected]
```
{{% /details %}}

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

{{% details title="show solution" mode-switcher="normalexpertmode" %}}
```bash
dagger call --mod github.com/purpleclay/daggerverse/[email protected] say --msg="Dagger puts a smile on my face!"
dagger call \
--mod github.com/purpleclay/daggerverse/[email protected] \
say --msg="Dagger puts a smile on my face!"
```
{{% /details %}}


## 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 github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
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 github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
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 github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
unlock --password env:SECRET
```
{{% /details %}}

Expand All @@ -256,18 +308,22 @@ 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 github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
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 github.com/puzzle/dagger-techlab/mod@0437877e0809d7aef35ea031a3a36eb943876e63 \
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 %}}
Loading

0 comments on commit abade41

Please sign in to comment.