github.com/kunnos/engine@v1.13.1/cli/command/stack/deploy_bundlefile.go (about)

     1  package stack
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  
     6  	"github.com/docker/docker/api/types"
     7  	"github.com/docker/docker/api/types/swarm"
     8  	"github.com/docker/docker/cli/command"
     9  	"github.com/docker/docker/cli/compose/convert"
    10  )
    11  
    12  func deployBundle(ctx context.Context, dockerCli *command.DockerCli, opts deployOptions) error {
    13  	bundle, err := loadBundlefile(dockerCli.Err(), opts.namespace, opts.bundlefile)
    14  	if err != nil {
    15  		return err
    16  	}
    17  
    18  	if err := checkDaemonIsSwarmManager(ctx, dockerCli); err != nil {
    19  		return err
    20  	}
    21  
    22  	namespace := convert.NewNamespace(opts.namespace)
    23  
    24  	networks := make(map[string]types.NetworkCreate)
    25  	for _, service := range bundle.Services {
    26  		for _, networkName := range service.Networks {
    27  			networks[networkName] = types.NetworkCreate{
    28  				Labels: convert.AddStackLabel(namespace, nil),
    29  			}
    30  		}
    31  	}
    32  
    33  	services := make(map[string]swarm.ServiceSpec)
    34  	for internalName, service := range bundle.Services {
    35  		name := namespace.Scope(internalName)
    36  
    37  		var ports []swarm.PortConfig
    38  		for _, portSpec := range service.Ports {
    39  			ports = append(ports, swarm.PortConfig{
    40  				Protocol:   swarm.PortConfigProtocol(portSpec.Protocol),
    41  				TargetPort: portSpec.Port,
    42  			})
    43  		}
    44  
    45  		nets := []swarm.NetworkAttachmentConfig{}
    46  		for _, networkName := range service.Networks {
    47  			nets = append(nets, swarm.NetworkAttachmentConfig{
    48  				Target:  namespace.Scope(networkName),
    49  				Aliases: []string{networkName},
    50  			})
    51  		}
    52  
    53  		serviceSpec := swarm.ServiceSpec{
    54  			Annotations: swarm.Annotations{
    55  				Name:   name,
    56  				Labels: convert.AddStackLabel(namespace, service.Labels),
    57  			},
    58  			TaskTemplate: swarm.TaskSpec{
    59  				ContainerSpec: swarm.ContainerSpec{
    60  					Image:   service.Image,
    61  					Command: service.Command,
    62  					Args:    service.Args,
    63  					Env:     service.Env,
    64  					// Service Labels will not be copied to Containers
    65  					// automatically during the deployment so we apply
    66  					// it here.
    67  					Labels: convert.AddStackLabel(namespace, nil),
    68  				},
    69  			},
    70  			EndpointSpec: &swarm.EndpointSpec{
    71  				Ports: ports,
    72  			},
    73  			Networks: nets,
    74  		}
    75  
    76  		services[internalName] = serviceSpec
    77  	}
    78  
    79  	if err := createNetworks(ctx, dockerCli, namespace, networks); err != nil {
    80  		return err
    81  	}
    82  	return deployServices(ctx, dockerCli, services, namespace, opts.sendRegistryAuth)
    83  }