Skip to content

Instantly share code, notes, and snippets.

@manics
Last active April 29, 2016 21:40
Show Gist options
  • Save manics/7ecc2e3f4edfbd24c0bb5f94c709a7ff to your computer and use it in GitHub Desktop.
Save manics/7ecc2e3f4edfbd24c0bb5f94c709a7ff to your computer and use it in GitHub Desktop.
Update Etcd SkyDNS with OpenStack instances

Openstack Etcd SkyDNS

A very simple polling agent that updates an Etcd database with SkyDNS entries for all OpenStack instances in a tenancy.

Example

docker run -d --name etcd quay.io/coreos/etcd \
    --listen-client-urls http://0.0.0.0:2379 \
    --advertise-client-urls http://0.0.0.0:2379 --debug
docker run -d --name skydns --link etcd:etcd skynetservices/skydns \
    -domain skydns.local -machines http://etcd:2379 -addr 0.0.0.0:53

source openrc.sh # Setup OpenStack environment variables
python openstack-etcd-dns.py --domain skydns.local --etcd etcd:2379
#!/usr/bin/env python
from argparse import ArgumentParser
from datetime import datetime
from etcd.client import Client
from re import sub
from sched import scheduler
from shade import openstack_cloud
import time
# Assumes OS_* connection variables have been set in the environment
p = ArgumentParser()
p.add_argument('--domain', default='local')
p.add_argument('--etcd', default='localhost:2379')
p.add_argument('--interval', type=int, default=30)
p.add_argument('--ttl', type=int, default=60)
p.add_argument('--ip', choices=("public", "private"), default="public")
args = p.parse_args()
osc = openstack_cloud()
try:
etcdh, etcdp = args.etcd.split(':')
etcdp = int(etcdp)
ec = Client(host=etcdh, port=etcdp)
except ValueError:
ec = Client(host=args.etcd)
assert args.domain, 'Domain required'
assert args.interval > 0, 'Interval must be > 0'
assert args.ttl > 0, 'TTL must be > 0'
keyprefix = '/skydns/' + '/'.join(reversed(args.domain.split('.')))
updater = scheduler(time.time, time.sleep)
def update_etcd():
if updater.empty():
updater.enter(args.interval, 1, update_etcd, ())
else:
print 'WARNING: Scheduler queue is non-empty'
for s in osc.list_servers():
name = sub('[^a-zA-Z0-9]', '-', s.name)
if args.ip == "private":
ip = s.private_v4
else:
ip = s.public_v4
if ip:
r = ec.write(
keyprefix + '/' + name, '{"host":"%s"}' % ip, ttl=args.ttl)
print '%s %s=%s' % (
datetime.isoformat(datetime.now()), r.key, r.value)
updater.run()
if __name__ == '__main__':
update_etcd()
python-etcd
shade
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment