Working for real now

This commit is contained in:
Reid 'arrdem' McKenzie 2021-05-08 22:35:04 -06:00
parent b7e35bcc2a
commit 9dcbf0b90c
4 changed files with 25 additions and 49 deletions

View file

@ -2,7 +2,7 @@ package(default_visibility = ["//visibility:public"])
py_library( py_library(
name = "gandi", name = "gandi",
srcs = glob(["src/python/gandi/client.py"], []), srcs = glob(["src/python/gandi/client.py"]),
imports = [ imports = [
"src/python" "src/python"
], ],

View file

@ -4,43 +4,19 @@ A quick and dirty public DNS script, super tightly coupled to my infrastructure.
import sys import sys
import os import os
def module_not_found_helper(excepthook):
def _helper(type, value, traceback):
if isinstance(value, ModuleNotFoundError):
path_fragment = value.name.replace(".", os.path.sep)
flag = False
for e in sys.path:
init = os.path.join(e, path_fragment, "__init__.py")
namedfile = os.path.join(e, path_fragment + ".py")
if os.path.exists(init):
print(f"Found candidate {init}", file=sys.stderr)
flag |= True
else:
print(f"{init} does not exist")
if os.path.exists(namedfile):
print(f"Found candidate {namedfile}", file=sys.stderr)
flag |= True
else:
print(f"{namedfile} does not exist")
if not flag:
print(f"Found no candidates on the PYTHONPATH", file=sys.stderr)
excepthook(type, value, traceback)
return _helper
sys.excepthook = module_not_found_helper(sys.excepthook)
import argparse import argparse
import re import re
from pprint import pprint from pprint import pprint
for e in sys.path:
print(e)
from gandi.client import GandiAPI
import jinja2 import jinja2
import pkg_resources import pkg_resources
import yaml import yaml
import meraki
from gandi.client import GandiAPI
from meraki import meraki
RECORD_LINE_PATTERN = re.compile( RECORD_LINE_PATTERN = re.compile(
@ -59,8 +35,7 @@ def update(m, k, f, *args, **kwargs):
def parse_zone_record(line): def parse_zone_record(line):
match = RECORD_LINE_PATTERN.search(line) if match := RECORD_LINE_PATTERN.search(line):
if match:
dat = match.groupdict() dat = match.groupdict()
dat = update(dat, "rrset_ttl", int) dat = update(dat, "rrset_ttl", int)
dat = update(dat, "rrset_values", lambda x: [x]) dat = update(dat, "rrset_values", lambda x: [x])
@ -91,12 +66,12 @@ def records_equate(lr, rr):
return True return True
def template_and_parse_zone(template_name, template_bindings): def template_and_parse_zone(template_file, template_bindings):
assert template_name is not None assert template_file is not None
assert template_bindings is not None assert template_bindings is not None
dat = pkg_resources.resource_string("zonefiles", template_name).decode("utf-8") with open(template_file) as f:
dat = jinja2.Template(dat).render(**template_bindings) dat = jinja2.Template(f.read()).render(**template_bindings)
uncommitted_records = [] uncommitted_records = []
for line in dat.splitlines(): for line in dat.splitlines():
@ -154,18 +129,23 @@ def diff_zones(left_zone, right_zone):
parser = argparse.ArgumentParser(description="\"Dynamic\" DNS updating for self-hosted services") parser = argparse.ArgumentParser(description="\"Dynamic\" DNS updating for self-hosted services")
parser.add_argument("--config", dest="config_file") parser.add_argument("--config", dest="config_file", required=True)
parser.add_argument("--templates", dest="template_dir", required=True)
parser.add_argument("--dry-run", dest="dry", action="store_true", default=False) parser.add_argument("--dry-run", dest="dry", action="store_true", default=False)
def main(): def main():
args = parser.parse_args() args = parser.parse_args()
config = yaml.load(open(args.config_file, "r")) config = yaml.safe_load(open(args.config_file, "r"))
dashboard = meraki.DashboardAPI(config["meraki"]["key"])
net = config["meraki"]["network"]
org = config["meraki"]["organization"]
device = config["meraki"]["router_serial"]
uplinks = meraki.getdeviceuplink(config["meraki"]["key"], uplinks = dashboard.appliance.getOrganizationApplianceUplinkStatuses(
config["meraki"]["network"], organizationId=org,
config["meraki"]["router_serial"], serials=[device]
True) )[0]["uplinks"]
template_bindings = { template_bindings = {
"local": { "local": {
@ -183,7 +163,7 @@ def main():
task = {"template": task + ".j2", task = {"template": task + ".j2",
"zones": [task]} "zones": [task]}
computed_zone = template_and_parse_zone(task["template"], template_bindings) computed_zone = template_and_parse_zone(os.path.join(args.template_dir, task["template"]), template_bindings)
for zone_name in task["zones"]: for zone_name in task["zones"]:
try: try:

View file

@ -1,4 +0,0 @@
python_library(
name="zonefiles",
sources=globs("*.j2"),
)