opts.go 4.7 KB

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