Skip to content

Commit

Permalink
Adding dry-run mode.
Browse files Browse the repository at this point in the history
This mode allows to process template resources and run check_cmd on the result without updating target files nor running reload_cmd. This mode is useful to run monitoring checks where it's nice to be able to detect problems with the templates at rendering and application level.
  • Loading branch information
jsargiot committed May 31, 2017
1 parent ad5f74f commit 176b6ad
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
clientKey string
confdir string
config Config // holds the global confd config.
dryRun bool
interval int
keepStageFile bool
logLevel string
Expand Down Expand Up @@ -62,6 +63,7 @@ type Config struct {
ClientCert string `toml:"client_cert"`
ClientKey string `toml:"client_key"`
ConfDir string `toml:"confdir"`
DryRun bool `toml:"dry-run"`
Interval int `toml:"interval"`
Noop bool `toml:"noop"`
Password string `toml:"password"`
Expand All @@ -87,6 +89,7 @@ func init() {
flag.StringVar(&clientKey, "client-key", "", "the client key")
flag.StringVar(&confdir, "confdir", "/etc/confd", "confd conf directory")
flag.StringVar(&configFile, "config-file", "", "the confd config file")
flag.BoolVar(&dryRun, "dry-run", false, "perform execution without modifying target file")
flag.IntVar(&interval, "interval", 600, "backend polling interval")
flag.BoolVar(&keepStageFile, "keep-stage-file", false, "keep staged files")
flag.StringVar(&logLevel, "log-level", "", "level which confd should log messages")
Expand Down Expand Up @@ -224,6 +227,7 @@ func initConfig() error {
templateConfig = template.Config{
ConfDir: config.ConfDir,
ConfigDir: filepath.Join(config.ConfDir, "conf.d"),
DryRun: config.DryRun,
KeepStageFile: keepStageFile,
Noop: config.Noop,
Prefix: config.Prefix,
Expand Down Expand Up @@ -290,6 +294,8 @@ func setConfigFromFlag(f *flag.Flag) {
config.ClientCaKeys = clientCaKeys
case "confdir":
config.ConfDir = confdir
case "dry-run":
config.DryRun = dryRun
case "node":
config.BackendNodes = nodes
case "interval":
Expand Down
1 change: 1 addition & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestInitConfigDefaultConfig(t *testing.T) {
ClientCert: "",
ClientKey: "",
ConfDir: "/etc/confd",
DryRun: false,
Interval: 600,
Noop: false,
Prefix: "",
Expand Down
2 changes: 2 additions & 0 deletions docs/command-line-flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Usage of confd:
confd conf directory (default "/etc/confd")
-config-file string
the confd config file
-dry-run
show pending changes and run check_cmd.
-interval int
backend polling interval (default 600)
-keep-stage-file
Expand Down
1 change: 1 addition & 0 deletions docs/configuration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Optional:
* `client_cert` (string) - The client cert file.
* `client_key` (string) - The client key file.
* `confdir` (string) - The path to confd configs. ("/etc/confd")
* `dry-run` (bool) - Process all template resources, run check_cmd but skip target update.
* `interval` (int) - The backend polling interval in seconds. (600)
* `log-level` (string) - level which confd should log messages ("info")
* `nodes` (array of strings) - List of backend nodes. (["http://127.0.0.1:4001"])
Expand Down
34 changes: 34 additions & 0 deletions docs/dry-run-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Dry-Run Mode

When in dry-run mode target configuration files will not be modified but `check_cmd` will be executed and execution returns error if the execution of `check_cmd` fails.

This mode behaves like `noop` mode but adds the execution of the `check_cmd` command.

Note: dry-run mode *does not* update target and *does not* run `reload_cmd`.

## Usage

### commandline flag

```
confd -dry-run
```

### configuration file

```
dry-run = true
```

### Example

```
confd -onetime -dry-run
```

-

```
2016-06-09T15:22:28-03:00 confd[29789]: INFO /tmp/myconfig.conf has md5sum 5fc1bd8022b5cbb5aca50b74817fa9c9 should be 7cfb29f5029fc9502f58665d66ce1c6c
2016-06-09T15:22:28-03:00 confd[29789]: WARNING Dry-run mode enabled. /tmp/myconfig.conf will not be modified
```
7 changes: 7 additions & 0 deletions resource/template/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
type Config struct {
ConfDir string
ConfigDir string
DryRun bool
KeepStageFile bool
Noop bool
Prefix string
Expand Down Expand Up @@ -55,6 +56,7 @@ type TemplateResource struct {
store memkv.Store
storeClient backends.StoreClient
syncOnly bool
dryRun bool
}

var ErrEmptySrc = errors.New("empty src template")
Expand Down Expand Up @@ -82,6 +84,7 @@ func NewTemplateResource(path string, config Config) (*TemplateResource, error)
tr.funcMap = newFuncMap()
tr.store = memkv.New()
tr.syncOnly = config.SyncOnly
tr.dryRun = config.DryRun
addFuncs(tr.funcMap, tr.store.FuncMap)

if config.Prefix != "" {
Expand Down Expand Up @@ -191,6 +194,10 @@ func (t *TemplateResource) sync() error {
return errors.New("Config check failed: " + err.Error())
}
}
if t.dryRun {
log.Warning("Dry-run mode enabled. " + t.Dest + " will not be modified")
return nil
}
log.Debug("Overwriting target config " + t.Dest)
err := os.Rename(staged, t.Dest)
if err != nil {
Expand Down

0 comments on commit 176b6ad

Please sign in to comment.