Initial version
This commit is contained in:
1
lib/__init__.py
Normal file
1
lib/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
__all__ = ["container", "nsupdate"]
|
||||
57
lib/container.py
Normal file
57
lib/container.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import docker
|
||||
|
||||
config = {}
|
||||
|
||||
|
||||
def get_container_name(container):
|
||||
"""
|
||||
Get name of container, try in the following order:
|
||||
- Check if hostname_label is set
|
||||
- Fall back to container Name
|
||||
"""
|
||||
x = container.attrs['Name'][1:]
|
||||
|
||||
if config['hostname_label'] in container.attrs['Config']['Labels']:
|
||||
x = container.attrs['Config']['Labels'][config['hostname_label']]
|
||||
|
||||
x = x.replace("_", "-") # Be compliant with RFC1035
|
||||
return x
|
||||
|
||||
|
||||
def get_container_ip(container):
|
||||
"""
|
||||
Get IP of container. Try in the following order
|
||||
- default_network
|
||||
- First found network
|
||||
- Fall back to ['NetworkSettings']['IPAddress']
|
||||
"""
|
||||
x = container.attrs['NetworkSettings']['IPAddress']
|
||||
|
||||
if next(iter(container.attrs['NetworkSettings']['Networks'])):
|
||||
network_name = next(iter(container.attrs['NetworkSettings']['Networks']))
|
||||
x = container.attrs['NetworkSettings']['Networks'][network_name]['IPAddress']
|
||||
|
||||
if config['default_network'] in container.attrs['NetworkSettings']['Networks']:
|
||||
x = container.attrs['NetworkSettings']['Networks'][config['default_network']]
|
||||
|
||||
return x
|
||||
|
||||
|
||||
def generate_container_list():
|
||||
client = docker.from_env()
|
||||
|
||||
container_list = client.containers.list()
|
||||
ipam4 = {}
|
||||
|
||||
for container in container_list:
|
||||
container_name = get_container_name(container)
|
||||
container_ip = get_container_ip(container)
|
||||
if container_ip:
|
||||
ipam4[container_name] = container_ip
|
||||
|
||||
return ipam4
|
||||
|
||||
|
||||
def init(_config):
|
||||
global config
|
||||
config = _config
|
||||
37
lib/nsupdate.py
Normal file
37
lib/nsupdate.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import dns.update
|
||||
import dns.query
|
||||
import dns.tsigkeyring
|
||||
import ipaddress
|
||||
|
||||
config = {}
|
||||
|
||||
|
||||
def add_records(records):
|
||||
keyring = dns.tsigkeyring.from_text({config['tsig_name']: config['tsig_key']})
|
||||
for hostname, ip in records.items():
|
||||
print("Adding record for " + hostname + "(" + ip + ")")
|
||||
|
||||
rrtype = "A"
|
||||
address = ipaddress.ip_address(ip)
|
||||
if isinstance(address, ipaddress.IPv4Address):
|
||||
rrtype = "A"
|
||||
if isinstance(address, ipaddress.IPv6Address):
|
||||
rrtype = "AAAA"
|
||||
|
||||
update = dns.update.Update(config['domain'], keyring=keyring)
|
||||
update.add(hostname, 60, rrtype, ip)
|
||||
dns.query.tcp(update, config['nameserver'], timeout=2)
|
||||
|
||||
|
||||
def delete_records(records):
|
||||
keyring = dns.tsigkeyring.from_text({config['tsig_name']: config['tsig_key']})
|
||||
for hostname, ip in records.items():
|
||||
print("Deleting record for " + hostname + "(" + ip + ")")
|
||||
update = dns.update.Update(config['domain'], keyring=keyring)
|
||||
update.delete(hostname)
|
||||
dns.query.tcp(update, config['nameserver'], timeout=2)
|
||||
|
||||
|
||||
def init(_config):
|
||||
global config
|
||||
config = _config
|
||||
Reference in New Issue
Block a user