Skip to content

Commit

Permalink
Merge branch '0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
kelseyhightower committed Nov 4, 2013
2 parents 3497f64 + 587f94d commit f4a8b3c
Show file tree
Hide file tree
Showing 22 changed files with 691 additions and 312 deletions.
98 changes: 63 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,102 @@

[![Build Status](https://travis-ci.org/kelseyhightower/confd.png?branch=master)](https://travis-ci.org/kelseyhightower/confd)

* IRC: `#confd` on Freenode
* Mailing list: [Google Groups](https://groups.google.com/forum/#!forum/confd-users)

`confd` is a lightweight configuration management tool focused on:

* keeping local configuration files up-to-date by polling [etcd](https://github.com/coreos/etcd) and processing [template resources](https://github.com/kelseyhightower/confd#template-resources).
* reloading applications to pick up new config file changes

## Getting Started
## Community

### Installing confd
* IRC: `#confd` on Freenode
* Mailing list: [Google Groups](https://groups.google.com/forum/#!forum/confd-users)
* Website: [www.confd.io](http://www.confd.io)

Download the latest binary from [Github](https://github.com/kelseyhightower/confd/releases/tag/v0.1.2).
## Quick Start

### Building
Before we begin be sure to [download and install confd](https://github.com/kelseyhightower/confd/wiki/Installation).

You can build confd from source:
### Add keys to etcd

This guide assumes you have a working [etcd](https://github.com/coreos/etcd#getting-started) server up and running and the ability to add new keys. Using the `etcdctl` command line tool add the following keys and values to etcd:

```
git clone https://github.com/kelseyhightower/confd.git
cd confd
go build
etcdctl set /myapp/database/url db.example.com
etcdctl set /myapp/database/user rob
```

This will produce the `confd` binary in the current directory.

## Usage
### Create the confdir

The following commands will process all the [template resources](https://github.com/kelseyhightower/confd#template-resources) found under `/etc/confd/conf.d`.
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:

### Poll the etcd cluster in 30 second intervals
```Bash
sudo mkdir -p /etc/confd/{conf.d,templates}
```

The "/production" string will be prefixed to keys when querying etcd at http://127.0.0.1:4001.
You don't have to use the default `confdir` location. For example you can create the confdir under your home directory. Then you tell confd to use the new `confdir` via the `-confdir` flag.

```
confd -i 30 -p '/production' -n 'http://127.0.0.1:4001'
```Bash
mkdir -p ~/confd/{conf.d,templates}
```

### Single run without polling
### Create a template resource config

Using default settings run one time and exit.
Template resources are defined in [TOML](https://github.com/mojombo/toml) config files under the `confdir` (i.e. ~/confd/conf.d/*.toml).

```
confd -onetime
Create the following template resource config and save it as `~/confd/conf.d/myconfig.toml`.

```toml
[template]
src = "myconfig.conf.tmpl"
dest = "/tmp/myconfig.conf"
keys = [
"/myapp/database/url",
"/myapp/database/user",
]
```

### Client authentication
### Create the source template

Same as above but authenticate with client certificates.
Source templates are plain old [Golang text templates](http://golang.org/pkg/text/template/#pkg-overview), and are stored under the `confdir` templates directory. Create the following source template and save it as `~/confd/templates/myconfig.conf.tmpl`

```
confd -onetime -key /etc/confd/ssl/client.key -cert /etc/confd/ssl/client.crt
# This a comment
[myconfig]
database_url = {{ .myapp_database_url }}
database_user = {{ .myapp_database_user }}
```

## Configuration
### Processing template resources

See the [Configuration Guide](https://github.com/kelseyhightower/confd/wiki/Configuration-Guide)
confd supports two modes of operation, daemon and onetime mode. In daemon mode, confd runs in the foreground and processing template resources every 5 mins by default. For this tutorial we are going to use onetime mode.

## Template Resources
Assuming you etcd server is running at http://127.0.0.1:4001 you can run the following command to process the `~/confd/conf.d/myconfig.toml` template resource:

See [Template Resources](https://github.com/kelseyhightower/confd/wiki/Template-Resources)
```
confd -verbose -onetime -node 'http://127.0.0.1:4001' -confdir ~/confd
```
Output:
```
2013-11-03T18:00:47-08:00 confd[21294]: NOTICE Starting confd
2013-11-03T18:00:47-08:00 confd[21294]: NOTICE etcd nodes set to http://127.0.0.1:4001
2013-11-03T18:00:47-08:00 confd[21294]: INFO Target config /tmp/myconfig.conf out of sync
2013-11-03T18:00:47-08:00 confd[21294]: INFO Target config /tmp/myconfig.conf has been updated
```

## Templates
The `dest` config should now be in sync with the template resource configuration.

See [Templates](https://github.com/kelseyhightower/confd/wiki/Templates)
```
cat /tmp/myconfig.conf
```

## Extras
Output:
```
# This a comment
[myconfig]
database_url = db.example.com
database_user = rob
```

### Configuration Management
## Next steps

- [confd-cookbook](https://github.com/rjocoleman/confd-cookbook)
Checkout the [confd wiki](https://github.com/kelseyhightower/confd/wiki/_pages) for more docs and [usage examples](https://github.com/kelseyhightower/confd/wiki/Usage-Examples).
45 changes: 29 additions & 16 deletions confd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,56 @@ package main
import (
"flag"
"os"
"strings"
"time"

"github.com/kelseyhightower/confd/config"
"github.com/kelseyhightower/confd/etcd/etcdutil"
"github.com/kelseyhightower/confd/log"
"github.com/kelseyhightower/confd/resource/template"
)

var (
configFile = ""
defaultConfigFile = "/etc/confd/confd.toml"
onetime bool
quiet bool
)

func init() {
flag.StringVar(&configFile, "C", "", "confd config file")
flag.StringVar(&configFile, "config-file", "", "the confd config file")
flag.BoolVar(&onetime, "onetime", false, "run once and exit")
flag.BoolVar(&quiet, "q", false, "silence non-error messages")
}

func main() {
// Most flags are defined in the confd/config package which allow us to
// override configuration settings from the cli. Parse the flags now to
// make them active.
// Most flags are defined in the confd/config package which allows us to
// override configuration settings from the command line. Parse the flags now
// to make them active.
flag.Parse()
// non-error messages are not printed by default, enable them now.
// If the "-q" flag was passed on the commandline non-error messages will
// not be printed.
log.SetQuiet(quiet)
log.Info("Starting confd")
if configFile == "" {
if IsFileExist(defaultConfigFile) {
configFile = defaultConfigFile
}
}
if err := loadConfig(configFile); err != nil {
// Initialize the global configuration.
log.Debug("Loading confd configuration")
if err := config.LoadConfig(configFile); err != nil {
log.Fatal(err.Error())
}
// Configure logging. While you can enable debug and verbose logging, however
// if quiet is set to true then debug and verbose messages will not be printed.
log.SetQuiet(config.Quiet())
log.SetVerbose(config.Verbose())
log.SetDebug(config.Debug())
log.Notice("Starting confd")
// Create the etcd client upfront and use it for the life of the process.
// The etcdClient is an http.Client and designed to be reused.
etcdClient, err := newEtcdClient(EtcdNodes(), ClientCert(), ClientKey())
log.Notice("etcd nodes set to " + strings.Join(config.EtcdNodes(), ", "))
etcdClient, err := etcdutil.NewEtcdClient(config.EtcdNodes(), config.ClientCert(), config.ClientKey())
if err != nil {
log.Fatal(err.Error())
}
for {
runErrors := ProcessTemplateResources(etcdClient)
runErrors := template.ProcessTemplateResources(etcdClient)
// If the -onetime flag is passed on the command line we immediately exit
// after processing the template config files.
if onetime {
Expand All @@ -58,7 +64,14 @@ func main() {
}
os.Exit(0)
}
// By default we poll etcd every 30 seconds
time.Sleep(time.Duration(Interval()) * time.Second)
time.Sleep(time.Duration(config.Interval()) * time.Second)
}
}

// IsFileExist reports whether path exits.
func IsFileExist(fpath string) bool {
if _, err := os.Stat(fpath); os.IsNotExist(err) {
return false
}
return true
}
161 changes: 0 additions & 161 deletions config.go

This file was deleted.

Loading

0 comments on commit f4a8b3c

Please sign in to comment.