Enhancements: Upgraded Actions, Improved Logging, Restructured Project (#1)
* update python project use setup tools, and upgrade github action version * add manual run trigger * add check not found check * remove TODO as not possible * Update doc and add default value of non required config --------- Co-authored-by: Snigdhajyoti Ghosh <snigdhasjg@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
f601634857
commit
2c9b686062
1
docker_nsupdate_ddns/lib/__init__.py
Normal file
1
docker_nsupdate_ddns/lib/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
__all__ = ["container", "nsupdate"]
|
||||
66
docker_nsupdate_ddns/lib/container.py
Normal file
66
docker_nsupdate_ddns/lib/container.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import logging
|
||||
|
||||
import docker
|
||||
|
||||
config = {}
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
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']]['IPAddress']
|
||||
|
||||
return x
|
||||
|
||||
|
||||
def generate_container_list():
|
||||
client = docker.from_env()
|
||||
|
||||
container_list = client.containers.list()
|
||||
ipam4 = {}
|
||||
|
||||
for container in container_list:
|
||||
if config['IGNORE_LABEL'] in container.attrs['Config']['Labels']:
|
||||
LOG.debug(f"Ignoring container {container.attrs['Name']} as ignore label present")
|
||||
continue
|
||||
|
||||
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
|
||||
44
docker_nsupdate_ddns/lib/nsupdate.py
Normal file
44
docker_nsupdate_ddns/lib/nsupdate.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import logging
|
||||
|
||||
import dns.update
|
||||
import dns.query
|
||||
import dns.tsigkeyring
|
||||
import ipaddress
|
||||
|
||||
config = {}
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def add_records(records):
|
||||
keyring = dns.tsigkeyring.from_text({config['TSIG_NAME']: config['TSIG_KEY']})
|
||||
|
||||
for hostname, ip in records.items():
|
||||
LOG.info(f"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, int(config['DNS_RECORD_TTL']), 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():
|
||||
LOG.info(f"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