github.com/mhy12345/docker@v1.12.3/daemon/cluster/convert/container.go (about) 1 package convert 2 3 import ( 4 "fmt" 5 "strings" 6 7 types "github.com/docker/engine-api/types/swarm" 8 swarmapi "github.com/docker/swarmkit/api" 9 "github.com/docker/swarmkit/protobuf/ptypes" 10 ) 11 12 func containerSpecFromGRPC(c *swarmapi.ContainerSpec) types.ContainerSpec { 13 containerSpec := types.ContainerSpec{ 14 Image: c.Image, 15 Labels: c.Labels, 16 Command: c.Command, 17 Args: c.Args, 18 Env: c.Env, 19 Dir: c.Dir, 20 User: c.User, 21 } 22 23 // Mounts 24 for _, m := range c.Mounts { 25 mount := types.Mount{ 26 Target: m.Target, 27 Source: m.Source, 28 Type: types.MountType(strings.ToLower(swarmapi.Mount_MountType_name[int32(m.Type)])), 29 ReadOnly: m.ReadOnly, 30 } 31 32 if m.BindOptions != nil { 33 mount.BindOptions = &types.BindOptions{ 34 Propagation: types.MountPropagation(strings.ToLower(swarmapi.Mount_BindOptions_MountPropagation_name[int32(m.BindOptions.Propagation)])), 35 } 36 } 37 38 if m.VolumeOptions != nil { 39 mount.VolumeOptions = &types.VolumeOptions{ 40 NoCopy: m.VolumeOptions.NoCopy, 41 Labels: m.VolumeOptions.Labels, 42 } 43 if m.VolumeOptions.DriverConfig != nil { 44 mount.VolumeOptions.DriverConfig = &types.Driver{ 45 Name: m.VolumeOptions.DriverConfig.Name, 46 Options: m.VolumeOptions.DriverConfig.Options, 47 } 48 } 49 } 50 containerSpec.Mounts = append(containerSpec.Mounts, mount) 51 } 52 53 if c.StopGracePeriod != nil { 54 grace, _ := ptypes.Duration(c.StopGracePeriod) 55 containerSpec.StopGracePeriod = &grace 56 } 57 return containerSpec 58 } 59 60 func containerToGRPC(c types.ContainerSpec) (*swarmapi.ContainerSpec, error) { 61 containerSpec := &swarmapi.ContainerSpec{ 62 Image: c.Image, 63 Labels: c.Labels, 64 Command: c.Command, 65 Args: c.Args, 66 Env: c.Env, 67 Dir: c.Dir, 68 User: c.User, 69 } 70 71 if c.StopGracePeriod != nil { 72 containerSpec.StopGracePeriod = ptypes.DurationProto(*c.StopGracePeriod) 73 } 74 75 // Mounts 76 for _, m := range c.Mounts { 77 mount := swarmapi.Mount{ 78 Target: m.Target, 79 Source: m.Source, 80 ReadOnly: m.ReadOnly, 81 } 82 83 if mountType, ok := swarmapi.Mount_MountType_value[strings.ToUpper(string(m.Type))]; ok { 84 mount.Type = swarmapi.Mount_MountType(mountType) 85 } else if string(m.Type) != "" { 86 return nil, fmt.Errorf("invalid MountType: %q", m.Type) 87 } 88 89 if m.BindOptions != nil { 90 if mountPropagation, ok := swarmapi.Mount_BindOptions_MountPropagation_value[strings.ToUpper(string(m.BindOptions.Propagation))]; ok { 91 mount.BindOptions = &swarmapi.Mount_BindOptions{Propagation: swarmapi.Mount_BindOptions_MountPropagation(mountPropagation)} 92 } else if string(m.BindOptions.Propagation) != "" { 93 return nil, fmt.Errorf("invalid MountPropagation: %q", m.BindOptions.Propagation) 94 95 } 96 97 } 98 99 if m.VolumeOptions != nil { 100 mount.VolumeOptions = &swarmapi.Mount_VolumeOptions{ 101 NoCopy: m.VolumeOptions.NoCopy, 102 Labels: m.VolumeOptions.Labels, 103 } 104 if m.VolumeOptions.DriverConfig != nil { 105 mount.VolumeOptions.DriverConfig = &swarmapi.Driver{ 106 Name: m.VolumeOptions.DriverConfig.Name, 107 Options: m.VolumeOptions.DriverConfig.Options, 108 } 109 } 110 } 111 112 containerSpec.Mounts = append(containerSpec.Mounts, mount) 113 } 114 115 return containerSpec, nil 116 }