Skip to content

Commit

Permalink
Merge pull request #43 from kelseyhightower/iteration
Browse files Browse the repository at this point in the history
Iteration
  • Loading branch information
kelseyhightower committed Feb 24, 2014
2 parents f9a9b09 + 0712c59 commit 57989ba
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
84 changes: 84 additions & 0 deletions docs/templates-interation-example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Templates - Interation Example

Using confd to manage nginx proxy config

## Add upstream servers to etcd

```Bash
curl http://127.0.0.1:4001/v2/keys/myapp/upstream -XPUT -d dir=true
curl http://127.0.0.1:4001/v2/keys/myapp/upstream/app2 -XPUT -d value="10.0.1.101:80"
curl http://127.0.0.1:4001/v2/keys/myapp/upstream/app1 -XPUT -d value="10.0.1.100:80"
```

## Create a template resource

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

```TOML
[template]
keys = [
"myapp/upstream",
]
owner = "nginx"
mode = "0644"
src = "myapp-nginx.tmpl"
dest = "/tmp/myapp.conf"
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 myapp {
{{range $server := .myapp_upstream}}
server {{$server.Value}};
{{end}}
}
server {
server_name www.example.com;
location / {
proxy_pass http://myapp;
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;
}
}
```

## Run confd

```
confd -onetime
2014-02-16T21:36:46-08:00 confd[2180]: INFO Target config /tmp/nginx.conf out of sync
2014-02-16T21:36:46-08:00 confd[2180]: INFO Target config /tmp/nginx.conf has been updated
```

Output

```
upstream myapp {
server 10.0.1.100:80;
server 10.0.1.101:80;
}
server {
server_name www.example.com;
location / {
proxy_pass http://myapp;
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;
}
}
```
3 changes: 2 additions & 1 deletion etcd/etcdutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ func GetValues(c EtcdClient, prefix string, keys []string) (map[string]interface
// nodeWalk recursively descends nodes, updating vars.
func nodeWalk(node *etcd.Node, prefix string, vars map[string]interface{}) error {
if node != nil {
key := pathToKey(node.Key, prefix)
if !node.Dir {
key := pathToKey(node.Key, prefix)
vars[key] = node.Value
} else {
vars[key] = node.Nodes
for _, node := range node.Nodes {
nodeWalk(&node, prefix, vars)
}
Expand Down
5 changes: 4 additions & 1 deletion resource/template/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"syscall"
Expand Down Expand Up @@ -89,7 +90,9 @@ func (t *TemplateResource) createStageFile() error {
return err
}
log.Debug("Compiling source template " + t.Src)
tmpl := template.Must(template.ParseFiles(t.Src))
tplFuncMap := make(template.FuncMap)
tplFuncMap["Base"] = path.Base
tmpl := template.Must(template.New(path.Base(t.Src)).Funcs(tplFuncMap).ParseFiles(t.Src))
if err = tmpl.Execute(temp, t.Vars); err != nil {
return err
}
Expand Down

0 comments on commit 57989ba

Please sign in to comment.