Use HTTP API for production deployment data

- Production fetches from local coolify-api.py at port 9876
- Development continues using SSH to query Coolify database
- Avoids need for docker socket access in nuc-portal container

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Alejandro Gutiérrez
2026-02-02 01:56:06 +00:00
parent 58308c9c62
commit 73ac2ddc21
2 changed files with 94 additions and 74 deletions

View File

@@ -1,6 +1,8 @@
import { NextResponse } from 'next/server';
const IS_PRODUCTION = process.env.NODE_ENV === 'production';
// Internal API endpoint for production (served by Python script on NUC host)
const DEPLOYMENTS_API_URL = 'http://192.168.1.3:9876/deployments';
export async function GET(
request: Request,
@@ -9,6 +11,22 @@ export async function GET(
const { uuid } = await params;
try {
let deployment: Record<string, unknown>;
if (IS_PRODUCTION) {
// In production, use internal HTTP API served by coolify-api.py on NUC host
const response = await fetch(`${DEPLOYMENTS_API_URL}/${uuid}`, {
cache: 'no-store',
signal: AbortSignal.timeout(30000),
});
if (!response.ok) {
throw new Error(`Deployments API error: ${response.status}`);
}
deployment = await response.json();
} else {
// In development, use SSH to call docker exec on NUC
const { exec } = await import('child_process');
const { promisify } = await import('util');
const execAsync = promisify(exec);
@@ -40,13 +58,7 @@ echo json_encode([
`;
const base64Code = Buffer.from(phpCode).toString('base64');
let command: string;
if (IS_PRODUCTION) {
command = `echo '${base64Code}' | base64 -d | docker exec -i coolify php artisan tinker`;
} else {
command = `ssh nuc "echo '${base64Code}' | base64 -d | docker exec -i coolify php artisan tinker"`;
}
const command = `ssh nuc "echo '${base64Code}' | base64 -d | docker exec -i coolify php artisan tinker"`;
const { stdout } = await execAsync(command, {
maxBuffer: 10 * 1024 * 1024,
@@ -76,7 +88,8 @@ echo json_encode([
throw new Error('No JSON output found');
}
const deployment = JSON.parse(jsonStr);
deployment = JSON.parse(jsonStr);
}
if (deployment.error) {
return NextResponse.json({ error: deployment.error }, { status: 404 });

View File

@@ -2,13 +2,30 @@ import { NextResponse } from 'next/server';
import type { Deployment, DeploymentStatus } from '@/lib/deployments';
const IS_PRODUCTION = process.env.NODE_ENV === 'production';
// Internal API endpoint for production (served by Python script on NUC host)
const DEPLOYMENTS_API_URL = 'http://192.168.1.3:9876/deployments';
async function fetchDeploymentsFromCoolify(): Promise<Deployment[]> {
let rawDeployments: Array<Record<string, unknown>>;
if (IS_PRODUCTION) {
// In production, use internal HTTP API served by coolify-api.py on NUC host
const response = await fetch(DEPLOYMENTS_API_URL, {
cache: 'no-store',
signal: AbortSignal.timeout(30000),
});
if (!response.ok) {
throw new Error(`Deployments API error: ${response.status}`);
}
rawDeployments = await response.json();
} else {
// In development, use SSH to call docker exec on NUC
const { exec } = await import('child_process');
const { promisify } = await import('util');
const execAsync = promisify(exec);
// Base64 encode the PHP code to avoid escaping issues
const phpCode = `
$deployments = \\App\\Models\\ApplicationDeploymentQueue::with('application')
->orderBy('created_at', 'desc')
@@ -34,37 +51,26 @@ echo json_encode($result->toArray());
`;
const base64Code = Buffer.from(phpCode).toString('base64');
let command: string;
if (IS_PRODUCTION) {
// Running on NUC - direct docker exec
command = `echo '${base64Code}' | base64 -d | docker exec -i coolify php artisan tinker`;
} else {
// Running locally - SSH to NUC
command = `ssh nuc "echo '${base64Code}' | base64 -d | docker exec -i coolify php artisan tinker"`;
}
const command = `ssh nuc "echo '${base64Code}' | base64 -d | docker exec -i coolify php artisan tinker"`;
const { stdout } = await execAsync(command, {
maxBuffer: 10 * 1024 * 1024, // 10MB buffer
timeout: 30000, // 30 second timeout
maxBuffer: 10 * 1024 * 1024,
timeout: 30000,
});
// The output contains tinker prompts (lines starting with > or .) followed by JSON
// Tinker outputs ". " prefix on continuation lines and the result
// Parse tinker output to find JSON
const lines = stdout.split('\n');
let jsonStr = '';
for (const line of lines) {
// Remove tinker prompt prefixes
let cleaned = line;
if (cleaned.startsWith('. ')) {
cleaned = cleaned.substring(2);
} else if (cleaned.startsWith('> ')) {
continue; // Skip command echo lines
continue;
}
const trimmed = cleaned.trim();
// Look for line starting with [{ which indicates JSON array of objects
if (trimmed.startsWith('[{') || trimmed.startsWith('[{"') || trimmed === '[]') {
jsonStr = trimmed;
break;
@@ -76,7 +82,8 @@ echo json_encode($result->toArray());
throw new Error('No JSON output found in tinker response');
}
const rawDeployments = JSON.parse(jsonStr);
rawDeployments = JSON.parse(jsonStr);
}
// Track latest deployment per application for "Current" badge
const latestByApp = new Map<string, string>();