github.com/kim0/docker@v0.6.2-0.20161130212042-4addda3f07e7/daemon/cluster/convert/container.go (about)

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