Skip to content

Commit 65ed8cf

Browse files
committed
Implemented docker#9147
Signed-off-by: Mehrad Dadar <[email protected]>
1 parent 5262d3b commit 65ed8cf

File tree

13 files changed

+83
-58
lines changed

13 files changed

+83
-58
lines changed

cmd/compose/remove.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func runRemove(ctx context.Context, backend api.Service, opts removeOptions, ser
6565
}
6666

6767
if opts.stop {
68-
err := backend.Stop(ctx, project, api.StopOptions{
68+
err := backend.Stop(ctx, project.Name, api.StopOptions{
6969
Services: services,
7070
})
7171
if err != nil {

cmd/compose/restart.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ func restartCommand(p *projectOptions, backend api.Service) *cobra.Command {
4949
}
5050

5151
func runRestart(ctx context.Context, backend api.Service, opts restartOptions, services []string) error {
52-
project, err := opts.toProject(services)
52+
projectName, err := opts.toProjectName()
5353
if err != nil {
5454
return err
5555
}
5656

5757
timeout := time.Duration(opts.timeout) * time.Second
58-
return backend.Restart(ctx, project, api.RestartOptions{
58+
return backend.Restart(ctx, projectName, api.RestartOptions{
5959
Timeout: &timeout,
6060
Services: services,
6161
})

cmd/compose/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,5 +240,5 @@ func startDependencies(ctx context.Context, backend api.Service, project types.P
240240
if err := backend.Create(ctx, &project, api.CreateOptions{}); err != nil {
241241
return err
242242
}
243-
return backend.Start(ctx, &project, api.StartOptions{})
243+
return backend.Start(ctx, project.Name, api.StartOptions{})
244244
}

cmd/compose/start.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ func startCommand(p *projectOptions, backend api.Service) *cobra.Command {
4343
}
4444

4545
func runStart(ctx context.Context, backend api.Service, opts startOptions, services []string) error {
46-
project, err := opts.toProject(services)
46+
projectName, err := opts.toProjectName()
4747
if err != nil {
4848
return err
4949
}
5050

51-
return backend.Start(ctx, project, api.StartOptions{})
51+
return backend.Start(ctx, projectName, api.StartOptions{
52+
AttachTo: services,
53+
})
5254
}

cmd/compose/stop.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func stopCommand(p *projectOptions, backend api.Service) *cobra.Command {
5353
}
5454

5555
func runStop(ctx context.Context, backend api.Service, opts stopOptions, services []string) error {
56-
project, err := opts.toProject(services)
56+
projectName, err := opts.toProjectName()
5757
if err != nil {
5858
return err
5959
}
@@ -63,7 +63,7 @@ func runStop(ctx context.Context, backend api.Service, opts stopOptions, service
6363
timeoutValue := time.Duration(opts.timeout) * time.Second
6464
timeout = &timeoutValue
6565
}
66-
return backend.Stop(ctx, project, api.StopOptions{
66+
return backend.Stop(ctx, projectName, api.StopOptions{
6767
Timeout: timeout,
6868
Services: services,
6969
})

pkg/api/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ type Service interface {
3737
// Create executes the equivalent to a `compose create`
3838
Create(ctx context.Context, project *types.Project, opts CreateOptions) error
3939
// Start executes the equivalent to a `compose start`
40-
Start(ctx context.Context, project *types.Project, options StartOptions) error
40+
Start(ctx context.Context, projectName string, options StartOptions) error
4141
// Restart restarts containers
42-
Restart(ctx context.Context, project *types.Project, options RestartOptions) error
42+
Restart(ctx context.Context, projectName string, options RestartOptions) error
4343
// Stop executes the equivalent to a `compose stop`
44-
Stop(ctx context.Context, project *types.Project, options StopOptions) error
44+
Stop(ctx context.Context, projectName string, options StopOptions) error
4545
// Up executes the equivalent to a `compose up`
4646
Up(ctx context.Context, project *types.Project, options UpOptions) error
4747
// Down executes the equivalent to a `compose down`

pkg/api/proxy.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ type ServiceProxy struct {
2828
PushFn func(ctx context.Context, project *types.Project, options PushOptions) error
2929
PullFn func(ctx context.Context, project *types.Project, opts PullOptions) error
3030
CreateFn func(ctx context.Context, project *types.Project, opts CreateOptions) error
31-
StartFn func(ctx context.Context, project *types.Project, options StartOptions) error
32-
RestartFn func(ctx context.Context, project *types.Project, options RestartOptions) error
33-
StopFn func(ctx context.Context, project *types.Project, options StopOptions) error
31+
StartFn func(ctx context.Context, projectName string, options StartOptions) error
32+
RestartFn func(ctx context.Context, projectName string, options RestartOptions) error
33+
StopFn func(ctx context.Context, projectName string, options StopOptions) error
3434
UpFn func(ctx context.Context, project *types.Project, options UpOptions) error
3535
DownFn func(ctx context.Context, projectName string, options DownOptions) error
3636
LogsFn func(ctx context.Context, projectName string, consumer LogConsumer, options LogOptions) error
@@ -141,36 +141,27 @@ func (s *ServiceProxy) Create(ctx context.Context, project *types.Project, optio
141141
}
142142

143143
// Start implements Service interface
144-
func (s *ServiceProxy) Start(ctx context.Context, project *types.Project, options StartOptions) error {
144+
func (s *ServiceProxy) Start(ctx context.Context, projectName string, options StartOptions) error {
145145
if s.StartFn == nil {
146146
return ErrNotImplemented
147147
}
148-
for _, i := range s.interceptors {
149-
i(ctx, project)
150-
}
151-
return s.StartFn(ctx, project, options)
148+
return s.StartFn(ctx, projectName, options)
152149
}
153150

154151
// Restart implements Service interface
155-
func (s *ServiceProxy) Restart(ctx context.Context, project *types.Project, options RestartOptions) error {
152+
func (s *ServiceProxy) Restart(ctx context.Context, projectName string, options RestartOptions) error {
156153
if s.RestartFn == nil {
157154
return ErrNotImplemented
158155
}
159-
for _, i := range s.interceptors {
160-
i(ctx, project)
161-
}
162-
return s.RestartFn(ctx, project, options)
156+
return s.RestartFn(ctx, projectName, options)
163157
}
164158

165159
// Stop implements Service interface
166-
func (s *ServiceProxy) Stop(ctx context.Context, project *types.Project, options StopOptions) error {
160+
func (s *ServiceProxy) Stop(ctx context.Context, projectName string, options StopOptions) error {
167161
if s.StopFn == nil {
168162
return ErrNotImplemented
169163
}
170-
for _, i := range s.interceptors {
171-
i(ctx, project)
172-
}
173-
return s.StopFn(ctx, project, options)
164+
return s.StopFn(ctx, projectName, options)
174165
}
175166

176167
// Up implements Service interface

pkg/compose/compose.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,34 @@ func escapeDollarSign(marshal []byte) []byte {
9292
escDollar := []byte{'$', '$'}
9393
return bytes.ReplaceAll(marshal, dollar, escDollar)
9494
}
95+
96+
// projectFromName builds a types.Project based on actual resources with compose labels set
97+
func (s *composeService) projectFromName(containers Containers, projectName string) *types.Project {
98+
project := &types.Project{
99+
Name: projectName,
100+
}
101+
if len(containers) == 0 {
102+
return project
103+
}
104+
set := map[string]moby.Container{}
105+
for _, c := range containers {
106+
set[c.Labels[api.ServiceLabel]] = c
107+
}
108+
for s, c := range set {
109+
service := types.ServiceConfig{
110+
Name: s,
111+
Image: c.Image,
112+
Labels: c.Labels,
113+
}
114+
dependencies := c.Labels[api.DependenciesLabel]
115+
if len(dependencies) > 0 {
116+
service.DependsOn = types.DependsOnConfig{}
117+
for _, d := range strings.Split(dependencies, ",") {
118+
service.DependsOn[d] = types.ServiceDependency{}
119+
}
120+
}
121+
project.Services = append(project.Services, service)
122+
}
123+
124+
return project
125+
}

pkg/compose/restart.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,28 @@ package compose
1919
import (
2020
"context"
2121

22-
"github.com/compose-spec/compose-go/types"
2322
"github.com/docker/compose/v2/pkg/api"
2423
"golang.org/x/sync/errgroup"
2524

2625
"github.com/docker/compose/v2/pkg/progress"
2726
"github.com/docker/compose/v2/pkg/utils"
2827
)
2928

30-
func (s *composeService) Restart(ctx context.Context, project *types.Project, options api.RestartOptions) error {
29+
func (s *composeService) Restart(ctx context.Context, projectName string, options api.RestartOptions) error {
3130
return progress.Run(ctx, func(ctx context.Context) error {
32-
return s.restart(ctx, project, options)
31+
return s.restart(ctx, projectName, options)
3332
})
3433
}
3534

36-
func (s *composeService) restart(ctx context.Context, project *types.Project, options api.RestartOptions) error {
37-
observedState, err := s.getContainers(ctx, project.Name, oneOffInclude, true)
35+
func (s *composeService) restart(ctx context.Context, projectName string, options api.RestartOptions) error {
36+
37+
observedState, err := s.getContainers(ctx, projectName, oneOffInclude, true)
3838
if err != nil {
3939
return err
4040
}
4141

42+
project := s.projectFromName(observedState, projectName)
43+
4244
if len(options.Services) == 0 {
4345
options.Services = project.ServiceNames()
4446
}

pkg/compose/start.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,21 @@ import (
2828
"github.com/docker/compose/v2/pkg/progress"
2929
)
3030

31-
func (s *composeService) Start(ctx context.Context, project *types.Project, options api.StartOptions) error {
31+
func (s *composeService) Start(ctx context.Context, projectName string, options api.StartOptions) error {
3232
return progress.Run(ctx, func(ctx context.Context) error {
33-
return s.start(ctx, project, options, nil)
33+
return s.start(ctx, projectName, options, nil)
3434
})
3535
}
3636

37-
func (s *composeService) start(ctx context.Context, project *types.Project, options api.StartOptions, listener api.ContainerEventListener) error {
38-
if len(options.AttachTo) == 0 {
39-
options.AttachTo = project.ServiceNames()
37+
func (s *composeService) start(ctx context.Context, projectName string, options api.StartOptions, listener api.ContainerEventListener) error {
38+
var containers Containers
39+
containers, err := s.getContainers(ctx, projectName, oneOffInclude, true, options.AttachTo...)
40+
if err != nil {
41+
return err
4042
}
4143

44+
project := s.projectFromName(containers, projectName)
45+
4246
eg, ctx := errgroup.WithContext(ctx)
4347
if listener != nil {
4448
attached, err := s.attach(ctx, project, listener, options.AttachTo)
@@ -53,7 +57,7 @@ func (s *composeService) start(ctx context.Context, project *types.Project, opti
5357
})
5458
}
5559

56-
err := InDependencyOrder(ctx, project, func(c context.Context, name string) error {
60+
err = InDependencyOrder(ctx, project, func(c context.Context, name string) error {
5761
service, err := project.GetService(name)
5862
if err != nil {
5963
return err

0 commit comments

Comments
 (0)