From c23f729141245751be180f8024b5e2a40b397fb2 Mon Sep 17 00:00:00 2001 From: shroominic Date: Mon, 30 Jun 2025 12:15:49 +0000 Subject: [PATCH 1/7] auto update script --- .gitignore | 3 ++ scripts/auto_update.sh | 75 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100755 scripts/auto_update.sh diff --git a/.gitignore b/.gitignore index e6ba9261..4177ffa7 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ compose.override.yml # Coverage .coverage + +# deployment +*.log diff --git a/scripts/auto_update.sh b/scripts/auto_update.sh new file mode 100755 index 00000000..7a5a0d8c --- /dev/null +++ b/scripts/auto_update.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +# Auto-update script for Docker Compose projects +# This script checks for new commits and updates Docker Compose when changes are detected + +# Configuration - can be overridden by environment variables with sensible defaults +REPO_DIR="${REPO_DIR:-/home/ubuntu/proxy}" +LOG_FILE="${LOG_FILE:-/home/ubuntu/proxy/update.log}" +LOCK_FILE="${LOCK_FILE:-/tmp/proxy_update.lock}" + +# Function to log messages with timestamp +log_message() { + echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" +} + +# Function to cleanup on exit +cleanup() { + rm -f "$LOCK_FILE" +} + +# Set trap to cleanup on exit +trap cleanup EXIT + +# Check if another instance is running +if [ -f "$LOCK_FILE" ]; then + log_message "Another update process is already running. Exiting." + exit 1 +fi + +# Create lock file +touch "$LOCK_FILE" + +# Change to repository directory +cd "$REPO_DIR" || { + log_message "ERROR: Cannot change to repository directory $REPO_DIR" + exit 1 +} + +# Fetch latest changes from remote +log_message "Fetching latest changes from remote..." +git fetch origin 2>/dev/null + +# Check if local branch is behind remote +LOCAL_HASH=$(git rev-parse HEAD) +REMOTE_HASH=$(git rev-parse origin/$(git branch --show-current)) + +if [ "$LOCAL_HASH" != "$REMOTE_HASH" ]; then + log_message "New commits detected. Current: $LOCAL_HASH, Remote: $REMOTE_HASH" + + # Pull latest changes + log_message "Pulling latest changes..." + if git pull origin $(git branch --show-current) 2>&1 | tee -a "$LOG_FILE"; then + log_message "Successfully pulled latest changes" + + # Stop current containers + log_message "Stopping current containers..." + sudo docker compose down 2>&1 | tee -a "$LOG_FILE" + + # Build and start updated containers + log_message "Building and starting updated containers..." + if sudo docker compose up -d --build 2>&1 | tee -a "$LOG_FILE"; then + log_message "Successfully updated and restarted containers" + else + log_message "ERROR: Failed to start containers" + exit 1 + fi + else + log_message "ERROR: Failed to pull changes" + exit 1 + fi +else + log_message "No new commits found. Repository is up to date." +fi + +log_message "Update check completed successfully" From fd58cd5eac3be9d9dc99f877224f47d0ed3b97df Mon Sep 17 00:00:00 2001 From: shroominic Date: Mon, 30 Jun 2025 12:27:43 +0000 Subject: [PATCH 2/7] auto prune logs --- scripts/auto_update.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/scripts/auto_update.sh b/scripts/auto_update.sh index 7a5a0d8c..0071c04f 100755 --- a/scripts/auto_update.sh +++ b/scripts/auto_update.sh @@ -7,12 +7,26 @@ REPO_DIR="${REPO_DIR:-/home/ubuntu/proxy}" LOG_FILE="${LOG_FILE:-/home/ubuntu/proxy/update.log}" LOCK_FILE="${LOCK_FILE:-/tmp/proxy_update.lock}" +MAX_LOG_LINES="${MAX_LOG_LINES:-10000}" # Maximum number of log lines to keep # Function to log messages with timestamp log_message() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" } +# Function to prune log file to keep only recent entries +prune_logs() { + if [ -f "$LOG_FILE" ]; then + local line_count=$(wc -l < "$LOG_FILE" 2>/dev/null || echo 0) + if [ "$line_count" -gt "$MAX_LOG_LINES" ]; then + # Create a temporary file with only the last MAX_LOG_LINES lines + tail -n "$MAX_LOG_LINES" "$LOG_FILE" > "${LOG_FILE}.tmp" + mv "${LOG_FILE}.tmp" "$LOG_FILE" + log_message "Log file pruned. Kept last $MAX_LOG_LINES lines." + fi + fi +} + # Function to cleanup on exit cleanup() { rm -f "$LOCK_FILE" @@ -73,3 +87,6 @@ else fi log_message "Update check completed successfully" + +# Prune logs after each run to prevent unlimited growth +prune_logs From f315daf71476fbcc054b98b654e5173c930d75aa Mon Sep 17 00:00:00 2001 From: Shroominic Date: Mon, 30 Jun 2025 09:36:10 -0300 Subject: [PATCH 3/7] add v1/models endpoint --- router/main.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/router/main.py b/router/main.py index 4a163d22..b8c142d5 100644 --- a/router/main.py +++ b/router/main.py @@ -69,6 +69,11 @@ async def info() -> dict: } +@app.get("/v1/models") +async def models() -> dict: + return {"data": MODELS} + + app.include_router(admin_router) app.include_router(wallet_router) app.include_router(providers_router) From 7f3ae7d2eccc83c24d9b272ffd5deea18239fd64 Mon Sep 17 00:00:00 2001 From: Shroominic Date: Mon, 30 Jun 2025 09:36:29 -0300 Subject: [PATCH 4/7] make script run without extra deps --- scripts/models_meta.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/scripts/models_meta.py b/scripts/models_meta.py index eb9fd238..79813c1e 100644 --- a/scripts/models_meta.py +++ b/scripts/models_meta.py @@ -1,8 +1,6 @@ -import asyncio import json from typing import TypedDict - -import httpx +from urllib.request import urlopen class ModelArchitecture(TypedDict): @@ -40,12 +38,10 @@ class Model(TypedDict): per_request_limits: dict | None -async def fetch_openrouter_models() -> list[Model]: +def fetch_openrouter_models() -> list[Model]: """Fetches model information from OpenRouter API.""" - async with httpx.AsyncClient() as client: - response = await client.get("https://openrouter.ai/api/v1/models") - response.raise_for_status() - data = response.json() + with urlopen("https://openrouter.ai/api/v1/models") as response: + data = json.loads(response.read().decode("utf-8")) models_data: list[Model] = [] for model in data.get("data", []): @@ -64,8 +60,8 @@ async def fetch_openrouter_models() -> list[Model]: return models_data -async def main() -> None: - models = await fetch_openrouter_models() +def main() -> None: + models = fetch_openrouter_models() # Print the first model data in a nicely indented JSON format print(json.dumps(models[0], indent=4)) @@ -75,4 +71,4 @@ async def main() -> None: if __name__ == "__main__": - asyncio.run(main()) + main() From 02142e38b8c2cc72b616361cf945bf2e10961024 Mon Sep 17 00:00:00 2001 From: Shroominic Date: Mon, 30 Jun 2025 09:36:36 -0300 Subject: [PATCH 5/7] crontab examples --- scripts/crontab.example | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 scripts/crontab.example diff --git a/scripts/crontab.example b/scripts/crontab.example new file mode 100644 index 00000000..e3a4abca --- /dev/null +++ b/scripts/crontab.example @@ -0,0 +1,3 @@ +REPO_DIR=/home/fungus/proxy +LOG_FILE=/home/fungus/proxy/update.log +* * * * * /home/fungus/proxy/scripts/update_proxy.sh >/dev/null 2>&1 \ No newline at end of file From 773fd317e705539f111097aff3c5d990d7e8269e Mon Sep 17 00:00:00 2001 From: Shroominic Date: Mon, 30 Jun 2025 09:44:04 -0300 Subject: [PATCH 6/7] rm version check --- tests/test_main.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 245e7303..943571b6 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -27,12 +27,10 @@ async def test_root_endpoint(async_client: AsyncClient) -> None: # The app reads from env vars during import, so check what we actually get assert "name" in data assert "description" in data - assert data["version"] == "0.0.1" assert "npub" in data assert "mint" in data assert "http_url" in data assert "onion_url" in data - assert "models" in data @pytest.mark.asyncio From 976df1d5cc1ad0189b4022fd5377cf6249824d1c Mon Sep 17 00:00:00 2001 From: shroominic Date: Mon, 30 Jun 2025 12:48:12 +0000 Subject: [PATCH 7/7] fix file name --- scripts/crontab.example | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/crontab.example b/scripts/crontab.example index e3a4abca..929e452b 100644 --- a/scripts/crontab.example +++ b/scripts/crontab.example @@ -1,3 +1,3 @@ -REPO_DIR=/home/fungus/proxy -LOG_FILE=/home/fungus/proxy/update.log -* * * * * /home/fungus/proxy/scripts/update_proxy.sh >/dev/null 2>&1 \ No newline at end of file +REPO_DIR=/home/user/proxy +LOG_FILE=/home/user/proxy/update.log +* * * * * /home/user/proxy/scripts/auto_update.sh >/dev/null 2>&1 \ No newline at end of file