Skip to content

Commit

Permalink
Add template docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kelseyhightower committed Feb 15, 2014
1 parent b0cb7b8 commit 96f1461
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
36 changes: 36 additions & 0 deletions docs/template-resources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Template Resources

Template resources are written in TOML and define a single template resource.
Template resources are stored under the `/etc/confd/conf.d` directory by default.

Required:

* `dest` (string) - output file where the template should be rendered.
* `keys` (array of strings) - An array of etcd keys. Keys will be looked up
with the configured prefix.
* `src` (string) - relative path of a [configuration template](templates.md).

Optional:

* `group` (string) - name of the group that should own the file.
* `mode` (string) - mode the file should be in.
* `owner` (string) - name of the user that should own the file.
* `reload_cmd` (string) - command to reload config.
* `check_cmd` (string) - command to check config. Use `{{ .src }}` to reference
the rendered source template.

Example:

```TOML
[template]
src = "nginx.conf.tmpl"
dest = "/etc/nginx/nginx.conf"
owner = "root"
group = "root"
mode = "0644"
keys = [
"/nginx",
]
check_cmd = "/usr/sbin/nginx -t -c {{ .src }}"
reload_cmd = "/usr/sbin/service nginx restart"
```
55 changes: 55 additions & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Templates

Templates define a single application configration template.
Templates are stored under the `/etc/confd/templates` directory by default.

Templates are written in Go's [`text/template`](http://golang.org/pkg/text/template/).

> Etcd keys are treated as paths and automatically transformed into keys for retrieval in templates. Underscores are used in place of forward slashes. _Values retrived from Etcd are never modified._
> For example `/foo/bar` becomes `foo_bar`.
> `foo_bar` is accessed as `{{ .foo_bar }}`

Example:
```Bash
$ etcdctl set /nginx/domain 'example.com'
$ etcdctl set /nginx/root '/var/www/example_dotcom'
$ etcdctl set /nginx/worker_processes '2'
```


`$ cat /etc/confd/templates/nginx.conf.tmpl`:
```Text
worker_processes {{ .nginx_worker_processes }};
server {
listen 80;
server_name www.{{ .nginx_domain }};
access_log /var/log/nginx/{{ .nginx_domain }}.access.log;
error_log /var/log/nginx/{{ .nginx_domain }}.log;
location / {
root {{ .nginx_root }};
index index.html index.htm;
}
}
```

Will produce `/etc/nginx/nginx.conf`:
```Text
worker_processes 2;
server {
listen 80;
server_name www.example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location / {
root /var/www/example_dotcom;
index index.html index.htm;
}
}
```

Go's [`text/template`](http://golang.org/pkg/text/template/) package is very powerful. For more details on it's capabilities see its [documentation.](http://golang.org/pkg/text/template/)

0 comments on commit 96f1461

Please sign in to comment.