github.com/kobeld/docker@v1.12.0-rc1/api/client/swarm/opts.go (about) 1 package swarm 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/docker/engine-api/types/swarm" 8 ) 9 10 const ( 11 defaultListenAddr = "0.0.0.0:2377" 12 // WORKER constant for worker name 13 WORKER = "WORKER" 14 // MANAGER constant for manager name 15 MANAGER = "MANAGER" 16 ) 17 18 var ( 19 defaultPolicies = []swarm.Policy{ 20 {Role: WORKER, Autoaccept: true}, 21 {Role: MANAGER, Autoaccept: false}, 22 } 23 ) 24 25 // NodeAddrOption is a pflag.Value for listen and remote addresses 26 type NodeAddrOption struct { 27 addr string 28 } 29 30 // String prints the representation of this flag 31 func (a *NodeAddrOption) String() string { 32 return a.addr 33 } 34 35 // Set the value for this flag 36 func (a *NodeAddrOption) Set(value string) error { 37 if !strings.Contains(value, ":") { 38 return fmt.Errorf("Invalud url, a host and port are required") 39 } 40 41 parts := strings.Split(value, ":") 42 if len(parts) != 2 { 43 return fmt.Errorf("Invalud url, too many colons") 44 } 45 46 a.addr = value 47 return nil 48 } 49 50 // Type returns the type of this flag 51 func (a *NodeAddrOption) Type() string { 52 return "node-addr" 53 } 54 55 // NewNodeAddrOption returns a new node address option 56 func NewNodeAddrOption() NodeAddrOption { 57 return NodeAddrOption{addr: defaultListenAddr} 58 } 59 60 // AutoAcceptOption is a value type for auto-accept policy 61 type AutoAcceptOption struct { 62 values map[string]bool 63 } 64 65 // String prints a string representation of this option 66 func (o *AutoAcceptOption) String() string { 67 keys := []string{} 68 for key := range o.values { 69 keys = append(keys, key) 70 } 71 return strings.Join(keys, " ") 72 } 73 74 // Set sets a new value on this option 75 func (o *AutoAcceptOption) Set(value string) error { 76 value = strings.ToUpper(value) 77 switch value { 78 case "", "NONE": 79 if accept, ok := o.values[WORKER]; ok && accept { 80 return fmt.Errorf("value NONE is incompatible with %s", WORKER) 81 } 82 if accept, ok := o.values[MANAGER]; ok && accept { 83 return fmt.Errorf("value NONE is incompatible with %s", MANAGER) 84 } 85 o.values[WORKER] = false 86 o.values[MANAGER] = false 87 case WORKER, MANAGER: 88 if accept, ok := o.values[value]; ok && !accept { 89 return fmt.Errorf("value NONE is incompatible with %s", value) 90 } 91 o.values[value] = true 92 default: 93 return fmt.Errorf("must be one of %s, %s, NONE", WORKER, MANAGER) 94 } 95 96 return nil 97 } 98 99 // Type returns the type of this option 100 func (o *AutoAcceptOption) Type() string { 101 return "auto-accept" 102 } 103 104 // Policies returns a representation of this option for the api 105 func (o *AutoAcceptOption) Policies(secret string) []swarm.Policy { 106 policies := []swarm.Policy{} 107 for _, p := range defaultPolicies { 108 if len(o.values) != 0 { 109 p.Autoaccept = o.values[string(p.Role)] 110 } 111 p.Secret = secret 112 policies = append(policies, p) 113 } 114 return policies 115 } 116 117 // NewAutoAcceptOption returns a new auto-accept option 118 func NewAutoAcceptOption() AutoAcceptOption { 119 return AutoAcceptOption{values: make(map[string]bool)} 120 }