diff --git a/misc/tools.func b/misc/tools.func index d12cef3be..5fbdfa740 100644 --- a/misc/tools.func +++ b/misc/tools.func @@ -1306,7 +1306,7 @@ setup_deb822_repo() { if grep -q "BEGIN PGP" "$tmp_gpg" 2>/dev/null; then # ASCII-armored — dearmor to binary - gpg --dearmor --yes -o "/etc/apt/keyrings/${name}.gpg" < "$tmp_gpg" || { + gpg --dearmor --yes -o "/etc/apt/keyrings/${name}.gpg" <"$tmp_gpg" || { msg_error "Failed to dearmor GPG key for ${name}" rm -f "$tmp_gpg" return 1 @@ -1567,31 +1567,54 @@ check_for_gh_release() { ensure_dependencies jq + # Build auth header if token is available + local header_args=() + [[ -n "${GITHUB_TOKEN:-}" ]] && header_args=(-H "Authorization: Bearer $GITHUB_TOKEN") + # Try /latest endpoint for non-pinned versions (most efficient) - local releases_json="" + local releases_json="" http_code="" if [[ -z "$pinned_version_in" ]]; then - releases_json=$(curl -fsSL --max-time 20 \ + http_code=$(curl -sSL --max-time 20 -w "%{http_code}" -o /tmp/gh_check.json \ -H 'Accept: application/vnd.github+json' \ -H 'X-GitHub-Api-Version: 2022-11-28' \ - "https://api.github.com/repos/${source}/releases/latest" 2>/dev/null) + "${header_args[@]}" \ + "https://api.github.com/repos/${source}/releases/latest" 2>/dev/null) || true - if [[ $? -eq 0 ]] && [[ -n "$releases_json" ]]; then - # Wrap single release in array for consistent processing - releases_json="[$releases_json]" + if [[ "$http_code" == "200" ]] && [[ -s /tmp/gh_check.json ]]; then + releases_json="[$(/dev/null) || true + + if [[ "$http_code" == "200" ]] && [[ -s /tmp/gh_check.json ]]; then + releases_json=$(/dev/null) || true + if [[ "$http_code" == "200" ]]; then + success=true + break + elif [[ "$http_code" == "403" ]]; then + if ((attempt < max_retries)); then + msg_warn "GitHub API rate limit hit, retrying in ${retry_delay}s... (attempt $attempt/$max_retries)" + sleep "$retry_delay" + retry_delay=$((retry_delay * 2)) + fi + else + sleep "$retry_delay" + fi ((attempt++)) done if ! $success; then - msg_error "Failed to fetch release metadata from $api_url after $max_retries attempts" + if [[ "$http_code" == "403" ]]; then + msg_error "GitHub API rate limit exceeded (HTTP 403)." + msg_error "To increase the limit, export a GitHub token before running the script:" + msg_error " export GITHUB_TOKEN=\"ghp_your_token_here\"" + else + msg_error "Failed to fetch release metadata from $api_url after $max_retries attempts (HTTP $http_code)" + fi return 1 fi - http_code="${resp:(-3)}" - [[ "$http_code" != "200" ]] && { - msg_error "GitHub API returned HTTP $http_code" - return 1 - } - local json tag_name json=$(/dev/null || \ - grep -oP 'repo/\K[0-9]+\.[0-9]+(\.[0-9]+)?' /etc/apt/sources.list.d/mariadb.list 2>/dev/null || echo "") + OLD_REPO_VERSION=$(grep -oP 'repo/\K[0-9]+\.[0-9]+(\.[0-9]+)?' /etc/apt/sources.list.d/mariadb.sources 2>/dev/null || + grep -oP 'repo/\K[0-9]+\.[0-9]+(\.[0-9]+)?' /etc/apt/sources.list.d/mariadb.list 2>/dev/null || echo "") # Check if old repo points to a different version if [[ -n "$OLD_REPO_VERSION" ]] && [[ "${OLD_REPO_VERSION%.*}" != "${MARIADB_VERSION%.*}" ]]; then @@ -5510,7 +5567,7 @@ EOF # Try to install each package individually for pkg in $MODULE_LIST; do - [[ "$pkg" == "php${PHP_VERSION}" ]] && continue # Already installed + [[ "$pkg" == "php${PHP_VERSION}" ]] && continue # Already installed $STD apt install -y "$pkg" 2>/dev/null || { msg_warn "Could not install $pkg - continuing without it" } @@ -6120,14 +6177,14 @@ function setup_meilisearch() { local MAX_WAIT=120 local WAITED=0 local TASK_RESULT="" - + while [[ $WAITED -lt $MAX_WAIT ]]; do TASK_RESULT=$(curl -s "http://${MEILI_HOST}:${MEILI_PORT}/tasks/${TASK_UID}" \ -H "Authorization: Bearer ${MEILI_MASTER_KEY}" 2>/dev/null) || true - + local TASK_STATUS TASK_STATUS=$(echo "$TASK_RESULT" | grep -oP '"status":\s*"\K[^"]+' || true) - + if [[ "$TASK_STATUS" == "succeeded" ]]; then # Extract dumpUid from the completed task details DUMP_UID=$(echo "$TASK_RESULT" | grep -oP '"dumpUid":\s*"\K[^"]+' || true) @@ -6165,7 +6222,7 @@ function setup_meilisearch() { local MEILI_DB_PATH MEILI_DB_PATH=$(grep -E "^db_path\s*=" /etc/meilisearch.toml 2>/dev/null | sed 's/.*=\s*"\(.*\)"/\1/' | tr -d ' ') MEILI_DB_PATH="${MEILI_DB_PATH:-/var/lib/meilisearch/data}" - + if [[ -d "$MEILI_DB_PATH" ]] && [[ -n "$(ls -A "$MEILI_DB_PATH" 2>/dev/null)" ]]; then local BACKUP_PATH="${MEILI_DB_PATH}.backup.$(date +%Y%m%d%H%M%S)" msg_warn "Backing up MeiliSearch data to ${BACKUP_PATH}" @@ -6193,12 +6250,12 @@ function setup_meilisearch() { local DUMP_FILE="${MEILI_DUMP_DIR}/${DUMP_UID}.dump" if [[ -f "$DUMP_FILE" ]]; then msg_info "Importing dump: ${DUMP_FILE}" - + # Start meilisearch with --import-dump flag # This is a one-time import that happens during startup /usr/bin/meilisearch --config-file-path /etc/meilisearch.toml --import-dump "$DUMP_FILE" & local MEILI_PID=$! - + # Wait for meilisearch to become healthy (import happens during startup) msg_info "Waiting for MeiliSearch to import and start..." local MAX_WAIT=300 @@ -6216,14 +6273,14 @@ function setup_meilisearch() { sleep 3 WAITED=$((WAITED + 3)) done - + # Stop the manual process kill $MEILI_PID 2>/dev/null || true sleep 2 - + # Start via systemd for proper management systemctl start meilisearch - + if systemctl is-active --quiet meilisearch; then msg_ok "MeiliSearch migrated successfully" else @@ -6311,14 +6368,14 @@ EOF MEILISEARCH_API_KEY="" for i in {1..10}; do MEILISEARCH_API_KEY=$(curl -s -X GET "http://${MEILISEARCH_HOST}:${MEILISEARCH_PORT}/keys" \ - -H "Authorization: Bearer ${MEILISEARCH_MASTER_KEY}" 2>/dev/null | \ + -H "Authorization: Bearer ${MEILISEARCH_MASTER_KEY}" 2>/dev/null | grep -o '"key":"[^"]*"' | head -n 1 | sed 's/"key":"//;s/"//') || true [[ -n "$MEILISEARCH_API_KEY" ]] && break sleep 2 done MEILISEARCH_API_KEY_UID=$(curl -s -X GET "http://${MEILISEARCH_HOST}:${MEILISEARCH_PORT}/keys" \ - -H "Authorization: Bearer ${MEILISEARCH_MASTER_KEY}" 2>/dev/null | \ + -H "Authorization: Bearer ${MEILISEARCH_MASTER_KEY}" 2>/dev/null | grep -o '"uid":"[^"]*"' | head -n 1 | sed 's/"uid":"//;s/"//') || true export MEILISEARCH_API_KEY @@ -7104,9 +7161,9 @@ function fetch_and_deploy_from_url() { # Auto-detect archive type using file description local file_desc file_desc=$(file -b "$tmpdir/$filename") - + local archive_type="unknown" - + if [[ "$file_desc" =~ gzip.*compressed|gzip\ compressed\ data ]]; then archive_type="tar" elif [[ "$file_desc" =~ Zip.*archive|ZIP\ archive ]]; then