opts.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package swarm
  2. import (
  3. "encoding/csv"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/docker/docker/opts"
  9. "github.com/docker/engine-api/types/swarm"
  10. "github.com/spf13/pflag"
  11. )
  12. const (
  13. defaultListenAddr = "0.0.0.0:2377"
  14. flagCertExpiry = "cert-expiry"
  15. flagDispatcherHeartbeat = "dispatcher-heartbeat"
  16. flagListenAddr = "listen-addr"
  17. flagAdvertiseAddr = "advertise-addr"
  18. flagToken = "token"
  19. flagTaskHistoryLimit = "task-history-limit"
  20. flagExternalCA = "external-ca"
  21. )
  22. type swarmOptions struct {
  23. taskHistoryLimit int64
  24. dispatcherHeartbeat time.Duration
  25. nodeCertExpiry time.Duration
  26. externalCA ExternalCAOption
  27. }
  28. // NodeAddrOption is a pflag.Value for listen and remote addresses
  29. type NodeAddrOption struct {
  30. addr string
  31. }
  32. // String prints the representation of this flag
  33. func (a *NodeAddrOption) String() string {
  34. return a.Value()
  35. }
  36. // Set the value for this flag
  37. func (a *NodeAddrOption) Set(value string) error {
  38. addr, err := opts.ParseTCPAddr(value, a.addr)
  39. if err != nil {
  40. return err
  41. }
  42. a.addr = addr
  43. return nil
  44. }
  45. // Type returns the type of this flag
  46. func (a *NodeAddrOption) Type() string {
  47. return "node-addr"
  48. }
  49. // Value returns the value of this option as addr:port
  50. func (a *NodeAddrOption) Value() string {
  51. return strings.TrimPrefix(a.addr, "tcp://")
  52. }
  53. // NewNodeAddrOption returns a new node address option
  54. func NewNodeAddrOption(addr string) NodeAddrOption {
  55. return NodeAddrOption{addr}
  56. }
  57. // NewListenAddrOption returns a NodeAddrOption with default values
  58. func NewListenAddrOption() NodeAddrOption {
  59. return NewNodeAddrOption(defaultListenAddr)
  60. }
  61. // ExternalCAOption is a Value type for parsing external CA specifications.
  62. type ExternalCAOption struct {
  63. values []*swarm.ExternalCA
  64. }
  65. // Set parses an external CA option.
  66. func (m *ExternalCAOption) Set(value string) error {
  67. parsed, err := parseExternalCA(value)
  68. if err != nil {
  69. return err
  70. }
  71. m.values = append(m.values, parsed)
  72. return nil
  73. }
  74. // Type returns the type of this option.
  75. func (m *ExternalCAOption) Type() string {
  76. return "external-ca"
  77. }
  78. // String returns a string repr of this option.
  79. func (m *ExternalCAOption) String() string {
  80. externalCAs := []string{}
  81. for _, externalCA := range m.values {
  82. repr := fmt.Sprintf("%s: %s", externalCA.Protocol, externalCA.URL)
  83. externalCAs = append(externalCAs, repr)
  84. }
  85. return strings.Join(externalCAs, ", ")
  86. }
  87. // Value returns the external CAs
  88. func (m *ExternalCAOption) Value() []*swarm.ExternalCA {
  89. return m.values
  90. }
  91. // parseExternalCA parses an external CA specification from the command line,
  92. // such as protocol=cfssl,url=https://example.com.
  93. func parseExternalCA(caSpec string) (*swarm.ExternalCA, error) {
  94. csvReader := csv.NewReader(strings.NewReader(caSpec))
  95. fields, err := csvReader.Read()
  96. if err != nil {
  97. return nil, err
  98. }
  99. externalCA := swarm.ExternalCA{
  100. Options: make(map[string]string),
  101. }
  102. var (
  103. hasProtocol bool
  104. hasURL bool
  105. )
  106. for _, field := range fields {
  107. parts := strings.SplitN(field, "=", 2)
  108. if len(parts) != 2 {
  109. return nil, fmt.Errorf("invalid field '%s' must be a key=value pair", field)
  110. }
  111. key, value := parts[0], parts[1]
  112. switch strings.ToLower(key) {
  113. case "protocol":
  114. hasProtocol = true
  115. if strings.ToLower(value) == string(swarm.ExternalCAProtocolCFSSL) {
  116. externalCA.Protocol = swarm.ExternalCAProtocolCFSSL
  117. } else {
  118. return nil, fmt.Errorf("unrecognized external CA protocol %s", value)
  119. }
  120. case "url":
  121. hasURL = true
  122. externalCA.URL = value
  123. default:
  124. externalCA.Options[key] = value
  125. }
  126. }
  127. if !hasProtocol {
  128. return nil, errors.New("the external-ca option needs a protocol= parameter")
  129. }
  130. if !hasURL {
  131. return nil, errors.New("the external-ca option needs a url= parameter")
  132. }
  133. return &externalCA, nil
  134. }
  135. func addSwarmFlags(flags *pflag.FlagSet, opts *swarmOptions) {
  136. flags.Int64Var(&opts.taskHistoryLimit, flagTaskHistoryLimit, 5, "Task history retention limit")
  137. flags.DurationVar(&opts.dispatcherHeartbeat, flagDispatcherHeartbeat, time.Duration(5*time.Second), "Dispatcher heartbeat period")
  138. flags.DurationVar(&opts.nodeCertExpiry, flagCertExpiry, time.Duration(90*24*time.Hour), "Validity period for node certificates")
  139. flags.Var(&opts.externalCA, flagExternalCA, "Specifications of one or more certificate signing endpoints")
  140. }
  141. func (opts *swarmOptions) ToSpec() swarm.Spec {
  142. spec := swarm.Spec{}
  143. spec.Orchestration.TaskHistoryRetentionLimit = opts.taskHistoryLimit
  144. spec.Dispatcher.HeartbeatPeriod = uint64(opts.dispatcherHeartbeat.Nanoseconds())
  145. spec.CAConfig.NodeCertExpiry = opts.nodeCertExpiry
  146. spec.CAConfig.ExternalCAs = opts.externalCA.Value()
  147. return spec
  148. }