2021-03-09 18:09:45 -03:00
|
|
|
/*
|
|
|
|
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 compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-13 09:49:01 +02:00
|
|
|
"strings"
|
2021-03-09 18:09:45 -03:00
|
|
|
|
2023-11-08 10:19:24 +01:00
|
|
|
"github.com/compose-spec/compose-go/v2/types"
|
2021-08-31 18:53:24 +02:00
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
|
|
|
"github.com/docker/compose/v2/pkg/progress"
|
|
|
|
"github.com/docker/compose/v2/pkg/utils"
|
2025-02-12 09:34:07 +01:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2022-10-18 15:21:11 +02:00
|
|
|
"golang.org/x/sync/errgroup"
|
2021-03-09 18:09:45 -03:00
|
|
|
)
|
|
|
|
|
2022-02-05 10:27:52 +03:30
|
|
|
func (s *composeService) Restart(ctx context.Context, projectName string, options api.RestartOptions) error {
|
2023-05-02 20:15:35 +02:00
|
|
|
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
|
2022-04-13 09:49:01 +02:00
|
|
|
return s.restart(ctx, strings.ToLower(projectName), options)
|
2023-05-11 18:45:00 +02:00
|
|
|
}, s.stdinfo(), "Restarting")
|
2021-06-03 13:57:56 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 10:27:52 +03:30
|
|
|
func (s *composeService) restart(ctx context.Context, projectName string, options api.RestartOptions) error {
|
2022-04-11 10:58:20 +02:00
|
|
|
containers, err := s.getContainers(ctx, projectName, oneOffExclude, true)
|
2021-03-09 18:09:45 -03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-20 08:09:14 +02:00
|
|
|
|
2022-04-11 10:58:20 +02:00
|
|
|
project := options.Project
|
|
|
|
if project == nil {
|
|
|
|
project, err = s.getProjectWithResources(ctx, containers, projectName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-24 23:46:07 +03:30
|
|
|
}
|
2022-02-05 10:27:52 +03:30
|
|
|
|
2023-08-31 09:43:39 +02:00
|
|
|
if options.NoDeps {
|
2023-12-29 11:45:45 +01:00
|
|
|
project, err = project.WithSelectedServices(options.Services, types.IgnoreDependencies)
|
2023-08-31 09:43:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-10 18:27:59 +02:00
|
|
|
// ignore depends_on relations which are not impacted by restarting service or not required
|
2025-02-12 09:34:07 +01:00
|
|
|
project, err = project.WithServicesTransform(func(_ string, s types.ServiceConfig) (types.ServiceConfig, error) {
|
2024-05-29 09:11:59 +02:00
|
|
|
for name, r := range s.DependsOn {
|
2023-02-15 14:30:09 +01:00
|
|
|
if !r.Restart {
|
2024-05-29 09:11:59 +02:00
|
|
|
delete(s.DependsOn, name)
|
2023-02-15 14:30:09 +01:00
|
|
|
}
|
|
|
|
}
|
2024-05-29 09:11:59 +02:00
|
|
|
return s, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-02-15 14:30:09 +01:00
|
|
|
}
|
|
|
|
|
2023-03-01 17:34:33 +01:00
|
|
|
if len(options.Services) != 0 {
|
2023-12-29 11:45:45 +01:00
|
|
|
project, err = project.WithSelectedServices(options.Services, types.IncludeDependents)
|
2023-02-13 15:53:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-20 08:09:14 +02:00
|
|
|
}
|
|
|
|
|
2021-06-24 16:35:38 +02:00
|
|
|
w := progress.ContextWriter(ctx)
|
2022-04-11 10:58:20 +02:00
|
|
|
return InDependencyOrder(ctx, project, func(c context.Context, service string) error {
|
2025-03-19 12:59:51 +01:00
|
|
|
config := project.Services[service]
|
|
|
|
err = s.waitDependencies(ctx, project, service, config.DependsOn, containers, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-24 16:35:38 +02:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
2025-02-10 13:15:23 +01:00
|
|
|
for _, ctr := range containers.filter(isService(service)) {
|
2021-06-24 16:35:38 +02:00
|
|
|
eg.Go(func() error {
|
2025-02-10 13:15:23 +01:00
|
|
|
eventName := getContainerProgressName(ctr)
|
2021-06-24 16:35:38 +02:00
|
|
|
w.Event(progress.RestartingEvent(eventName))
|
2022-10-18 15:21:11 +02:00
|
|
|
timeout := utils.DurationSecondToInt(options.Timeout)
|
2025-03-19 12:59:51 +01:00
|
|
|
err = s.apiClient().ContainerRestart(ctx, ctr.ID, container.StopOptions{Timeout: timeout})
|
2025-02-10 13:15:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-06-24 16:35:38 +02:00
|
|
|
}
|
2025-02-10 13:15:23 +01:00
|
|
|
w.Event(progress.StartedEvent(eventName))
|
|
|
|
return nil
|
2021-06-24 16:35:38 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return eg.Wait()
|
2021-03-09 18:09:45 -03:00
|
|
|
})
|
|
|
|
}
|