326 lines
9.3 KiB
Bash
326 lines
9.3 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
# =====================================================
|
||
|
|
# Installer JSON API Integration Library
|
||
|
|
# =====================================================
|
||
|
|
# Functions to store and retrieve installer JSON via PostgREST API
|
||
|
|
|
||
|
|
# Store installer JSON in database via PostgREST
|
||
|
|
# Usage: store_installer_json_in_db <ctid> <customer_email> <postgrest_url> <service_role_key> <json_output>
|
||
|
|
# Returns: 0 on success, 1 on failure
|
||
|
|
store_installer_json_in_db() {
|
||
|
|
local ctid="$1"
|
||
|
|
local customer_email="$2"
|
||
|
|
local postgrest_url="$3"
|
||
|
|
local service_role_key="$4"
|
||
|
|
local json_output="$5"
|
||
|
|
|
||
|
|
info "Storing installer JSON in database for CTID ${ctid}..."
|
||
|
|
|
||
|
|
# Validate inputs
|
||
|
|
[[ -n "$ctid" ]] || { warn "CTID is empty"; return 1; }
|
||
|
|
[[ -n "$customer_email" ]] || { warn "Customer email is empty"; return 1; }
|
||
|
|
[[ -n "$postgrest_url" ]] || { warn "PostgREST URL is empty"; return 1; }
|
||
|
|
[[ -n "$service_role_key" ]] || { warn "Service role key is empty"; return 1; }
|
||
|
|
[[ -n "$json_output" ]] || { warn "JSON output is empty"; return 1; }
|
||
|
|
|
||
|
|
# Validate JSON
|
||
|
|
if ! echo "$json_output" | python3 -m json.tool >/dev/null 2>&1; then
|
||
|
|
warn "Invalid JSON output"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Prepare API request payload
|
||
|
|
local payload
|
||
|
|
payload=$(cat <<EOF
|
||
|
|
{
|
||
|
|
"customer_email_param": "${customer_email}",
|
||
|
|
"lxc_id_param": ${ctid},
|
||
|
|
"installer_json_param": ${json_output}
|
||
|
|
}
|
||
|
|
EOF
|
||
|
|
)
|
||
|
|
|
||
|
|
# Make API request
|
||
|
|
local response
|
||
|
|
local http_code
|
||
|
|
|
||
|
|
response=$(curl -sS -w "\n%{http_code}" -X POST "${postgrest_url}/rpc/store_installer_json" \
|
||
|
|
-H "Content-Type: application/json" \
|
||
|
|
-H "Authorization: Bearer ${service_role_key}" \
|
||
|
|
-H "Prefer: return=representation" \
|
||
|
|
-d "${payload}" 2>&1)
|
||
|
|
|
||
|
|
# Extract HTTP code from last line
|
||
|
|
http_code=$(echo "$response" | tail -n1)
|
||
|
|
response=$(echo "$response" | sed '$d')
|
||
|
|
|
||
|
|
# Check HTTP status
|
||
|
|
if [[ "$http_code" -ge 200 && "$http_code" -lt 300 ]]; then
|
||
|
|
# Check if response indicates success
|
||
|
|
if echo "$response" | grep -q '"success":\s*true'; then
|
||
|
|
info "Installer JSON stored successfully in database"
|
||
|
|
return 0
|
||
|
|
else
|
||
|
|
warn "API returned success HTTP code but response indicates failure: ${response}"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
else
|
||
|
|
warn "Failed to store installer JSON (HTTP ${http_code}): ${response}"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# Retrieve installer JSON from database via PostgREST
|
||
|
|
# Usage: get_installer_json_by_email <customer_email> <postgrest_url>
|
||
|
|
# Returns: JSON on stdout, exit code 0 on success
|
||
|
|
get_installer_json_by_email() {
|
||
|
|
local customer_email="$1"
|
||
|
|
local postgrest_url="$2"
|
||
|
|
|
||
|
|
info "Retrieving installer JSON for ${customer_email}..."
|
||
|
|
|
||
|
|
# Validate inputs
|
||
|
|
[[ -n "$customer_email" ]] || { warn "Customer email is empty"; return 1; }
|
||
|
|
[[ -n "$postgrest_url" ]] || { warn "PostgREST URL is empty"; return 1; }
|
||
|
|
|
||
|
|
# Prepare API request payload
|
||
|
|
local payload
|
||
|
|
payload=$(cat <<EOF
|
||
|
|
{
|
||
|
|
"customer_email_param": "${customer_email}"
|
||
|
|
}
|
||
|
|
EOF
|
||
|
|
)
|
||
|
|
|
||
|
|
# Make API request
|
||
|
|
local response
|
||
|
|
local http_code
|
||
|
|
|
||
|
|
response=$(curl -sS -w "\n%{http_code}" -X POST "${postgrest_url}/rpc/get_instance_config_by_email" \
|
||
|
|
-H "Content-Type: application/json" \
|
||
|
|
-d "${payload}" 2>&1)
|
||
|
|
|
||
|
|
# Extract HTTP code from last line
|
||
|
|
http_code=$(echo "$response" | tail -n1)
|
||
|
|
response=$(echo "$response" | sed '$d')
|
||
|
|
|
||
|
|
# Check HTTP status
|
||
|
|
if [[ "$http_code" -ge 200 && "$http_code" -lt 300 ]]; then
|
||
|
|
# Check if response is empty array
|
||
|
|
if [[ "$response" == "[]" ]]; then
|
||
|
|
warn "No instance found for email: ${customer_email}"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Output JSON
|
||
|
|
echo "$response"
|
||
|
|
return 0
|
||
|
|
else
|
||
|
|
warn "Failed to retrieve installer JSON (HTTP ${http_code}): ${response}"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# Retrieve installer JSON by CTID (requires service role key)
|
||
|
|
# Usage: get_installer_json_by_ctid <ctid> <postgrest_url> <service_role_key>
|
||
|
|
# Returns: JSON on stdout, exit code 0 on success
|
||
|
|
get_installer_json_by_ctid() {
|
||
|
|
local ctid="$1"
|
||
|
|
local postgrest_url="$2"
|
||
|
|
local service_role_key="$3"
|
||
|
|
|
||
|
|
info "Retrieving installer JSON for CTID ${ctid}..."
|
||
|
|
|
||
|
|
# Validate inputs
|
||
|
|
[[ -n "$ctid" ]] || { warn "CTID is empty"; return 1; }
|
||
|
|
[[ -n "$postgrest_url" ]] || { warn "PostgREST URL is empty"; return 1; }
|
||
|
|
[[ -n "$service_role_key" ]] || { warn "Service role key is empty"; return 1; }
|
||
|
|
|
||
|
|
# Prepare API request payload
|
||
|
|
local payload
|
||
|
|
payload=$(cat <<EOF
|
||
|
|
{
|
||
|
|
"ctid_param": ${ctid}
|
||
|
|
}
|
||
|
|
EOF
|
||
|
|
)
|
||
|
|
|
||
|
|
# Make API request
|
||
|
|
local response
|
||
|
|
local http_code
|
||
|
|
|
||
|
|
response=$(curl -sS -w "\n%{http_code}" -X POST "${postgrest_url}/rpc/get_instance_config_by_ctid" \
|
||
|
|
-H "Content-Type: application/json" \
|
||
|
|
-H "Authorization: Bearer ${service_role_key}" \
|
||
|
|
-d "${payload}" 2>&1)
|
||
|
|
|
||
|
|
# Extract HTTP code from last line
|
||
|
|
http_code=$(echo "$response" | tail -n1)
|
||
|
|
response=$(echo "$response" | sed '$d')
|
||
|
|
|
||
|
|
# Check HTTP status
|
||
|
|
if [[ "$http_code" -ge 200 && "$http_code" -lt 300 ]]; then
|
||
|
|
# Check if response is empty array
|
||
|
|
if [[ "$response" == "[]" ]]; then
|
||
|
|
warn "No instance found for CTID: ${ctid}"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Output JSON
|
||
|
|
echo "$response"
|
||
|
|
return 0
|
||
|
|
else
|
||
|
|
warn "Failed to retrieve installer JSON (HTTP ${http_code}): ${response}"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# Get public config (no authentication required)
|
||
|
|
# Usage: get_public_config <postgrest_url>
|
||
|
|
# Returns: JSON on stdout, exit code 0 on success
|
||
|
|
get_public_config() {
|
||
|
|
local postgrest_url="$1"
|
||
|
|
|
||
|
|
info "Retrieving public config..."
|
||
|
|
|
||
|
|
# Validate inputs
|
||
|
|
[[ -n "$postgrest_url" ]] || { warn "PostgREST URL is empty"; return 1; }
|
||
|
|
|
||
|
|
# Make API request
|
||
|
|
local response
|
||
|
|
local http_code
|
||
|
|
|
||
|
|
response=$(curl -sS -w "\n%{http_code}" -X POST "${postgrest_url}/rpc/get_public_config" \
|
||
|
|
-H "Content-Type: application/json" \
|
||
|
|
-d '{}' 2>&1)
|
||
|
|
|
||
|
|
# Extract HTTP code from last line
|
||
|
|
http_code=$(echo "$response" | tail -n1)
|
||
|
|
response=$(echo "$response" | sed '$d')
|
||
|
|
|
||
|
|
# Check HTTP status
|
||
|
|
if [[ "$http_code" -ge 200 && "$http_code" -lt 300 ]]; then
|
||
|
|
# Output JSON
|
||
|
|
echo "$response"
|
||
|
|
return 0
|
||
|
|
else
|
||
|
|
warn "Failed to retrieve public config (HTTP ${http_code}): ${response}"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# Apply installer JSON API schema to database
|
||
|
|
# Usage: apply_installer_json_api_schema <ctid>
|
||
|
|
# Returns: 0 on success, 1 on failure
|
||
|
|
apply_installer_json_api_schema() {
|
||
|
|
local ctid="$1"
|
||
|
|
|
||
|
|
info "Applying installer JSON API schema to database..."
|
||
|
|
|
||
|
|
# Validate inputs
|
||
|
|
[[ -n "$ctid" ]] || { warn "CTID is empty"; return 1; }
|
||
|
|
|
||
|
|
# Check if SQL file exists
|
||
|
|
local sql_file="${SCRIPT_DIR}/sql/add_installer_json_api.sql"
|
||
|
|
if [[ ! -f "$sql_file" ]]; then
|
||
|
|
warn "SQL file not found: ${sql_file}"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Copy SQL file to container
|
||
|
|
info "Copying SQL file to container..."
|
||
|
|
pct_push_text "$ctid" "/tmp/add_installer_json_api.sql" "$(cat "$sql_file")"
|
||
|
|
|
||
|
|
# Execute SQL in PostgreSQL container
|
||
|
|
info "Executing SQL in PostgreSQL container..."
|
||
|
|
local result
|
||
|
|
result=$(pct_exec "$ctid" -- bash -c "
|
||
|
|
docker exec customer-postgres psql -U customer -d customer -f /tmp/add_installer_json_api.sql 2>&1
|
||
|
|
" || echo "FAILED")
|
||
|
|
|
||
|
|
if echo "$result" | grep -qi "error\|failed"; then
|
||
|
|
warn "Failed to apply SQL schema: ${result}"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
info "SQL schema applied successfully"
|
||
|
|
|
||
|
|
# Cleanup
|
||
|
|
pct_exec "$ctid" -- rm -f /tmp/add_installer_json_api.sql 2>/dev/null || true
|
||
|
|
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
# Test API connectivity
|
||
|
|
# Usage: test_api_connectivity <postgrest_url>
|
||
|
|
# Returns: 0 on success, 1 on failure
|
||
|
|
test_api_connectivity() {
|
||
|
|
local postgrest_url="$1"
|
||
|
|
|
||
|
|
info "Testing API connectivity to ${postgrest_url}..."
|
||
|
|
|
||
|
|
# Validate inputs
|
||
|
|
[[ -n "$postgrest_url" ]] || { warn "PostgREST URL is empty"; return 1; }
|
||
|
|
|
||
|
|
# Test with public config endpoint
|
||
|
|
local response
|
||
|
|
local http_code
|
||
|
|
|
||
|
|
response=$(curl -sS -w "\n%{http_code}" -X POST "${postgrest_url}/rpc/get_public_config" \
|
||
|
|
-H "Content-Type: application/json" \
|
||
|
|
-d '{}' 2>&1)
|
||
|
|
|
||
|
|
# Extract HTTP code from last line
|
||
|
|
http_code=$(echo "$response" | tail -n1)
|
||
|
|
|
||
|
|
# Check HTTP status
|
||
|
|
if [[ "$http_code" -ge 200 && "$http_code" -lt 300 ]]; then
|
||
|
|
info "API connectivity test successful"
|
||
|
|
return 0
|
||
|
|
else
|
||
|
|
warn "API connectivity test failed (HTTP ${http_code})"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# Verify installer JSON was stored correctly
|
||
|
|
# Usage: verify_installer_json_stored <ctid> <customer_email> <postgrest_url>
|
||
|
|
# Returns: 0 on success, 1 on failure
|
||
|
|
verify_installer_json_stored() {
|
||
|
|
local ctid="$1"
|
||
|
|
local customer_email="$2"
|
||
|
|
local postgrest_url="$3"
|
||
|
|
|
||
|
|
info "Verifying installer JSON was stored for CTID ${ctid}..."
|
||
|
|
|
||
|
|
# Retrieve installer JSON
|
||
|
|
local response
|
||
|
|
response=$(get_installer_json_by_email "$customer_email" "$postgrest_url")
|
||
|
|
|
||
|
|
if [[ $? -ne 0 ]]; then
|
||
|
|
warn "Failed to retrieve installer JSON for verification"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Check if CTID matches
|
||
|
|
local stored_ctid
|
||
|
|
stored_ctid=$(echo "$response" | python3 -c "import json,sys; d=json.load(sys.stdin); print(d[0]['ctid'] if d else '')" 2>/dev/null || echo "")
|
||
|
|
|
||
|
|
if [[ "$stored_ctid" == "$ctid" ]]; then
|
||
|
|
info "Installer JSON verified successfully (CTID: ${stored_ctid})"
|
||
|
|
return 0
|
||
|
|
else
|
||
|
|
warn "Installer JSON verification failed (expected CTID: ${ctid}, got: ${stored_ctid})"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# Export functions
|
||
|
|
export -f store_installer_json_in_db
|
||
|
|
export -f get_installer_json_by_email
|
||
|
|
export -f get_installer_json_by_ctid
|
||
|
|
export -f get_public_config
|
||
|
|
export -f apply_installer_json_api_schema
|
||
|
|
export -f test_api_connectivity
|
||
|
|
export -f verify_installer_json_stored
|