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

[fix] --parallel has precedence over COMPOSE_PARALLEL_LIMIT #10566

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 30 additions & 5 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@
"github.com/docker/compose/v2/pkg/utils"
)

const (
// ComposeParallelLimit set the limit running concurrent operation on docker engine
ComposeParallelLimit = "COMPOSE_PARALLEL_LIMIT"
// ComposeProjectName define the project name to be used, instead of guessing from parent directory
ComposeProjectName = "COMPOSE_PROJECT_NAME"
// ComposeCompatibility try to mimic compose v1 as much as possible
ComposeCompatibility = "COMPOSE_COMPATIBILITY"
// ComposeRemoveOrphans remove “orphaned" containers, i.e. containers tagged for current project but not declared as service
ComposeRemoveOrphans = "COMPOSE_REMOVE_ORPHANS"
// ComposeIgnoreOrphans ignore "orphaned" containers
ComposeIgnoreOrphans = "COMPOSE_IGNORE_ORPHANS"
)

// Command defines a compose CLI command as a func with args
type Command func(context.Context, []string) error

Expand Down Expand Up @@ -145,7 +158,7 @@
if len(o.ConfigPaths) > 0 || o.ProjectName == "" {
p, err := o.ToProject(services, cli.WithDiscardEnvFile)
if err != nil {
envProjectName := os.Getenv("COMPOSE_PROJECT_NAME")
envProjectName := os.Getenv(ComposeProjectName)
if envProjectName != "" {
return nil, envProjectName, nil
}
Expand All @@ -162,7 +175,7 @@
return o.ProjectName, nil
}

envProjectName := os.Getenv("COMPOSE_PROJECT_NAME")
envProjectName := os.Getenv("ComposeProjectName")

Check warning on line 178 in cmd/compose/compose.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/compose.go#L178

Added line #L178 was not covered by tests
if envProjectName != "" {
return envProjectName, nil
}
Expand All @@ -180,7 +193,7 @@
return nil, compose.WrapComposeError(err)
}

if o.Compatibility || utils.StringToBool(options.Environment["COMPOSE_COMPATIBILITY"]) {
if o.Compatibility || utils.StringToBool(options.Environment[ComposeCompatibility]) {
api.Separator = "_"
}

Expand Down Expand Up @@ -335,10 +348,22 @@
opts.EnvFiles[i] = file
}
}
if v, ok := os.LookupEnv("COMPOSE_PARALLEL_LIMIT"); ok && !cmd.Flags().Changed("parallel") {

composeCmd := cmd
for {
if composeCmd.Name() == PluginName {
break
}
if !composeCmd.HasParent() {
return fmt.Errorf("error parsing command line, expected %q", PluginName)
}

Check warning on line 359 in cmd/compose/compose.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/compose.go#L358-L359

Added lines #L358 - L359 were not covered by tests
composeCmd = composeCmd.Parent()
}

if v, ok := os.LookupEnv(ComposeParallelLimit); ok && !composeCmd.Flags().Changed("parallel") {
i, err := strconv.Atoi(v)
if err != nil {
return fmt.Errorf("COMPOSE_PARALLEL_LIMIT must be an integer (found: %q)", v)
return fmt.Errorf("%s must be an integer (found: %q)", ComposeParallelLimit, v)

Check warning on line 366 in cmd/compose/compose.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/compose.go#L366

Added line #L366 was not covered by tests
}
parallel = i
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/compose/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func downCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
ValidArgsFunction: noCompletion(),
}
flags := downCmd.Flags()
removeOrphans := utils.StringToBool(os.Getenv("COMPOSE_REMOVE_ORPHANS"))
removeOrphans := utils.StringToBool(os.Getenv(ComposeRemoveOrphans))
flags.BoolVar(&opts.removeOrphans, "remove-orphans", removeOrphans, "Remove containers for services not defined in the Compose file.")
flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")
flags.BoolVarP(&opts.volumes, "volumes", "v", false, "Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.")
Expand Down
2 changes: 1 addition & 1 deletion cmd/compose/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func killCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
}

flags := cmd.Flags()
removeOrphans := utils.StringToBool(os.Getenv("COMPOSE_REMOVE_ORPHANS"))
removeOrphans := utils.StringToBool(os.Getenv(ComposeRemoveOrphans))
flags.BoolVar(&opts.removeOrphans, "remove-orphans", removeOrphans, "Remove containers for services not defined in the Compose file.")
flags.StringVarP(&opts.signal, "signal", "s", "SIGKILL", "SIGNAL to send to the container.")

Expand Down
3 changes: 2 additions & 1 deletion cmd/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ func runCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *co
if err != nil {
return err
}
opts.ignoreOrphans = utils.StringToBool(project.Environment["COMPOSE_IGNORE_ORPHANS"])

opts.ignoreOrphans = utils.StringToBool(project.Environment[ComposeIgnoreOrphans])
return runRun(ctx, backend, project, opts, createOpts, streams)
}),
ValidArgsFunction: completeServiceNames(p),
Expand Down
4 changes: 2 additions & 2 deletions cmd/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@
return validateFlags(&up, &create)
}),
RunE: p.WithServices(func(ctx context.Context, project *types.Project, services []string) error {
create.ignoreOrphans = utils.StringToBool(project.Environment["COMPOSE_IGNORE_ORPHANS"])
create.ignoreOrphans = utils.StringToBool(project.Environment[ComposeIgnoreOrphans])
if create.ignoreOrphans && create.removeOrphans {
return fmt.Errorf("COMPOSE_IGNORE_ORPHANS and --remove-orphans cannot be combined")
return fmt.Errorf("%s and --remove-orphans cannot be combined", ComposeIgnoreOrphans)

Check warning on line 87 in cmd/compose/up.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/up.go#L87

Added line #L87 was not covered by tests
}
return runUp(ctx, streams, backend, create, up, project, services)
}),
Expand Down