fix: P0 — add User-Agent + retry to bash urllib, retry to service_spec_gen, empty-list guard

This commit is contained in:
“Naeel”
2026-06-30 18:11:52 +04:00
parent 1739120a98
commit 48ad16c241
4 changed files with 93 additions and 40 deletions
+3
View File
@@ -54,3 +54,6 @@ universal_rebuild/docs_template_gen_v2
secrets/test.token.new
secrets/*.token.new
terraform-provider-nubes_*
universal_rebuild/service_spec_gen
universal_rebuild/service_ops_gen
universal_rebuild/service_params_gen
+33 -12
View File
@@ -139,6 +139,14 @@ fi
rm -f "$FAILURES_FILE"
# Проверка что список сервисов не пустой
svc_count=$(grep -cv '^\s*$\|^\s*#' "$SERVICES_FILE" || true)
if [[ "$svc_count" -eq 0 ]]; then
echo "Error: services list is empty or all commented out: $SERVICES_FILE" >&2
exit 2
fi
echo "Generating YAML for ${svc_count} services from ${SERVICES_FILE}"
# Читаем список сервисов, пропуская пустые строки и комментарии.
while IFS= read -r line; do
line="${line//$'\r'/}"
@@ -164,28 +172,41 @@ while IFS= read -r line; do
# Если имя не указано в списке — подтягиваем по API.
if [[ -z "$svc_name" ]]; then
svc_name=$(python3 - <<PY
import json
import urllib.parse
import urllib.request
import json, sys, time, urllib.parse, urllib.request
sid = "${sid}"
endpoint = "${API_ENDPOINT}"
token = "${NUBES_API_TOKEN}"
max_retries = 3
params = urllib.parse.urlencode({"endpoint": f"/services/{sid}"})
url = f"{endpoint}?{params}"
req = urllib.request.Request(url)
if token:
req.add_header("Authorization", f"Bearer {token}")
with urllib.request.urlopen(req) as resp:
data = json.loads(resp.read().decode("utf-8"))
svc = data.get("svc", {})
name = svc.get("svcShort") or svc.get("name") or svc.get("title") or f"service_{sid}"
print(name)
for attempt in range(1, max_retries + 1):
try:
req = urllib.request.Request(url)
if token:
req.add_header("Authorization", f"Bearer {token}")
req.add_header("User-Agent", "Mozilla/5.0 (compatible; Terraform-Provider-Nubes/BashGenerator)")
with urllib.request.urlopen(req, timeout=30) as resp:
data = json.loads(resp.read().decode("utf-8"))
svc = data.get("svc", {})
name = svc.get("svcShort") or svc.get("name") or svc.get("title") or f"service_{sid}"
print(name)
sys.exit(0)
except Exception as e:
if attempt < max_retries:
time.sleep(2 * attempt)
else:
print(f"ERROR: failed to fetch service {sid} after {max_retries} attempts: {e}", file=sys.stderr)
sys.exit(1)
PY
)
if [[ "$svc_name" == ERROR:* ]]; then
echo "$svc_name" >&2
echo "${sid} api_error" >> "$FAILURES_FILE"
continue
fi
fi
# Один YAML на сервис формируется Go-генератором.
+5 -5
View File
@@ -25,11 +25,11 @@ func debugLog(format string, args ...interface{}) {
// UniversalClient handles Nubes API logic
type UniversalClient struct {
HttpClient *http.Client
ApiEndpoint string
ApiToken string
ProviderVersion string
LogLevel string
HttpClient *http.Client
ApiEndpoint string
ApiToken string
ProviderVersion string
LogLevel string
}
// Request models
@@ -388,30 +388,59 @@ func (c *apiClient) getServiceOperation(svcOperationId int) (serviceOperationInf
}
func (c *apiClient) getViaProxy(endpoint string, out interface{}) error {
// The API uses a proxy query parameter called "endpoint".
req, err := http.NewRequest("GET", c.endpoint, nil)
if err != nil {
return err
const maxRetries = 3
baseDelay := 2 * time.Second
var lastErr error
for attempt := 0; attempt <= maxRetries; attempt++ {
if attempt > 0 {
time.Sleep(baseDelay * time.Duration(1<<(attempt-1)))
}
req, err := http.NewRequest("GET", c.endpoint, nil)
if err != nil {
return err
}
q := req.URL.Query()
q.Set("endpoint", endpoint)
req.URL.RawQuery = q.Encode()
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; Terraform-Provider-Nubes/Generator)")
resp, err := c.httpClient.Do(req)
if err != nil {
lastErr = err
if attempt < maxRetries {
continue
}
return err
}
body, readErr := io.ReadAll(resp.Body)
resp.Body.Close()
if readErr != nil {
lastErr = readErr
if attempt < maxRetries {
continue
}
return readErr
}
if resp.StatusCode != 200 {
err := fmt.Errorf("status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
// Retry on transient errors
if attempt < maxRetries && (resp.StatusCode == 429 || resp.StatusCode >= 500) {
lastErr = err
continue
}
return err
}
return json.Unmarshal(body, out)
}
q := req.URL.Query()
q.Set("endpoint", endpoint)
req.URL.RawQuery = q.Encode()
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
// User-Agent: браузерный, чтобы пройти DDoS-Guard (см. docs/ops/API_TOKENS.md).
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; Terraform-Provider-Nubes/Generator)")
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
}
dec := json.NewDecoder(resp.Body)
return dec.Decode(out)
return fmt.Errorf("getViaProxy failed after %d retries: %w", maxRetries, lastErr)
}
func (c *apiClient) collectOperations(ops []operationInfo) ([]OperationSpec, bool, bool, bool, error) {