Skip to content

Commit

Permalink
Merge remote-tracking branch 'HeavyHorst/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Fisher committed Oct 27, 2016
2 parents 460bd7e + 1265714 commit 0ee6a8e
Show file tree
Hide file tree
Showing 37 changed files with 11,906 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ script:
- bash integration/redis/test.sh
- bash integration/rancher/test.sh
- bash integration/vault/test.sh
- bash integration/file/test.sh
- bash integration/zookeeper/test.sh
- bash integration/dynamodb/test.sh
9 changes: 9 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion backends/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/kelseyhightower/confd/backends/env"
"github.com/kelseyhightower/confd/backends/etcd"
"github.com/kelseyhightower/confd/backends/etcdv3"
"github.com/kelseyhightower/confd/backends/file"
"github.com/kelseyhightower/confd/backends/rancher"
"github.com/kelseyhightower/confd/backends/redis"
"github.com/kelseyhightower/confd/backends/stackengine"
Expand All @@ -30,7 +31,13 @@ func New(config Config) (StoreClient, error) {
config.Backend = "etcd"
}
backendNodes := config.BackendNodes
log.Info("Backend nodes set to " + strings.Join(backendNodes, ", "))

if config.Backend == "file" {
log.Info("Backend source(s) set to " + config.YAMLFile)
} else {
log.Info("Backend source(s) set to " + strings.Join(backendNodes, ", "))
}

switch config.Backend {
case "consul":
return consul.New(config.BackendNodes, config.Scheme,
Expand All @@ -50,6 +57,8 @@ func New(config Config) (StoreClient, error) {
return redis.NewRedisClient(backendNodes, config.ClientKey)
case "env":
return env.NewEnvClient()
case "file":
return file.NewFileClient(config.YAMLFile)
case "vault":
vaultConfig := map[string]string{
"app-id": config.AppID,
Expand Down
1 change: 1 addition & 0 deletions backends/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ type Config struct {
Username string
AppID string
UserID string
YAMLFile string
}
96 changes: 96 additions & 0 deletions backends/file/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package file

import (
"fmt"
"io/ioutil"
"strings"

"github.com/fsnotify/fsnotify"
"github.com/kelseyhightower/confd/log"
"gopkg.in/yaml.v2"
)

var replacer = strings.NewReplacer("/", "_")

// Client provides a shell for the yaml client
type Client struct {
filepath string
}

func NewFileClient(filepath string) (*Client, error) {
return &Client{filepath}, nil
}

func (c *Client) GetValues(keys []string) (map[string]string, error) {
yamlMap := make(map[interface{}]interface{})
vars := make(map[string]string)

data, err := ioutil.ReadFile(c.filepath)
if err != nil {
return vars, err
}
err = yaml.Unmarshal(data, &yamlMap)
if err != nil {
return vars, err
}

nodeWalk(yamlMap, "", vars)
log.Debug(fmt.Sprintf("Key Map: %#v", vars))

return vars, nil
}

// nodeWalk recursively descends nodes, updating vars.
func nodeWalk(node map[interface{}]interface{}, key string, vars map[string]string) error {
for k, v := range node {
key := key + "/" + k.(string)

switch v.(type) {
case map[interface{}]interface{}:
nodeWalk(v.(map[interface{}]interface{}), key, vars)
case []interface{}:
for _, j := range v.([]interface{}) {
switch j.(type) {
case map[interface{}]interface{}:
nodeWalk(j.(map[interface{}]interface{}), key, vars)
case string:
vars[key+"/"+j.(string)] = ""
}
}
case string:
vars[key] = v.(string)
}
}
return nil
}

func (c *Client) WatchPrefix(prefix string, keys []string, waitIndex uint64, stopChan chan bool) (uint64, error) {
if waitIndex == 0 {
return 1, nil
}

watcher, err := fsnotify.NewWatcher()
if err != nil {
return 0, err
}
defer watcher.Close()

err = watcher.Add(c.filepath)
if err != nil {
return 0, err
}

for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
return 1, nil
}
case err := <-watcher.Errors:
return 0, err
case <-stopChan:
return 0, nil
}
}
return waitIndex, nil
}
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var (
watch bool
appID string
userID string
yamlFile string
)

// A Config structure is used to configure confd.
Expand Down Expand Up @@ -76,6 +77,7 @@ type Config struct {
Watch bool `toml:"watch"`
AppID string `toml:"app_id"`
UserID string `toml:"user_id"`
YAMLFile string `toml:"file"`
}

func init() {
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.StringVar(&yamlFile, "file", "", "the YAML/JSON file to watch for changes")
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 @@ -221,6 +224,7 @@ func initConfig() error {
Username: config.Username,
AppID: config.AppID,
UserID: config.UserID,
YAMLFile: config.YAMLFile,
}
// Template configuration.
templateConfig = template.Config{
Expand Down Expand Up @@ -322,5 +326,7 @@ func setConfigFromFlag(f *flag.Flag) {
config.AppID = appID
case "user-id":
config.UserID = userID
case "file":
config.YAMLFile = yamlFile
}
}
25 changes: 25 additions & 0 deletions integration/file/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

cat <<EOT >> test.yaml
key: foobar
database:
- host: 127.0.0.1
- password: p@sSw0rd
- port: "3306"
- username: confd
upstream:
- app1: 10.0.1.10:8080
- app2: 10.0.1.11:8080
prefix:
database:
- host: 127.0.0.1
- password: p@sSw0rd
- port: "3306"
- username: confd
upstream:
app1: 10.0.1.10:8080
app2: 10.0.1.11:8080
EOT

# Run confd
confd --onetime --log-level debug --confdir ./integration/confdir --backend file --file test.yaml --watch
6 changes: 6 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0ee6a8e

Please sign in to comment.