@@ -21,7 +21,6 @@ import (
21
21
"context"
22
22
"encoding/json"
23
23
"fmt"
24
- "os"
25
24
"os/exec"
26
25
"slices"
27
26
"strconv"
@@ -32,8 +31,6 @@ import (
32
31
"github.com/docker/cli/cli-plugins/manager"
33
32
"github.com/docker/compose/v2/pkg/progress"
34
33
"github.com/spf13/cobra"
35
- "go.opentelemetry.io/otel"
36
- "go.opentelemetry.io/otel/propagation"
37
34
"golang.org/x/sync/errgroup"
38
35
)
39
36
@@ -51,7 +48,11 @@ func (s *composeService) ensureModels(ctx context.Context, project *types.Projec
51
48
}
52
49
53
50
cmd := exec .CommandContext (ctx , dockerModel .Path , "ls" , "--json" )
54
- s .setupChildProcess (ctx , cmd )
51
+ err = s .prepareShellOut (ctx , project , cmd )
52
+ if err != nil {
53
+ return err
54
+ }
55
+
55
56
output , err := cmd .CombinedOutput ()
56
57
if err != nil {
57
58
return fmt .Errorf ("error checking available models: %w" , err )
@@ -85,32 +86,29 @@ func (s *composeService) ensureModels(ctx context.Context, project *types.Projec
85
86
eg .Go (func () error {
86
87
w := progress .ContextWriter (gctx )
87
88
if ! slices .Contains (availableModels , config .Model ) {
88
- err = s .pullModel (gctx , dockerModel , config , quietPull , w )
89
+ err = s .pullModel (gctx , dockerModel , project , config , quietPull , w )
89
90
if err != nil {
90
91
return err
91
92
}
92
93
}
93
- err = s .configureModel (gctx , dockerModel , config , w )
94
- if err != nil {
95
- return err
96
- }
97
- w .Event (progress .CreatedEvent (config .Name ))
98
- return nil
94
+ return s .configureModel (gctx , dockerModel , project , config , w )
99
95
})
100
96
}
101
97
return eg .Wait ()
102
98
}
103
99
104
- func (s * composeService ) pullModel (ctx context.Context , dockerModel * manager.Plugin , model types.ModelConfig , quietPull bool , w progress.Writer ) error {
100
+ func (s * composeService ) pullModel (ctx context.Context , dockerModel * manager.Plugin , project * types. Project , model types.ModelConfig , quietPull bool , w progress.Writer ) error {
105
101
w .Event (progress.Event {
106
102
ID : model .Name ,
107
103
Status : progress .Working ,
108
104
Text : "Pulling" ,
109
105
})
110
106
111
107
cmd := exec .CommandContext (ctx , dockerModel .Path , "pull" , model .Model )
112
- s .setupChildProcess (ctx , cmd )
113
-
108
+ err := s .prepareShellOut (ctx , project , cmd )
109
+ if err != nil {
110
+ return err
111
+ }
114
112
stream , err := cmd .StdoutPipe ()
115
113
if err != nil {
116
114
return err
@@ -150,7 +148,7 @@ func (s *composeService) pullModel(ctx context.Context, dockerModel *manager.Plu
150
148
return err
151
149
}
152
150
153
- func (s * composeService ) configureModel (ctx context.Context , dockerModel * manager.Plugin , config types.ModelConfig , w progress.Writer ) error {
151
+ func (s * composeService ) configureModel (ctx context.Context , dockerModel * manager.Plugin , project * types. Project , config types.ModelConfig , w progress.Writer ) error {
154
152
w .Event (progress.Event {
155
153
ID : config .Name ,
156
154
Status : progress .Working ,
@@ -167,13 +165,20 @@ func (s *composeService) configureModel(ctx context.Context, dockerModel *manage
167
165
args = append (args , config .RuntimeFlags ... )
168
166
}
169
167
cmd := exec .CommandContext (ctx , dockerModel .Path , args ... )
170
- s .setupChildProcess (ctx , cmd )
168
+ err := s .prepareShellOut (ctx , project , cmd )
169
+ if err != nil {
170
+ return err
171
+ }
171
172
return cmd .Run ()
172
173
}
173
174
174
175
func (s * composeService ) setModelVariables (ctx context.Context , dockerModel * manager.Plugin , project * types.Project ) error {
175
176
cmd := exec .CommandContext (ctx , dockerModel .Path , "status" , "--json" )
176
- s .setupChildProcess (ctx , cmd )
177
+ err := s .prepareShellOut (ctx , project , cmd )
178
+ if err != nil {
179
+ return err
180
+ }
181
+
177
182
statusOut , err := cmd .CombinedOutput ()
178
183
if err != nil {
179
184
return fmt .Errorf ("error checking docker-model status: %w" , err )
@@ -211,19 +216,6 @@ func (s *composeService) setModelVariables(ctx context.Context, dockerModel *man
211
216
return nil
212
217
}
213
218
214
- func (s * composeService ) setupChildProcess (gctx context.Context , cmd * exec.Cmd ) {
215
- // exec provider command with same environment Compose is running
216
- env := types .NewMapping (os .Environ ())
217
- // but remove DOCKER_CLI_PLUGIN... variable so plugin can detect it run standalone
218
- delete (env , manager .ReexecEnvvar )
219
- // propagate opentelemetry context to child process, see https://github.com/open-telemetry/oteps/blob/main/text/0258-env-context-baggage-carriers.md
220
- carrier := propagation.MapCarrier {}
221
- otel .GetTextMapPropagator ().Inject (gctx , & carrier )
222
- env .Merge (types .Mapping (carrier ))
223
- env ["DOCKER_CONTEXT" ] = s .dockerCli .CurrentContext ()
224
- cmd .Env = env .Values ()
225
- }
226
-
227
219
type Model struct {
228
220
Id string `json:"id"`
229
221
Tags []string `json:"tags"`
0 commit comments