Merge branch 'master' of github.com:echohead/cornfig

This commit is contained in:
Tim Miller 2013-01-29 11:04:22 -08:00
commit 4be669080d
2 changed files with 42 additions and 9 deletions

View File

@ -1,13 +1,13 @@
cornfig
=======
Apply configuration from cloud metadata.
Apply cornfiguration from cloud metadata.
# What does it do?
it turns a cloud-metdata file like this:
```
it turns a cloud-metadata file like this:
```javascript
{"config": {"keystone": {"database": {"host": "127.0.0.1", "user": "keystone", "password": "foobar"}}}}
```
into service config files like this:
@ -17,7 +17,7 @@ connection = mysql://keystone:foobar@127.0.0.1/keystone
...other settings...
```
# But HOW??
# but... but HOW??
Just pass it the path to a directory tree of templates:
```
@ -40,11 +40,13 @@ e.g.
```
If a template is executable it will be treated as an **executable template**.
Otherwise, it will be treated as a **moustache template**.
Otherwise, it will be treated as a **mustache template**.
## Moustache Templates
## Mustache Templates
If you don't need any logic, just some string substitution, use a moustache template:
If you don't need any logic, just some string substitution, use a mustache template.
Metadata settings are accessed with dot ('.') notation:
```
[sql]
@ -53,16 +55,44 @@ connection = mysql://{{keystone.database.user}}:{{keystone.database.password}@{{
## Executable Templates
An executable template is a script which accepts parameters in environment variables, and writes a config file to standard out.
Configuration requiring logic is expressed in executable templates.
An executable template is a script which accepts parameters via environment variables or standard in, and writes a config file to standard out.
The output of the script will be written to the path corresponding to the executable template's path in the template tree.
e.g.
```
```bash
#/bin/sh
echo "[sql]"
echo "connection = mysql://$keystone_database_user:$keystone_database_password@$keystone_database_user/keystone"
```
TODO: the script is passed the contents of the metadata file on stdin, so you can use a higher-level languages, too:
```ruby
#!/usr/bin/env ruby
require 'json'
params = JSON.parse STDIN.read
puts "connection = mysql://#{c['keystone']['database']['user']}:#{c['keystone']['database']['password']}@#{c['keystone']['database']['host']}/keystone"
```
You could even embed mustache in a heredoc, and use that:
```ruby
#!/usr/bin/env ruby
require 'json'
require 'mustache'
params = JSON.parse STDIN.read
template = <<-eos
[sql]
connection = mysql://{{keystone.database.user}}:{{keystone.database.password}}@{{keystone.database.host}}/keystone
[log]
...
eos
# tweak params here...
puts Mustache.render(template, params)
```

View File

@ -22,6 +22,7 @@ def write_file(path, contents):
out.write(contents)
out.close()
# return a map of filenames->filecontents
def build_tree(templates, config):
res = {}
for in_file, out_file in templates:
@ -50,6 +51,8 @@ def render_executable(path, config):
def read_config(path):
return json.loads(open(path).read())
# flatten a nested hash into a one-level hash
# {x: {a: b} } => {x.a: b}
def flatten(d, prefix='', res=None):
res = res or {}
for k, v in d.items():