浏览代码

more of udm-dns working

Breandan Dezendorf 2 年之前
父节点
当前提交
1d653443ad

+ 8 - 0
dezendorf/applications/udm-dns/Dockerfile

@@ -0,0 +1,8 @@
+FROM python:3
+
+ADD gopy.py /
+ADD requirements.txt /
+
+RUN pip3 install -r requirements.txt
+
+ENTRYPOINT ["python3", "udm-dns.py"]

+ 4 - 0
dezendorf/applications/udm-dns/requirements.txt

@@ -0,0 +1,4 @@
+unificontrol
+google-api-python-client
+google-auth-httplib2
+google-auth-oauthlib

+ 60 - 60
dezendorf/applications/udm-dns/udm-dns.py

@@ -1,11 +1,11 @@
 #!/usr/bin/env python
+from __future__ import print_function
 
 import re
 from datetime import timedelta
 import argparse
 import unificontrol
 
-from __future__ import print_function
 import os.path
 
 from google.auth.transport.requests import Request
@@ -19,9 +19,8 @@ import math
 SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
 SPREADSHEET_ID = ''
 
-class ClientEntry:
-  def __init__(self, entryNumber, hostname, address, macAddress, k8s):
-    self.entryNumber = entryNumber
+class Client:
+  def __init__(self, hostname, address, macAddress, k8s):
     self.hostname = hostname
     self.address = address
     self.macAddress = macAddress
@@ -31,70 +30,71 @@ class ClientEntry:
     entry = "    {} {} # {}\n".format(self.hostname, self.address, self.macAddress)
     return entry
 
-def main():
-  Subtitles = []
+def getClientsSheets(args):
+  print("SheetID: {}".format(args.sheetid))
+  c = Client(hostname="tesseract", address="192.168.1.87", macAddress="00:00:00:AA:BB:CC", k8s=False)
+  return c
+
+def getClientsUnifi(args):
+  c = Client(hostname="gondor", address="192.168.1.9", macAddress="00:00:00:AA:BB:CC", k8s=False)
+  return c
+
+def getClientsPihole(args):
+  c = Client(hostname="morgul", address="192.168.1.1", macAddress="00:00:00:AA:BB:CC", k8s=False)
+  return c
 
+def main():
   parser = argparse.ArgumentParser()
 
-  parser.add_argument("--k8s", required=False, help="Render k8s hostname yaml", action='store_true')
-  parser.add_argument("--hardware", required=False, help="Render hardware hostname yaml", action='store_true')
-  parser.add_argument("--yamlfile", required=False, help="Filename to store rendered yaml", type=str)
 
-  parser.add_argument("--unifi", required=False, help="Sync client addresses to unifi controller", action='store_true')
+  parser.add_argument("--pihole", required=False, help="Render k8s configMap for pihole use", action='store_true')
+  parser.add_argument("--pifile", required=False, help="Filename to store rendered yaml", type=str)
+
+  parser.add_argument("--unifi",   required=False, help="Sync client addresses to unifi controller", action='store_true')
+  parser.add_argument("--hostname", required=False, help="Unifi Hostname", type=str, default='192.168.1.1')
+  parser.add_argument("--username", required=False, help="Unifi Username", type=str, default='admin')
+  parser.add_argument("--password", required=False, help="Unifi Password", type=str, default='admin')
+
+  parser.add_argument("--sheet", required=False, help="Sync client address to Google Sheet", action='store_true')
+  parser.add_argument("--sheetid", required=False, help="Google Sheet ID to read from", type=str)
 
-  parser.add_argument("--sheet", required=True, help="Google Sheet ID to read from", type=str)
+  parser.add_argument("--readfrom", required=True, help="Which datasource to read from (unifi, pihole, sheets)", type=str, action='append')
+  parser.add_argument("--writeto", required=True, help="Which datasource to write to (unifi, pihole, sheets)", type=str, action='append')
 
+
+  clients = []
   args = parser.parse_args()
+  for read in args.readfrom:
+    results = []
+    print("Reading from {}".format(read))
+    if read == "sheets":
+      results.append(getClientsSheets(args))
+
+    if read == "unifi":
+      results.append(getClientsUnifi(args))
+    
+    if read == "pihole":
+      results.append(getClientsPihole(args))
+
+    for result in results:
+      for c in clients:
+        print("{}".format(c.address))
+      print("Merging {} results from {} into Clients".format(result.hostname, read))
+      clients.append(result)
+
+  print("Len: {}".format(len(clients)))
+  for c in clients:
+    print("Clients: {} {} {}".format(c.address, c.hostname, c.macAddress))
+
+  for write in args.writeto:
+    print("Writing to {}".format(write))
+    for c in clients:
+      print("Discovered Client: {}".format(c.hostname))
+      if write == "pihole":
+        print(c.formatYamlEntry)
+      
+
 
-  SPREADSHEET_ID = args.sheet
-
-  print("Opening gsheet: {}".format(args.sheet))
-
-  with open(args.file, "r+") as file:
-    entryNumber = 0
-    text = ""
-    timestamp = ""
-    for line in file:
-      m1 = re.match(r"^([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}) --> ([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})", line)
-      if line == "\n":
-        if (entryNumber > 0) and (timestamp is not None):
-          s = SubtitleEntry(entryNumber, text, timestamp.groups()[0], timestamp.groups()[1])
-          Subtitles.append(s)
-          text = ""
-          timestamp = ""
-          entryNumber = 0
-      elif re.match(r"^[0-9]+$", line):
-        entryNumber = int(line.rstrip())
-      elif m1:
-        timestamp = m1
-      else:
-        text += line
-
-  if args.inplace == True:
-    args.outfile = args.file
-    args.file = args.file + ".backup"
-    with open(args.file, 'w') as o:
-      print("Writing backup to " + args.file)
-      for entry in Subtitles:
-        print(entry.formatEntry(), file=o)
-
-  print("Updating subtitle file: " + args.outfile)
-  lastEntryNumber = Subtitles[-1].entryNumber
-  with open(args.outfile, 'w') as o:
-    print("Shifting all entries by {} seconds".format(args.seconds))
-    for idx, entry in enumerate(Subtitles):
-      if idx == 0:
-        print("First subtitle line: {}".format(entry.text))
-        if args.strip_first and entry.text != "":
-          print("Stripping first line - {}".format(entry.text))
-          entry.text = ""
-      if idx == lastEntryNumber - 1:
-        print("Last subtitle line: {}".format(entry.text))
-        if args.strip_last and entry.text != "":
-          print("Stripping last line - {}".format(entry.text))
-          entry.text = ""
-      entry.shift(args.seconds)
-      print(entry.formatEntry(), file=o)
 
 if __name__ == "__main__":
   main()