9
9
"path/filepath"
10
10
11
11
"github.com/compose-spec/compose-ref/internal"
12
- "github.com/docker/docker/api/types/filters"
13
12
"gopkg.in/yaml.v2"
14
13
15
14
"github.com/compose-spec/compose-go/loader"
@@ -129,7 +128,7 @@ func doUp(project string, config *compose.Config) error {
129
128
return err
130
129
}
131
130
132
- observedState , err := collectContainers (cli , project )
131
+ observedState , err := internal . CollectContainers (cli , project )
133
132
if err != nil {
134
133
return err
135
134
}
@@ -169,7 +168,7 @@ func doUp(project string, config *compose.Config) error {
169
168
}
170
169
171
170
// Some containers exist for service but with an obsolete configuration. We need to replace them
172
- err = removeContainers (cli , containers )
171
+ err = internal . RemoveContainers (cli , containers )
173
172
if err != nil {
174
173
return err
175
174
}
@@ -182,33 +181,14 @@ func doUp(project string, config *compose.Config) error {
182
181
183
182
// Remaining containers in observed state don't have a matching service in Compose file => orphaned to be removed
184
183
for _ , orphaned := range observedState {
185
- err = removeContainers (cli , orphaned )
184
+ err = internal . RemoveContainers (cli , orphaned )
186
185
if err != nil {
187
186
return err
188
187
}
189
188
}
190
189
return nil
191
190
}
192
191
193
- func removeContainers (cli * client.Client , containers []types.Container ) error {
194
- ctx := context .Background ()
195
- for _ , c := range containers {
196
- if serviceName , ok := c .Labels [internal .LabelService ]; ok {
197
- fmt .Printf ("Stopping containers for service %s ... " , serviceName )
198
- }
199
- err := cli .ContainerStop (ctx , c .ID , nil )
200
- if err != nil {
201
- return err
202
- }
203
- err = cli .ContainerRemove (ctx , c .ID , types.ContainerRemoveOptions {})
204
- if err != nil {
205
- return err
206
- }
207
- fmt .Println (c .ID )
208
- }
209
- return nil
210
- }
211
-
212
192
func createService (cli * client.Client , project string , prjDir string , s compose.ServiceConfig , networks map [string ]string ) error {
213
193
ctx := context .Background ()
214
194
@@ -296,28 +276,6 @@ func createService(cli *client.Client, project string, prjDir string, s compose.
296
276
return nil
297
277
}
298
278
299
- func collectContainers (cli * client.Client , project string ) (map [string ][]types.Container , error ) {
300
- containerList , err := cli .ContainerList (context .Background (), types.ContainerListOptions {
301
- All : true ,
302
- Filters : filters .NewArgs (filters .Arg ("label" , internal .LabelProject + "=" + project )),
303
- })
304
- if err != nil {
305
- return nil , err
306
- }
307
- containers := map [string ][]types.Container {}
308
- for _ , c := range containerList {
309
- service := c .Labels [internal .LabelService ]
310
- l , ok := containers [service ]
311
- if ! ok {
312
- l = []types.Container {c }
313
- } else {
314
- l = append (l , c )
315
- }
316
- containers [service ] = l
317
- }
318
- return containers , nil
319
- }
320
-
321
279
func doDown (project string , config * compose.Config ) error {
322
280
cli , err := getClient ()
323
281
if err != nil {
@@ -339,13 +297,13 @@ func doDown(project string, config *compose.Config) error {
339
297
}
340
298
341
299
func removeServices (cli * client.Client , project string ) error {
342
- containers , err := collectContainers (cli , project )
300
+ containers , err := internal . CollectContainers (cli , project )
343
301
if err != nil {
344
302
return err
345
303
}
346
304
347
305
for _ , replicaList := range containers {
348
- err = removeContainers (cli , replicaList )
306
+ err = internal . RemoveContainers (cli , replicaList )
349
307
if err != nil {
350
308
return err
351
309
}
0 commit comments