mirror of https://github.com/status-im/consul.git
Merge pull request #910 from hashicorp/f-update-persisted
Update agent-persisted services/checks
This commit is contained in:
commit
ccbb1bb67f
|
@ -535,7 +535,6 @@ func (a *Agent) ResumeSync() {
|
||||||
// persistService saves a service definition to a JSON file in the data dir
|
// persistService saves a service definition to a JSON file in the data dir
|
||||||
func (a *Agent) persistService(service *structs.NodeService) error {
|
func (a *Agent) persistService(service *structs.NodeService) error {
|
||||||
svcPath := filepath.Join(a.config.DataDir, servicesDir, stringHash(service.ID))
|
svcPath := filepath.Join(a.config.DataDir, servicesDir, stringHash(service.ID))
|
||||||
if _, err := os.Stat(svcPath); os.IsNotExist(err) {
|
|
||||||
wrapped := persistedService{
|
wrapped := persistedService{
|
||||||
Token: a.state.ServiceToken(service.ID),
|
Token: a.state.ServiceToken(service.ID),
|
||||||
Service: service,
|
Service: service,
|
||||||
|
@ -547,7 +546,7 @@ func (a *Agent) persistService(service *structs.NodeService) error {
|
||||||
if err := os.MkdirAll(filepath.Dir(svcPath), 0700); err != nil {
|
if err := os.MkdirAll(filepath.Dir(svcPath), 0700); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fh, err := os.OpenFile(svcPath, os.O_CREATE|os.O_WRONLY, 0600)
|
fh, err := os.OpenFile(svcPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -555,7 +554,6 @@ func (a *Agent) persistService(service *structs.NodeService) error {
|
||||||
if _, err := fh.Write(encoded); err != nil {
|
if _, err := fh.Write(encoded); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -571,9 +569,6 @@ func (a *Agent) purgeService(serviceID string) error {
|
||||||
// persistCheck saves a check definition to the local agent's state directory
|
// persistCheck saves a check definition to the local agent's state directory
|
||||||
func (a *Agent) persistCheck(check *structs.HealthCheck, chkType *CheckType) error {
|
func (a *Agent) persistCheck(check *structs.HealthCheck, chkType *CheckType) error {
|
||||||
checkPath := filepath.Join(a.config.DataDir, checksDir, stringHash(check.CheckID))
|
checkPath := filepath.Join(a.config.DataDir, checksDir, stringHash(check.CheckID))
|
||||||
if _, err := os.Stat(checkPath); !os.IsNotExist(err) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the persisted check
|
// Create the persisted check
|
||||||
wrapped := persistedCheck{
|
wrapped := persistedCheck{
|
||||||
|
@ -589,7 +584,7 @@ func (a *Agent) persistCheck(check *structs.HealthCheck, chkType *CheckType) err
|
||||||
if err := os.MkdirAll(filepath.Dir(checkPath), 0700); err != nil {
|
if err := os.MkdirAll(filepath.Dir(checkPath), 0700); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fh, err := os.OpenFile(checkPath, os.O_CREATE|os.O_WRONLY, 0600)
|
fh, err := os.OpenFile(checkPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -533,11 +533,9 @@ func TestAgent_PersistService(t *testing.T) {
|
||||||
if err := agent.AddService(svc, nil, true, "mytoken"); err != nil {
|
if err := agent.AddService(svc, nil, true, "mytoken"); err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(file); err != nil {
|
if _, err := os.Stat(file); err != nil {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected, err := json.Marshal(persistedService{
|
expected, err := json.Marshal(persistedService{
|
||||||
Token: "mytoken",
|
Token: "mytoken",
|
||||||
Service: svc,
|
Service: svc,
|
||||||
|
@ -552,6 +550,26 @@ func TestAgent_PersistService(t *testing.T) {
|
||||||
if !bytes.Equal(expected, content) {
|
if !bytes.Equal(expected, content) {
|
||||||
t.Fatalf("bad: %s", string(content))
|
t.Fatalf("bad: %s", string(content))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Updates service definition on disk
|
||||||
|
svc.Port = 8001
|
||||||
|
if err := agent.AddService(svc, nil, true, "mytoken"); err != nil {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
expected, err = json.Marshal(persistedService{
|
||||||
|
Token: "mytoken",
|
||||||
|
Service: svc,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
content, err = ioutil.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(expected, content) {
|
||||||
|
t.Fatalf("bad: %s", string(content))
|
||||||
|
}
|
||||||
agent.Shutdown()
|
agent.Shutdown()
|
||||||
|
|
||||||
// Should load it back during later start
|
// Should load it back during later start
|
||||||
|
@ -561,12 +579,16 @@ func TestAgent_PersistService(t *testing.T) {
|
||||||
}
|
}
|
||||||
defer agent2.Shutdown()
|
defer agent2.Shutdown()
|
||||||
|
|
||||||
if _, ok := agent2.state.services[svc.ID]; !ok {
|
restored, ok := agent2.state.services[svc.ID]
|
||||||
|
if !ok {
|
||||||
t.Fatalf("bad: %#v", agent2.state.services)
|
t.Fatalf("bad: %#v", agent2.state.services)
|
||||||
}
|
}
|
||||||
if agent2.state.serviceTokens[svc.ID] != "mytoken" {
|
if agent2.state.serviceTokens[svc.ID] != "mytoken" {
|
||||||
t.Fatalf("bad: %#v", agent2.state.services[svc.ID])
|
t.Fatalf("bad: %#v", agent2.state.services[svc.ID])
|
||||||
}
|
}
|
||||||
|
if restored.Port != 8001 {
|
||||||
|
t.Fatalf("bad: %#v", restored)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAgent_persistedService_compat(t *testing.T) {
|
func TestAgent_persistedService_compat(t *testing.T) {
|
||||||
|
@ -731,11 +753,9 @@ func TestAgent_PersistCheck(t *testing.T) {
|
||||||
if err := agent.AddCheck(check, chkType, true, "mytoken"); err != nil {
|
if err := agent.AddCheck(check, chkType, true, "mytoken"); err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(file); err != nil {
|
if _, err := os.Stat(file); err != nil {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected, err := json.Marshal(persistedCheck{
|
expected, err := json.Marshal(persistedCheck{
|
||||||
Check: check,
|
Check: check,
|
||||||
ChkType: chkType,
|
ChkType: chkType,
|
||||||
|
@ -751,6 +771,27 @@ func TestAgent_PersistCheck(t *testing.T) {
|
||||||
if !bytes.Equal(expected, content) {
|
if !bytes.Equal(expected, content) {
|
||||||
t.Fatalf("bad: %s", string(content))
|
t.Fatalf("bad: %s", string(content))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Updates the check definition on disk
|
||||||
|
check.Name = "mem1"
|
||||||
|
if err := agent.AddCheck(check, chkType, true, "mytoken"); err != nil {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
expected, err = json.Marshal(persistedCheck{
|
||||||
|
Check: check,
|
||||||
|
ChkType: chkType,
|
||||||
|
Token: "mytoken",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
content, err = ioutil.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(expected, content) {
|
||||||
|
t.Fatalf("bad: %s", string(content))
|
||||||
|
}
|
||||||
agent.Shutdown()
|
agent.Shutdown()
|
||||||
|
|
||||||
// Should load it back during later start
|
// Should load it back during later start
|
||||||
|
@ -767,6 +808,9 @@ func TestAgent_PersistCheck(t *testing.T) {
|
||||||
if result.Status != structs.HealthCritical {
|
if result.Status != structs.HealthCritical {
|
||||||
t.Fatalf("bad: %#v", result)
|
t.Fatalf("bad: %#v", result)
|
||||||
}
|
}
|
||||||
|
if result.Name != "mem1" {
|
||||||
|
t.Fatalf("bad: %#v", result)
|
||||||
|
}
|
||||||
|
|
||||||
// Should have restored the monitor
|
// Should have restored the monitor
|
||||||
if _, ok := agent2.checkMonitors[check.CheckID]; !ok {
|
if _, ok := agent2.checkMonitors[check.CheckID]; !ok {
|
||||||
|
|
Loading…
Reference in New Issue