Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick Start Guide for confd using the "file" backend

Quick Start Guide for confd

from https://github.com/kelseyhightower/confd/blob/master/docs/quick-start-guide.md

Select a backend

I have selected for you. You are using the file backend. I found the original guide to be confusing because all the backend specifics were on one page and the scrolling made it hard to follow. Sometimes, less is more.

Install

Before we begin be sure to download and install confd.

if ! which confd; then
    if [[ $(uname) == "Darwin" ]]; then
        url=https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-darwin-amd64
    else
        url=https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64
    fi
    curl -sLo /usr/local/bin/confd $url
    chmod +x /usr/local/bin/confd
fi

Add keys

myapp.yaml

myapp:
  database:
    url: db.example.com
    user: rob

Create the confdir

The confdir is where template resource configs and source templates are stored.

sudo mkdir -p /etc/confd/{conf.d,templates}

Create a template resource config

Template resources are defined in TOML config files under the confdir.

/etc/confd/conf.d/myconfig.toml

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

Create the source template

Source templates are Golang text templates.

/etc/confd/templates/myconfig.conf.tmpl

[myconfig]
database_url = {{getv "/myapp/database/url"}}
database_user = {{getv "/myapp/database/user"}}

Process the template

confd supports two modes of operation daemon and onetime. In daemon mode confd polls a backend for changes and updates destination configuration files if necessary.

# defaults to confdir /etc/confd
confd -onetime -backend file -file myapp.yaml
# or
confd -onetime -backend file -file myapp.yaml -confdir ./confd

Demo script

./confd_demo.sh

#!/bin/bash

# get confd
if ! which confd >/dev/null; then
    if [[ $(uname) == "Darwin" ]]; then
            url=https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-darwin-amd64
	else
            url=https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64
	fi
	curl -sLo /usr/local/bin/confd $url
    chmod +x /usr/local/bin/confd
fi

# create configuration
mkdir -p confd/{conf.d,templates}
cat >confd/conf.d/myconfig.toml <<EOF
[template]
src = "myconfig.conf.tmpl"
dest = "/tmp/myconfig.conf"
keys = [
    "/myapp/database/url",
    "/myapp/database/user",
]
EOF
cat >confd/templates/myconfig.conf.tmpl <<EOF
[myconfig]
database_url = {{getv "/myapp/database/url"}}
database_user = {{getv "/myapp/database/user"}}
EOF
cat >confd/myapp.yaml <<EOF
myapp:
  database:
    url: db.example.com
    user: rob
EOF
cat >confd/myapp.json <<EOF
{
  "myapp": {
    "database": {
      "url": "db.example.com",
      "user": "rob"
    }
  }
}
EOF

# turn on feedback
set -o xtrace

# run confd on yaml file
rm -f /tmp/myconfig.conf
confd -onetime -backend file -file confd/myapp.yaml -confdir ./confd

# verify
cat /tmp/myconfig.conf

# run confd on json file
rm -f /tmp/myconfig.conf
confd -onetime -backend file -file confd/myapp.json -confdir ./confd

# verify
cat /tmp/myconfig.conf

output

$ ./confd_demo.sh
+ rm -f /tmp/myconfig.conf
+ confd -onetime -backend file -file confd/myapp.yaml -confdir ./confd
2019-10-17T20:05:56-04:00 ip-192-168-0-101.ec2.internal confd[30131]: INFO Backend set to file
2019-10-17T20:05:56-04:00 ip-192-168-0-101.ec2.internal confd[30131]: INFO Starting confd
2019-10-17T20:05:56-04:00 ip-192-168-0-101.ec2.internal confd[30131]: INFO Backend source(s) set to confd/myapp.yaml
2019-10-17T20:05:56-04:00 ip-192-168-0-101.ec2.internal confd[30131]: INFO Target config /tmp/myconfig.conf out of sync
2019-10-17T20:05:56-04:00 ip-192-168-0-101.ec2.internal confd[30131]: INFO Target config /tmp/myconfig.conf has been updated
+ cat /tmp/myconfig.conf
[myconfig]
database_url = db.example.com
database_user = rob
+ rm -f /tmp/myconfig.conf
+ confd -onetime -backend file -file confd/myapp.json -confdir ./confd
2019-10-17T20:05:56-04:00 ip-192-168-0-101.ec2.internal confd[30134]: INFO Backend set to file
2019-10-17T20:05:56-04:00 ip-192-168-0-101.ec2.internal confd[30134]: INFO Starting confd
2019-10-17T20:05:56-04:00 ip-192-168-0-101.ec2.internal confd[30134]: INFO Backend source(s) set to confd/myapp.json
2019-10-17T20:05:56-04:00 ip-192-168-0-101.ec2.internal confd[30134]: INFO Target config /tmp/myconfig.conf out of sync
2019-10-17T20:05:56-04:00 ip-192-168-0-101.ec2.internal confd[30134]: INFO Target config /tmp/myconfig.conf has been updated
+ cat /tmp/myconfig.conf
[myconfig]
database_url = db.example.com
database_user = rob

Rapid prototyping

After running the confd_demo.sh you can begin rapid prototyping via:

cat >confd/confd.toml <<EOF
confdir = "./confd"
backend = "file"
file = ["./confd/myapp.json"]
interval = 2
EOF

confd -config-file confd/confd.toml &

if [[ $(uname) == "Darwin" ]]; then
    fswatch /tmp/myconfig.conf | xargs -n 1 cat
else
    inotifywait -m -q --format '%w' -e close_write /tmp/myconfig.conf | xargs -n1 cat
fi

Then you can edit your ./confd/myapp.json and ./confd/templates/myconfig.conf.tmpl and watch the changes happen every 2 (the interval value in confd/confd.toml) seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.