file change on agent
This commit is contained in:
parent
6217874cb0
commit
eb99a1718b
3 changed files with 182 additions and 45 deletions
108
app.py
108
app.py
|
@ -10,6 +10,9 @@ from dotenv import load_dotenv
|
|||
import re
|
||||
import signal
|
||||
import sys
|
||||
import time
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
@ -51,6 +54,29 @@ caddy_proxies = {} # Server name -> {domain: target}
|
|||
nginx_proxies = {} # Server name -> {domain: target}
|
||||
timestamps = {} # Server name -> timestamp
|
||||
|
||||
# File change monitoring
|
||||
file_observer = None
|
||||
|
||||
class ConfigFileHandler(FileSystemEventHandler):
|
||||
"""Watch for changes to configuration files and update data"""
|
||||
def on_modified(self, event):
|
||||
if USE_LOCAL_CADDYFILE and event.src_path == CADDYFILE_PATH:
|
||||
logger.info(f"Local Caddyfile changed, updating entries")
|
||||
entries = parse_local_caddyfile()
|
||||
if entries:
|
||||
caddy_proxies[LOCAL_SERVER_NAME] = entries
|
||||
timestamps[LOCAL_SERVER_NAME] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
logger.info(f"Updated {len(entries)} entries from local Caddyfile")
|
||||
|
||||
# For Nginx, we need to check if the modified file is in the nginx config directory
|
||||
if USE_LOCAL_NGINX and NGINX_CONFIG_PATH in event.src_path and event.src_path.endswith('.conf'):
|
||||
logger.info(f"Local Nginx config changed, updating entries")
|
||||
entries = parse_nginx_configs()
|
||||
if entries:
|
||||
nginx_proxies[f"{LOCAL_SERVER_NAME} Nginx"] = entries
|
||||
timestamps[f"{LOCAL_SERVER_NAME} Nginx"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
logger.info(f"Updated {len(entries)} entries from local Nginx configs")
|
||||
|
||||
def verify_token(token):
|
||||
"""Verify the JWT token from an agent"""
|
||||
if not API_KEY:
|
||||
|
@ -266,42 +292,72 @@ def delete_server():
|
|||
logger.info(f"Deleted server: {server_name}")
|
||||
return jsonify({"status": "success", "message": f"Server {server_name} deleted"})
|
||||
|
||||
# Initialize with local files if available
|
||||
def start_file_monitoring():
|
||||
"""Start monitoring configuration files for changes"""
|
||||
global file_observer
|
||||
|
||||
if not (USE_LOCAL_CADDYFILE or USE_LOCAL_NGINX):
|
||||
logger.info("No local configuration files to monitor")
|
||||
return
|
||||
|
||||
event_handler = ConfigFileHandler()
|
||||
file_observer = Observer()
|
||||
|
||||
if USE_LOCAL_CADDYFILE:
|
||||
# Monitor the Caddyfile
|
||||
file_observer.schedule(
|
||||
event_handler,
|
||||
path=os.path.dirname(CADDYFILE_PATH),
|
||||
recursive=False
|
||||
)
|
||||
logger.info(f"Monitoring local Caddyfile at {CADDYFILE_PATH}")
|
||||
|
||||
if USE_LOCAL_NGINX:
|
||||
# Monitor Nginx config directory
|
||||
file_observer.schedule(
|
||||
event_handler,
|
||||
path=NGINX_CONFIG_PATH,
|
||||
recursive=True # Monitor all subdirectories too
|
||||
)
|
||||
logger.info(f"Monitoring local Nginx configs at {NGINX_CONFIG_PATH}")
|
||||
|
||||
file_observer.start()
|
||||
logger.info("File monitoring started")
|
||||
|
||||
# Add cleanup function for graceful shutdown
|
||||
def stop_file_monitoring():
|
||||
"""Stop file monitoring"""
|
||||
if file_observer:
|
||||
file_observer.stop()
|
||||
file_observer.join()
|
||||
logger.info("File monitoring stopped")
|
||||
|
||||
# Enhanced signal handler
|
||||
def signal_handler(sig, frame):
|
||||
logger.info("Shutdown signal received, exiting gracefully...")
|
||||
stop_file_monitoring()
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
# Initialize with local files and start monitoring
|
||||
if USE_LOCAL_CADDYFILE:
|
||||
entries = parse_local_caddyfile()
|
||||
if entries:
|
||||
caddy_proxies[LOCAL_SERVER_NAME] = entries # Use LOCAL_SERVER_NAME for local configs
|
||||
caddy_proxies[LOCAL_SERVER_NAME] = entries
|
||||
timestamps[LOCAL_SERVER_NAME] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
logger.info(f"Loaded {len(entries)} entries from local Caddyfile")
|
||||
|
||||
if USE_LOCAL_NGINX:
|
||||
entries = parse_nginx_configs()
|
||||
if entries:
|
||||
nginx_proxies[f"{LOCAL_SERVER_NAME} Nginx"] = entries # Use LOCAL_SERVER_NAME for Nginx too
|
||||
nginx_proxies[f"{LOCAL_SERVER_NAME} Nginx"] = entries
|
||||
timestamps[f"{LOCAL_SERVER_NAME} Nginx"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
logger.info(f"Loaded {len(entries)} entries from local Nginx configs")
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
logger.info("Shutdown signal received, exiting gracefully...")
|
||||
sys.exit(0)
|
||||
# Start file monitoring after initializing
|
||||
start_file_monitoring()
|
||||
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
if __name__ == '__main__':
|
||||
if USE_LOCAL_CADDYFILE:
|
||||
logger.info(f"Local Caddyfile found at {CADDYFILE_PATH} - will display its data")
|
||||
# Load it initially
|
||||
local_data = parse_local_caddyfile()
|
||||
if local_data:
|
||||
caddy_proxies[LOCAL_SERVER_NAME] = local_data
|
||||
timestamps[LOCAL_SERVER_NAME] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
if not API_KEY:
|
||||
logger.warning("API_KEY not set - running without authentication!")
|
||||
|
||||
# Use HTTPS in production
|
||||
if DEBUG_MODE:
|
||||
app.run(host='0.0.0.0', port=5000, debug=True)
|
||||
else:
|
||||
app.run(host='0.0.0.0', port=5000)
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=5000, debug=DEBUG_MODE)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue