Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions home-manager/services/cliproxyapi/scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,29 @@ if [ -n "${OBJECTSTORE_ENDPOINT:-}" ] && [ -n "${OBJECTSTORE_ACCESS_KEY:-}" ]; t
fi
fi
fi

# CRITICAL: Always sync local auth files to R2 after merge
# This ensures any files that exist locally but not in R2 get uploaded,
# preventing "key does not exist" errors when cliproxyapi reads from object storage
if [ -d "$AUTH_DIR" ] && [ -n "$(ls -A "$AUTH_DIR" 2>/dev/null)" ]; then
echo "Syncing local auth files to R2..." >&2
AWS_ACCESS_KEY_ID="${OBJECTSTORE_ACCESS_KEY}" \
AWS_SECRET_ACCESS_KEY="${OBJECTSTORE_SECRET_KEY}" \
@aws@ s3 sync \
--endpoint-url="${OBJECTSTORE_ENDPOINT}" \
--no-progress \
"$AUTH_DIR/" \
"s3://cliproxyapi/auths/" 2>&1 && echo "✅ Auth files synced to R2 auths/" >&2 || echo "⚠️ Failed to sync auth files to R2" >&2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The error handling for this critical sync operation has a couple of issues:

  1. Incorrect Error Redirection: The use of 2>&1 sends the aws command's standard error to standard output. Given that your service configuration separates stdout and stderr into different log files, this would cause important aws error messages to be logged in /tmp/cliproxyapi.log instead of /tmp/cliproxyapi.error.log, making debugging more difficult.

  2. Failure Is Not Fatal: The || echo ... construct prevents a non-zero exit code from the aws command from propagating. Since set -e is active, a failure in this critical step should terminate the script. However, the current implementation will only print a warning and continue, which could lead to the very "key does not exist" errors this change is intended to fix.

My suggestion below corrects both issues by removing the redirection and ensuring the script exits upon failure.

Suggested change
"s3://cliproxyapi/auths/" 2>&1 && echo "✅ Auth files synced to R2 auths/" >&2 || echo "⚠️ Failed to sync auth files to R2" >&2
"s3://cliproxyapi/auths/" && echo "✅ Auth files synced to R2 auths/" >&2 || { echo "⚠️ Failed to sync auth files to R2" >&2; exit 1; }


# Also sync to backup location for redundancy
AWS_ACCESS_KEY_ID="${OBJECTSTORE_ACCESS_KEY}" \
AWS_SECRET_ACCESS_KEY="${OBJECTSTORE_SECRET_KEY}" \
@aws@ s3 sync \
--endpoint-url="${OBJECTSTORE_ENDPOINT}" \
--no-progress \
"$AUTH_DIR/" \
"s3://cliproxyapi/backup/auths/" 2>&1 && echo "✅ Auth files synced to R2 backup/" >&2 || true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the primary sync command, this line redirects stderr to stdout (2>&1). This can make debugging more difficult by sending aws error messages to the standard log file instead of the error log file.

To improve logging consistency, I recommend removing the 2>&1 redirection. The || true part correctly ensures that a failure in this non-critical backup sync does not halt the script.

Suggested change
"s3://cliproxyapi/backup/auths/" 2>&1 && echo "✅ Auth files synced to R2 backup/" >&2 || true
"s3://cliproxyapi/backup/auths/" && echo "✅ Auth files synced to R2 backup/" >&2 || true

fi
else
echo "⚠️ Skipping auth sync: OBJECTSTORE credentials not set" >&2
fi
Expand Down
Loading