Skip to content

Commit

Permalink
restore setEnvWithDotEnv
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Jul 8, 2024
1 parent 11d5ecd commit 68d5d03
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 11 deletions.
33 changes: 32 additions & 1 deletion cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import (
"syscall"

"github.com/compose-spec/compose-go/v2/cli"
"github.com/compose-spec/compose-go/v2/dotenv"
"github.com/compose-spec/compose-go/v2/loader"
"github.com/compose-spec/compose-go/v2/types"
composegoutils "github.com/compose-spec/compose-go/v2/utils"
"github.com/docker/buildx/util/logutil"
dockercli "github.com/docker/cli/cli"
"github.com/docker/cli/cli-plugins/manager"
Expand Down Expand Up @@ -447,6 +449,11 @@ func RootCommand(dockerCli command.Cli, backend Backend) *cobra.Command { //noli
if verbose {
logrus.SetLevel(logrus.TraceLevel)
}

err := setEnvWithDotEnv(opts)
if err != nil {
return err
}
if noAnsi {
if ansi != "auto" {
return errors.New(`cannot specify DEPRECATED "--no-ansi" and "--ansi". Please use only "--ansi"`)
Expand Down Expand Up @@ -538,7 +545,7 @@ func RootCommand(dockerCli command.Cli, backend Backend) *cobra.Command { //noli
}

// dry run detection
ctx, err := backend.DryRunMode(ctx, dryRun)
ctx, err = backend.DryRunMode(ctx, dryRun)
if err != nil {
return err
}
Expand Down Expand Up @@ -638,6 +645,30 @@ func RootCommand(dockerCli command.Cli, backend Backend) *cobra.Command { //noli
return c
}

func setEnvWithDotEnv(opts ProjectOptions) error {
options, err := cli.NewProjectOptions(opts.ConfigPaths,
cli.WithWorkingDirectory(opts.ProjectDir),
cli.WithOsEnv,
cli.WithEnvFiles(opts.EnvFiles...),
cli.WithDotEnv,
)
if err != nil {
return nil
}
envFromFile, err := dotenv.GetEnvFromFile(composegoutils.GetAsEqualsMap(os.Environ()), options.EnvFiles)
if err != nil {
return nil
}
for k, v := range envFromFile {
if _, ok := os.LookupEnv(k); !ok {
if err = os.Setenv(k, v); err != nil {
return nil
}
}
}
return err
}

var printerModes = []string{
ui.ModeAuto,
ui.ModeTTY,
Expand Down
13 changes: 6 additions & 7 deletions pkg/compose/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,16 @@ func isRunning() containerPredicate {
}
}

func isNotService(services ...string) containerPredicate {
return func(c moby.Container) bool {
service := c.Labels[api.ServiceLabel]
return !utils.StringContains(services, service)
}
}

// isOrphaned is a predicate to select containers without a matching service definition in compose project
func isOrphaned(project *types.Project) containerPredicate {
services := append(project.ServiceNames(), project.DisabledServiceNames()...)
return func(c moby.Container) bool {
// One-off container
v, ok := c.Labels[api.OneoffLabel]
if ok && v == "True" {
return c.State == ContainerExited || c.State == ContainerDead
}
// Service that is not defined in the compose model
service := c.Labels[api.ServiceLabel]
return !utils.StringContains(services, service)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ func (s *composeService) create(ctx context.Context, project *types.Project, opt
return err
}

allServiceNames := append(project.ServiceNames(), project.DisabledServiceNames()...)
orphans := observedState.filter(isNotService(allServiceNames...))
orphans := observedState.filter(isOrphaned(project))
if len(orphans) > 0 && !options.IgnoreOrphans {
if options.RemoveOrphans {
err := s.removeContainers(ctx, orphans, nil, false)
Expand Down
4 changes: 4 additions & 0 deletions pkg/compose/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ func (s *composeService) stopAndRemoveContainer(ctx context.Context, container m
w := progress.ContextWriter(ctx)
eventName := getContainerProgressName(container)
err := s.stopContainer(ctx, w, container, timeout)
if errdefs.IsNotFound(err) {
w.Event(progress.RemovedEvent(eventName))
return nil
}
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/e2e/compose_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestLocalComposeRun(t *testing.T) {
"Hello one more time")
lines = Lines(res.Stdout())
assert.Equal(t, lines[len(lines)-1], "Hello one more time", res.Stdout())
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
assert.Assert(t, strings.Contains(res.Combined(), "orphan"))
})

t.Run("check run container exited", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/e2e/fixtures/orphans/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMPOSE_REMOVE_ORPHANS=true
7 changes: 7 additions & 0 deletions pkg/e2e/fixtures/orphans/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
orphan:
profiles: [run]
image: alpine
command: echo hello
test:
image: nginx:alpine
39 changes: 39 additions & 0 deletions pkg/e2e/orphans_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"strings"
"testing"

"gotest.tools/v3/assert"
)

func TestRemoveOrphans(t *testing.T) {
c := NewCLI(t)

const projectName = "compose-e2e-orphans"

c.RunDockerComposeCmd(t, "-f", "./fixtures/orphans/compose.yaml", "-p", projectName, "run", "orphan")
res := c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--all")
assert.Check(t, strings.Contains(res.Combined(), "compose-e2e-orphans-orphan-run-"))

c.RunDockerComposeCmd(t, "-f", "./fixtures/orphans/compose.yaml", "-p", projectName, "up", "-d")

res = c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--all")
assert.Check(t, !strings.Contains(res.Combined(), "compose-e2e-orphans-orphan-run-"))
}

0 comments on commit 68d5d03

Please sign in to comment.