Skip to content

Instantly share code, notes, and snippets.

@ashw7n
Created February 15, 2015 21:02
Show Gist options
  • Save ashw7n/75e474c82dfa9bdc2aba to your computer and use it in GitHub Desktop.
Save ashw7n/75e474c82dfa9bdc2aba to your computer and use it in GitHub Desktop.
confd

Run etcd as follows (I use boot2dokcer, so i get the ip first and let etcd use it for its addr parameter)

BIP=$(boot2docker ip)
docker run -t -i -p 4001:4001  quay.io/coreos/etcd -addr $BIP:4001

#update etcd with some initial values
curl http://$BIP:4001/v2/keys/myapp/database/url -XPUT -d value="cs:///org/c/mynewsql/members"
curl http://$BIP:4001/v2/keys/myapp/database/user -XPUT -d value="newurl//ashwin"

Get the latest version of confd from here (https://github.com/kelseyhightower/confd/releases). Confd support was added in version 0.7.0

confd -watch=true -backend=etcd -verbose=true -node=$BIP:4001

Update etcd with these values to see confd picking up the changes.

curl http://$BIP:4001/v2/keys/myapp/database/url -XPUT -d value="cs:///org/c/oldsql/members"
[template]
src = "myconfig.conf.tmpl"
dest = "/tmp/myconfig.conf"
keys = [
"/myapp/database",
]
check_cmd = "/etc/confd/do_check.py check_cmd {{.src}}"
reload_cmd = "/etc/confd/do_check.py reload_cmd /tmp/myconfig.conf"
[myconfig]
database_url = {{getv "/myapp/database/url"}}
database_user = {{getv "/myapp/database/user"}}
#! /usr/bin/python
import sys
import ConfigParser
import os
#This is an exmaple to work with confd. Please see the accompanying confd
# configuration also stored in file at '/etc/confd/conf.d/myconfig.toml'
if __name__ == '__main__':
if len(sys.argv) < 3:
print("Usage: %s check_cmd|reload_cmd conf_file" % (sys.argv[0]))
sys.exit()
run_type = sys.argv[1]
conf_file = sys.argv[2]
print("Got type = %s, file = %s " % (run_type, conf_file))
config = ConfigParser.ConfigParser()
config.read(conf_file)
print(config.get("myconfig", "database_url"))
if "check_cmd" == run_type:
# Note that check_cmd is passed a temp file by confd, it this check
# succeeds,
# config is parsed, so that consitute sa a good check
sys.exit(0)
else if "reload_cmd" == run_type:
# if we have to do something on a config change, reload here
# In this example, we are just writing the configuration in to new file
with open("/tmp/test_%s_out.txt" % run_type, "w") as file:
file.write(config.get("myconfig", "database_url"))
else:
sys.exit(100) # dont know what to do
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment