wip
This commit is contained in:
parent
82b2885576
commit
fd5840df94
5 changed files with 394 additions and 104 deletions
51
agent.py
51
agent.py
|
@ -25,6 +25,7 @@ SERVER_NAME = os.getenv('SERVER_NAME', socket.gethostname())
|
|||
API_KEY = os.getenv('API_KEY')
|
||||
CHECK_INTERVAL = int(os.getenv('CHECK_INTERVAL', '60'))
|
||||
VERIFY_SSL = os.getenv('VERIFY_SSL', 'true').lower() == 'true'
|
||||
SERVER_TYPE = os.getenv('SERVER_TYPE', 'caddy').lower() # 'caddy' or 'nginx'
|
||||
|
||||
# Setup logging
|
||||
logging.basicConfig(
|
||||
|
@ -34,10 +35,11 @@ logging.basicConfig(
|
|||
logger = logging.getLogger('caddy-agent')
|
||||
|
||||
# Debug configuration
|
||||
logger.info(f"Starting Caddy agent with configuration:")
|
||||
logger.info(f"Starting {SERVER_TYPE} agent with configuration:")
|
||||
logger.info(f"- DASHBOARD_URL: {DASHBOARD_URL}")
|
||||
logger.info(f"- SERVER_NAME: {SERVER_NAME}")
|
||||
logger.info(f"- CADDYFILE_PATH: {CADDYFILE_PATH}")
|
||||
logger.info(f"- SERVER_TYPE: {SERVER_TYPE}")
|
||||
logger.info(f"- VERIFY_SSL: {VERIFY_SSL}")
|
||||
logger.info(f"- API_KEY set: {'Yes' if API_KEY else 'No'}")
|
||||
|
||||
|
@ -101,19 +103,21 @@ def parse_caddyfile():
|
|||
def create_auth_token():
|
||||
"""Create a JWT token for authentication"""
|
||||
if not API_KEY:
|
||||
logger.error("API_KEY is not set. Authentication will fail.")
|
||||
logger.error("Cannot create authentication token: API_KEY not set")
|
||||
return None
|
||||
|
||||
# Create a token that expires in 5 minutes
|
||||
# Create token with 5 minute expiry
|
||||
payload = {
|
||||
'server': SERVER_NAME,
|
||||
'exp': datetime.now() + timedelta(minutes=5)
|
||||
"exp": datetime.utcnow() + timedelta(minutes=5),
|
||||
"iat": datetime.utcnow(),
|
||||
"sub": SERVER_NAME
|
||||
}
|
||||
|
||||
try:
|
||||
return jwt.encode(payload, API_KEY, algorithm='HS256')
|
||||
token = jwt.encode(payload, API_KEY, algorithm="HS256")
|
||||
return token
|
||||
except Exception as e:
|
||||
logger.error(f"Error creating authentication token: {e}")
|
||||
logger.error(f"Error creating JWT token: {e}")
|
||||
return None
|
||||
|
||||
def send_update(force=False):
|
||||
|
@ -134,7 +138,8 @@ def send_update(force=False):
|
|||
data = {
|
||||
"server": SERVER_NAME,
|
||||
"entries": current_data,
|
||||
"timestamp": current_time.isoformat()
|
||||
"timestamp": current_time.isoformat(),
|
||||
"type": SERVER_TYPE # Add the server type
|
||||
}
|
||||
|
||||
# Create authentication token
|
||||
|
@ -166,39 +171,33 @@ def send_update(force=False):
|
|||
except requests.exceptions.RequestException as e:
|
||||
logger.error(f"Connection error sending update: {e}")
|
||||
|
||||
class CaddyfileHandler(FileSystemEventHandler):
|
||||
"""Watch for changes to the Caddyfile"""
|
||||
|
||||
class CaddyFileHandler(FileSystemEventHandler):
|
||||
"""Watch for changes to the Caddyfile and send updates"""
|
||||
def on_modified(self, event):
|
||||
if event.src_path == CADDYFILE_PATH:
|
||||
logger.info(f"Caddyfile modified: {event.src_path}")
|
||||
logger.info(f"Caddyfile changed, sending update")
|
||||
send_update()
|
||||
|
||||
def main():
|
||||
"""Main function to run the agent"""
|
||||
logger.info(f"Starting Caddy agent for {SERVER_NAME}")
|
||||
logger.info(f"Monitoring Caddyfile at: {CADDYFILE_PATH}")
|
||||
logger.info(f"Dashboard URL: {DASHBOARD_URL}")
|
||||
|
||||
# Initial update
|
||||
"""Main function to start the agent"""
|
||||
# Send initial update
|
||||
send_update(force=True)
|
||||
|
||||
# Setup file watching
|
||||
# Set up file watching
|
||||
event_handler = CaddyFileHandler()
|
||||
observer = Observer()
|
||||
event_handler = CaddyfileHandler()
|
||||
|
||||
# Watch the directory containing the Caddyfile
|
||||
caddyfile_dir = os.path.dirname(CADDYFILE_PATH)
|
||||
observer.schedule(event_handler, caddyfile_dir, recursive=False)
|
||||
observer.schedule(event_handler, path=os.path.dirname(CADDYFILE_PATH), recursive=False)
|
||||
observer.start()
|
||||
|
||||
try:
|
||||
logger.info(f"Agent started. Watching {CADDYFILE_PATH} for changes")
|
||||
while True:
|
||||
# Periodic check even if file doesn't change
|
||||
time.sleep(CHECK_INTERVAL)
|
||||
# Send periodic updates
|
||||
send_update()
|
||||
time.sleep(CHECK_INTERVAL)
|
||||
except KeyboardInterrupt:
|
||||
observer.stop()
|
||||
|
||||
observer.join()
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue