Skip to content

Commit

Permalink
all key must have the / prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
kelseyhightower committed Jul 9, 2014
1 parent a82eb14 commit 800598f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 83 deletions.
6 changes: 5 additions & 1 deletion backends/consul/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package consul

import (
"path"
"strings"

"github.com/armon/consul-api"
)

Expand All @@ -26,12 +29,13 @@ func NewConsulClient(nodes []string) (*Client, error) {
func (c *Client) GetValues(keys []string) (map[string]string, error) {
vars := make(map[string]string)
for _, key := range keys {
key := strings.TrimPrefix(key, "/")
pairs, _, err := c.client.List(key, nil)
if err != nil {
return vars, err
}
for _, p := range pairs {
vars[p.Key] = string(p.Value)
vars[path.Join("/", p.Key)] = string(p.Value)
}
}
return vars, nil
Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func initConfig() error {
BackendNodes: []string{"127.0.0.1:4001"},
ConfDir: "/etc/confd",
Interval: 600,
Prefix: "/",
Scheme: "http",
}
// Update config from the TOML configuration file.
Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestInitConfigDefaultConfig(t *testing.T) {
Debug: false,
Interval: 600,
Noop: false,
Prefix: "",
Prefix: "/",
Quiet: false,
SRVDomain: "",
Scheme: "http",
Expand Down
79 changes: 78 additions & 1 deletion docs/quick-start-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export MYAPP_DATABASE_URL=db.example.com
export MYAPP_DATABASE_USER=rob
```


### Create the confdir

The confdir is where template resource configs and source templates are stored. The default confdir is `/etc/confd`. Create the confdir by executing the following command:
Expand Down Expand Up @@ -102,3 +101,81 @@ Output:
database_url = db.example.com
database_user = rob
```

## Advanced Example

Using confd to manage nginx proxy config of several apps in subdomains

## Add two apps with upstream servers to etcd

myapp
```
etcdctl set /myapp/subdomain myapp
etcdctl set /myapp/upstream/app2 "10.0.1.100:80"
etcdctl set /myapp/upstream/app1 "10.0.1.101:80"
```

yourapp
```
etcdctl set /yourapp/subdomain yourapp
etcdctl set /yourapp/upstream/app2 "10.0.1.102:80"
etcdctl set /yourapp/upstream/app1 "10.0.1.103:80"
```

## Create template resources

/etc/confd/conf.d/myapp-nginx.toml

```
[template]
prefix = "myapp"
src = "nginx.tmpl"
dest = "/tmp/myapp.conf"
owner = "nginx"
mode = "0644"
keys = [
"/subdomain",
"/upstream",
]
check_cmd = "/usr/sbin/nginx -t -c {{.src}}"
reload_cmd = "/usr/sbin/service nginx reload"
```

/etc/confd/conf.d/yourapp-nginx.toml

```
[template]
prefix = "yourapp"
src = "nginx.tmpl"
dest = "/tmp/yourapp.conf"
owner = "nginx"
mode = "0644"
keys = [
"/subdomain",
"/upstream",
]
check_cmd = "/usr/sbin/nginx -t -c {{.src}}"
reload_cmd = "/usr/sbin/service nginx reload"
```

## Create a source template

/etc/confd/templates/nginx.tmpl
```
upstream {{getv "/subdomain"}} {
{{range getvs "/upstream/*"}}
server {{.}};
{{end}}
}
server {
server_name {{getv "/subdomain"}}.example.com;
location / {
proxy_pass http://{{getv "/subdomain"}};
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
```
77 changes: 0 additions & 77 deletions docs/templates-scoped-resources.md

This file was deleted.

3 changes: 0 additions & 3 deletions resource/template/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"io"
"os"
"path"
"strings"
"syscall"

"github.com/kelseyhightower/confd/log"
Expand All @@ -24,8 +23,6 @@ type fileInfo struct {
Md5 string
}

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

func appendPrefix(prefix string, keys []string) []string {
s := make([]string, len(keys))
for i, k := range keys {
Expand Down

0 comments on commit 800598f

Please sign in to comment.