Skip to content

Commit

Permalink
Adding check-only 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 Jun 17, 2016
1 parent c6622ed commit f3edef9
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 @@ -41,6 +41,7 @@ var (
srvDomain string
srvRecord string
syncOnly bool
checkOnly bool
table string
templateConfig template.Config
backendsConfig backends.Config
Expand Down Expand Up @@ -70,6 +71,7 @@ type Config struct {
SRVRecord string `toml:"srv_record"`
Scheme string `toml:"scheme"`
SyncOnly bool `toml:"sync-only"`
CheckOnly bool `toml:"check-only"`
Table string `toml:"table"`
Username string `toml:"username"`
LogLevel string `toml:"log-level"`
Expand Down Expand Up @@ -99,6 +101,7 @@ func init() {
flag.StringVar(&srvDomain, "srv-domain", "", "the name of the resource record")
flag.StringVar(&srvRecord, "srv-record", "", "the SRV record to search for backends nodes. Example: _etcd-client._tcp.example.com")
flag.BoolVar(&syncOnly, "sync-only", false, "sync without check_cmd and reload_cmd")
flag.BoolVar(&checkOnly, "check-only", false, "show pending changes and run check_cmd")
flag.StringVar(&authType, "auth-type", "", "Vault auth backend type to use (only used with -backend=vault)")
flag.StringVar(&appID, "app-id", "", "Vault app-id to use with the app-id backend (only used with -backend=vault and auth-type=app-id)")
flag.StringVar(&userID, "user-id", "", "Vault user-id to use with the app-id backend (only used with -backend=value and auth-type=app-id)")
Expand Down Expand Up @@ -228,6 +231,7 @@ func initConfig() error {
Noop: config.Noop,
Prefix: config.Prefix,
SyncOnly: config.SyncOnly,
CheckOnly: config.CheckOnly,
TemplateDir: filepath.Join(config.ConfDir, "templates"),
}
return nil
Expand Down Expand Up @@ -308,6 +312,8 @@ func setConfigFromFlag(f *flag.Flag) {
config.SRVRecord = srvRecord
case "sync-only":
config.SyncOnly = syncOnly
case "check-only":
config.CheckOnly = checkOnly
case "table":
config.Table = table
case "username":
Expand Down
1 change: 1 addition & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestInitConfigDefaultConfig(t *testing.T) {
SRVDomain: "",
Scheme: "http",
Table: "",
CheckOnly: false,
}
if err := initConfig(); err != nil {
t.Errorf(err.Error())
Expand Down
34 changes: 34 additions & 0 deletions docs/check-only-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Check-only Mode

When in check-only 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: check-only mode *does not* update target and *does not* run `reload_cmd`.

## Usage

### commandline flag

```
confd -check-only
```

### configuration file

```
check-only = true
```

### Example

```
confd -onetime -check-only
```

-

```
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 Check only mode enabled. /tmp/myconfig.conf will not be modified
```
2 changes: 2 additions & 0 deletions docs/command-line-flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Usage of confd:
the SRV record to search for backends nodes. Example: _etcd-client._tcp.example.com
-sync-only
sync without check_cmd and reload_cmd
-check-only
show pending changes and run check_cmd.
-table string
the name of the DynamoDB table (only used with -backend=dynamodb)
-user-id string
Expand Down
1 change: 1 addition & 0 deletions docs/configuration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Optional:
* `srv_domain` (string) - The name of the resource record.
* `srv_record` (string) - The SRV record to search for backends nodes.
* `sync-only` (bool) - sync without check_cmd and reload_cmd.
* `check-only` (bool) - Process all template resources, run check_cmd but skip target update.
* `watch` (bool) - Enable watch support.

Example:
Expand Down
7 changes: 7 additions & 0 deletions resource/template/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Config struct {
Prefix string
StoreClient backends.StoreClient
SyncOnly bool
CheckOnly bool
TemplateDir string
}

Expand Down Expand Up @@ -55,6 +56,7 @@ type TemplateResource struct {
store memkv.Store
storeClient backends.StoreClient
syncOnly bool
checkOnly 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.checkOnly = config.CheckOnly
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.checkOnly {
log.Warning("Check only 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 f3edef9

Please sign in to comment.