add reload button

This commit is contained in:
matt committed 2026-11-03 03:01:00 -08:00
1 parent 910555f363
commit b642f2af7d
4 files changed
+217 -9

No files matched your search

+42 -4
View File
@@ -1,13 +1,27 @@
import yaml
import re
from datetime import datetime
from flask import Flask, request, jsonify
from flask import Flask, request, jsonify, abort
import threading
app = Flask(__name__)
### adding reload route
# Global cache and lock
data = None
_data_lock = threading.Lock()
def load_yaml():
"""Load the YAML file and return its contents."""
with open('{{ gpo_yaml_path }}', 'r', encoding='utf-8') as f:
return yaml.safe_load(f)
# Load GPO data
with open('{{ gpo_yaml_path }}', 'r') as file:
data = yaml.safe_load(file)
with _data_lock:
data = load_yaml()
# O.G. method
#with open('{{ gpo_yaml_path }}', 'r') as file:
# data = yaml.safe_load(file)
def strip_dn(dn: str, base_suffix: str = ",OU=Manufacturing,OU=Tesla Systems,DC=teslamotors,DC=com") -> str:
suffix = base_suffix.strip()
@@ -126,6 +140,30 @@ def linked_ous():
return jsonify(linked)
# reload index route
@app.route('/reload_index', methods=['GET'])
def reload_index():
try:
new_data = load_yaml()
except Exception as exc:
# Log the exception if you have a logger; here we just abort
abort(500, description=f'Failed to load YAML: {exc}')
# Thread‑safe write to the global cache
with _data_lock:
global data
data = new_data
# Return the re‑loaded data (or some confirmation)
return jsonify({
'status': 'reloaded',
'gpo_links': data[0]['gpo_links']
})
# with open('{{ gpo_yaml_path }}', 'r') as file:
# data = yaml.safe_load(file)
# return jsonify({'status': 'reloaded', 'data': data})
# test route
@app.route('/test', methods=['GET'])
def test():
@@ -137,7 +175,7 @@ def test():
except TypeError:
# Handle cases where data might be of a different type than expected
return jsonify({"error": "TypeError: Data is not a dictionary"}), 400
return jsonify({"result": "test", "OU_Root": root_ou})
return jsonify({"result": "test", "OU_Root": root_ou, "gpo_links": data[0]['gpo_links']})
if __name__ == '__main__':
app.run(debug=False, host='{{ api_service_bind_ip }}', port={{ api_service_port }})