Skip to content
Merged
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions home-manager/services/cliproxyapi/scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,33 @@ cliproxy_sync_auth_to_s3() {
local auth_dir="$1"
cliproxy_s3_sync "$auth_dir/" "$(cliproxy_auth_s3_uri)"
}

cliproxy_usage_s3_uri() {
printf 's3://%s/usage-export.json' "${OBJECTSTORE_BUCKET:?OBJECTSTORE_BUCKET is required}"
}

cliproxy_upload_usage_to_s3() {
local src="$1"
[ -f "$src" ] || return 0
cliproxy_has_objectstore_credentials || return 0
AWS_ACCESS_KEY_ID="$OBJECTSTORE_ACCESS_KEY" \
AWS_SECRET_ACCESS_KEY="$OBJECTSTORE_SECRET_KEY" \
@aws@ s3 cp \
--endpoint-url="$OBJECTSTORE_ENDPOINT" \
Comment on lines +70 to +73

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

For consistency with cliproxy_s3_sync and to provide better error reporting if variables are unexpectedly empty, consider using the ${VAR:?} expansion syntax for credentials and the endpoint.

Suggested change
AWS_ACCESS_KEY_ID="$OBJECTSTORE_ACCESS_KEY" \
AWS_SECRET_ACCESS_KEY="$OBJECTSTORE_SECRET_KEY" \
@aws@ s3 cp \
--endpoint-url="$OBJECTSTORE_ENDPOINT" \
AWS_ACCESS_KEY_ID="${OBJECTSTORE_ACCESS_KEY:?OBJECTSTORE_ACCESS_KEY is required}" \
AWS_SECRET_ACCESS_KEY="${OBJECTSTORE_SECRET_KEY:?OBJECTSTORE_SECRET_KEY is required}" \
@aws@ s3 cp \
--endpoint-url="${OBJECTSTORE_ENDPOINT:?OBJECTSTORE_ENDPOINT is required}" \
References
  1. Maintain consistency with established patterns for writing scripts that are extracted from Nix expressions.

--no-progress \
"$src" \
"$(cliproxy_usage_s3_uri)" || true
}

cliproxy_download_usage_from_s3() {
local dst="$1"
cliproxy_has_objectstore_credentials || return 0
mkdir -p "$(dirname "$dst")"
AWS_ACCESS_KEY_ID="$OBJECTSTORE_ACCESS_KEY" \
AWS_SECRET_ACCESS_KEY="$OBJECTSTORE_SECRET_KEY" \
@aws@ s3 cp \
--endpoint-url="$OBJECTSTORE_ENDPOINT" \
Comment on lines +83 to +86

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

For consistency with cliproxy_s3_sync and to provide better error reporting if variables are unexpectedly empty, consider using the ${VAR:?} expansion syntax for credentials and the endpoint.

Suggested change
AWS_ACCESS_KEY_ID="$OBJECTSTORE_ACCESS_KEY" \
AWS_SECRET_ACCESS_KEY="$OBJECTSTORE_SECRET_KEY" \
@aws@ s3 cp \
--endpoint-url="$OBJECTSTORE_ENDPOINT" \
AWS_ACCESS_KEY_ID="${OBJECTSTORE_ACCESS_KEY:?OBJECTSTORE_ACCESS_KEY is required}" \
AWS_SECRET_ACCESS_KEY="${OBJECTSTORE_SECRET_KEY:?OBJECTSTORE_SECRET_KEY is required}" \
@aws@ s3 cp \
--endpoint-url="${OBJECTSTORE_ENDPOINT:?OBJECTSTORE_ENDPOINT is required}" \
References
  1. Maintain consistency with established patterns for writing scripts that are extracted from Nix expressions.

--no-progress \
"$(cliproxy_usage_s3_uri)" \
"$dst" || true
}
6 changes: 6 additions & 0 deletions home-manager/services/cliproxyapi/scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ if cliproxy_has_objectstore_credentials; then
if [ -n "$(ls -A "$AUTH_DIR" 2>/dev/null)" ]; then
cliproxy_sync_auth_to_s3 "$AUTH_DIR"
fi

if [ ! -f "$USAGE_EXPORT_FILE" ]; then
echo "⚠️ Usage export missing locally; hydrating from S3" >&2
cliproxy_download_usage_from_s3 "$USAGE_EXPORT_FILE"
fi
fi

# Generate config from template
Expand Down Expand Up @@ -118,6 +123,7 @@ usage_export() {
-H "Authorization: Bearer ${MANAGEMENT_KEY}" \
"${MANAGEMENT_URL}/usage/export" \
-o "$USAGE_EXPORT_FILE" || true
cliproxy_upload_usage_to_s3 "$USAGE_EXPORT_FILE"
}

wait_for_management() {
Expand Down
Loading