opts.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. // WORKER constant for worker name
  15. WORKER = "WORKER"
  16. // MANAGER constant for manager name
  17. MANAGER = "MANAGER"
  18. flagAutoAccept = "auto-accept"
  19. flagCertExpiry = "cert-expiry"
  20. flagDispatcherHeartbeat = "dispatcher-heartbeat"
  21. flagListenAddr = "listen-addr"
  22. flagSecret = "secret"
  23. flagTaskHistoryLimit = "task-history-limit"
  24. flagExternalCA = "external-ca"
  25. )
  26. var (
  27. defaultPolicies = []swarm.Policy{
  28. {Role: WORKER, Autoaccept: true},
  29. {Role: MANAGER, Autoaccept: false},
  30. }
  31. )
  32. type swarmOptions struct {
  33. autoAccept AutoAcceptOption
  34. secret string
  35. taskHistoryLimit int64
  36. dispatcherHeartbeat time.Duration
  37. nodeCertExpiry time.Duration
  38. externalCA ExternalCAOption
  39. }
  40. // NodeAddrOption is a pflag.Value for listen and remote addresses
  41. type NodeAddrOption struct {
  42. addr string
  43. }
  44. // String prints the representation of this flag
  45. func (a *NodeAddrOption) String() string {
  46. return a.Value()
  47. }
  48. // Set the value for this flag
  49. func (a *NodeAddrOption) Set(value string) error {
  50. addr, err := opts.ParseTCPAddr(value, a.addr)
  51. if err != nil {
  52. return err
  53. }
  54. a.addr = addr
  55. return nil
  56. }
  57. // Type returns the type of this flag
  58. func (a *NodeAddrOption) Type() string {
  59. return "node-addr"
  60. }
  61. // Value returns the value of this option as addr:port
  62. func (a *NodeAddrOption) Value() string {
  63. return strings.TrimPrefix(a.addr, "tcp://")
  64. }
  65. // NewNodeAddrOption returns a new node address option
  66. func NewNodeAddrOption(addr string) NodeAddrOption {
  67. return NodeAddrOption{addr}
  68. }
  69. // NewListenAddrOption returns a NodeAddrOption with default values
  70. func NewListenAddrOption() NodeAddrOption {
  71. return NewNodeAddrOption(defaultListenAddr)
  72. }
  73. // AutoAcceptOption is a value type for auto-accept policy
  74. type AutoAcceptOption struct {
  75. values map[string]bool
  76. }
  77. // String prints a string representation of this option
  78. func (o *AutoAcceptOption) String() string {
  79. keys := []string{}
  80. for key, value := range o.values {
  81. keys = append(keys, fmt.Sprintf("%s=%v", strings.ToLower(key), value))
  82. }
  83. return strings.Join(keys, ", ")
  84. }
  85. // Set sets a new value on this option
  86. func (o *AutoAcceptOption) Set(value string) error {
  87. value = strings.ToUpper(value)
  88. switch value {
  89. case "", "NONE":
  90. if accept, ok := o.values[WORKER]; ok && accept {
  91. return fmt.Errorf("value NONE is incompatible with %s", WORKER)
  92. }
  93. if accept, ok := o.values[MANAGER]; ok && accept {
  94. return fmt.Errorf("value NONE is incompatible with %s", MANAGER)
  95. }
  96. o.values[WORKER] = false
  97. o.values[MANAGER] = false
  98. case WORKER, MANAGER:
  99. if accept, ok := o.values[value]; ok && !accept {
  100. return fmt.Errorf("value NONE is incompatible with %s", value)
  101. }
  102. o.values[value] = true
  103. default:
  104. return fmt.Errorf("must be one of %s, %s, NONE", WORKER, MANAGER)
  105. }
  106. return nil
  107. }
  108. // Type returns the type of this option
  109. func (o *AutoAcceptOption) Type() string {
  110. return "auto-accept"
  111. }
  112. // Policies returns a representation of this option for the api
  113. func (o *AutoAcceptOption) Policies(secret *string) []swarm.Policy {
  114. policies := []swarm.Policy{}
  115. for _, p := range defaultPolicies {
  116. if len(o.values) != 0 {
  117. p.Autoaccept = o.values[string(p.Role)]
  118. }
  119. p.Secret = secret
  120. policies = append(policies, p)
  121. }
  122. return policies
  123. }
  124. // NewAutoAcceptOption returns a new auto-accept option
  125. func NewAutoAcceptOption() AutoAcceptOption {
  126. return AutoAcceptOption{values: make(map[string]bool)}
  127. }
  128. // ExternalCAOption is a Value type for parsing external CA specifications.
  129. type ExternalCAOption struct {
  130. values []*swarm.ExternalCA
  131. }
  132. // Set parses an external CA option.
  133. func (m *ExternalCAOption) Set(value string) error {
  134. parsed, err := parseExternalCA(value)
  135. if err != nil {
  136. return err
  137. }
  138. m.values = append(m.values, parsed)
  139. return nil
  140. }
  141. // Type returns the type of this option.
  142. func (m *ExternalCAOption) Type() string {
  143. return "external-ca"
  144. }
  145. // String returns a string repr of this option.
  146. func (m *ExternalCAOption) String() string {
  147. externalCAs := []string{}
  148. for _, externalCA := range m.values {
  149. repr := fmt.Sprintf("%s: %s", externalCA.Protocol, externalCA.URL)
  150. externalCAs = append(externalCAs, repr)
  151. }
  152. return strings.Join(externalCAs, ", ")
  153. }
  154. // Value returns the external CAs
  155. func (m *ExternalCAOption) Value() []*swarm.ExternalCA {
  156. return m.values
  157. }
  158. // parseExternalCA parses an external CA specification from the command line,
  159. // such as protocol=cfssl,url=https://example.com.
  160. func parseExternalCA(caSpec string) (*swarm.ExternalCA, error) {
  161. csvReader := csv.NewReader(strings.NewReader(caSpec))
  162. fields, err := csvReader.Read()
  163. if err != nil {
  164. return nil, err
  165. }
  166. externalCA := swarm.ExternalCA{
  167. Options: make(map[string]string),
  168. }
  169. var (
  170. hasProtocol bool
  171. hasURL bool
  172. )
  173. for _, field := range fields {
  174. parts := strings.SplitN(field, "=", 2)
  175. if len(parts) != 2 {
  176. return nil, fmt.Errorf("invalid field '%s' must be a key=value pair", field)
  177. }
  178. key, value := parts[0], parts[1]
  179. switch strings.ToLower(key) {
  180. case "protocol":
  181. hasProtocol = true
  182. if strings.ToLower(value) == string(swarm.ExternalCAProtocolCFSSL) {
  183. externalCA.Protocol = swarm.ExternalCAProtocolCFSSL
  184. } else {
  185. return nil, fmt.Errorf("unrecognized external CA protocol %s", value)
  186. }
  187. case "url":
  188. hasURL = true
  189. externalCA.URL = value
  190. default:
  191. externalCA.Options[key] = value
  192. }
  193. }
  194. if !hasProtocol {
  195. return nil, errors.New("the external-ca option needs a protocol= parameter")
  196. }
  197. if !hasURL {
  198. return nil, errors.New("the external-ca option needs a url= parameter")
  199. }
  200. return &externalCA, nil
  201. }
  202. func addSwarmFlags(flags *pflag.FlagSet, opts *swarmOptions) {
  203. flags.Var(&opts.autoAccept, flagAutoAccept, "Auto acceptance policy (worker, manager or none)")
  204. flags.StringVar(&opts.secret, flagSecret, "", "Set secret value needed to join a cluster")
  205. flags.Int64Var(&opts.taskHistoryLimit, flagTaskHistoryLimit, 10, "Task history retention limit")
  206. flags.DurationVar(&opts.dispatcherHeartbeat, flagDispatcherHeartbeat, time.Duration(5*time.Second), "Dispatcher heartbeat period")
  207. flags.DurationVar(&opts.nodeCertExpiry, flagCertExpiry, time.Duration(90*24*time.Hour), "Validity period for node certificates")
  208. flags.Var(&opts.externalCA, flagExternalCA, "Specifications of one or more certificate signing endpoints")
  209. }
  210. func (opts *swarmOptions) ToSpec() swarm.Spec {
  211. spec := swarm.Spec{}
  212. if opts.secret != "" {
  213. spec.AcceptancePolicy.Policies = opts.autoAccept.Policies(&opts.secret)
  214. } else {
  215. spec.AcceptancePolicy.Policies = opts.autoAccept.Policies(nil)
  216. }
  217. spec.Orchestration.TaskHistoryRetentionLimit = opts.taskHistoryLimit
  218. spec.Dispatcher.HeartbeatPeriod = uint64(opts.dispatcherHeartbeat.Nanoseconds())
  219. spec.CAConfig.NodeCertExpiry = opts.nodeCertExpiry
  220. spec.CAConfig.ExternalCAs = opts.externalCA.Value()
  221. return spec
  222. }