opts.go 4.6 KB

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