fix logging, reload unbound, return when no changes
This commit is contained in:
parent
72f3b6a833
commit
e0ba9a86af
23
syncer.py
23
syncer.py
|
@ -23,8 +23,6 @@ class WatchdogHandler(FileSystemEventHandler):
|
||||||
|
|
||||||
|
|
||||||
def file_modified(args):
|
def file_modified(args):
|
||||||
logger = logging.getLogger()
|
|
||||||
logger.setLevel(logging.INFO)
|
|
||||||
response = requests.get(args.opnsense + "/api/unbound/settings/searchHostOverride", auth=(args.api_key, args.api_secret), verify=args.insecure)
|
response = requests.get(args.opnsense + "/api/unbound/settings/searchHostOverride", auth=(args.api_key, args.api_secret), verify=args.insecure)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
|
@ -36,6 +34,10 @@ def file_modified(args):
|
||||||
to_remove = current_overrides - pihole_custom
|
to_remove = current_overrides - pihole_custom
|
||||||
to_add = pihole_custom - current_overrides
|
to_add = pihole_custom - current_overrides
|
||||||
|
|
||||||
|
if not len(to_remove) and not len(to_add):
|
||||||
|
logger.info('No changes.')
|
||||||
|
return
|
||||||
|
|
||||||
for item_ip, item_domain in to_remove:
|
for item_ip, item_domain in to_remove:
|
||||||
item_data = current_overrides_uuid[item_domain]
|
item_data = current_overrides_uuid[item_domain]
|
||||||
p = requests.post(args.opnsense + f"/api/unbound/settings/delHostOverride/{item_data['uuid']}", auth=(args.api_key, args.api_secret), verify=args.insecure)
|
p = requests.post(args.opnsense + f"/api/unbound/settings/delHostOverride/{item_data['uuid']}", auth=(args.api_key, args.api_secret), verify=args.insecure)
|
||||||
|
@ -65,6 +67,13 @@ def file_modified(args):
|
||||||
raise Exception(f'Failed to add item {item_domain}: {p.status_code} - {p.text}')
|
raise Exception(f'Failed to add item {item_domain}: {p.status_code} - {p.text}')
|
||||||
else:
|
else:
|
||||||
logger.info(f'Added: {item_domain}')
|
logger.info(f'Added: {item_domain}')
|
||||||
|
|
||||||
|
p_reload = requests.post(args.opnsense + "/api/unbound/service/reconfigure", auth=(args.api_key, args.api_secret), verify=args.insecure)
|
||||||
|
if p_reload.status_code != 200:
|
||||||
|
raise Exception(f'Failed to reload service: {p_reload.status_code} - {p_reload.text}')
|
||||||
|
else:
|
||||||
|
logger.info(f'Unbound reloaded.')
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise Exception(f"Failed to fetch host overrides: {response.status_code} - {response.text}")
|
raise Exception(f"Failed to fetch host overrides: {response.status_code} - {response.text}")
|
||||||
|
|
||||||
|
@ -78,14 +87,22 @@ if __name__ == '__main__':
|
||||||
parser.add_argument('--insecure', action='store_true', help="Don't verify SSL.")
|
parser.add_argument('--insecure', action='store_true', help="Don't verify SSL.")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
logging.basicConfig()
|
||||||
|
logger = logging.getLogger('syncer')
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
|
logger.info('Running initial sync...')
|
||||||
|
file_modified(args)
|
||||||
|
|
||||||
event_handler = WatchdogHandler(args)
|
event_handler = WatchdogHandler(args)
|
||||||
observer = Observer()
|
observer = Observer()
|
||||||
observer.schedule(event_handler, path='/etc/pihole/', recursive=False)
|
observer.schedule(event_handler, path='/etc/pihole/', recursive=False)
|
||||||
observer.start()
|
observer.start()
|
||||||
|
logger.info('Started watchdog.')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
time.sleep(1)
|
time.sleep(100)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
observer.stop()
|
observer.stop()
|
||||||
observer.join()
|
observer.join()
|
||||||
|
|
Loading…
Reference in New Issue