Skip to content

Commit

Permalink
fix: Allow to use the COMPOSE_FILE variable in finch compose (#994)
Browse files Browse the repository at this point in the history
nerdctl allows us to launch containers using a Docker Compose file
specified in the COMPOSE_FILE environment variable.

For example, suppose we have created the following Docker Compose file.

```
> cat a.yaml
services:
  test:
    image: amazonlinux:2023
```

By specifying this Docker Compose file in the COMPOSE_FILE environment
variable and running nerdctl compose up, we can start the container.

```
> sudo COMPOSE_FILE=a.yaml _output/nerdctl compose up
INFO[0000] Ensuring image amazonlinux:2023
INFO[0000] Re-creating container nerdctl-test-1
INFO[0000] Attaching to logs
INFO[0000] Container "nerdctl-test-1" exited
INFO[0000] All the containers have exited
INFO[0000] Stopping containers (forcibly)
INFO[0000] Stopping container nerdctl-test-1
```

However, since the COMPOSE_FILE environment variable is not passed in
finch compose, the following error occurs.

```
> COMPOSE_FILE=a.yaml finch compose up
FATA[0000] no configuration file provided: not found
FATA[0000] exit status 1
```

And this bug is reported in the following issue.

- #347

Therefore, this fix allows the finch compose command to use the Docker
Compose file specified in the COMPOSE_FILE environment variable.

Issue #, if available: #347

*Description of changes:* The details are described in this commit
message.

*Testing done:* Yes



- [x] I've reviewed the guidance in CONTRIBUTING.md


#### License Acceptance

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license.

---------

Signed-off-by: Hayato Kiwata <[email protected]>
  • Loading branch information
haytok authored Jun 28, 2024
1 parent 9d85f25 commit 17d4bc8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 6 deletions.
15 changes: 12 additions & 3 deletions cmd/finch/nerdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"path/filepath"
"runtime"
"strings"

"golang.org/x/exp/slices"
Expand Down Expand Up @@ -234,15 +235,23 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error {

passedEnvs := []string{
"COSIGN_PASSWORD", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN",
"AWS_SESSION_TOKEN", "COMPOSE_FILE",
}

var passedEnvArgs []string
for _, e := range passedEnvs {
v, b := nc.systemDeps.LookupEnv(e)
if b {
passedEnvArgs = append(passedEnvArgs, fmt.Sprintf("%s=%s", e, v))
if !b {
continue
}
if runtime.GOOS == "windows" && e == "COMPOSE_FILE" {
wslPath, err := convertToWSLPath(nc.systemDeps, v)
if err != nil {
return err
}
v = wslPath
}
passedEnvArgs = append(passedEnvArgs, fmt.Sprintf("%s=%s", e, v))
}

var additionalEnv []string
Expand Down
4 changes: 4 additions & 0 deletions cmd/finch/nerdctl_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"github.com/runfinch/finch/pkg/flog"
)

func convertToWSLPath(_ NerdctlCommandSystemDeps, _ string) (string, error) {
return "", nil
}

var aliasMap = map[string]string{}

var argHandlerMap = map[string]map[string]argHandler{}
Expand Down
25 changes: 25 additions & 0 deletions cmd/finch/nerdctl_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestNerdctlCommand_runAdaptor(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "info").Return(c)
c.EXPECT().Run()
Expand Down Expand Up @@ -114,6 +115,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "build", "-t", "demo", ".").Return(c)
c.EXPECT().Run()
Expand Down Expand Up @@ -143,6 +145,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
logger.EXPECT().SetLevel(flog.Debug)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "pull", "test:tag").Return(c)
c.EXPECT().Run()
Expand Down Expand Up @@ -174,6 +177,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("ARG2")
ncsd.EXPECT().LookupEnv("ARG3")
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)

lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
"--rm", "-e", "ARG1=val1", "alpine:latest", "env").Return(c)
Expand Down Expand Up @@ -206,6 +210,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("ARG2")
ncsd.EXPECT().LookupEnv("ARG3").Return("val3", true)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
"--rm", "-e", "ARG3=val3", "alpine:latest", "env").Return(c)
c.EXPECT().Run()
Expand Down Expand Up @@ -238,6 +243,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("ARG2")
ncsd.EXPECT().LookupEnv("ARG3").Return("val3", true)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
"--rm", "-e", "ARG3=val3", "alpine:latest", "env").Return(c)
c.EXPECT().Run()
Expand Down Expand Up @@ -272,6 +278,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("ARG2")
ncsd.EXPECT().LookupEnv("NOTSETARG")
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
lcc.EXPECT().
Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run", "--rm", "-e", "ARG1=val1", "alpine:latest", "env").
Return(c)
Expand Down Expand Up @@ -308,6 +315,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("ARG2")
ncsd.EXPECT().LookupEnv("NOTSETARG")
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
lcc.EXPECT().
Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run", "--rm", "-e", "ARG1=val1", "alpine:latest", "env").
Return(c)
Expand Down Expand Up @@ -343,6 +351,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("ARG2").Return("val2", true)
ncsd.EXPECT().LookupEnv("NOTSETARG")
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
lcc.EXPECT().
Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run", "--rm", "-e", "ARG2=val2", "alpine:latest", "env").
Return(c)
Expand Down Expand Up @@ -393,6 +402,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
logger.EXPECT().Debugf(`Resolving special IP "host-gateway" to %q for host %q`, "192.168.5.2", "name")
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
"--rm", "--add-host", "name:192.168.5.2", "alpine:latest").Return(c)
Expand Down Expand Up @@ -422,6 +432,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
"--rm", "--add-host", "name:0.0.0.0", "alpine:latest").Return(c)
Expand Down Expand Up @@ -451,6 +462,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
"--rm", "--add-host", "alpine:latest").Return(c)
Expand Down Expand Up @@ -481,6 +493,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
logger.EXPECT().Debugf(`Resolving special IP "host-gateway" to %q for host %q`, "192.168.5.2", "name")
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
"--rm", "--add-host=name:192.168.5.2", "alpine:latest").Return(c)
Expand Down Expand Up @@ -510,6 +523,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)

c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
Expand Down Expand Up @@ -543,6 +557,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "run",
"--rm", "-v", "/tmp:/tmp1/tmp2:rro", "--volume", "/tmp:/tmp1:rprivate,rro", "-v=/tmp:/tmp1/tmp2/tmp3/tmp4:rro",
Expand Down Expand Up @@ -576,6 +591,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "container", "run",
"--rm", "-v", "/tmp:/tmp1/tmp2:rro", "--volume", "/tmp:/tmp1:rprivate,rro",
Expand Down Expand Up @@ -606,6 +622,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
lcc.EXPECT().RunWithReplacingStdout(
testStdoutRs, "shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "pull", "test:tag", "--help").Return(nil)
},
Expand Down Expand Up @@ -633,6 +650,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
lcc.EXPECT().RunWithReplacingStdout(
testStdoutRs, "shell", limaInstanceName, "sudo", "-E", nerdctlCmdName, "pull", "test:tag", "--help").
Return(fmt.Errorf("failed to replace"))
Expand Down Expand Up @@ -661,6 +679,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("test", true)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", "COSIGN_PASSWORD=test", nerdctlCmdName,
"push", "--sign=cosign", "test:tag").Return(c)
Expand Down Expand Up @@ -690,6 +709,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("test", true)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", "COSIGN_PASSWORD=test", nerdctlCmdName,
"pull", "--verify=cosign", "test:tag").Return(c)
Expand Down Expand Up @@ -719,6 +739,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("test", true)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)
c := mocks.NewCommand(ctrl)
lcc.EXPECT().Create("shell", limaInstanceName, "sudo", "-E", "COSIGN_PASSWORD=test",
nerdctlCmdName, "pull", "test:tag").Return(c)
Expand Down Expand Up @@ -750,6 +771,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("TEST_SECRET_ACCESS_KEY", true)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("TEST_SESSION_TOKEN", true)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)

awsCmd := mocks.NewCommand(ctrl)
ecc.EXPECT().Create(
Expand Down Expand Up @@ -809,6 +831,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("TEST_SECRET_ACCESS_KEY", false)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("TEST_SESSION_TOKEN", false)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)

awsCmd := mocks.NewCommand(ctrl)
ecc.EXPECT().Create(
Expand Down Expand Up @@ -865,6 +888,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("TEST_SECRET_ACCESS_KEY", true)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("TEST_SESSION_TOKEN", true)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)

awsCmd := mocks.NewCommand(ctrl)
ecc.EXPECT().Create(
Expand Down Expand Up @@ -917,6 +941,7 @@ func TestNerdctlCommand_run(t *testing.T) {
ncsd.EXPECT().LookupEnv("AWS_SECRET_ACCESS_KEY").Return("TEST_SECRET_ACCESS_KEY", true)
ncsd.EXPECT().LookupEnv("AWS_SESSION_TOKEN").Return("TEST_SESSION_TOKEN", true)
ncsd.EXPECT().LookupEnv("COSIGN_PASSWORD").Return("", false)
ncsd.EXPECT().LookupEnv("COMPOSE_FILE").Return("", false)

awsCmd := mocks.NewCommand(ctrl)
ecc.EXPECT().Create(
Expand Down
Loading

0 comments on commit 17d4bc8

Please sign in to comment.