2016-06-13 22:56:23 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
2016-06-30 17:39:39 -04:00
|
|
|
"encoding/csv"
|
|
|
|
"errors"
|
2016-06-13 22:56:23 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-06-21 17:27:04 -04:00
|
|
|
"time"
|
2016-06-13 22:56:23 -04:00
|
|
|
|
2016-06-21 17:27:04 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/engine-api/types/swarm"
|
2016-06-21 17:27:04 -04:00
|
|
|
"github.com/spf13/pflag"
|
2016-06-13 22:56:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-06-21 17:27:04 -04:00
|
|
|
defaultListenAddr = "0.0.0.0:2377"
|
2016-06-29 17:01:25 -04:00
|
|
|
|
2016-06-17 15:42:16 -04:00
|
|
|
flagCertExpiry = "cert-expiry"
|
|
|
|
flagDispatcherHeartbeat = "dispatcher-heartbeat"
|
|
|
|
flagListenAddr = "listen-addr"
|
2016-06-30 21:07:35 -04:00
|
|
|
flagAdvertiseAddr = "advertise-addr"
|
2016-07-27 10:14:29 -04:00
|
|
|
flagQuiet = "quiet"
|
|
|
|
flagRotate = "rotate"
|
2016-07-20 14:15:08 -04:00
|
|
|
flagToken = "token"
|
2016-06-17 15:42:16 -04:00
|
|
|
flagTaskHistoryLimit = "task-history-limit"
|
2016-06-30 17:39:39 -04:00
|
|
|
flagExternalCA = "external-ca"
|
2016-06-13 22:56:23 -04:00
|
|
|
)
|
|
|
|
|
2016-06-21 17:27:04 -04:00
|
|
|
type swarmOptions struct {
|
|
|
|
taskHistoryLimit int64
|
|
|
|
dispatcherHeartbeat time.Duration
|
|
|
|
nodeCertExpiry time.Duration
|
2016-06-30 17:39:39 -04:00
|
|
|
externalCA ExternalCAOption
|
2016-06-21 17:27:04 -04:00
|
|
|
}
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
// NodeAddrOption is a pflag.Value for listen and remote addresses
|
|
|
|
type NodeAddrOption struct {
|
|
|
|
addr string
|
|
|
|
}
|
|
|
|
|
|
|
|
// String prints the representation of this flag
|
|
|
|
func (a *NodeAddrOption) String() string {
|
2016-06-17 15:42:16 -04:00
|
|
|
return a.Value()
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the value for this flag
|
|
|
|
func (a *NodeAddrOption) Set(value string) error {
|
2016-06-21 17:27:04 -04:00
|
|
|
addr, err := opts.ParseTCPAddr(value, a.addr)
|
2016-06-17 15:42:16 -04:00
|
|
|
if err != nil {
|
2016-06-21 17:27:04 -04:00
|
|
|
return err
|
2016-06-17 15:42:16 -04:00
|
|
|
}
|
2016-06-21 17:27:04 -04:00
|
|
|
a.addr = addr
|
2016-06-13 22:56:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of this flag
|
|
|
|
func (a *NodeAddrOption) Type() string {
|
|
|
|
return "node-addr"
|
|
|
|
}
|
|
|
|
|
2016-06-17 15:42:16 -04:00
|
|
|
// Value returns the value of this option as addr:port
|
|
|
|
func (a *NodeAddrOption) Value() string {
|
2016-06-21 17:27:04 -04:00
|
|
|
return strings.TrimPrefix(a.addr, "tcp://")
|
2016-06-17 15:42:16 -04:00
|
|
|
}
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
// NewNodeAddrOption returns a new node address option
|
2016-06-21 17:27:04 -04:00
|
|
|
func NewNodeAddrOption(addr string) NodeAddrOption {
|
|
|
|
return NodeAddrOption{addr}
|
2016-06-17 15:42:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewListenAddrOption returns a NodeAddrOption with default values
|
|
|
|
func NewListenAddrOption() NodeAddrOption {
|
2016-06-21 17:27:04 -04:00
|
|
|
return NewNodeAddrOption(defaultListenAddr)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
2016-06-30 17:39:39 -04:00
|
|
|
// ExternalCAOption is a Value type for parsing external CA specifications.
|
|
|
|
type ExternalCAOption struct {
|
|
|
|
values []*swarm.ExternalCA
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set parses an external CA option.
|
|
|
|
func (m *ExternalCAOption) Set(value string) error {
|
|
|
|
parsed, err := parseExternalCA(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.values = append(m.values, parsed)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of this option.
|
|
|
|
func (m *ExternalCAOption) Type() string {
|
|
|
|
return "external-ca"
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string repr of this option.
|
|
|
|
func (m *ExternalCAOption) String() string {
|
|
|
|
externalCAs := []string{}
|
|
|
|
for _, externalCA := range m.values {
|
|
|
|
repr := fmt.Sprintf("%s: %s", externalCA.Protocol, externalCA.URL)
|
|
|
|
externalCAs = append(externalCAs, repr)
|
|
|
|
}
|
|
|
|
return strings.Join(externalCAs, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the external CAs
|
|
|
|
func (m *ExternalCAOption) Value() []*swarm.ExternalCA {
|
|
|
|
return m.values
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseExternalCA parses an external CA specification from the command line,
|
|
|
|
// such as protocol=cfssl,url=https://example.com.
|
|
|
|
func parseExternalCA(caSpec string) (*swarm.ExternalCA, error) {
|
|
|
|
csvReader := csv.NewReader(strings.NewReader(caSpec))
|
|
|
|
fields, err := csvReader.Read()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
externalCA := swarm.ExternalCA{
|
|
|
|
Options: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
hasProtocol bool
|
|
|
|
hasURL bool
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, field := range fields {
|
|
|
|
parts := strings.SplitN(field, "=", 2)
|
|
|
|
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, fmt.Errorf("invalid field '%s' must be a key=value pair", field)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, value := parts[0], parts[1]
|
|
|
|
|
|
|
|
switch strings.ToLower(key) {
|
|
|
|
case "protocol":
|
|
|
|
hasProtocol = true
|
|
|
|
if strings.ToLower(value) == string(swarm.ExternalCAProtocolCFSSL) {
|
|
|
|
externalCA.Protocol = swarm.ExternalCAProtocolCFSSL
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("unrecognized external CA protocol %s", value)
|
|
|
|
}
|
|
|
|
case "url":
|
|
|
|
hasURL = true
|
|
|
|
externalCA.URL = value
|
|
|
|
default:
|
|
|
|
externalCA.Options[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasProtocol {
|
|
|
|
return nil, errors.New("the external-ca option needs a protocol= parameter")
|
|
|
|
}
|
|
|
|
if !hasURL {
|
|
|
|
return nil, errors.New("the external-ca option needs a url= parameter")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &externalCA, nil
|
|
|
|
}
|
|
|
|
|
2016-06-21 17:27:04 -04:00
|
|
|
func addSwarmFlags(flags *pflag.FlagSet, opts *swarmOptions) {
|
2016-07-22 21:09:54 -04:00
|
|
|
flags.Int64Var(&opts.taskHistoryLimit, flagTaskHistoryLimit, 5, "Task history retention limit")
|
2016-06-21 17:27:04 -04:00
|
|
|
flags.DurationVar(&opts.dispatcherHeartbeat, flagDispatcherHeartbeat, time.Duration(5*time.Second), "Dispatcher heartbeat period")
|
|
|
|
flags.DurationVar(&opts.nodeCertExpiry, flagCertExpiry, time.Duration(90*24*time.Hour), "Validity period for node certificates")
|
2016-06-30 17:39:39 -04:00
|
|
|
flags.Var(&opts.externalCA, flagExternalCA, "Specifications of one or more certificate signing endpoints")
|
2016-06-21 17:27:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *swarmOptions) ToSpec() swarm.Spec {
|
|
|
|
spec := swarm.Spec{}
|
|
|
|
spec.Orchestration.TaskHistoryRetentionLimit = opts.taskHistoryLimit
|
|
|
|
spec.Dispatcher.HeartbeatPeriod = uint64(opts.dispatcherHeartbeat.Nanoseconds())
|
|
|
|
spec.CAConfig.NodeCertExpiry = opts.nodeCertExpiry
|
2016-06-30 17:39:39 -04:00
|
|
|
spec.CAConfig.ExternalCAs = opts.externalCA.Value()
|
2016-06-21 17:27:04 -04:00
|
|
|
return spec
|
|
|
|
}
|