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

Commit

Permalink
Adds delete_data_dir_on_stop config option
Browse files Browse the repository at this point in the history
- This is so single-node etcd deployments can be restarted

[#152475519]

Signed-off-by: Brenda Chan <[email protected]>

Adds options to etcdfab config

[#152475519]

Signed-off-by: Travis Hall <[email protected]>
  • Loading branch information
tvs authored and bsnchan committed Nov 3, 2017
1 parent cc015d7 commit 782a9b7
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
4 changes: 4 additions & 0 deletions jobs/etcd/spec
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,7 @@ properties:
etcd.enable_debug_logging:
description: "Enables etcd's debug logging"
default: false

options.delete_data_dir_on_stop:
description: "Boolean flag to delete etcd data dir on application stop"
default: true
1 change: 1 addition & 0 deletions jobs/etcd/templates/etcdfab.json.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
external_ip: discover_external_ip,
},
etcd: p('etcd'),
options: p('options')
}.to_json
%>
4 changes: 3 additions & 1 deletion src/etcdfab/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ func (a Application) Stop() error {
a.removeSelfFromCluster(cfg)
}

a.removeDataDir(cfg)
if cfg.Options.DeleteDataDirOnStop == true {
a.removeDataDir(cfg)
}

a.logger.Info("application.kill")
err = a.kill(cfg.PidFile())
Expand Down
67 changes: 67 additions & 0 deletions src/etcdfab/application/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"

"code.cloudfoundry.org/lager"

Expand Down Expand Up @@ -684,6 +685,9 @@ var _ = Describe("Application", func() {
"client_ip": "some-client-ip",
"advertise_urls_dns_suffix": "some-dns-suffix",
},
"options": map[string]interface{}{
"delete_data_dir_on_stop": true,
},
}
configFileName = createConfig(tmpDir, "config-file", configuration)

Expand Down Expand Up @@ -712,6 +716,9 @@ var _ = Describe("Application", func() {
AdvertiseURLsDNSSuffix: "some-dns-suffix",
Machines: []string{"some-ip-1", "some-ip-2"},
},
Options: config.Options{
DeleteDataDirOnStop: true,
},
}

app = application.New(application.NewArgs{
Expand Down Expand Up @@ -822,6 +829,66 @@ var _ = Describe("Application", func() {
})
})

Context("If delete_data_dir_on_stop is set to true", func() {
BeforeEach(func() {
fileOut, err := os.Create(strings.Join([]string{dataDir, "foo"}, "/"))
Expect(err).NotTo(HaveOccurred())
defer fileOut.Close()

keepDataDirConfiguration := map[string]interface{}{
"node": map[string]interface{}{
"name": "some_name",
"index": 3,
"external_ip": "some-external-ip",
},
"etcd": map[string]interface{}{
"etcd_path": "path-to-etcd",
"cert_dir": "some/cert/dir",
"run_dir": runDir,
"data_dir": dataDir,
"heartbeat_interval_in_milliseconds": 10,
"election_timeout_in_milliseconds": 20,
"peer_require_ssl": false,
"peer_ip": "some-peer-ip",
"require_ssl": false,
"client_ip": "some-client-ip",
"advertise_urls_dns_suffix": "some-dns-suffix",
},
"options": map[string]interface{}{
"delete_data_dir_on_stop": false,
},
}

keepDataDirConfigFileName := createConfig(tmpDir, "config-file", keepDataDirConfiguration)

app = application.New(application.NewArgs{
Command: fakeCommand,
ConfigFilePath: keepDataDirConfigFileName,
LinkConfigFilePath: linkConfigFileName,
EtcdClient: fakeEtcdClient,
ClusterController: fakeClusterController,
SyncController: fakeSyncController,
OutWriter: &outWriter,
ErrWriter: &errWriter,
Logger: fakeLogger,
})
})

It("stops etcd and doesn't clean up", func() {
err := app.Stop()
Expect(err).NotTo(HaveOccurred())

By("keeping the contents inside data dir", func() {
d, err := os.Open(dataDir)
Expect(err).NotTo(HaveOccurred())
defer d.Close()
files, err := d.Readdirnames(-1)
Expect(err).NotTo(HaveOccurred())
Expect(len(files)).ToNot(Equal(0))
})
})
})

Context("when it cannot read the config file", func() {
BeforeEach(func() {
app = application.New(application.NewArgs{
Expand Down
9 changes: 7 additions & 2 deletions src/etcdfab/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ type Etcd struct {
EnableDebugLogging bool `json:"enable_debug_logging"`
}

type Options struct {
DeleteDataDirOnStop bool `json:"delete_data_dir_on_stop"`
}

type Config struct {
Node Node
Etcd Etcd
Node Node
Etcd Etcd
Options Options
}

func defaultConfig() Config {
Expand Down
10 changes: 10 additions & 0 deletions src/etcdfab/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ var _ = Describe("Config", func() {
"advertise_urls_dns_suffix": "some-dns-suffix",
"enable_debug_logging": true,
},
"options": map[string]interface{}{
"delete_data_dir_on_stop": false,
},
}
configFilePath = writeConfigurationFile(tmpDir, "config-file", configuration)

Expand Down Expand Up @@ -101,6 +104,9 @@ var _ = Describe("Config", func() {
Machines: []string{"some-ip-1", "some-ip-2", "some-ip-3"},
EnableDebugLogging: true,
},
Options: config.Options{
DeleteDataDirOnStop: false,
},
}))
})

Expand Down Expand Up @@ -134,6 +140,9 @@ var _ = Describe("Config", func() {
AdvertiseURLsDNSSuffix: "some-dns-suffix",
EnableDebugLogging: true,
},
Options: config.Options{
DeleteDataDirOnStop: false,
},
}))
})
})
Expand All @@ -152,6 +161,7 @@ var _ = Describe("Config", func() {
Expect(cfg.Etcd.CertDir).To(Equal("/var/vcap/jobs/etcd/config/certs"))
Expect(cfg.Etcd.RunDir).To(Equal("/var/vcap/sys/run/etcd"))
Expect(cfg.Etcd.DataDir).To(Equal("/var/vcap/store/etcd"))
Expect(cfg.Options.DeleteDataDirOnStop).To(Equal(false))
})

Context("failure cases", func() {
Expand Down

0 comments on commit 782a9b7

Please sign in to comment.