Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Used /var/vcap/data/ for consul agent running in client mode.
Browse files Browse the repository at this point in the history
- Applies to keyring and data dir.

[#126901423]

Signed-off-by: Kevin Kelani <[email protected]>
  • Loading branch information
angelachin authored and Pivotal committed Aug 17, 2016
1 parent 0cfd37d commit e10f410
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 138 deletions.
3 changes: 2 additions & 1 deletion jobs/consul_agent/templates/pre-start.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/bash -exu

LOG_DIR=/var/vcap/sys/log/consul_agent
DATA_DIR=/var/vcap/store/consul_agent
DATA_DIR=<%= p('consul.agent.mode') == 'server' ? '/var/vcap/store/consul_agent' :
'/var/vcap/data/consul_agent' %>
CONF_DIR=/var/vcap/jobs/consul_agent/config
CERT_DIR=$CONF_DIR/certs
PKG=/var/vcap/packages/consul
Expand Down
3 changes: 2 additions & 1 deletion src/confab/chaperon/config_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ var _ = Describe("ConfigWriter", func() {
dataDir, err = ioutil.TempDir("", "")
Expect(err).NotTo(HaveOccurred())

cfg = config.Default()
cfg = config.Config{}
cfg.Consul.Agent.DnsConfig.MaxStale = "5s"
cfg.Node = config.ConfigNode{Name: "node", Index: 0}
cfg.Path.ConsulConfigDir = configDir
cfg.Path.DataDir = dataDir
Expand Down
2 changes: 1 addition & 1 deletion src/confab/chaperon/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var _ = Describe("Controller", func() {

serviceDefiner = &fakes.ServiceDefiner{}

confabConfig := config.Default()
confabConfig := config.Config{}
confabConfig.Node = config.ConfigNode{Name: "node", Index: 0}

controller = chaperon.Controller{
Expand Down
24 changes: 20 additions & 4 deletions src/confab/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,12 @@ type ConfigConsulAgentServers struct {
WAN []string `json:"wan"`
}

func Default() Config {
func defaultConfig() Config {
return Config{
Path: ConfigPath{
AgentPath: "/var/vcap/packages/consul/bin/consul",
ConsulConfigDir: "/var/vcap/jobs/consul_agent/config",
PIDFile: "/var/vcap/sys/run/consul_agent/consul_agent.pid",
KeyringFile: "/var/vcap/store/consul_agent/serf/local.keyring",
DataDir: "/var/vcap/store/consul_agent",
},
Consul: ConfigConsul{
Agent: ConfigConsulAgent{
Expand All @@ -81,10 +79,28 @@ func Default() Config {
}

func ConfigFromJSON(configData []byte) (Config, error) {
config := Default()
config := defaultConfig()

if err := json.Unmarshal(configData, &config); err != nil {
return Config{}, err
}

if config.Path.KeyringFile == "" {
if config.Consul.Agent.Mode == "server" {
config.Path.KeyringFile = "/var/vcap/store/consul_agent/serf/local.keyring"
} else {
config.Path.KeyringFile = "/var/vcap/data/consul_agent/serf/local.keyring"
}
}

if config.Path.DataDir == "" {
if config.Consul.Agent.Mode == "server" {
config.Path.DataDir = "/var/vcap/store/consul_agent"
} else {
config.Path.DataDir = "/var/vcap/data/consul_agent"
}
}

return config, nil

}
247 changes: 117 additions & 130 deletions src/confab/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,148 +8,135 @@ import (
)

var _ = Describe("Config", func() {
Describe("Default", func() {
It("returns a default configuration", func() {
Expect(config.Default()).To(Equal(config.Config{
Consul: config.ConfigConsul{
Agent: config.ConfigConsulAgent{
Servers: config.ConfigConsulAgentServers{
LAN: []string{},
WAN: []string{},
},
DnsConfig: config.ConfigConsulAgentDnsConfig{
AllowStale: false,
MaxStale: "5s",
},
},
},
Path: config.ConfigPath{
AgentPath: "/var/vcap/packages/consul/bin/consul",
ConsulConfigDir: "/var/vcap/jobs/consul_agent/config",
PIDFile: "/var/vcap/sys/run/consul_agent/consul_agent.pid",
KeyringFile: "/var/vcap/store/consul_agent/serf/local.keyring",
DataDir: "/var/vcap/store/consul_agent",
},
Confab: config.ConfigConfab{
TimeoutInSeconds: 55,
},
}))
})
})

Describe("ConfigFromJSON", func() {
It("returns a config given JSON", func() {
json := []byte(`{
"node": {
"name": "nodename",
"index": 1234,
"external_ip": "10.0.0.1"
},
"path": {
"agent_path": "/path/to/agent",
"consul_config_dir": "/consul/config/dir",
"pid_file": "/path/to/pidfile",
"keyring_file": "/path/to/keyring",
"data_dir": "/path/to/data/dir"
},
"consul": {
"agent": {
"services": {
"myservice": {
"name" : "myservicename"
Context("when given a fully populated config", func() {
It("returns a non-default config", func() {
json := []byte(`{
"node": {
"name": "nodename",
"index": 1234,
"external_ip": "10.0.0.1"
},
"path": {
"agent_path": "/path/to/agent",
"consul_config_dir": "/consul/config/dir",
"pid_file": "/path/to/pidfile",
"keyring_file": "/path/to/keyring",
"data_dir": "/path/to/data/dir"
},
"consul": {
"agent": {
"services": {
"myservice": {
"name" : "myservicename"
}
},
"mode": "server",
"datacenter": "dc1",
"log_level": "debug",
"protocol_version": 1,
"servers": {
"lan": ["server1", "server2", "server3"],
"wan": ["wan-server1", "wan-server2", "wan-server3"]
},
"dns_config": {
"allow_stale": true,
"max_stale": "15s"
}
},
"mode": "server",
"datacenter": "dc1",
"log_level": "debug",
"protocol_version": 1,
"servers": {
"lan": ["server1", "server2", "server3"],
"wan": ["wan-server1", "wan-server2", "wan-server3"]
},
"dns_config": {
"allow_stale": true,
"max_stale": "15s"
}
"encrypt_keys": ["key-1", "key-2"]
},
"encrypt_keys": ["key-1", "key-2"]
},
"confab": {
"timeout_in_seconds": 30
}
}`)
"confab": {
"timeout_in_seconds": 30
}
}`)

cfg, err := config.ConfigFromJSON(json)
Expect(err).NotTo(HaveOccurred())
Expect(cfg).To(Equal(config.Config{
Path: config.ConfigPath{
AgentPath: "/path/to/agent",
ConsulConfigDir: "/consul/config/dir",
PIDFile: "/path/to/pidfile",
KeyringFile: "/path/to/keyring",
DataDir: "/path/to/data/dir",
},
Node: config.ConfigNode{
Name: "nodename",
Index: 1234,
ExternalIP: "10.0.0.1",
},
Consul: config.ConfigConsul{
Agent: config.ConfigConsulAgent{
Services: map[string]config.ServiceDefinition{
"myservice": {
Name: "myservicename",
cfg, err := config.ConfigFromJSON(json)
Expect(err).NotTo(HaveOccurred())
Expect(cfg).To(Equal(config.Config{
Path: config.ConfigPath{
AgentPath: "/path/to/agent",
ConsulConfigDir: "/consul/config/dir",
PIDFile: "/path/to/pidfile",
KeyringFile: "/path/to/keyring",
DataDir: "/path/to/data/dir",
},
Node: config.ConfigNode{
Name: "nodename",
Index: 1234,
ExternalIP: "10.0.0.1",
},
Consul: config.ConfigConsul{
Agent: config.ConfigConsulAgent{
Services: map[string]config.ServiceDefinition{
"myservice": {
Name: "myservicename",
},
},
Mode: "server",
Datacenter: "dc1",
LogLevel: "debug",
ProtocolVersion: 1,
Servers: config.ConfigConsulAgentServers{
LAN: []string{"server1", "server2", "server3"},
WAN: []string{"wan-server1", "wan-server2", "wan-server3"},
},
DnsConfig: config.ConfigConsulAgentDnsConfig{
AllowStale: true,
MaxStale: "15s",
},
},
Mode: "server",
Datacenter: "dc1",
LogLevel: "debug",
ProtocolVersion: 1,
Servers: config.ConfigConsulAgentServers{
LAN: []string{"server1", "server2", "server3"},
WAN: []string{"wan-server1", "wan-server2", "wan-server3"},
},
DnsConfig: config.ConfigConsulAgentDnsConfig{
AllowStale: true,
MaxStale: "15s",
},
EncryptKeys: []string{"key-1", "key-2"},
},
Confab: config.ConfigConfab{
TimeoutInSeconds: 30,
},
EncryptKeys: []string{"key-1", "key-2"},
},
Confab: config.ConfigConfab{
TimeoutInSeconds: 30,
},
}))
}))
})
})

It("returns a config with default values", func() {
json := []byte(`{}`)
cfg, err := config.ConfigFromJSON(json)
Expect(err).NotTo(HaveOccurred())
Expect(cfg).To(Equal(config.Config{
Path: config.ConfigPath{
AgentPath: "/var/vcap/packages/consul/bin/consul",
ConsulConfigDir: "/var/vcap/jobs/consul_agent/config",
PIDFile: "/var/vcap/sys/run/consul_agent/consul_agent.pid",
KeyringFile: "/var/vcap/store/consul_agent/serf/local.keyring",
DataDir: "/var/vcap/store/consul_agent",
},
Consul: config.ConfigConsul{
Agent: config.ConfigConsulAgent{
Servers: config.ConfigConsulAgentServers{
LAN: []string{},
WAN: []string{},
},
DnsConfig: config.ConfigConsulAgentDnsConfig{
AllowStale: false,
MaxStale: "5s",
Context("when passing an empty config", func() {
It("returns a config with default values", func() {
json := []byte(`{}`)
cfg, err := config.ConfigFromJSON(json)

Expect(err).NotTo(HaveOccurred())
Expect(cfg).To(Equal(config.Config{
Path: config.ConfigPath{
AgentPath: "/var/vcap/packages/consul/bin/consul",
ConsulConfigDir: "/var/vcap/jobs/consul_agent/config",
PIDFile: "/var/vcap/sys/run/consul_agent/consul_agent.pid",
KeyringFile: "/var/vcap/data/consul_agent/serf/local.keyring",
DataDir: "/var/vcap/data/consul_agent",
},
Consul: config.ConfigConsul{
Agent: config.ConfigConsulAgent{
Servers: config.ConfigConsulAgentServers{
LAN: []string{},
WAN: []string{},
},
DnsConfig: config.ConfigConsulAgentDnsConfig{
AllowStale: false,
MaxStale: "5s",
},
},
},
},
Confab: config.ConfigConfab{
TimeoutInSeconds: 55,
},
}))
Confab: config.ConfigConfab{
TimeoutInSeconds: 55,
},
}))
})
})

Context("when passing an config that is in server mode and has no specified keyring file and data dir", func() {
It("returns a config with keyring file and data dir paths containing /var/vcap/store/", func() {
json := []byte(`{"consul": {"agent": {"mode": "server"}}}`)
cfg, err := config.ConfigFromJSON(json)

Expect(err).NotTo(HaveOccurred())
Expect(cfg.Path.KeyringFile).To(Equal("/var/vcap/store/consul_agent/serf/local.keyring"))
Expect(cfg.Path.DataDir).To(Equal("/var/vcap/store/consul_agent"))
})
})

It("returns an error on invalid json", func() {
Expand Down
4 changes: 3 additions & 1 deletion src/confab/config/consul_config_definer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ var _ = Describe("ConsulConfigDefiner", func() {

Describe("dns_config", func() {
It("defaults to consul defaults", func() {
Expect(config.Default().Consul.Agent.DnsConfig).To(Equal(config.ConfigConsulAgentDnsConfig{
cfg := config.Config{}
cfg.Consul.Agent.DnsConfig.MaxStale = "5s"
Expect(cfg.Consul.Agent.DnsConfig).To(Equal(config.ConfigConsulAgentDnsConfig{
// https://www.consul.io/docs/agent/options.html#allow_stale
AllowStale: false,
// https://www.consul.io/docs/agent/options.html#max_stale
Expand Down

0 comments on commit e10f410

Please sign in to comment.