Spaces:
Sleeping
Sleeping
Update src/index.ts
Browse files- src/index.ts +146 -108
src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import * as http from 'node:http';
|
| 2 |
-
import { exec, spawn
|
| 3 |
import * as fs from 'node:fs';
|
| 4 |
import * as path from 'node:path';
|
| 5 |
import { fileURLToPath } from 'node:url';
|
|
@@ -104,57 +104,113 @@ async function cloneOrPull(repoUrl: string, targetDir: string): Promise<void> {
|
|
| 104 |
}
|
| 105 |
|
| 106 |
// ============================================================
|
| 107 |
-
//
|
| 108 |
// ============================================================
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
}
|
| 122 |
-
console.warn('β οΈ No EXPOSE directive found. Using default port 7000.');
|
| 123 |
-
return 7000;
|
| 124 |
}
|
| 125 |
|
| 126 |
// ============================================================
|
| 127 |
-
// π€ DEPLOY AGENT (
|
| 128 |
// ============================================================
|
| 129 |
-
function
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
}
|
| 155 |
|
| 156 |
// ============================================================
|
| 157 |
-
// MEMORY COPY
|
| 158 |
// ============================================================
|
| 159 |
function copyMemoryFolder(srcPath: string, dstPath: string): void {
|
| 160 |
const srcMemory = path.join(srcPath, 'memory');
|
|
@@ -178,8 +234,7 @@ function loadFailedVersions(): string[] {
|
|
| 178 |
try {
|
| 179 |
if (fs.existsSync(FAILED_VERSIONS_FILE)) {
|
| 180 |
const data = fs.readFileSync(FAILED_VERSIONS_FILE, 'utf-8');
|
| 181 |
-
|
| 182 |
-
if (Array.isArray(parsed)) return parsed;
|
| 183 |
}
|
| 184 |
} catch (e) {
|
| 185 |
console.warn('β οΈ Failed to load failed-versions.json');
|
|
@@ -189,8 +244,7 @@ function loadFailedVersions(): string[] {
|
|
| 189 |
|
| 190 |
function saveFailedVersion(sha: string): void {
|
| 191 |
try {
|
| 192 |
-
let failed = loadFailedVersions();
|
| 193 |
-
failed = failed.filter(v => v !== sha);
|
| 194 |
failed.push(sha);
|
| 195 |
if (failed.length > MAX_FAILED_VERSIONS) {
|
| 196 |
failed = failed.slice(-MAX_FAILED_VERSIONS);
|
|
@@ -203,15 +257,13 @@ function saveFailedVersion(sha: string): void {
|
|
| 203 |
}
|
| 204 |
|
| 205 |
function isVersionFailed(sha: string): boolean {
|
| 206 |
-
|
| 207 |
-
return failed.includes(sha);
|
| 208 |
}
|
| 209 |
|
| 210 |
function clearFailedVersion(sha: string): void {
|
| 211 |
try {
|
| 212 |
-
const failed = loadFailedVersions();
|
| 213 |
-
|
| 214 |
-
fs.writeFileSync(FAILED_VERSIONS_FILE, JSON.stringify(updated, null, 2));
|
| 215 |
console.log(`ποΈ Failed version ${sha} removed from tracking list.`);
|
| 216 |
} catch (e) {
|
| 217 |
console.error('β Failed to clear failed version:', e);
|
|
@@ -221,14 +273,14 @@ function clearFailedVersion(sha: string): void {
|
|
| 221 |
// ============================================================
|
| 222 |
// STATE
|
| 223 |
// ============================================================
|
| 224 |
-
let
|
| 225 |
let prodVersionPath: string | null = null;
|
| 226 |
let prodCommitSha: string | null = null;
|
| 227 |
-
let
|
| 228 |
let isUpdating: boolean = false;
|
| 229 |
|
| 230 |
// ============================================================
|
| 231 |
-
// π§ MAIN UPDATE LOGIC
|
| 232 |
// ============================================================
|
| 233 |
async function checkAndUpdate(): Promise<void> {
|
| 234 |
if (isUpdating) {
|
|
@@ -237,10 +289,10 @@ async function checkAndUpdate(): Promise<void> {
|
|
| 237 |
}
|
| 238 |
|
| 239 |
isUpdating = true;
|
| 240 |
-
let
|
| 241 |
let testVersionPath: string | null = null;
|
| 242 |
let testCommitSha: string | null = null;
|
| 243 |
-
let
|
| 244 |
|
| 245 |
try {
|
| 246 |
console.log('π Checking for updates...');
|
|
@@ -251,7 +303,6 @@ async function checkAndUpdate(): Promise<void> {
|
|
| 251 |
console.log(`π Latest SHA: ${latestSha.substring(0, 7)}`);
|
| 252 |
console.log(`π Current Prod SHA: ${prodCommitSha ? prodCommitSha.substring(0, 7) : 'none'}`);
|
| 253 |
|
| 254 |
-
// 1. New code?
|
| 255 |
if (latestSha === prodCommitSha) {
|
| 256 |
console.log('β
No new code. Current version is already running.');
|
| 257 |
if (prodCommitSha && isVersionFailed(prodCommitSha)) {
|
|
@@ -261,23 +312,18 @@ async function checkAndUpdate(): Promise<void> {
|
|
| 261 |
return;
|
| 262 |
}
|
| 263 |
|
| 264 |
-
// 2. Previously failed?
|
| 265 |
if (isVersionFailed(latestSha)) {
|
| 266 |
console.log(`β³ Version ${latestSha.substring(0, 7)} is marked as FAILED. Waiting for a newer version.`);
|
| 267 |
isUpdating = false;
|
| 268 |
return;
|
| 269 |
}
|
| 270 |
|
| 271 |
-
|
| 272 |
-
|
| 273 |
testCommitSha = latestSha;
|
| 274 |
testVersionPath = path.join(CONFIG.VERSIONS_DIR, `v2_${Date.now()}`);
|
| 275 |
await cloneOrPull(CONFIG.RES1_REPO, testVersionPath);
|
| 276 |
|
| 277 |
-
// Read EXPOSE port from Res1 Dockerfile
|
| 278 |
-
testPort = await getExposedPort(testVersionPath);
|
| 279 |
-
console.log(`π Test agent will run on port ${testPort}`);
|
| 280 |
-
|
| 281 |
// Copy memory from production if exists
|
| 282 |
if (prodVersionPath) {
|
| 283 |
copyMemoryFolder(prodVersionPath, testVersionPath);
|
|
@@ -285,79 +331,71 @@ async function checkAndUpdate(): Promise<void> {
|
|
| 285 |
console.log('β οΈ No previous version. Starting fresh without memory copy.');
|
| 286 |
}
|
| 287 |
|
| 288 |
-
// Deploy test
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
}
|
| 300 |
|
| 301 |
-
//
|
| 302 |
-
|
| 303 |
-
const
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
setTimeout(() => {
|
| 310 |
-
if (!prodProcess?.killed) prodProcess?.kill('SIGKILL');
|
| 311 |
-
}, 3000);
|
| 312 |
}
|
| 313 |
|
| 314 |
// Update state
|
| 315 |
-
|
| 316 |
prodVersionPath = testVersionPath;
|
| 317 |
prodCommitSha = testCommitSha;
|
| 318 |
-
|
| 319 |
testVersionPath = null;
|
| 320 |
testCommitSha = null;
|
| 321 |
-
|
| 322 |
|
| 323 |
if (prodCommitSha && isVersionFailed(prodCommitSha)) {
|
| 324 |
clearFailedVersion(prodCommitSha);
|
| 325 |
}
|
| 326 |
|
| 327 |
-
console.log(`β
Successfully deployed V2 (${latestSha.substring(0, 7)}) on port ${
|
| 328 |
|
| 329 |
} catch (error: any) {
|
| 330 |
console.error('β Deployment failed:', error);
|
| 331 |
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
testProcess.kill('SIGTERM');
|
| 336 |
-
setTimeout(() => {
|
| 337 |
-
if (!testProcess?.killed) testProcess?.kill('SIGKILL');
|
| 338 |
-
}, 2000);
|
| 339 |
-
testProcess = null;
|
| 340 |
}
|
| 341 |
|
| 342 |
-
// Save failed version
|
| 343 |
if (testCommitSha) {
|
| 344 |
saveFailedVersion(testCommitSha);
|
| 345 |
}
|
| 346 |
|
| 347 |
-
// Send email
|
| 348 |
await sendEmail(
|
| 349 |
'Deployment FAILED',
|
| 350 |
-
`Version: ${testCommitSha ? testCommitSha.substring(0, 7) : 'unknown'}\nError: ${error.message}\nPort ${
|
| 351 |
);
|
| 352 |
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
console.log(`π Keeping old version running on port ${prodPort}`);
|
| 356 |
} else {
|
| 357 |
console.log('β οΈ No previous version available.');
|
| 358 |
}
|
| 359 |
|
| 360 |
-
// Clean up test directory
|
| 361 |
if (testVersionPath && fs.existsSync(testVersionPath)) {
|
| 362 |
try {
|
| 363 |
fs.rmSync(testVersionPath, { recursive: true, force: true });
|
|
@@ -389,8 +427,8 @@ const server = http.createServer(async (req, res) => {
|
|
| 389 |
const failed = loadFailedVersions();
|
| 390 |
res.writeHead(200, { 'Content-Type': 'application/json' });
|
| 391 |
res.end(JSON.stringify({
|
| 392 |
-
prodRunning:
|
| 393 |
-
|
| 394 |
prodVersion: prodCommitSha ? prodCommitSha.substring(0, 7) : 'none',
|
| 395 |
isUpdating,
|
| 396 |
failedVersions: failed.map(s => s.substring(0, 7))
|
|
|
|
| 1 |
import * as http from 'node:http';
|
| 2 |
+
import { exec, spawn } from 'node:child_process';
|
| 3 |
import * as fs from 'node:fs';
|
| 4 |
import * as path from 'node:path';
|
| 5 |
import { fileURLToPath } from 'node:url';
|
|
|
|
| 104 |
}
|
| 105 |
|
| 106 |
// ============================================================
|
| 107 |
+
// π³ DOCKER UTILITIES FOR RES1
|
| 108 |
// ============================================================
|
| 109 |
+
function execDocker(cmd: string): Promise<string> {
|
| 110 |
+
return new Promise((resolve, reject) => {
|
| 111 |
+
exec(cmd, (error, stdout, stderr) => {
|
| 112 |
+
if (error) reject(stderr || error.message);
|
| 113 |
+
else resolve(stdout.trim());
|
| 114 |
+
});
|
| 115 |
+
});
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
async function buildAndRunRes1(
|
| 119 |
+
versionDir: string,
|
| 120 |
+
containerName: string,
|
| 121 |
+
portMapping: string // e.g., "7000:7000" or "2000:2000"
|
| 122 |
+
): Promise<number> {
|
| 123 |
+
const imageTag = `oracus-${Date.now()}`;
|
| 124 |
+
console.log(`π³ Building Docker image ${imageTag} from ${versionDir}...`);
|
| 125 |
+
await execDocker(`docker build -t ${imageTag} ${versionDir}`);
|
| 126 |
+
|
| 127 |
+
console.log(`π³ Running container ${containerName} with port ${portMapping}...`);
|
| 128 |
+
await execDocker(
|
| 129 |
+
`docker run -d --name ${containerName} -p ${portMapping} ${imageTag}`
|
| 130 |
+
);
|
| 131 |
+
|
| 132 |
+
// Find the actual host port mapped
|
| 133 |
+
const inspect = await execDocker(
|
| 134 |
+
`docker inspect ${containerName} --format='{{range $p, $conf := .NetworkSettings.Ports}}{{(index $conf 0).HostPort}} {{end}}'`
|
| 135 |
+
);
|
| 136 |
+
const ports = inspect.trim().split(/\s+/);
|
| 137 |
+
if (ports.length === 0) throw new Error('No ports exposed');
|
| 138 |
+
const hostPort = parseInt(ports[0], 10);
|
| 139 |
+
console.log(`β
Container running on host port ${hostPort}`);
|
| 140 |
+
return hostPort;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
async function stopAndRemoveContainer(containerName: string): Promise<void> {
|
| 144 |
+
try {
|
| 145 |
+
await execDocker(`docker stop ${containerName}`);
|
| 146 |
+
await execDocker(`docker rm ${containerName}`);
|
| 147 |
+
console.log(`π§Ή Removed container ${containerName}`);
|
| 148 |
+
} catch (e) {
|
| 149 |
+
console.warn(`β οΈ Could not remove container ${containerName}:`, e);
|
| 150 |
}
|
|
|
|
|
|
|
| 151 |
}
|
| 152 |
|
| 153 |
// ============================================================
|
| 154 |
+
// π€ DEPLOY AGENT (USING DOCKER)
|
| 155 |
// ============================================================
|
| 156 |
+
async function deployAgentWithDocker(
|
| 157 |
+
versionDir: string,
|
| 158 |
+
containerName: string,
|
| 159 |
+
port: number
|
| 160 |
+
): Promise<number> {
|
| 161 |
+
// Res1's Dockerfile exposes ports; we map the same port internally
|
| 162 |
+
// but let Docker assign a random host port (-P) or we can map explicitly.
|
| 163 |
+
// Here we use explicit mapping: hostPort:containerPort
|
| 164 |
+
// Since Res1 listens on the port we give (via PORT env), we map that.
|
| 165 |
+
// But Res1's Dockerfile may have multiple EXPOSE; we'll use the first one.
|
| 166 |
+
// Simpler: just use -P (random host port) and inspect.
|
| 167 |
+
// But we need health check on that port.
|
| 168 |
+
// We'll build with PORT env set in the container.
|
| 169 |
+
// Actually, we can pass -e PORT=7000 during docker run.
|
| 170 |
+
// But Res1's code reads PORT env. So we set it.
|
| 171 |
+
const imageTag = `oracus-${Date.now()}`;
|
| 172 |
+
console.log(`π³ Building Docker image ${imageTag} from ${versionDir}...`);
|
| 173 |
+
await execDocker(`docker build -t ${imageTag} ${versionDir}`);
|
| 174 |
+
|
| 175 |
+
console.log(`π³ Running container ${containerName} with PORT=${port}...`);
|
| 176 |
+
// Run with -P to let Docker assign random host ports, but we also set PORT env.
|
| 177 |
+
await execDocker(
|
| 178 |
+
`docker run -d --name ${containerName} -P -e PORT=${port} ${imageTag}`
|
| 179 |
+
);
|
| 180 |
+
|
| 181 |
+
// Inspect to get mapped host port for the container's exposed port
|
| 182 |
+
const inspect = await execDocker(
|
| 183 |
+
`docker inspect ${containerName} --format='{{range $p, $conf := .NetworkSettings.Ports}}{{(index $conf 0).HostPort}} {{end}}'`
|
| 184 |
+
);
|
| 185 |
+
const ports = inspect.trim().split(/\s+/);
|
| 186 |
+
if (ports.length === 0) throw new Error('No ports exposed by container');
|
| 187 |
+
const hostPort = parseInt(ports[0], 10);
|
| 188 |
+
console.log(`β
Container running on host port ${hostPort}`);
|
| 189 |
+
|
| 190 |
+
// Health check on hostPort
|
| 191 |
+
console.log(`β³ Waiting for health check on port ${hostPort}...`);
|
| 192 |
+
let healthy = false;
|
| 193 |
+
for (let attempt = 0; attempt < 20; attempt++) {
|
| 194 |
+
try {
|
| 195 |
+
const res = await axios.get(`http://localhost:${hostPort}/health`, { timeout: 2000 });
|
| 196 |
+
if (res.status === 200) {
|
| 197 |
+
healthy = true;
|
| 198 |
+
break;
|
| 199 |
+
}
|
| 200 |
+
} catch (e) {
|
| 201 |
+
// ignore
|
| 202 |
+
}
|
| 203 |
+
await new Promise(r => setTimeout(r, 1000));
|
| 204 |
+
}
|
| 205 |
+
if (!healthy) {
|
| 206 |
+
throw new Error(`Health check failed on port ${hostPort}`);
|
| 207 |
+
}
|
| 208 |
+
console.log(`β
Health check passed on port ${hostPort}`);
|
| 209 |
+
return hostPort;
|
| 210 |
}
|
| 211 |
|
| 212 |
// ============================================================
|
| 213 |
+
// MEMORY COPY (between version directories)
|
| 214 |
// ============================================================
|
| 215 |
function copyMemoryFolder(srcPath: string, dstPath: string): void {
|
| 216 |
const srcMemory = path.join(srcPath, 'memory');
|
|
|
|
| 234 |
try {
|
| 235 |
if (fs.existsSync(FAILED_VERSIONS_FILE)) {
|
| 236 |
const data = fs.readFileSync(FAILED_VERSIONS_FILE, 'utf-8');
|
| 237 |
+
return JSON.parse(data);
|
|
|
|
| 238 |
}
|
| 239 |
} catch (e) {
|
| 240 |
console.warn('β οΈ Failed to load failed-versions.json');
|
|
|
|
| 244 |
|
| 245 |
function saveFailedVersion(sha: string): void {
|
| 246 |
try {
|
| 247 |
+
let failed = loadFailedVersions().filter(v => v !== sha);
|
|
|
|
| 248 |
failed.push(sha);
|
| 249 |
if (failed.length > MAX_FAILED_VERSIONS) {
|
| 250 |
failed = failed.slice(-MAX_FAILED_VERSIONS);
|
|
|
|
| 257 |
}
|
| 258 |
|
| 259 |
function isVersionFailed(sha: string): boolean {
|
| 260 |
+
return loadFailedVersions().includes(sha);
|
|
|
|
| 261 |
}
|
| 262 |
|
| 263 |
function clearFailedVersion(sha: string): void {
|
| 264 |
try {
|
| 265 |
+
const failed = loadFailedVersions().filter(v => v !== sha);
|
| 266 |
+
fs.writeFileSync(FAILED_VERSIONS_FILE, JSON.stringify(failed, null, 2));
|
|
|
|
| 267 |
console.log(`ποΈ Failed version ${sha} removed from tracking list.`);
|
| 268 |
} catch (e) {
|
| 269 |
console.error('β Failed to clear failed version:', e);
|
|
|
|
| 273 |
// ============================================================
|
| 274 |
// STATE
|
| 275 |
// ============================================================
|
| 276 |
+
let prodContainerName: string | null = null;
|
| 277 |
let prodVersionPath: string | null = null;
|
| 278 |
let prodCommitSha: string | null = null;
|
| 279 |
+
let prodHostPort: number | null = null;
|
| 280 |
let isUpdating: boolean = false;
|
| 281 |
|
| 282 |
// ============================================================
|
| 283 |
+
// π§ MAIN UPDATE LOGIC (Docker-based)
|
| 284 |
// ============================================================
|
| 285 |
async function checkAndUpdate(): Promise<void> {
|
| 286 |
if (isUpdating) {
|
|
|
|
| 289 |
}
|
| 290 |
|
| 291 |
isUpdating = true;
|
| 292 |
+
let testContainerName: string | null = null;
|
| 293 |
let testVersionPath: string | null = null;
|
| 294 |
let testCommitSha: string | null = null;
|
| 295 |
+
let testHostPort: number | null = null;
|
| 296 |
|
| 297 |
try {
|
| 298 |
console.log('π Checking for updates...');
|
|
|
|
| 303 |
console.log(`π Latest SHA: ${latestSha.substring(0, 7)}`);
|
| 304 |
console.log(`π Current Prod SHA: ${prodCommitSha ? prodCommitSha.substring(0, 7) : 'none'}`);
|
| 305 |
|
|
|
|
| 306 |
if (latestSha === prodCommitSha) {
|
| 307 |
console.log('β
No new code. Current version is already running.');
|
| 308 |
if (prodCommitSha && isVersionFailed(prodCommitSha)) {
|
|
|
|
| 312 |
return;
|
| 313 |
}
|
| 314 |
|
|
|
|
| 315 |
if (isVersionFailed(latestSha)) {
|
| 316 |
console.log(`β³ Version ${latestSha.substring(0, 7)} is marked as FAILED. Waiting for a newer version.`);
|
| 317 |
isUpdating = false;
|
| 318 |
return;
|
| 319 |
}
|
| 320 |
|
| 321 |
+
console.log(`π New version: ${latestSha.substring(0, 7)}. Deploying test container on port 2000...`);
|
| 322 |
+
|
| 323 |
testCommitSha = latestSha;
|
| 324 |
testVersionPath = path.join(CONFIG.VERSIONS_DIR, `v2_${Date.now()}`);
|
| 325 |
await cloneOrPull(CONFIG.RES1_REPO, testVersionPath);
|
| 326 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
// Copy memory from production if exists
|
| 328 |
if (prodVersionPath) {
|
| 329 |
copyMemoryFolder(prodVersionPath, testVersionPath);
|
|
|
|
| 331 |
console.log('β οΈ No previous version. Starting fresh without memory copy.');
|
| 332 |
}
|
| 333 |
|
| 334 |
+
// Deploy test container using Res1's Dockerfile, mapping internal 2000 to host
|
| 335 |
+
// But we use -P, so we don't need to specify host port; Docker assigns random.
|
| 336 |
+
// However, Res1's Dockerfile must have EXPOSE 2000. It does.
|
| 337 |
+
// We'll run with PORT=2000 env.
|
| 338 |
+
testContainerName = `oracus-test-${Date.now()}`;
|
| 339 |
+
testHostPort = await deployAgentWithDocker(testVersionPath, testContainerName, 2000);
|
| 340 |
+
console.log(`β
Test container running on host port ${testHostPort} (internal 2000)`);
|
| 341 |
+
|
| 342 |
+
// Promote to production (port 7000)
|
| 343 |
+
console.log(`π Promoting to production (internal port 7000)...`);
|
| 344 |
+
|
| 345 |
+
// Stop and remove test container
|
| 346 |
+
if (testContainerName) {
|
| 347 |
+
await stopAndRemoveContainer(testContainerName);
|
| 348 |
+
testContainerName = null;
|
| 349 |
}
|
| 350 |
|
| 351 |
+
// Build and run production container with internal port 7000
|
| 352 |
+
const prodContainerNameNew = `oracus-prod-${Date.now()}`;
|
| 353 |
+
const prodHostPortNew = await deployAgentWithDocker(testVersionPath, prodContainerNameNew, 7000);
|
| 354 |
+
console.log(`β
Production container running on host port ${prodHostPortNew} (internal 7000)`);
|
| 355 |
+
|
| 356 |
+
// Stop old production container
|
| 357 |
+
if (prodContainerName) {
|
| 358 |
+
await stopAndRemoveContainer(prodContainerName);
|
|
|
|
|
|
|
|
|
|
| 359 |
}
|
| 360 |
|
| 361 |
// Update state
|
| 362 |
+
prodContainerName = prodContainerNameNew;
|
| 363 |
prodVersionPath = testVersionPath;
|
| 364 |
prodCommitSha = testCommitSha;
|
| 365 |
+
prodHostPort = prodHostPortNew;
|
| 366 |
testVersionPath = null;
|
| 367 |
testCommitSha = null;
|
| 368 |
+
testHostPort = null;
|
| 369 |
|
| 370 |
if (prodCommitSha && isVersionFailed(prodCommitSha)) {
|
| 371 |
clearFailedVersion(prodCommitSha);
|
| 372 |
}
|
| 373 |
|
| 374 |
+
console.log(`β
Successfully deployed V2 (${latestSha.substring(0, 7)}) on host port ${prodHostPort}`);
|
| 375 |
|
| 376 |
} catch (error: any) {
|
| 377 |
console.error('β Deployment failed:', error);
|
| 378 |
|
| 379 |
+
if (testContainerName) {
|
| 380 |
+
await stopAndRemoveContainer(testContainerName);
|
| 381 |
+
testContainerName = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
}
|
| 383 |
|
|
|
|
| 384 |
if (testCommitSha) {
|
| 385 |
saveFailedVersion(testCommitSha);
|
| 386 |
}
|
| 387 |
|
|
|
|
| 388 |
await sendEmail(
|
| 389 |
'Deployment FAILED',
|
| 390 |
+
`Version: ${testCommitSha ? testCommitSha.substring(0, 7) : 'unknown'}\nError: ${error.message}\nPort ${prodHostPort || '?'} is unchanged.`
|
| 391 |
);
|
| 392 |
|
| 393 |
+
if (prodContainerName) {
|
| 394 |
+
console.log(`π Keeping old version running on host port ${prodHostPort}`);
|
|
|
|
| 395 |
} else {
|
| 396 |
console.log('β οΈ No previous version available.');
|
| 397 |
}
|
| 398 |
|
|
|
|
| 399 |
if (testVersionPath && fs.existsSync(testVersionPath)) {
|
| 400 |
try {
|
| 401 |
fs.rmSync(testVersionPath, { recursive: true, force: true });
|
|
|
|
| 427 |
const failed = loadFailedVersions();
|
| 428 |
res.writeHead(200, { 'Content-Type': 'application/json' });
|
| 429 |
res.end(JSON.stringify({
|
| 430 |
+
prodRunning: prodContainerName !== null,
|
| 431 |
+
prodHostPort: prodHostPort,
|
| 432 |
prodVersion: prodCommitSha ? prodCommitSha.substring(0, 7) : 'none',
|
| 433 |
isUpdating,
|
| 434 |
failedVersions: failed.map(s => s.substring(0, 7))
|