2016-06-08 13:47:46 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-02 14:57:40 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-10-31 15:43:47 -04:00
|
|
|
"sort"
|
2016-10-25 17:41:45 -04:00
|
|
|
"strings"
|
2016-06-08 13:47:46 -04:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-11-02 14:57:40 -04:00
|
|
|
"github.com/aanand/compose-file/loader"
|
|
|
|
composetypes "github.com/aanand/compose-file/types"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-10-25 17:41:45 -04:00
|
|
|
"github.com/docker/docker/api/types/mount"
|
2016-11-02 14:57:40 -04:00
|
|
|
networktypes "github.com/docker/docker/api/types/network"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-06-08 13:47:46 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 14:54:01 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-11-02 14:57:40 -04:00
|
|
|
servicecmd "github.com/docker/docker/cli/command/service"
|
2016-10-28 17:30:20 -04:00
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
|
|
|
"github.com/docker/docker/opts"
|
2016-11-02 14:57:40 -04:00
|
|
|
"github.com/docker/go-connections/nat"
|
2016-06-08 13:47:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultNetworkDriver = "overlay"
|
|
|
|
)
|
|
|
|
|
|
|
|
type deployOptions struct {
|
2016-11-02 14:57:40 -04:00
|
|
|
composefile string
|
2016-07-15 16:17:03 -04:00
|
|
|
namespace string
|
|
|
|
sendRegistryAuth bool
|
2016-06-08 13:47:46 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 14:54:01 -04:00
|
|
|
func newDeployCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-08 13:47:46 -04:00
|
|
|
var opts deployOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "deploy [OPTIONS] STACK",
|
|
|
|
Aliases: []string{"up"},
|
2016-11-02 14:57:40 -04:00
|
|
|
Short: "Deploy a new stack or update an existing stack",
|
2016-06-08 13:47:46 -04:00
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-11-02 14:57:40 -04:00
|
|
|
opts.namespace = args[0]
|
2016-06-08 13:47:46 -04:00
|
|
|
return runDeploy(dockerCli, opts)
|
|
|
|
},
|
2016-11-02 20:43:32 -04:00
|
|
|
Tags: map[string]string{"experimental": "", "version": "1.25"},
|
2016-06-08 13:47:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2016-11-02 14:57:40 -04:00
|
|
|
addComposefileFlag(&opts.composefile, flags)
|
2016-07-15 16:17:03 -04:00
|
|
|
addRegistryAuthFlag(&opts.sendRegistryAuth, flags)
|
2016-06-08 13:47:46 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:54:01 -04:00
|
|
|
func runDeploy(dockerCli *command.DockerCli, opts deployOptions) error {
|
2016-11-02 14:57:40 -04:00
|
|
|
configDetails, err := getConfigDetails(opts)
|
2016-06-08 13:47:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-02 14:57:40 -04:00
|
|
|
config, err := loader.Load(configDetails)
|
2016-07-29 06:12:20 -04:00
|
|
|
if err != nil {
|
2016-10-31 15:43:47 -04:00
|
|
|
if fpe, ok := err.(*loader.ForbiddenPropertiesError); ok {
|
|
|
|
return fmt.Errorf("Compose file contains unsupported options:\n\n%s\n",
|
|
|
|
propertyWarnings(fpe.Properties))
|
|
|
|
}
|
|
|
|
|
2016-07-29 06:12:20 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-31 15:43:47 -04:00
|
|
|
unsupportedProperties := loader.GetUnsupportedProperties(configDetails)
|
|
|
|
if len(unsupportedProperties) > 0 {
|
|
|
|
fmt.Printf("Ignoring unsupported options: %s\n\n",
|
|
|
|
strings.Join(unsupportedProperties, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
deprecatedProperties := loader.GetDeprecatedProperties(configDetails)
|
|
|
|
if len(deprecatedProperties) > 0 {
|
|
|
|
fmt.Printf("Ignoring deprecated options:\n\n%s\n\n",
|
|
|
|
propertyWarnings(deprecatedProperties))
|
|
|
|
}
|
|
|
|
|
2016-06-08 13:47:46 -04:00
|
|
|
ctx := context.Background()
|
2016-11-02 14:57:40 -04:00
|
|
|
if err := createNetworks(ctx, dockerCli, config.Networks, opts.namespace); err != nil {
|
2016-06-08 13:47:46 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-11-02 14:57:40 -04:00
|
|
|
return deployServices(ctx, dockerCli, config, opts.namespace, opts.sendRegistryAuth)
|
2016-06-08 13:47:46 -04:00
|
|
|
}
|
|
|
|
|
2016-10-31 15:43:47 -04:00
|
|
|
func propertyWarnings(properties map[string]string) string {
|
|
|
|
var msgs []string
|
|
|
|
for name, description := range properties {
|
|
|
|
msgs = append(msgs, fmt.Sprintf("%s: %s", name, description))
|
|
|
|
}
|
|
|
|
sort.Strings(msgs)
|
|
|
|
return strings.Join(msgs, "\n\n")
|
|
|
|
}
|
|
|
|
|
2016-11-02 14:57:40 -04:00
|
|
|
func getConfigDetails(opts deployOptions) (composetypes.ConfigDetails, error) {
|
|
|
|
var details composetypes.ConfigDetails
|
|
|
|
var err error
|
|
|
|
|
|
|
|
details.WorkingDir, err = os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return details, err
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile, err := getConfigFile(opts.composefile)
|
|
|
|
if err != nil {
|
|
|
|
return details, err
|
2016-06-08 13:47:46 -04:00
|
|
|
}
|
2016-11-02 14:57:40 -04:00
|
|
|
// TODO: support multiple files
|
|
|
|
details.ConfigFiles = []composetypes.ConfigFile{*configFile}
|
|
|
|
return details, nil
|
|
|
|
}
|
2016-06-08 13:47:46 -04:00
|
|
|
|
2016-11-02 14:57:40 -04:00
|
|
|
func getConfigFile(filename string) (*composetypes.ConfigFile, error) {
|
|
|
|
bytes, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-06-08 13:47:46 -04:00
|
|
|
}
|
2016-10-25 17:41:45 -04:00
|
|
|
config, err := loader.ParseYAML(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &composetypes.ConfigFile{
|
|
|
|
Filename: filename,
|
|
|
|
Config: config,
|
|
|
|
}, nil
|
2016-06-08 13:47:46 -04:00
|
|
|
}
|
|
|
|
|
2016-11-02 14:57:40 -04:00
|
|
|
func createNetworks(
|
2016-06-08 13:47:46 -04:00
|
|
|
ctx context.Context,
|
2016-09-08 14:54:01 -04:00
|
|
|
dockerCli *command.DockerCli,
|
2016-11-02 14:57:40 -04:00
|
|
|
networks map[string]composetypes.NetworkConfig,
|
2016-06-08 13:47:46 -04:00
|
|
|
namespace string,
|
|
|
|
) error {
|
|
|
|
client := dockerCli.Client()
|
|
|
|
|
|
|
|
existingNetworks, err := getNetworks(ctx, client, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
existingNetworkMap := make(map[string]types.NetworkResource)
|
|
|
|
for _, network := range existingNetworks {
|
|
|
|
existingNetworkMap[network.Name] = network
|
|
|
|
}
|
|
|
|
|
2016-11-02 14:57:40 -04:00
|
|
|
for internalName, network := range networks {
|
2016-10-25 17:41:45 -04:00
|
|
|
if network.External.Name != "" {
|
2016-11-02 14:57:40 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-06-08 13:47:46 -04:00
|
|
|
|
|
|
|
name := fmt.Sprintf("%s_%s", namespace, internalName)
|
|
|
|
if _, exists := existingNetworkMap[name]; exists {
|
|
|
|
continue
|
|
|
|
}
|
2016-11-02 14:57:40 -04:00
|
|
|
|
|
|
|
createOpts := types.NetworkCreate{
|
|
|
|
// TODO: support network labels from compose file
|
|
|
|
Labels: getStackLabels(namespace, nil),
|
|
|
|
Driver: network.Driver,
|
|
|
|
Options: network.DriverOpts,
|
|
|
|
}
|
|
|
|
|
|
|
|
if network.Ipam.Driver != "" {
|
|
|
|
createOpts.IPAM = &networktypes.IPAM{
|
|
|
|
Driver: network.Ipam.Driver,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: IPAMConfig.Config
|
|
|
|
|
|
|
|
if createOpts.Driver == "" {
|
|
|
|
createOpts.Driver = defaultNetworkDriver
|
|
|
|
}
|
|
|
|
|
2016-06-08 13:47:46 -04:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "Creating network %s\n", name)
|
|
|
|
if _, err := client.NetworkCreate(ctx, name, createOpts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-02 14:57:40 -04:00
|
|
|
func convertNetworks(
|
|
|
|
networks map[string]*composetypes.ServiceNetworkConfig,
|
|
|
|
namespace string,
|
|
|
|
name string,
|
|
|
|
) []swarm.NetworkAttachmentConfig {
|
2016-06-08 13:47:46 -04:00
|
|
|
nets := []swarm.NetworkAttachmentConfig{}
|
2016-11-02 14:57:40 -04:00
|
|
|
for networkName, network := range networks {
|
2016-06-08 13:47:46 -04:00
|
|
|
nets = append(nets, swarm.NetworkAttachmentConfig{
|
2016-11-02 14:57:40 -04:00
|
|
|
// TODO: only do this name mangling in one function
|
|
|
|
Target: namespace + "_" + networkName,
|
|
|
|
Aliases: append(network.Aliases, name),
|
2016-06-08 13:47:46 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return nets
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:41:45 -04:00
|
|
|
func convertVolumes(
|
|
|
|
serviceVolumes []string,
|
|
|
|
stackVolumes map[string]composetypes.VolumeConfig,
|
|
|
|
namespace string,
|
|
|
|
) ([]mount.Mount, error) {
|
|
|
|
var mounts []mount.Mount
|
|
|
|
|
|
|
|
for _, volumeString := range serviceVolumes {
|
|
|
|
var (
|
|
|
|
source, target string
|
|
|
|
mountType mount.Type
|
|
|
|
readOnly bool
|
|
|
|
volumeOptions *mount.VolumeOptions
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: split Windows path mappings properly
|
|
|
|
parts := strings.SplitN(volumeString, ":", 3)
|
|
|
|
|
|
|
|
if len(parts) == 3 {
|
|
|
|
source = parts[0]
|
|
|
|
target = parts[1]
|
|
|
|
if parts[2] == "ro" {
|
|
|
|
readOnly = true
|
|
|
|
}
|
|
|
|
} else if len(parts) == 2 {
|
|
|
|
source = parts[0]
|
|
|
|
target = parts[1]
|
|
|
|
} else if len(parts) == 1 {
|
|
|
|
target = parts[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: catch Windows paths here
|
|
|
|
if strings.HasPrefix(source, "/") {
|
|
|
|
mountType = mount.TypeBind
|
|
|
|
} else {
|
|
|
|
mountType = mount.TypeVolume
|
|
|
|
|
|
|
|
stackVolume, exists := stackVolumes[source]
|
|
|
|
if !exists {
|
|
|
|
// TODO: better error message (include service name)
|
|
|
|
return nil, fmt.Errorf("Undefined volume: %s", source)
|
|
|
|
}
|
|
|
|
|
|
|
|
if stackVolume.External.Name != "" {
|
|
|
|
source = stackVolume.External.Name
|
|
|
|
} else {
|
|
|
|
volumeOptions = &mount.VolumeOptions{
|
|
|
|
Labels: stackVolume.Labels,
|
|
|
|
}
|
|
|
|
|
|
|
|
if stackVolume.Driver != "" {
|
|
|
|
volumeOptions.DriverConfig = &mount.Driver{
|
|
|
|
Name: stackVolume.Driver,
|
|
|
|
Options: stackVolume.DriverOpts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove this duplication
|
|
|
|
source = fmt.Sprintf("%s_%s", namespace, source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mounts = append(mounts, mount.Mount{
|
|
|
|
Type: mountType,
|
|
|
|
Source: source,
|
|
|
|
Target: target,
|
|
|
|
ReadOnly: readOnly,
|
|
|
|
VolumeOptions: volumeOptions,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return mounts, nil
|
|
|
|
}
|
|
|
|
|
2016-06-08 13:47:46 -04:00
|
|
|
func deployServices(
|
|
|
|
ctx context.Context,
|
2016-09-08 14:54:01 -04:00
|
|
|
dockerCli *command.DockerCli,
|
2016-11-02 14:57:40 -04:00
|
|
|
config *composetypes.Config,
|
2016-06-08 13:47:46 -04:00
|
|
|
namespace string,
|
2016-07-15 16:17:03 -04:00
|
|
|
sendAuth bool,
|
2016-06-08 13:47:46 -04:00
|
|
|
) error {
|
|
|
|
apiClient := dockerCli.Client()
|
|
|
|
out := dockerCli.Out()
|
2016-11-02 14:57:40 -04:00
|
|
|
services := config.Services
|
|
|
|
volumes := config.Volumes
|
2016-06-08 13:47:46 -04:00
|
|
|
|
|
|
|
existingServices, err := getServices(ctx, apiClient, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
existingServiceMap := make(map[string]swarm.Service)
|
|
|
|
for _, service := range existingServices {
|
|
|
|
existingServiceMap[service.Spec.Name] = service
|
|
|
|
}
|
|
|
|
|
2016-11-02 14:57:40 -04:00
|
|
|
for _, service := range services {
|
|
|
|
name := fmt.Sprintf("%s_%s", namespace, service.Name)
|
2016-06-08 13:47:46 -04:00
|
|
|
|
2016-11-02 14:57:40 -04:00
|
|
|
serviceSpec, err := convertService(namespace, service, volumes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-06-08 13:47:46 -04:00
|
|
|
}
|
|
|
|
|
2016-07-15 16:17:03 -04:00
|
|
|
encodedAuth := ""
|
|
|
|
if sendAuth {
|
|
|
|
// Retrieve encoded auth token from the image reference
|
|
|
|
image := serviceSpec.TaskTemplate.ContainerSpec.Image
|
2016-09-09 15:38:00 -04:00
|
|
|
encodedAuth, err = command.RetrieveAuthTokenFromImage(ctx, dockerCli, image)
|
2016-07-15 16:17:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 13:47:46 -04:00
|
|
|
if service, exists := existingServiceMap[name]; exists {
|
|
|
|
fmt.Fprintf(out, "Updating service %s (id: %s)\n", name, service.ID)
|
|
|
|
|
2016-07-15 16:17:03 -04:00
|
|
|
updateOpts := types.ServiceUpdateOptions{}
|
|
|
|
if sendAuth {
|
|
|
|
updateOpts.EncodedRegistryAuth = encodedAuth
|
|
|
|
}
|
2016-06-08 13:47:46 -04:00
|
|
|
if err := apiClient.ServiceUpdate(
|
|
|
|
ctx,
|
|
|
|
service.ID,
|
|
|
|
service.Version,
|
|
|
|
serviceSpec,
|
2016-07-15 16:17:03 -04:00
|
|
|
updateOpts,
|
2016-06-08 13:47:46 -04:00
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(out, "Creating service %s\n", name)
|
|
|
|
|
2016-07-15 16:17:03 -04:00
|
|
|
createOpts := types.ServiceCreateOptions{}
|
|
|
|
if sendAuth {
|
|
|
|
createOpts.EncodedRegistryAuth = encodedAuth
|
|
|
|
}
|
|
|
|
if _, err := apiClient.ServiceCreate(ctx, serviceSpec, createOpts); err != nil {
|
2016-06-08 13:47:46 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-02 14:57:40 -04:00
|
|
|
|
|
|
|
func convertService(
|
|
|
|
namespace string,
|
|
|
|
service composetypes.ServiceConfig,
|
|
|
|
volumes map[string]composetypes.VolumeConfig,
|
|
|
|
) (swarm.ServiceSpec, error) {
|
|
|
|
// TODO: remove this duplication
|
|
|
|
name := fmt.Sprintf("%s_%s", namespace, service.Name)
|
|
|
|
|
|
|
|
endpoint, err := convertEndpointSpec(service.Ports)
|
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mode, err := convertDeployMode(service.Deploy.Mode, service.Deploy.Replicas)
|
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:41:45 -04:00
|
|
|
mounts, err := convertVolumes(service.Volumes, volumes, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
2016-10-28 17:30:20 -04:00
|
|
|
resources, err := convertResources(service.Deploy.Resources)
|
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
restartPolicy, err := convertRestartPolicy(
|
|
|
|
service.Restart, service.Deploy.RestartPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return swarm.ServiceSpec{}, err
|
|
|
|
}
|
|
|
|
|
2016-11-02 14:57:40 -04:00
|
|
|
serviceSpec := swarm.ServiceSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: name,
|
2016-10-28 17:30:20 -04:00
|
|
|
Labels: getStackLabels(namespace, service.Deploy.Labels),
|
2016-11-02 14:57:40 -04:00
|
|
|
},
|
|
|
|
TaskTemplate: swarm.TaskSpec{
|
|
|
|
ContainerSpec: swarm.ContainerSpec{
|
2016-10-28 17:30:20 -04:00
|
|
|
Image: service.Image,
|
|
|
|
Command: service.Entrypoint,
|
|
|
|
Args: service.Command,
|
|
|
|
Hostname: service.Hostname,
|
|
|
|
Env: convertEnvironment(service.Environment),
|
|
|
|
Labels: getStackLabels(namespace, service.Labels),
|
|
|
|
Dir: service.WorkingDir,
|
|
|
|
User: service.User,
|
|
|
|
Mounts: mounts,
|
|
|
|
StopGracePeriod: service.StopGracePeriod,
|
2016-11-02 14:57:40 -04:00
|
|
|
},
|
2016-10-28 17:30:20 -04:00
|
|
|
Resources: resources,
|
|
|
|
RestartPolicy: restartPolicy,
|
2016-11-02 14:57:40 -04:00
|
|
|
Placement: &swarm.Placement{
|
|
|
|
Constraints: service.Deploy.Placement.Constraints,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
EndpointSpec: endpoint,
|
|
|
|
Mode: mode,
|
|
|
|
Networks: convertNetworks(service.Networks, namespace, service.Name),
|
2016-10-28 17:30:20 -04:00
|
|
|
UpdateConfig: convertUpdateConfig(service.Deploy.UpdateConfig),
|
2016-11-02 14:57:40 -04:00
|
|
|
}
|
|
|
|
|
2016-10-28 17:30:20 -04:00
|
|
|
return serviceSpec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertRestartPolicy(restart string, source *composetypes.RestartPolicy) (*swarm.RestartPolicy, error) {
|
|
|
|
// TODO: log if restart is being ignored
|
|
|
|
if source == nil {
|
|
|
|
policy, err := runconfigopts.ParseRestartPolicy(restart)
|
2016-11-02 14:57:40 -04:00
|
|
|
if err != nil {
|
2016-10-28 17:30:20 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// TODO: is this an accurate convertion?
|
|
|
|
switch {
|
|
|
|
case policy.IsNone(), policy.IsAlways(), policy.IsUnlessStopped():
|
|
|
|
return nil, nil
|
|
|
|
case policy.IsOnFailure():
|
|
|
|
attempts := uint64(policy.MaximumRetryCount)
|
|
|
|
return &swarm.RestartPolicy{
|
|
|
|
Condition: swarm.RestartPolicyConditionOnFailure,
|
|
|
|
MaxAttempts: &attempts,
|
|
|
|
}, nil
|
2016-11-02 14:57:40 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-31 15:43:47 -04:00
|
|
|
attempts := uint64(*source.MaxAttempts)
|
2016-10-28 17:30:20 -04:00
|
|
|
return &swarm.RestartPolicy{
|
|
|
|
Condition: swarm.RestartPolicyCondition(source.Condition),
|
|
|
|
Delay: source.Delay,
|
2016-10-31 15:43:47 -04:00
|
|
|
MaxAttempts: &attempts,
|
2016-10-28 17:30:20 -04:00
|
|
|
Window: source.Window,
|
|
|
|
}, nil
|
|
|
|
}
|
2016-11-02 14:57:40 -04:00
|
|
|
|
2016-10-28 17:30:20 -04:00
|
|
|
func convertUpdateConfig(source *composetypes.UpdateConfig) *swarm.UpdateConfig {
|
|
|
|
if source == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &swarm.UpdateConfig{
|
|
|
|
Parallelism: source.Parallelism,
|
|
|
|
Delay: source.Delay,
|
|
|
|
FailureAction: source.FailureAction,
|
|
|
|
Monitor: source.Monitor,
|
|
|
|
MaxFailureRatio: source.MaxFailureRatio,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertResources(source composetypes.Resources) (*swarm.ResourceRequirements, error) {
|
|
|
|
resources := &swarm.ResourceRequirements{}
|
|
|
|
if source.Limits != nil {
|
|
|
|
cpus, err := opts.ParseCPUs(source.Limits.NanoCPUs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resources.Limits = &swarm.Resources{
|
|
|
|
NanoCPUs: cpus,
|
|
|
|
MemoryBytes: int64(source.Limits.MemoryBytes),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if source.Reservations != nil {
|
|
|
|
cpus, err := opts.ParseCPUs(source.Reservations.NanoCPUs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resources.Reservations = &swarm.Resources{
|
|
|
|
NanoCPUs: cpus,
|
|
|
|
MemoryBytes: int64(source.Reservations.MemoryBytes),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resources, nil
|
2016-11-02 14:57:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func convertEndpointSpec(source []string) (*swarm.EndpointSpec, error) {
|
|
|
|
portConfigs := []swarm.PortConfig{}
|
|
|
|
ports, portBindings, err := nat.ParsePortSpecs(source)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for port := range ports {
|
|
|
|
portConfigs = append(
|
|
|
|
portConfigs,
|
|
|
|
servicecmd.ConvertPortToPortConfig(port, portBindings)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &swarm.EndpointSpec{Ports: portConfigs}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertEnvironment(source map[string]string) []string {
|
|
|
|
var output []string
|
|
|
|
|
|
|
|
for name, value := range source {
|
|
|
|
output = append(output, fmt.Sprintf("%s=%s", name, value))
|
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2016-10-28 17:30:20 -04:00
|
|
|
func convertDeployMode(mode string, replicas *uint64) (swarm.ServiceMode, error) {
|
2016-11-02 14:57:40 -04:00
|
|
|
serviceMode := swarm.ServiceMode{}
|
|
|
|
|
|
|
|
switch mode {
|
|
|
|
case "global":
|
2016-10-28 17:30:20 -04:00
|
|
|
if replicas != nil {
|
2016-11-02 14:57:40 -04:00
|
|
|
return serviceMode, fmt.Errorf("replicas can only be used with replicated mode")
|
|
|
|
}
|
|
|
|
serviceMode.Global = &swarm.GlobalService{}
|
2016-11-02 09:10:34 -04:00
|
|
|
case "replicated", "":
|
2016-10-28 17:30:20 -04:00
|
|
|
serviceMode.Replicated = &swarm.ReplicatedService{Replicas: replicas}
|
2016-11-02 14:57:40 -04:00
|
|
|
default:
|
|
|
|
return serviceMode, fmt.Errorf("Unknown mode: %s", mode)
|
|
|
|
}
|
|
|
|
return serviceMode, nil
|
|
|
|
}
|