2016-06-13 22:56:23 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
mounttypes "github.com/docker/docker/api/types/mount"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
|
|
|
"github.com/docker/go-connections/nat"
|
|
|
|
units "github.com/docker/go-units"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type int64Value interface {
|
|
|
|
Value() int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type memBytes int64
|
|
|
|
|
|
|
|
func (m *memBytes) String() string {
|
2016-06-15 13:11:23 -04:00
|
|
|
return units.BytesSize(float64(m.Value()))
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memBytes) Set(value string) error {
|
|
|
|
val, err := units.RAMInBytes(value)
|
|
|
|
*m = memBytes(val)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memBytes) Type() string {
|
|
|
|
return "MemoryBytes"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memBytes) Value() int64 {
|
|
|
|
return int64(*m)
|
|
|
|
}
|
|
|
|
|
|
|
|
type nanoCPUs int64
|
|
|
|
|
|
|
|
func (c *nanoCPUs) String() string {
|
2016-06-15 13:11:23 -04:00
|
|
|
return big.NewRat(c.Value(), 1e9).FloatString(3)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nanoCPUs) Set(value string) error {
|
|
|
|
cpu, ok := new(big.Rat).SetString(value)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Failed to parse %v as a rational number", value)
|
|
|
|
}
|
|
|
|
nano := cpu.Mul(cpu, big.NewRat(1e9, 1))
|
|
|
|
if !nano.IsInt() {
|
|
|
|
return fmt.Errorf("value is too precise")
|
|
|
|
}
|
|
|
|
*c = nanoCPUs(nano.Num().Int64())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nanoCPUs) Type() string {
|
|
|
|
return "NanoCPUs"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nanoCPUs) Value() int64 {
|
|
|
|
return int64(*c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DurationOpt is an option type for time.Duration that uses a pointer. This
|
|
|
|
// allows us to get nil values outside, instead of defaulting to 0
|
|
|
|
type DurationOpt struct {
|
|
|
|
value *time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a new value on the option
|
|
|
|
func (d *DurationOpt) Set(s string) error {
|
|
|
|
v, err := time.ParseDuration(s)
|
|
|
|
d.value = &v
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of this option
|
|
|
|
func (d *DurationOpt) Type() string {
|
|
|
|
return "duration-ptr"
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string repr of this option
|
|
|
|
func (d *DurationOpt) String() string {
|
|
|
|
if d.value != nil {
|
|
|
|
return d.value.String()
|
|
|
|
}
|
|
|
|
return "none"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the time.Duration
|
|
|
|
func (d *DurationOpt) Value() *time.Duration {
|
|
|
|
return d.value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uint64Opt represents a uint64.
|
|
|
|
type Uint64Opt struct {
|
|
|
|
value *uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a new value on the option
|
|
|
|
func (i *Uint64Opt) Set(s string) error {
|
|
|
|
v, err := strconv.ParseUint(s, 0, 64)
|
|
|
|
i.value = &v
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of this option
|
|
|
|
func (i *Uint64Opt) Type() string {
|
|
|
|
return "uint64-ptr"
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string repr of this option
|
|
|
|
func (i *Uint64Opt) String() string {
|
|
|
|
if i.value != nil {
|
|
|
|
return fmt.Sprintf("%v", *i.value)
|
|
|
|
}
|
|
|
|
return "none"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the uint64
|
|
|
|
func (i *Uint64Opt) Value() *uint64 {
|
|
|
|
return i.value
|
|
|
|
}
|
|
|
|
|
|
|
|
// MountOpt is a Value type for parsing mounts
|
|
|
|
type MountOpt struct {
|
2016-08-15 12:13:18 -04:00
|
|
|
values []mounttypes.Mount
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set a new mount value
|
|
|
|
func (m *MountOpt) Set(value string) error {
|
|
|
|
csvReader := csv.NewReader(strings.NewReader(value))
|
|
|
|
fields, err := csvReader.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-15 12:13:18 -04:00
|
|
|
mount := mounttypes.Mount{}
|
2016-06-13 22:56:23 -04:00
|
|
|
|
2016-08-15 12:13:18 -04:00
|
|
|
volumeOptions := func() *mounttypes.VolumeOptions {
|
2016-06-13 22:56:23 -04:00
|
|
|
if mount.VolumeOptions == nil {
|
2016-08-15 12:13:18 -04:00
|
|
|
mount.VolumeOptions = &mounttypes.VolumeOptions{
|
2016-06-13 22:56:23 -04:00
|
|
|
Labels: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 12:25:39 -04:00
|
|
|
if mount.VolumeOptions.DriverConfig == nil {
|
2016-08-15 12:13:18 -04:00
|
|
|
mount.VolumeOptions.DriverConfig = &mounttypes.Driver{}
|
2016-06-14 12:25:39 -04:00
|
|
|
}
|
2016-06-13 22:56:23 -04:00
|
|
|
return mount.VolumeOptions
|
|
|
|
}
|
|
|
|
|
2016-08-15 12:13:18 -04:00
|
|
|
bindOptions := func() *mounttypes.BindOptions {
|
2016-06-24 11:57:17 -04:00
|
|
|
if mount.BindOptions == nil {
|
2016-08-15 12:13:18 -04:00
|
|
|
mount.BindOptions = new(mounttypes.BindOptions)
|
2016-06-24 11:57:17 -04:00
|
|
|
}
|
|
|
|
return mount.BindOptions
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
setValueOnMap := func(target map[string]string, value string) {
|
|
|
|
parts := strings.SplitN(value, "=", 2)
|
|
|
|
if len(parts) == 1 {
|
|
|
|
target[value] = ""
|
|
|
|
} else {
|
|
|
|
target[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 12:13:18 -04:00
|
|
|
mount.Type = mounttypes.TypeVolume // default to volume mounts
|
2016-07-05 14:43:28 -04:00
|
|
|
// Set writable as the default
|
2016-06-13 22:56:23 -04:00
|
|
|
for _, field := range fields {
|
|
|
|
parts := strings.SplitN(field, "=", 2)
|
2016-07-20 19:34:45 -04:00
|
|
|
key := strings.ToLower(parts[0])
|
2016-07-05 14:43:28 -04:00
|
|
|
|
2016-07-20 19:34:45 -04:00
|
|
|
if len(parts) == 1 {
|
2016-07-25 12:28:05 -04:00
|
|
|
switch key {
|
|
|
|
case "readonly", "ro":
|
2016-07-20 19:34:45 -04:00
|
|
|
mount.ReadOnly = true
|
|
|
|
continue
|
2016-07-25 12:28:05 -04:00
|
|
|
case "volume-nocopy":
|
2016-07-20 19:34:45 -04:00
|
|
|
volumeOptions().NoCopy = true
|
|
|
|
continue
|
|
|
|
}
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(parts) != 2 {
|
2016-06-15 10:41:57 -04:00
|
|
|
return fmt.Errorf("invalid field '%s' must be a key=value pair", field)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
2016-07-20 19:34:45 -04:00
|
|
|
value := parts[1]
|
|
|
|
switch key {
|
2016-06-13 22:56:23 -04:00
|
|
|
case "type":
|
2016-08-15 12:13:18 -04:00
|
|
|
mount.Type = mounttypes.Type(strings.ToLower(value))
|
2016-07-25 12:28:05 -04:00
|
|
|
case "source", "src":
|
2016-06-13 22:56:23 -04:00
|
|
|
mount.Source = value
|
2016-07-25 12:28:05 -04:00
|
|
|
case "target", "dst", "destination":
|
2016-06-13 22:56:23 -04:00
|
|
|
mount.Target = value
|
2016-07-20 19:34:45 -04:00
|
|
|
case "readonly", "ro":
|
2016-07-25 12:28:05 -04:00
|
|
|
mount.ReadOnly, err = strconv.ParseBool(value)
|
2016-06-13 22:56:23 -04:00
|
|
|
if err != nil {
|
2016-07-25 12:28:05 -04:00
|
|
|
return fmt.Errorf("invalid value for %s: %s", key, value)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
case "bind-propagation":
|
2016-08-15 12:13:18 -04:00
|
|
|
bindOptions().Propagation = mounttypes.Propagation(strings.ToLower(value))
|
2016-07-05 14:43:28 -04:00
|
|
|
case "volume-nocopy":
|
|
|
|
volumeOptions().NoCopy, err = strconv.ParseBool(value)
|
2016-06-13 22:56:23 -04:00
|
|
|
if err != nil {
|
2016-06-15 13:11:23 -04:00
|
|
|
return fmt.Errorf("invalid value for populate: %s", value)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
case "volume-label":
|
|
|
|
setValueOnMap(volumeOptions().Labels, value)
|
|
|
|
case "volume-driver":
|
|
|
|
volumeOptions().DriverConfig.Name = value
|
2016-07-20 19:05:33 -04:00
|
|
|
case "volume-opt":
|
2016-06-13 22:56:23 -04:00
|
|
|
if volumeOptions().DriverConfig.Options == nil {
|
|
|
|
volumeOptions().DriverConfig.Options = make(map[string]string)
|
|
|
|
}
|
|
|
|
setValueOnMap(volumeOptions().DriverConfig.Options, value)
|
|
|
|
default:
|
2016-06-29 15:14:55 -04:00
|
|
|
return fmt.Errorf("unexpected key '%s' in '%s'", key, field)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if mount.Type == "" {
|
|
|
|
return fmt.Errorf("type is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if mount.Target == "" {
|
|
|
|
return fmt.Errorf("target is required")
|
|
|
|
}
|
|
|
|
|
2016-07-05 14:43:28 -04:00
|
|
|
if mount.VolumeOptions != nil && mount.Source == "" {
|
|
|
|
return fmt.Errorf("source is required when specifying volume-* options")
|
|
|
|
}
|
|
|
|
|
2016-08-15 12:13:18 -04:00
|
|
|
if mount.Type == mounttypes.TypeBind && mount.VolumeOptions != nil {
|
|
|
|
return fmt.Errorf("cannot mix 'volume-*' options with mount type '%s'", mounttypes.TypeBind)
|
2016-07-05 14:43:28 -04:00
|
|
|
}
|
2016-08-15 12:13:18 -04:00
|
|
|
if mount.Type == mounttypes.TypeVolume && mount.BindOptions != nil {
|
|
|
|
return fmt.Errorf("cannot mix 'bind-*' options with mount type '%s'", mounttypes.TypeVolume)
|
2016-07-05 14:43:28 -04:00
|
|
|
}
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
m.values = append(m.values, mount)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of this option
|
|
|
|
func (m *MountOpt) Type() string {
|
|
|
|
return "mount"
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string repr of this option
|
|
|
|
func (m *MountOpt) String() string {
|
|
|
|
mounts := []string{}
|
|
|
|
for _, mount := range m.values {
|
2016-06-15 13:11:23 -04:00
|
|
|
repr := fmt.Sprintf("%s %s %s", mount.Type, mount.Source, mount.Target)
|
|
|
|
mounts = append(mounts, repr)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
return strings.Join(mounts, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the mounts
|
2016-08-15 12:13:18 -04:00
|
|
|
func (m *MountOpt) Value() []mounttypes.Mount {
|
2016-06-13 22:56:23 -04:00
|
|
|
return m.values
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateOptions struct {
|
|
|
|
parallelism uint64
|
|
|
|
delay time.Duration
|
2016-07-22 14:35:51 -04:00
|
|
|
onFailure string
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type resourceOptions struct {
|
|
|
|
limitCPU nanoCPUs
|
|
|
|
limitMemBytes memBytes
|
|
|
|
resCPU nanoCPUs
|
|
|
|
resMemBytes memBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resourceOptions) ToResourceRequirements() *swarm.ResourceRequirements {
|
|
|
|
return &swarm.ResourceRequirements{
|
|
|
|
Limits: &swarm.Resources{
|
|
|
|
NanoCPUs: r.limitCPU.Value(),
|
|
|
|
MemoryBytes: r.limitMemBytes.Value(),
|
|
|
|
},
|
|
|
|
Reservations: &swarm.Resources{
|
|
|
|
NanoCPUs: r.resCPU.Value(),
|
|
|
|
MemoryBytes: r.resMemBytes.Value(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type restartPolicyOptions struct {
|
|
|
|
condition string
|
|
|
|
delay DurationOpt
|
|
|
|
maxAttempts Uint64Opt
|
|
|
|
window DurationOpt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *restartPolicyOptions) ToRestartPolicy() *swarm.RestartPolicy {
|
|
|
|
return &swarm.RestartPolicy{
|
|
|
|
Condition: swarm.RestartPolicyCondition(r.condition),
|
|
|
|
Delay: r.delay.Value(),
|
|
|
|
MaxAttempts: r.maxAttempts.Value(),
|
|
|
|
Window: r.window.Value(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertNetworks(networks []string) []swarm.NetworkAttachmentConfig {
|
|
|
|
nets := []swarm.NetworkAttachmentConfig{}
|
|
|
|
for _, network := range networks {
|
|
|
|
nets = append(nets, swarm.NetworkAttachmentConfig{Target: network})
|
|
|
|
}
|
|
|
|
return nets
|
|
|
|
}
|
|
|
|
|
|
|
|
type endpointOptions struct {
|
|
|
|
mode string
|
|
|
|
ports opts.ListOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *endpointOptions) ToEndpointSpec() *swarm.EndpointSpec {
|
|
|
|
portConfigs := []swarm.PortConfig{}
|
|
|
|
// We can ignore errors because the format was already validated by ValidatePort
|
|
|
|
ports, portBindings, _ := nat.ParsePortSpecs(e.ports.GetAll())
|
|
|
|
|
|
|
|
for port := range ports {
|
|
|
|
portConfigs = append(portConfigs, convertPortToPortConfig(port, portBindings)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &swarm.EndpointSpec{
|
2016-06-17 22:06:12 -04:00
|
|
|
Mode: swarm.ResolutionMode(strings.ToLower(e.mode)),
|
2016-06-13 22:56:23 -04:00
|
|
|
Ports: portConfigs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertPortToPortConfig(
|
|
|
|
port nat.Port,
|
|
|
|
portBindings map[nat.Port][]nat.PortBinding,
|
|
|
|
) []swarm.PortConfig {
|
|
|
|
ports := []swarm.PortConfig{}
|
|
|
|
|
|
|
|
for _, binding := range portBindings[port] {
|
|
|
|
hostPort, _ := strconv.ParseUint(binding.HostPort, 10, 16)
|
|
|
|
ports = append(ports, swarm.PortConfig{
|
|
|
|
//TODO Name: ?
|
|
|
|
Protocol: swarm.PortConfigProtocol(strings.ToLower(port.Proto())),
|
|
|
|
TargetPort: uint32(port.Int()),
|
|
|
|
PublishedPort: uint32(hostPort),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return ports
|
|
|
|
}
|
|
|
|
|
2016-07-08 21:44:18 -04:00
|
|
|
type logDriverOptions struct {
|
|
|
|
name string
|
|
|
|
opts opts.ListOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLogDriverOptions() logDriverOptions {
|
|
|
|
return logDriverOptions{opts: opts.NewListOpts(runconfigopts.ValidateEnv)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ldo *logDriverOptions) toLogDriver() *swarm.Driver {
|
|
|
|
if ldo.name == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the log driver only if specified.
|
|
|
|
return &swarm.Driver{
|
|
|
|
Name: ldo.name,
|
|
|
|
Options: runconfigopts.ConvertKVStringsToMap(ldo.opts.GetAll()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
// ValidatePort validates a string is in the expected format for a port definition
|
|
|
|
func ValidatePort(value string) (string, error) {
|
|
|
|
portMappings, err := nat.ParsePortSpec(value)
|
|
|
|
for _, portMapping := range portMappings {
|
|
|
|
if portMapping.Binding.HostIP != "" {
|
|
|
|
return "", fmt.Errorf("HostIP is not supported by a service.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type serviceOptions struct {
|
2016-07-25 05:38:01 -04:00
|
|
|
name string
|
|
|
|
labels opts.ListOpts
|
|
|
|
containerLabels opts.ListOpts
|
|
|
|
image string
|
|
|
|
args []string
|
|
|
|
env opts.ListOpts
|
|
|
|
workdir string
|
|
|
|
user string
|
2016-08-01 21:34:51 -04:00
|
|
|
groups []string
|
2016-07-25 05:38:01 -04:00
|
|
|
mounts MountOpt
|
2016-06-13 22:56:23 -04:00
|
|
|
|
|
|
|
resources resourceOptions
|
|
|
|
stopGrace DurationOpt
|
|
|
|
|
|
|
|
replicas Uint64Opt
|
|
|
|
mode string
|
|
|
|
|
|
|
|
restartPolicy restartPolicyOptions
|
|
|
|
constraints []string
|
|
|
|
update updateOptions
|
|
|
|
networks []string
|
|
|
|
endpoint endpointOptions
|
2016-06-29 20:08:00 -04:00
|
|
|
|
|
|
|
registryAuth bool
|
2016-07-08 21:44:18 -04:00
|
|
|
|
|
|
|
logDriver logDriverOptions
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newServiceOptions() *serviceOptions {
|
|
|
|
return &serviceOptions{
|
2016-07-25 05:38:01 -04:00
|
|
|
labels: opts.NewListOpts(runconfigopts.ValidateEnv),
|
|
|
|
containerLabels: opts.NewListOpts(runconfigopts.ValidateEnv),
|
|
|
|
env: opts.NewListOpts(runconfigopts.ValidateEnv),
|
2016-06-13 22:56:23 -04:00
|
|
|
endpoint: endpointOptions{
|
|
|
|
ports: opts.NewListOpts(ValidatePort),
|
|
|
|
},
|
2016-07-08 21:44:18 -04:00
|
|
|
logDriver: newLogDriverOptions(),
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) {
|
|
|
|
var service swarm.ServiceSpec
|
|
|
|
|
|
|
|
service = swarm.ServiceSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: opts.name,
|
|
|
|
Labels: runconfigopts.ConvertKVStringsToMap(opts.labels.GetAll()),
|
|
|
|
},
|
|
|
|
TaskTemplate: swarm.TaskSpec{
|
|
|
|
ContainerSpec: swarm.ContainerSpec{
|
|
|
|
Image: opts.image,
|
|
|
|
Args: opts.args,
|
|
|
|
Env: opts.env.GetAll(),
|
2016-07-25 05:38:01 -04:00
|
|
|
Labels: runconfigopts.ConvertKVStringsToMap(opts.containerLabels.GetAll()),
|
2016-06-13 22:56:23 -04:00
|
|
|
Dir: opts.workdir,
|
|
|
|
User: opts.user,
|
2016-08-01 21:34:51 -04:00
|
|
|
Groups: opts.groups,
|
2016-06-13 22:56:23 -04:00
|
|
|
Mounts: opts.mounts.Value(),
|
|
|
|
StopGracePeriod: opts.stopGrace.Value(),
|
|
|
|
},
|
2016-08-23 19:50:15 -04:00
|
|
|
Networks: convertNetworks(opts.networks),
|
2016-06-13 22:56:23 -04:00
|
|
|
Resources: opts.resources.ToResourceRequirements(),
|
|
|
|
RestartPolicy: opts.restartPolicy.ToRestartPolicy(),
|
|
|
|
Placement: &swarm.Placement{
|
|
|
|
Constraints: opts.constraints,
|
|
|
|
},
|
2016-07-08 21:44:18 -04:00
|
|
|
LogDriver: opts.logDriver.toLogDriver(),
|
2016-06-13 22:56:23 -04:00
|
|
|
},
|
2016-08-23 19:50:15 -04:00
|
|
|
Networks: convertNetworks(opts.networks),
|
|
|
|
Mode: swarm.ServiceMode{},
|
2016-06-13 22:56:23 -04:00
|
|
|
UpdateConfig: &swarm.UpdateConfig{
|
2016-07-22 14:35:51 -04:00
|
|
|
Parallelism: opts.update.parallelism,
|
|
|
|
Delay: opts.update.delay,
|
|
|
|
FailureAction: opts.update.onFailure,
|
2016-06-13 22:56:23 -04:00
|
|
|
},
|
|
|
|
EndpointSpec: opts.endpoint.ToEndpointSpec(),
|
|
|
|
}
|
|
|
|
|
|
|
|
switch opts.mode {
|
|
|
|
case "global":
|
|
|
|
if opts.replicas.Value() != nil {
|
|
|
|
return service, fmt.Errorf("replicas can only be used with replicated mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
service.Mode.Global = &swarm.GlobalService{}
|
|
|
|
case "replicated":
|
|
|
|
service.Mode.Replicated = &swarm.ReplicatedService{
|
|
|
|
Replicas: opts.replicas.Value(),
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return service, fmt.Errorf("Unknown mode: %s", opts.mode)
|
|
|
|
}
|
|
|
|
return service, nil
|
|
|
|
}
|
|
|
|
|
2016-06-29 20:08:00 -04:00
|
|
|
// addServiceFlags adds all flags that are common to both `create` and `update`.
|
2016-06-13 22:56:23 -04:00
|
|
|
// Any flags that are not common are added separately in the individual command
|
|
|
|
func addServiceFlags(cmd *cobra.Command, opts *serviceOptions) {
|
|
|
|
flags := cmd.Flags()
|
2016-06-14 12:33:50 -04:00
|
|
|
flags.StringVar(&opts.name, flagName, "", "Service name")
|
2016-06-13 22:56:23 -04:00
|
|
|
|
2016-08-01 14:45:28 -04:00
|
|
|
flags.StringVarP(&opts.workdir, flagWorkdir, "w", "", "Working directory inside the container")
|
2016-08-02 09:58:02 -04:00
|
|
|
flags.StringVarP(&opts.user, flagUser, "u", "", "Username or UID (format: <name|uid>[:<group|gid>])")
|
2016-08-01 21:34:51 -04:00
|
|
|
flags.StringSliceVar(&opts.groups, flagGroupAdd, []string{}, "Add additional user groups to the container")
|
2016-06-13 22:56:23 -04:00
|
|
|
|
2016-06-14 18:37:27 -04:00
|
|
|
flags.Var(&opts.resources.limitCPU, flagLimitCPU, "Limit CPUs")
|
|
|
|
flags.Var(&opts.resources.limitMemBytes, flagLimitMemory, "Limit Memory")
|
|
|
|
flags.Var(&opts.resources.resCPU, flagReserveCPU, "Reserve CPUs")
|
|
|
|
flags.Var(&opts.resources.resMemBytes, flagReserveMemory, "Reserve Memory")
|
2016-06-17 11:01:46 -04:00
|
|
|
flags.Var(&opts.stopGrace, flagStopGracePeriod, "Time to wait before force killing a container")
|
2016-06-13 22:56:23 -04:00
|
|
|
|
2016-06-14 12:33:50 -04:00
|
|
|
flags.Var(&opts.replicas, flagReplicas, "Number of tasks")
|
2016-06-13 22:56:23 -04:00
|
|
|
|
2016-07-07 05:32:19 -04:00
|
|
|
flags.StringVar(&opts.restartPolicy.condition, flagRestartCondition, "", "Restart when condition is met (none, on-failure, or any)")
|
2016-06-14 18:37:27 -04:00
|
|
|
flags.Var(&opts.restartPolicy.delay, flagRestartDelay, "Delay between restart attempts")
|
|
|
|
flags.Var(&opts.restartPolicy.maxAttempts, flagRestartMaxAttempts, "Maximum number of restarts before giving up")
|
2016-06-23 10:26:43 -04:00
|
|
|
flags.Var(&opts.restartPolicy.window, flagRestartWindow, "Window used to evaluate the restart policy")
|
2016-06-13 22:56:23 -04:00
|
|
|
|
2016-07-22 18:06:52 -04:00
|
|
|
flags.Uint64Var(&opts.update.parallelism, flagUpdateParallelism, 1, "Maximum number of tasks updated simultaneously (0 to update all at once)")
|
2016-06-14 12:33:50 -04:00
|
|
|
flags.DurationVar(&opts.update.delay, flagUpdateDelay, time.Duration(0), "Delay between updates")
|
2016-07-22 14:35:51 -04:00
|
|
|
flags.StringVar(&opts.update.onFailure, flagUpdateFailureAction, "pause", "Action on update failure (pause|continue)")
|
2016-06-14 12:33:50 -04:00
|
|
|
|
2016-07-05 08:18:49 -04:00
|
|
|
flags.StringVar(&opts.endpoint.mode, flagEndpointMode, "", "Endpoint mode (vip or dnsrr)")
|
2016-06-29 20:08:00 -04:00
|
|
|
|
2016-07-20 14:15:08 -04:00
|
|
|
flags.BoolVar(&opts.registryAuth, flagRegistryAuth, false, "Send registry authentication details to swarm agents")
|
2016-07-08 21:44:18 -04:00
|
|
|
|
|
|
|
flags.StringVar(&opts.logDriver.name, flagLogDriver, "", "Logging driver for service")
|
|
|
|
flags.Var(&opts.logDriver.opts, flagLogOpt, "Logging driver options")
|
2016-06-14 12:33:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2016-07-25 05:38:01 -04:00
|
|
|
flagConstraint = "constraint"
|
|
|
|
flagConstraintRemove = "constraint-rm"
|
|
|
|
flagConstraintAdd = "constraint-add"
|
|
|
|
flagContainerLabel = "container-label"
|
|
|
|
flagContainerLabelRemove = "container-label-rm"
|
|
|
|
flagContainerLabelAdd = "container-label-add"
|
|
|
|
flagEndpointMode = "endpoint-mode"
|
|
|
|
flagEnv = "env"
|
|
|
|
flagEnvRemove = "env-rm"
|
|
|
|
flagEnvAdd = "env-add"
|
2016-08-01 21:34:51 -04:00
|
|
|
flagGroupAdd = "group-add"
|
|
|
|
flagGroupRemove = "group-rm"
|
2016-07-25 05:38:01 -04:00
|
|
|
flagLabel = "label"
|
|
|
|
flagLabelRemove = "label-rm"
|
|
|
|
flagLabelAdd = "label-add"
|
|
|
|
flagLimitCPU = "limit-cpu"
|
|
|
|
flagLimitMemory = "limit-memory"
|
|
|
|
flagMode = "mode"
|
|
|
|
flagMount = "mount"
|
|
|
|
flagMountRemove = "mount-rm"
|
|
|
|
flagMountAdd = "mount-add"
|
|
|
|
flagName = "name"
|
|
|
|
flagNetwork = "network"
|
|
|
|
flagPublish = "publish"
|
|
|
|
flagPublishRemove = "publish-rm"
|
|
|
|
flagPublishAdd = "publish-add"
|
|
|
|
flagReplicas = "replicas"
|
|
|
|
flagReserveCPU = "reserve-cpu"
|
|
|
|
flagReserveMemory = "reserve-memory"
|
|
|
|
flagRestartCondition = "restart-condition"
|
|
|
|
flagRestartDelay = "restart-delay"
|
|
|
|
flagRestartMaxAttempts = "restart-max-attempts"
|
|
|
|
flagRestartWindow = "restart-window"
|
|
|
|
flagStopGracePeriod = "stop-grace-period"
|
|
|
|
flagUpdateDelay = "update-delay"
|
|
|
|
flagUpdateFailureAction = "update-failure-action"
|
|
|
|
flagUpdateParallelism = "update-parallelism"
|
|
|
|
flagUser = "user"
|
2016-08-01 14:45:28 -04:00
|
|
|
flagWorkdir = "workdir"
|
2016-07-25 05:38:01 -04:00
|
|
|
flagRegistryAuth = "with-registry-auth"
|
|
|
|
flagLogDriver = "log-driver"
|
|
|
|
flagLogOpt = "log-opt"
|
2016-06-14 12:33:50 -04:00
|
|
|
)
|