|
| 1 | +import os |
| 2 | +import re |
| 3 | +import time |
| 4 | +import docker |
| 5 | +import CloudFlare |
| 6 | + |
| 7 | +__author__ = "David Chidell" |
| 8 | + |
| 9 | +class TraefikUpdater: |
| 10 | + def __init__(self): |
| 11 | + self.target_domain = os.environ['TARGET_DOMAIN'] |
| 12 | + excluded = os.environ.get('EXCLUDED_DOMAINS') |
| 13 | + if excluded is None: |
| 14 | + self.excluded_domains = [] |
| 15 | + else: |
| 16 | + self.excluded_domains = excluded.split(',') |
| 17 | + |
| 18 | + self.tld_info = {} |
| 19 | + self.get_domain_vars() |
| 20 | + |
| 21 | + self.host_pattern = re.compile('\`([a-zA-Z0-9\.]+)\`') |
| 22 | + |
| 23 | + self.cf = CloudFlare.CloudFlare(email=os.environ['CF_EMAIL'] , token=os.environ['CF_TOKEN']) |
| 24 | + self.dkr = docker.from_env() |
| 25 | + |
| 26 | + def enter_update_loop(self): |
| 27 | + print(f'Listening for new containers...') |
| 28 | + t = int(time.time()) |
| 29 | + for event in self.dkr.events(since=t, filters={'status': 'start'}, decode=True): |
| 30 | + if event.get('status') == 'start': |
| 31 | + try: |
| 32 | + container = self.dkr.containers.get(event.get('id')) |
| 33 | + except docker.errors.NotFound as e: |
| 34 | + pass |
| 35 | + else: |
| 36 | + if container.labels.get("traefik.enable",'False').upper() == "TRUE": |
| 37 | + print(f'New container online: {container.name}, processing...') |
| 38 | + self.process_container(container) |
| 39 | + |
| 40 | + def process_containers(self): |
| 41 | + containers = self.dkr.containers.list(filters={"status":"running","label":"traefik.enable=true"}) |
| 42 | + print(f'Found {len(containers)} existing containers to process') |
| 43 | + for container in containers: |
| 44 | + self.process_container(container) |
| 45 | + print(f'Finished bulk updating containers!') |
| 46 | + |
| 47 | + def process_container(self,container): |
| 48 | + for label, value in container.labels.items(): |
| 49 | + if 'rule' in label and 'Host' in value: |
| 50 | + domains = self.host_pattern.findall(value) |
| 51 | + print(f'Found domains: {domains} for container: {container.name}') |
| 52 | + for domain in domains: |
| 53 | + self.update_domain(domain) |
| 54 | + |
| 55 | + def update_domain(self,domain): |
| 56 | + dom_split = domain.split('.') |
| 57 | + if len(dom_split) >= 3: |
| 58 | + tld = '.'.join(dom_split[1:]) |
| 59 | + else: |
| 60 | + tld = domain |
| 61 | + |
| 62 | + if self.tld_info.get(tld) is None: |
| 63 | + print(f'TLD {tld} not in updatable list') |
| 64 | + return False |
| 65 | + |
| 66 | + dom_info = self.tld_info[tld] |
| 67 | + common_dict = {i: dom_info[i] for i in ('type', 'content', 'proxied')} |
| 68 | + post_dict = {**{'name':domain},**common_dict} |
| 69 | + try: |
| 70 | + get_records = self.cf.zones.dns_records.get(dom_info['zone'], params={'name':domain}) |
| 71 | + if len(get_records) == 0: |
| 72 | + post_record = self.cf.zones.dns_records.post(dom_info['zone'], data=post_dict) |
| 73 | + print(f'New record created: {domain}') |
| 74 | + else: |
| 75 | + for record in get_records: |
| 76 | + post_record = self.cf.zones.dns_records.put(dom_info['zone'], record['id'], data=post_dict) |
| 77 | + print(f'Existing record updated: {domain}') |
| 78 | + |
| 79 | + except CloudFlare.exceptions.CloudFlareAPIError as e: |
| 80 | + print(f'API call failed: {str(e)}') |
| 81 | + return False |
| 82 | + return True |
| 83 | + |
| 84 | + def get_domain_vars(self): |
| 85 | + tld_count = 0 |
| 86 | + self.tld_info = {} |
| 87 | + while True: |
| 88 | + tld_count += 1 |
| 89 | + try: |
| 90 | + domain = os.environ[f'DOMAIN{tld_count}'] |
| 91 | + zone = os.environ[f'DOMAIN{tld_count}_ZONE_ID'] |
| 92 | + try: |
| 93 | + proxied = os.environ.get(f'DOMAIN{tld_count}_PROXIED',"TRUE").upper() == 'TRUE' |
| 94 | + except KeyError: |
| 95 | + proxied = false |
| 96 | + self.tld_info[domain] = {"zone":zone, "proxied":proxied, "type":"CNAME", "content":self.target_domain} |
| 97 | + except KeyError: |
| 98 | + break |
| 99 | + print(f'Found {tld_count-1} TLDs! {self.tld_info}') |
0 commit comments