opts.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package swarm
  2. import (
  3. "fmt"
  4. "net"
  5. "strconv"
  6. "strings"
  7. "github.com/docker/engine-api/types/swarm"
  8. )
  9. const (
  10. defaultListenAddr = "0.0.0.0"
  11. defaultListenPort uint16 = 2377
  12. // WORKER constant for worker name
  13. WORKER = "WORKER"
  14. // MANAGER constant for manager name
  15. MANAGER = "MANAGER"
  16. flagAutoAccept = "auto-accept"
  17. flagCertExpiry = "cert-expiry"
  18. flagDispatcherHeartbeat = "dispatcher-heartbeat"
  19. flagListenAddr = "listen-addr"
  20. flagSecret = "secret"
  21. flagTaskHistoryLimit = "task-history-limit"
  22. )
  23. var (
  24. defaultPolicies = []swarm.Policy{
  25. {Role: WORKER, Autoaccept: true},
  26. {Role: MANAGER, Autoaccept: false},
  27. }
  28. )
  29. // NodeAddrOption is a pflag.Value for listen and remote addresses
  30. type NodeAddrOption struct {
  31. addr string
  32. port uint16
  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. if !strings.Contains(value, ":") {
  41. a.addr = value
  42. return nil
  43. }
  44. host, port, err := net.SplitHostPort(value)
  45. if err != nil {
  46. return fmt.Errorf("Invalid url, %v", err)
  47. }
  48. portInt, err := strconv.ParseUint(port, 10, 16)
  49. if err != nil {
  50. return fmt.Errorf("invalid url, %v", err)
  51. }
  52. a.port = uint16(portInt)
  53. if host != "" {
  54. a.addr = host
  55. }
  56. return nil
  57. }
  58. // Type returns the type of this flag
  59. func (a *NodeAddrOption) Type() string {
  60. return "node-addr"
  61. }
  62. // Value returns the value of this option as addr:port
  63. func (a *NodeAddrOption) Value() string {
  64. return net.JoinHostPort(a.addr, strconv.Itoa(int(a.port)))
  65. }
  66. // NewNodeAddrOption returns a new node address option
  67. func NewNodeAddrOption(host string, port uint16) NodeAddrOption {
  68. return NodeAddrOption{addr: host, port: port}
  69. }
  70. // NewListenAddrOption returns a NodeAddrOption with default values
  71. func NewListenAddrOption() NodeAddrOption {
  72. return NewNodeAddrOption(defaultListenAddr, defaultListenPort)
  73. }
  74. // AutoAcceptOption is a value type for auto-accept policy
  75. type AutoAcceptOption struct {
  76. values map[string]bool
  77. }
  78. // String prints a string representation of this option
  79. func (o *AutoAcceptOption) String() string {
  80. keys := []string{}
  81. for key, value := range o.values {
  82. keys = append(keys, fmt.Sprintf("%s=%v", strings.ToLower(key), value))
  83. }
  84. return strings.Join(keys, ", ")
  85. }
  86. // Set sets a new value on this option
  87. func (o *AutoAcceptOption) Set(value string) error {
  88. value = strings.ToUpper(value)
  89. switch value {
  90. case "", "NONE":
  91. if accept, ok := o.values[WORKER]; ok && accept {
  92. return fmt.Errorf("value NONE is incompatible with %s", WORKER)
  93. }
  94. if accept, ok := o.values[MANAGER]; ok && accept {
  95. return fmt.Errorf("value NONE is incompatible with %s", MANAGER)
  96. }
  97. o.values[WORKER] = false
  98. o.values[MANAGER] = false
  99. case WORKER, MANAGER:
  100. if accept, ok := o.values[value]; ok && !accept {
  101. return fmt.Errorf("value NONE is incompatible with %s", value)
  102. }
  103. o.values[value] = true
  104. default:
  105. return fmt.Errorf("must be one of %s, %s, NONE", WORKER, MANAGER)
  106. }
  107. return nil
  108. }
  109. // Type returns the type of this option
  110. func (o *AutoAcceptOption) Type() string {
  111. return "auto-accept"
  112. }
  113. // Policies returns a representation of this option for the api
  114. func (o *AutoAcceptOption) Policies(secret *string) []swarm.Policy {
  115. policies := []swarm.Policy{}
  116. for _, p := range defaultPolicies {
  117. if len(o.values) != 0 {
  118. p.Autoaccept = o.values[string(p.Role)]
  119. }
  120. p.Secret = secret
  121. policies = append(policies, p)
  122. }
  123. return policies
  124. }
  125. // NewAutoAcceptOption returns a new auto-accept option
  126. func NewAutoAcceptOption() AutoAcceptOption {
  127. return AutoAcceptOption{values: make(map[string]bool)}
  128. }