github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/nomad/structs/config/consul.go (about) 1 package config 2 3 import ( 4 "strings" 5 "time" 6 7 consul "github.com/hashicorp/consul/api" 8 "github.com/hashicorp/nomad/helper" 9 ) 10 11 // ConsulConfig contains the configuration information necessary to 12 // communicate with a Consul Agent in order to: 13 // 14 // - Register services and their checks with Consul 15 // 16 // - Bootstrap this Nomad Client with the list of Nomad Servers registered 17 // with Consul 18 // 19 // Both the Agent and the executor need to be able to import ConsulConfig. 20 type ConsulConfig struct { 21 // ServerServiceName is the name of the service that Nomad uses to register 22 // servers with Consul 23 ServerServiceName string `mapstructure:"server_service_name"` 24 25 // ClientServiceName is the name of the service that Nomad uses to register 26 // clients with Consul 27 ClientServiceName string `mapstructure:"client_service_name"` 28 29 // AutoAdvertise determines if this Nomad Agent will advertise its 30 // services via Consul. When true, Nomad Agent will register 31 // services with Consul. 32 AutoAdvertise *bool `mapstructure:"auto_advertise"` 33 34 // ChecksUseAdvertise specifies that Consul checks should use advertise 35 // address instead of bind address 36 ChecksUseAdvertise *bool `mapstructure:"checks_use_advertise"` 37 38 // Addr is the address of the local Consul agent 39 Addr string `mapstructure:"address"` 40 41 // Timeout is used by Consul HTTP Client 42 Timeout time.Duration `mapstructure:"timeout"` 43 44 // Token is used to provide a per-request ACL token. This options overrides 45 // the agent's default token 46 Token string `mapstructure:"token"` 47 48 // Auth is the information to use for http access to Consul agent 49 Auth string `mapstructure:"auth"` 50 51 // EnableSSL sets the transport scheme to talk to the Consul agent as https 52 EnableSSL *bool `mapstructure:"ssl"` 53 54 // VerifySSL enables or disables SSL verification when the transport scheme 55 // for the consul api client is https 56 VerifySSL *bool `mapstructure:"verify_ssl"` 57 58 // CAFile is the path to the ca certificate used for Consul communication 59 CAFile string `mapstructure:"ca_file"` 60 61 // CertFile is the path to the certificate for Consul communication 62 CertFile string `mapstructure:"cert_file"` 63 64 // KeyFile is the path to the private key for Consul communication 65 KeyFile string `mapstructure:"key_file"` 66 67 // ServerAutoJoin enables Nomad servers to find peers by querying Consul and 68 // joining them 69 ServerAutoJoin *bool `mapstructure:"server_auto_join"` 70 71 // ClientAutoJoin enables Nomad servers to find addresses of Nomad servers 72 // and register with them 73 ClientAutoJoin *bool `mapstructure:"client_auto_join"` 74 } 75 76 // DefaultConsulConfig() returns the canonical defaults for the Nomad 77 // `consul` configuration. 78 func DefaultConsulConfig() *ConsulConfig { 79 return &ConsulConfig{ 80 ServerServiceName: "nomad", 81 ClientServiceName: "nomad-client", 82 AutoAdvertise: helper.BoolToPtr(true), 83 ChecksUseAdvertise: helper.BoolToPtr(false), 84 EnableSSL: helper.BoolToPtr(false), 85 VerifySSL: helper.BoolToPtr(true), 86 ServerAutoJoin: helper.BoolToPtr(true), 87 ClientAutoJoin: helper.BoolToPtr(true), 88 Timeout: 5 * time.Second, 89 } 90 } 91 92 // Merge merges two Consul Configurations together. 93 func (a *ConsulConfig) Merge(b *ConsulConfig) *ConsulConfig { 94 result := a.Copy() 95 96 if b.ServerServiceName != "" { 97 result.ServerServiceName = b.ServerServiceName 98 } 99 if b.ClientServiceName != "" { 100 result.ClientServiceName = b.ClientServiceName 101 } 102 if b.AutoAdvertise != nil { 103 result.AutoAdvertise = helper.BoolToPtr(*b.AutoAdvertise) 104 } 105 if b.Addr != "" { 106 result.Addr = b.Addr 107 } 108 if b.Timeout != 0 { 109 result.Timeout = b.Timeout 110 } 111 if b.Token != "" { 112 result.Token = b.Token 113 } 114 if b.Auth != "" { 115 result.Auth = b.Auth 116 } 117 if b.EnableSSL != nil { 118 result.EnableSSL = helper.BoolToPtr(*b.EnableSSL) 119 } 120 if b.VerifySSL != nil { 121 result.VerifySSL = helper.BoolToPtr(*b.VerifySSL) 122 } 123 if b.CAFile != "" { 124 result.CAFile = b.CAFile 125 } 126 if b.CertFile != "" { 127 result.CertFile = b.CertFile 128 } 129 if b.KeyFile != "" { 130 result.KeyFile = b.KeyFile 131 } 132 if b.ServerAutoJoin != nil { 133 result.ServerAutoJoin = helper.BoolToPtr(*b.ServerAutoJoin) 134 } 135 if b.ClientAutoJoin != nil { 136 result.ClientAutoJoin = helper.BoolToPtr(*b.ClientAutoJoin) 137 } 138 if b.ChecksUseAdvertise != nil { 139 result.ChecksUseAdvertise = helper.BoolToPtr(*b.ChecksUseAdvertise) 140 } 141 return result 142 } 143 144 // ApiConfig returns a usable Consul config that can be passed directly to 145 // hashicorp/consul/api. NOTE: datacenter is not set 146 func (c *ConsulConfig) ApiConfig() (*consul.Config, error) { 147 config := consul.DefaultConfig() 148 if c.Addr != "" { 149 config.Address = c.Addr 150 } 151 if c.Token != "" { 152 config.Token = c.Token 153 } 154 if c.Timeout != 0 { 155 config.HttpClient.Timeout = c.Timeout 156 } 157 if c.Auth != "" { 158 var username, password string 159 if strings.Contains(c.Auth, ":") { 160 split := strings.SplitN(c.Auth, ":", 2) 161 username = split[0] 162 password = split[1] 163 } else { 164 username = c.Auth 165 } 166 167 config.HttpAuth = &consul.HttpBasicAuth{ 168 Username: username, 169 Password: password, 170 } 171 } 172 if c.EnableSSL != nil && *c.EnableSSL { 173 config.Scheme = "https" 174 config.TLSConfig = consul.TLSConfig{ 175 Address: config.Address, 176 CAFile: c.CAFile, 177 CertFile: c.CertFile, 178 KeyFile: c.KeyFile, 179 } 180 if c.VerifySSL != nil { 181 config.TLSConfig.InsecureSkipVerify = !*c.VerifySSL 182 } 183 } 184 185 return config, nil 186 } 187 188 // Copy returns a copy of this Consul config. 189 func (c *ConsulConfig) Copy() *ConsulConfig { 190 if c == nil { 191 return nil 192 } 193 194 nc := new(ConsulConfig) 195 *nc = *c 196 197 // Copy the bools 198 if nc.AutoAdvertise != nil { 199 nc.AutoAdvertise = helper.BoolToPtr(*nc.AutoAdvertise) 200 } 201 if nc.ChecksUseAdvertise != nil { 202 nc.ChecksUseAdvertise = helper.BoolToPtr(*nc.ChecksUseAdvertise) 203 } 204 if nc.EnableSSL != nil { 205 nc.EnableSSL = helper.BoolToPtr(*nc.EnableSSL) 206 } 207 if nc.VerifySSL != nil { 208 nc.VerifySSL = helper.BoolToPtr(*nc.VerifySSL) 209 } 210 if nc.ServerAutoJoin != nil { 211 nc.ServerAutoJoin = helper.BoolToPtr(*nc.ServerAutoJoin) 212 } 213 if nc.ClientAutoJoin != nil { 214 nc.ClientAutoJoin = helper.BoolToPtr(*nc.ClientAutoJoin) 215 } 216 217 return nc 218 }