2016-06-14 02:52:49 +00:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2016-10-13 18:28:32 +00:00
|
|
|
container "github.com/docker/docker/api/types/container"
|
2016-09-06 18:18:12 +00:00
|
|
|
mounttypes "github.com/docker/docker/api/types/mount"
|
|
|
|
types "github.com/docker/docker/api/types/swarm"
|
2016-06-14 02:52:49 +00:00
|
|
|
swarmapi "github.com/docker/swarmkit/api"
|
|
|
|
"github.com/docker/swarmkit/protobuf/ptypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
func containerSpecFromGRPC(c *swarmapi.ContainerSpec) types.ContainerSpec {
|
|
|
|
containerSpec := types.ContainerSpec{
|
2016-10-27 11:44:19 +00:00
|
|
|
Image: c.Image,
|
|
|
|
Labels: c.Labels,
|
|
|
|
Command: c.Command,
|
|
|
|
Args: c.Args,
|
|
|
|
Hostname: c.Hostname,
|
|
|
|
Env: c.Env,
|
|
|
|
Dir: c.Dir,
|
|
|
|
User: c.User,
|
|
|
|
Groups: c.Groups,
|
2016-11-04 18:31:44 +00:00
|
|
|
TTY: c.TTY,
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mounts
|
|
|
|
for _, m := range c.Mounts {
|
2016-08-15 16:13:18 +00:00
|
|
|
mount := mounttypes.Mount{
|
2016-06-14 02:52:49 +00:00
|
|
|
Target: m.Target,
|
|
|
|
Source: m.Source,
|
2016-08-15 16:13:18 +00:00
|
|
|
Type: mounttypes.Type(strings.ToLower(swarmapi.Mount_MountType_name[int32(m.Type)])),
|
2016-07-05 18:43:28 +00:00
|
|
|
ReadOnly: m.ReadOnly,
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if m.BindOptions != nil {
|
2016-08-15 16:13:18 +00:00
|
|
|
mount.BindOptions = &mounttypes.BindOptions{
|
|
|
|
Propagation: mounttypes.Propagation(strings.ToLower(swarmapi.Mount_BindOptions_MountPropagation_name[int32(m.BindOptions.Propagation)])),
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.VolumeOptions != nil {
|
2016-08-15 16:13:18 +00:00
|
|
|
mount.VolumeOptions = &mounttypes.VolumeOptions{
|
2016-07-05 18:43:28 +00:00
|
|
|
NoCopy: m.VolumeOptions.NoCopy,
|
|
|
|
Labels: m.VolumeOptions.Labels,
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
if m.VolumeOptions.DriverConfig != nil {
|
2016-08-15 16:13:18 +00:00
|
|
|
mount.VolumeOptions.DriverConfig = &mounttypes.Driver{
|
2016-06-14 02:52:49 +00:00
|
|
|
Name: m.VolumeOptions.DriverConfig.Name,
|
|
|
|
Options: m.VolumeOptions.DriverConfig.Options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
containerSpec.Mounts = append(containerSpec.Mounts, mount)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.StopGracePeriod != nil {
|
|
|
|
grace, _ := ptypes.Duration(c.StopGracePeriod)
|
|
|
|
containerSpec.StopGracePeriod = &grace
|
|
|
|
}
|
2016-10-13 18:28:32 +00:00
|
|
|
|
|
|
|
if c.Healthcheck != nil {
|
|
|
|
containerSpec.Healthcheck = healthConfigFromGRPC(c.Healthcheck)
|
|
|
|
}
|
|
|
|
|
2016-06-14 02:52:49 +00:00
|
|
|
return containerSpec
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerToGRPC(c types.ContainerSpec) (*swarmapi.ContainerSpec, error) {
|
|
|
|
containerSpec := &swarmapi.ContainerSpec{
|
2016-10-27 11:44:19 +00:00
|
|
|
Image: c.Image,
|
|
|
|
Labels: c.Labels,
|
|
|
|
Command: c.Command,
|
|
|
|
Args: c.Args,
|
|
|
|
Hostname: c.Hostname,
|
|
|
|
Env: c.Env,
|
|
|
|
Dir: c.Dir,
|
|
|
|
User: c.User,
|
|
|
|
Groups: c.Groups,
|
2016-11-04 18:31:44 +00:00
|
|
|
TTY: c.TTY,
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.StopGracePeriod != nil {
|
|
|
|
containerSpec.StopGracePeriod = ptypes.DurationProto(*c.StopGracePeriod)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mounts
|
|
|
|
for _, m := range c.Mounts {
|
|
|
|
mount := swarmapi.Mount{
|
|
|
|
Target: m.Target,
|
|
|
|
Source: m.Source,
|
2016-07-05 18:43:28 +00:00
|
|
|
ReadOnly: m.ReadOnly,
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if mountType, ok := swarmapi.Mount_MountType_value[strings.ToUpper(string(m.Type))]; ok {
|
|
|
|
mount.Type = swarmapi.Mount_MountType(mountType)
|
|
|
|
} else if string(m.Type) != "" {
|
|
|
|
return nil, fmt.Errorf("invalid MountType: %q", m.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.BindOptions != nil {
|
|
|
|
if mountPropagation, ok := swarmapi.Mount_BindOptions_MountPropagation_value[strings.ToUpper(string(m.BindOptions.Propagation))]; ok {
|
|
|
|
mount.BindOptions = &swarmapi.Mount_BindOptions{Propagation: swarmapi.Mount_BindOptions_MountPropagation(mountPropagation)}
|
|
|
|
} else if string(m.BindOptions.Propagation) != "" {
|
|
|
|
return nil, fmt.Errorf("invalid MountPropagation: %q", m.BindOptions.Propagation)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.VolumeOptions != nil {
|
|
|
|
mount.VolumeOptions = &swarmapi.Mount_VolumeOptions{
|
2016-07-05 18:43:28 +00:00
|
|
|
NoCopy: m.VolumeOptions.NoCopy,
|
|
|
|
Labels: m.VolumeOptions.Labels,
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
if m.VolumeOptions.DriverConfig != nil {
|
|
|
|
mount.VolumeOptions.DriverConfig = &swarmapi.Driver{
|
|
|
|
Name: m.VolumeOptions.DriverConfig.Name,
|
|
|
|
Options: m.VolumeOptions.DriverConfig.Options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
containerSpec.Mounts = append(containerSpec.Mounts, mount)
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:28:32 +00:00
|
|
|
if c.Healthcheck != nil {
|
|
|
|
containerSpec.Healthcheck = healthConfigToGRPC(c.Healthcheck)
|
|
|
|
}
|
|
|
|
|
2016-06-14 02:52:49 +00:00
|
|
|
return containerSpec, nil
|
|
|
|
}
|
2016-10-13 18:28:32 +00:00
|
|
|
|
|
|
|
func healthConfigFromGRPC(h *swarmapi.HealthConfig) *container.HealthConfig {
|
|
|
|
interval, _ := ptypes.Duration(h.Interval)
|
|
|
|
timeout, _ := ptypes.Duration(h.Timeout)
|
|
|
|
return &container.HealthConfig{
|
|
|
|
Test: h.Test,
|
|
|
|
Interval: interval,
|
|
|
|
Timeout: timeout,
|
|
|
|
Retries: int(h.Retries),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func healthConfigToGRPC(h *container.HealthConfig) *swarmapi.HealthConfig {
|
|
|
|
return &swarmapi.HealthConfig{
|
|
|
|
Test: h.Test,
|
|
|
|
Interval: ptypes.DurationProto(h.Interval),
|
|
|
|
Timeout: ptypes.DurationProto(h.Timeout),
|
|
|
|
Retries: int32(h.Retries),
|
|
|
|
}
|
|
|
|
}
|