github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/daemon/cluster/convert/container.go (about)

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