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'; import { NextResponse } from 'next/server';
const IS_PRODUCTION = process.env.NODE_ENV === 'production'; 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( export async function GET(
request: Request, request: Request,
@@ -9,6 +11,22 @@ export async function GET(
const { uuid } = await params; const { uuid } = await params;
try { 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 { exec } = await import('child_process');
const { promisify } = await import('util'); const { promisify } = await import('util');
const execAsync = promisify(exec); const execAsync = promisify(exec);
@@ -40,13 +58,7 @@ echo json_encode([
`; `;
const base64Code = Buffer.from(phpCode).toString('base64'); const base64Code = Buffer.from(phpCode).toString('base64');
const command = `ssh nuc "echo '${base64Code}' | base64 -d | docker exec -i coolify php artisan tinker"`;
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 { stdout } = await execAsync(command, { const { stdout } = await execAsync(command, {
maxBuffer: 10 * 1024 * 1024, maxBuffer: 10 * 1024 * 1024,
@@ -76,7 +88,8 @@ echo json_encode([
throw new Error('No JSON output found'); throw new Error('No JSON output found');
} }
const deployment = JSON.parse(jsonStr); deployment = JSON.parse(jsonStr);
}
if (deployment.error) { if (deployment.error) {
return NextResponse.json({ error: deployment.error }, { status: 404 }); 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'; import type { Deployment, DeploymentStatus } from '@/lib/deployments';
const IS_PRODUCTION = process.env.NODE_ENV === 'production'; 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[]> { 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 { exec } = await import('child_process');
const { promisify } = await import('util'); const { promisify } = await import('util');
const execAsync = promisify(exec); const execAsync = promisify(exec);
// Base64 encode the PHP code to avoid escaping issues
const phpCode = ` const phpCode = `
$deployments = \\App\\Models\\ApplicationDeploymentQueue::with('application') $deployments = \\App\\Models\\ApplicationDeploymentQueue::with('application')
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
@@ -34,37 +51,26 @@ echo json_encode($result->toArray());
`; `;
const base64Code = Buffer.from(phpCode).toString('base64'); const base64Code = Buffer.from(phpCode).toString('base64');
const command = `ssh nuc "echo '${base64Code}' | base64 -d | docker exec -i coolify php artisan tinker"`;
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 { stdout } = await execAsync(command, { const { stdout } = await execAsync(command, {
maxBuffer: 10 * 1024 * 1024, // 10MB buffer maxBuffer: 10 * 1024 * 1024,
timeout: 30000, // 30 second timeout timeout: 30000,
}); });
// The output contains tinker prompts (lines starting with > or .) followed by JSON // Parse tinker output to find JSON
// Tinker outputs ". " prefix on continuation lines and the result
const lines = stdout.split('\n'); const lines = stdout.split('\n');
let jsonStr = ''; let jsonStr = '';
for (const line of lines) { for (const line of lines) {
// Remove tinker prompt prefixes
let cleaned = line; let cleaned = line;
if (cleaned.startsWith('. ')) { if (cleaned.startsWith('. ')) {
cleaned = cleaned.substring(2); cleaned = cleaned.substring(2);
} else if (cleaned.startsWith('> ')) { } else if (cleaned.startsWith('> ')) {
continue; // Skip command echo lines continue;
} }
const trimmed = cleaned.trim(); const trimmed = cleaned.trim();
// Look for line starting with [{ which indicates JSON array of objects
if (trimmed.startsWith('[{') || trimmed.startsWith('[{"') || trimmed === '[]') { if (trimmed.startsWith('[{') || trimmed.startsWith('[{"') || trimmed === '[]') {
jsonStr = trimmed; jsonStr = trimmed;
break; break;
@@ -76,7 +82,8 @@ echo json_encode($result->toArray());
throw new Error('No JSON output found in tinker response'); 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 // Track latest deployment per application for "Current" badge
const latestByApp = new Map<string, string>(); const latestByApp = new Map<string, string>();