@@ -7,20 +7,16 @@ import (
7
7
"log"
8
8
"os"
9
9
"path/filepath"
10
- "strings"
11
10
12
11
"github.com/compose-spec/compose-ref/internal"
13
12
"github.com/docker/docker/api/types/filters"
14
- "github.com/docker/docker/api/types/mount"
15
- "github.com/docker/docker/errdefs"
16
13
"gopkg.in/yaml.v2"
17
14
18
15
"github.com/compose-spec/compose-go/loader"
19
16
compose "github.com/compose-spec/compose-go/types"
20
17
"github.com/docker/docker/api/types"
21
18
"github.com/docker/docker/api/types/container"
22
19
"github.com/docker/docker/api/types/strslice"
23
- "github.com/docker/docker/api/types/volume"
24
20
"github.com/docker/docker/client"
25
21
"github.com/docker/go-units"
26
22
commandLine "github.com/urfave/cli/v2"
@@ -128,11 +124,9 @@ func doUp(project string, config *compose.Config) error {
128
124
return err
129
125
}
130
126
131
- for defaultVolumeName , volumeConfig := range config .Volumes {
132
- err = createVolume (cli , project , defaultVolumeName , volumeConfig )
133
- if err != nil {
134
- return err
135
- }
127
+ err = internal .GetVolumesFromConfig (cli , project , config )
128
+ if err != nil {
129
+ return err
136
130
}
137
131
138
132
observedState , err := collectContainers (cli , project )
@@ -242,7 +236,7 @@ func createService(cli *client.Client, project string, prjDir string, s compose.
242
236
243
237
fmt .Printf ("Creating container for service %s ... " , s .Name )
244
238
networkMode := internal .NetworkMode (s , networks )
245
- mounts , err := createContainerMounts (s , prjDir )
239
+ mounts , err := internal . CreateContainerMounts (s , prjDir )
246
240
if err != nil {
247
241
return err
248
242
}
@@ -302,95 +296,6 @@ func createService(cli *client.Client, project string, prjDir string, s compose.
302
296
return nil
303
297
}
304
298
305
- func createVolume (cli * client.Client , project string , volumeDefaultName string , volumeConfig compose.VolumeConfig ) error {
306
- name := volumeDefaultName
307
- if volumeConfig .Name != "" {
308
- name = volumeConfig .Name
309
- }
310
- volumeID := fmt .Sprintf ("%s_%s" , strings .Trim (project , "-_" ), name )
311
-
312
- ctx := context .Background ()
313
-
314
- // If volume already exists, return here
315
- if volumeConfig .External .Name != "" {
316
- fmt .Printf ("Volume %s declared as external. No new volume will be created.\n " , name )
317
- }
318
- _ , err := cli .VolumeInspect (ctx , volumeID )
319
- if err == nil {
320
- return nil
321
- }
322
- if ! errdefs .IsNotFound (err ) {
323
- return err
324
- }
325
-
326
- // If volume is marked as external but doesn't already exist then return an error
327
- if volumeConfig .External .Name != "" {
328
- return fmt .Errorf ("Volume %s declared as external, but could not be found. " +
329
- "Please create the volume manually using `docker volume create --name=%s` and try again.\n " , name , name )
330
- }
331
-
332
- fmt .Printf ("Creating volume %q with %s driver\n " , name , volumeConfig .Driver )
333
- _ , err = cli .VolumeCreate (ctx , volume.VolumeCreateBody {
334
- Name : volumeID ,
335
- Driver : volumeConfig .Driver ,
336
- DriverOpts : volumeConfig .DriverOpts ,
337
- Labels : map [string ]string {
338
- internal .LabelProject : project ,
339
- internal .LabelVolume : name ,
340
- },
341
- })
342
-
343
- return err
344
- }
345
-
346
- func createContainerMounts (s compose.ServiceConfig , prjDir string ) ([]mount.Mount , error ) {
347
- var mounts []mount.Mount
348
- for _ , v := range s .Volumes {
349
- source := v .Source
350
- if ! filepath .IsAbs (source ) {
351
- source = filepath .Join (prjDir , source )
352
- }
353
- mounts = append (mounts , mount.Mount {
354
- Type : mount .Type (v .Type ),
355
- Source : source ,
356
- Target : v .Target ,
357
- ReadOnly : v .ReadOnly ,
358
- Consistency : mount .Consistency (v .Consistency ),
359
- BindOptions : buildBindOption (v .Bind ),
360
- VolumeOptions : buildVolumeOptions (v .Volume ),
361
- TmpfsOptions : buildTmpfsOptions (v .Tmpfs ),
362
- })
363
- }
364
- return mounts , nil
365
- }
366
-
367
- func buildBindOption (bind * compose.ServiceVolumeBind ) * mount.BindOptions {
368
- if bind == nil {
369
- return nil
370
- }
371
- return & mount.BindOptions {
372
- Propagation : mount .Propagation (bind .Propagation ),
373
- }
374
- }
375
-
376
- func buildVolumeOptions (vol * compose.ServiceVolumeVolume ) * mount.VolumeOptions {
377
- if vol == nil {
378
- return nil
379
- }
380
- return & mount.VolumeOptions {
381
- NoCopy : vol .NoCopy ,
382
- }
383
- }
384
-
385
- func buildTmpfsOptions (tmpfs * compose.ServiceVolumeTmpfs ) * mount.TmpfsOptions {
386
- if tmpfs == nil {
387
- return nil
388
- }
389
- return & mount.TmpfsOptions {
390
- SizeBytes : tmpfs .Size ,
391
- }
392
- }
393
-
394
299
func collectContainers (cli * client.Client , project string ) (map [string ][]types.Container , error ) {
395
300
containerList , err := cli .ContainerList (context .Background (), types.ContainerListOptions {
396
301
All : true ,
@@ -413,26 +318,6 @@ func collectContainers(cli *client.Client, project string) (map[string][]types.C
413
318
return containers , nil
414
319
}
415
320
416
- func collectVolumes (cli * client.Client , project string ) (map [string ][]types.Volume , error ) {
417
- filter := filters .NewArgs (filters .Arg ("label" , internal .LabelProject + "=" + project ))
418
- list , err := cli .VolumeList (context .Background (), filter )
419
- if err != nil {
420
- return nil , err
421
- }
422
- volumes := map [string ][]types.Volume {}
423
- for _ , v := range list .Volumes {
424
- resource := v .Labels [internal .LabelVolume ]
425
- l , ok := volumes [resource ]
426
- if ! ok {
427
- l = []types.Volume {* v }
428
- } else {
429
- l = append (l , * v )
430
- }
431
- volumes [resource ] = l
432
- }
433
- return volumes , nil
434
- }
435
-
436
321
func doDown (project string , config * compose.Config ) error {
437
322
cli , err := getClient ()
438
323
if err != nil {
@@ -442,7 +327,7 @@ func doDown(project string, config *compose.Config) error {
442
327
if err != nil {
443
328
return err
444
329
}
445
- err = destroyVolumes (cli , project )
330
+ err = internal . RemoveVolumes (cli , project )
446
331
if err != nil {
447
332
return err
448
333
}
@@ -468,33 +353,6 @@ func removeServices(cli *client.Client, project string) error {
468
353
return nil
469
354
}
470
355
471
- func destroyVolumes (cli * client.Client , project string ) error {
472
- volumes , err := collectVolumes (cli , project )
473
- if err != nil {
474
- return err
475
- }
476
- for volumeName , volume := range volumes {
477
- err = destroyVolume (cli , volume , volumeName )
478
- if err != nil {
479
- return err
480
- }
481
- }
482
- return nil
483
- }
484
-
485
- func destroyVolume (cli * client.Client , volume []types.Volume , volumeName string ) error {
486
- ctx := context .Background ()
487
- for _ , v := range volume {
488
- fmt .Printf ("Deleting volume %s ... " , volumeName )
489
- err := cli .VolumeRemove (ctx , v .Name , false )
490
- if err != nil {
491
- return err
492
- }
493
- fmt .Println (v .Name )
494
- }
495
- return nil
496
- }
497
-
498
356
func load (file string ) (* compose.Config , error ) {
499
357
b , err := ioutil .ReadFile (file )
500
358
if err != nil {
0 commit comments