add support for restart for depends_on

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-02-15 14:30:09 +01:00 committed by Nicolas De loof
parent 8d56db5e1d
commit e831ea826b
7 changed files with 32 additions and 16 deletions

View File

@ -99,7 +99,7 @@ type ProjectOptions struct {
ConfigPaths []string ConfigPaths []string
WorkDir string WorkDir string
ProjectDir string ProjectDir string
EnvFile string EnvFiles []string
Compatibility bool Compatibility bool
} }
@ -132,7 +132,7 @@ func (o *ProjectOptions) addProjectFlags(f *pflag.FlagSet) {
f.StringArrayVar(&o.Profiles, "profile", []string{}, "Specify a profile to enable") f.StringArrayVar(&o.Profiles, "profile", []string{}, "Specify a profile to enable")
f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name") f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files") f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
f.StringVar(&o.EnvFile, "env-file", "", "Specify an alternate environment file.") f.StringArrayVar(&o.EnvFiles, "env-file", nil, "Specify an alternate environment file.")
f.StringVar(&o.ProjectDir, "project-directory", "", "Specify an alternate working directory\n(default: the path of the, first specified, Compose file)") f.StringVar(&o.ProjectDir, "project-directory", "", "Specify an alternate working directory\n(default: the path of the, first specified, Compose file)")
f.StringVar(&o.WorkDir, "workdir", "", "DEPRECATED! USE --project-directory INSTEAD.\nSpecify an alternate working directory\n(default: the path of the, first specified, Compose file)") f.StringVar(&o.WorkDir, "workdir", "", "DEPRECATED! USE --project-directory INSTEAD.\nSpecify an alternate working directory\n(default: the path of the, first specified, Compose file)")
f.BoolVar(&o.Compatibility, "compatibility", false, "Run compose in backward compatibility mode") f.BoolVar(&o.Compatibility, "compatibility", false, "Run compose in backward compatibility mode")
@ -198,8 +198,8 @@ func (o *ProjectOptions) ToProject(services []string, po ...cli.ProjectOptionsFn
api.ConfigFilesLabel: strings.Join(project.ComposeFiles, ","), api.ConfigFilesLabel: strings.Join(project.ComposeFiles, ","),
api.OneoffLabel: "False", // default, will be overridden by `run` command api.OneoffLabel: "False", // default, will be overridden by `run` command
} }
if o.EnvFile != "" { if len(o.EnvFiles) != 0 {
s.CustomLabels[api.EnvironmentFileLabel] = o.EnvFile s.CustomLabels[api.EnvironmentFileLabel] = strings.Join(o.EnvFiles, ",")
} }
project.Services[i] = s project.Services[i] = s
} }
@ -229,7 +229,7 @@ func (o *ProjectOptions) toProjectOptions(po ...cli.ProjectOptionsFn) (*cli.Proj
append(po, append(po,
cli.WithWorkingDirectory(o.ProjectDir), cli.WithWorkingDirectory(o.ProjectDir),
cli.WithOsEnv, cli.WithOsEnv,
cli.WithEnvFile(o.EnvFile), cli.WithEnvFiles(o.EnvFiles...),
cli.WithDotEnv, cli.WithDotEnv,
cli.WithConfigFileEnv, cli.WithConfigFileEnv,
cli.WithDefaultConfigPath, cli.WithDefaultConfigPath,
@ -322,11 +322,14 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
opts.ProjectDir = opts.WorkDir opts.ProjectDir = opts.WorkDir
fmt.Fprint(os.Stderr, aec.Apply("option '--workdir' is DEPRECATED at root level! Please use '--project-directory' instead.\n", aec.RedF)) fmt.Fprint(os.Stderr, aec.Apply("option '--workdir' is DEPRECATED at root level! Please use '--project-directory' instead.\n", aec.RedF))
} }
if opts.EnvFile != "" && !filepath.IsAbs(opts.EnvFile) { for i, file := range opts.EnvFiles {
opts.EnvFile, err = filepath.Abs(opts.EnvFile) if !filepath.IsAbs(file) {
file, err = filepath.Abs(file)
if err != nil { if err != nil {
return err return err
} }
opts.EnvFiles[i] = file
}
} }
if v, ok := os.LookupEnv("COMPOSE_PARALLEL_LIMIT"); ok && !cmd.Flags().Changed("parallel") { if v, ok := os.LookupEnv("COMPOSE_PARALLEL_LIMIT"); ok && !cmd.Flags().Changed("parallel") {
i, err := strconv.Atoi(v) i, err := strconv.Atoi(v)

View File

@ -32,7 +32,9 @@ func TestFilterServices(t *testing.T) {
}, },
{ {
Name: "bar", Name: "bar",
NetworkMode: types.NetworkModeServicePrefix + "zot", DependsOn: map[string]types.ServiceDependency{
"zot": {},
},
}, },
{ {
Name: "zot", Name: "zot",

View File

@ -41,7 +41,7 @@ Docker Compose
|:-----------------------|:--------------|:--------|:----------------------------------------------------------------------------------------------------| |:-----------------------|:--------------|:--------|:----------------------------------------------------------------------------------------------------|
| `--ansi` | `string` | `auto` | Control when to print ANSI control characters ("never"\|"always"\|"auto") | | `--ansi` | `string` | `auto` | Control when to print ANSI control characters ("never"\|"always"\|"auto") |
| `--compatibility` | | | Run compose in backward compatibility mode | | `--compatibility` | | | Run compose in backward compatibility mode |
| `--env-file` | `string` | | Specify an alternate environment file. | | `--env-file` | `stringArray` | | Specify an alternate environment file. |
| `-f`, `--file` | `stringArray` | | Compose configuration files | | `-f`, `--file` | `stringArray` | | Compose configuration files |
| `--parallel` | `int` | `-1` | Control max parallelism, -1 for unlimited | | `--parallel` | `int` | `-1` | Control max parallelism, -1 for unlimited |
| `--profile` | `stringArray` | | Specify a profile to enable | | `--profile` | `stringArray` | | Specify a profile to enable |

View File

@ -198,7 +198,8 @@ options:
kubernetes: false kubernetes: false
swarm: false swarm: false
- option: env-file - option: env-file
value_type: string value_type: stringArray
default_value: '[]'
description: Specify an alternate environment file. description: Specify an alternate environment file.
deprecated: false deprecated: false
hidden: false hidden: false

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.20
require ( require (
github.com/AlecAivazis/survey/v2 v2.3.6 github.com/AlecAivazis/survey/v2 v2.3.6
github.com/buger/goterm v1.0.4 github.com/buger/goterm v1.0.4
github.com/compose-spec/compose-go v1.10.0 github.com/compose-spec/compose-go v1.11.0
github.com/containerd/console v1.0.3 github.com/containerd/console v1.0.3
github.com/containerd/containerd v1.6.18 github.com/containerd/containerd v1.6.18
github.com/cucumber/godog v0.0.0-00010101000000-000000000000 github.com/cucumber/godog v0.0.0-00010101000000-000000000000

4
go.sum
View File

@ -165,8 +165,8 @@ github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWH
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/compose-spec/compose-go v1.10.0 h1:MGrEv+WyETQWB4ARKTHRTvoZ0CZGi8lyFlveGNMej40= github.com/compose-spec/compose-go v1.11.0 h1:YLl0wf4YU9ZVei6mqLxAfI2gWOrqnTsPBAcIe9cO9Zk=
github.com/compose-spec/compose-go v1.10.0/go.mod h1:Tb5Ae2PsYN3GTqYqzl2IRbTPiJtPZZjMw8UKUvmehFk= github.com/compose-spec/compose-go v1.11.0/go.mod h1:huuiqxbQTZLkagcN9D/1tEKZwMXVetYeIWtjCAVsoXw=
github.com/containerd/cgroups v1.0.4 h1:jN/mbWBEaz+T1pi5OFtnkQ+8qnmEbAr1Oo1FRm5B0dA= github.com/containerd/cgroups v1.0.4 h1:jN/mbWBEaz+T1pi5OFtnkQ+8qnmEbAr1Oo1FRm5B0dA=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw= github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U= github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=

View File

@ -47,6 +47,16 @@ func (s *composeService) restart(ctx context.Context, projectName string, option
} }
} }
// ignore depends_on relations which are not impacted by restarting service
for i, service := range project.Services {
for name, r := range service.DependsOn {
if !r.Restart {
delete(service.DependsOn, name)
}
}
project.Services[i] = service
}
if len(options.Services) == 0 { if len(options.Services) == 0 {
err = project.ForServices(options.Services) err = project.ForServices(options.Services)
if err != nil { if err != nil {