@deinossrl/dgp-agent 1.4.21 → 1.4.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +88 -20
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -202,8 +202,67 @@ async function fetchPlatformConfig() {
|
|
|
202
202
|
// Variable para config de la plataforma (se carga async)
|
|
203
203
|
let platformConfig = null;
|
|
204
204
|
|
|
205
|
+
/**
|
|
206
|
+
* Obtiene la ruta del archivo de SSH key del agente
|
|
207
|
+
*/
|
|
208
|
+
function getDgpSshKeyPath() {
|
|
209
|
+
const os = require('os');
|
|
210
|
+
const path = require('path');
|
|
211
|
+
return path.join(os.homedir(), '.ssh', 'dgp_key');
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Guarda una SSH key en ~/.ssh/dgp_key
|
|
216
|
+
*/
|
|
217
|
+
function saveSshKeyLocally(sshPrivateKey) {
|
|
218
|
+
try {
|
|
219
|
+
const fs = require('fs');
|
|
220
|
+
const os = require('os');
|
|
221
|
+
const path = require('path');
|
|
222
|
+
|
|
223
|
+
const sshDir = path.join(os.homedir(), '.ssh');
|
|
224
|
+
if (!fs.existsSync(sshDir)) {
|
|
225
|
+
fs.mkdirSync(sshDir, { recursive: true, mode: 0o700 });
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const keyPath = getDgpSshKeyPath();
|
|
229
|
+
// Normalizar saltos de línea
|
|
230
|
+
const normalizedKey = sshPrivateKey.replace(/\\n/g, '\n').replace(/\r\n/g, '\n');
|
|
231
|
+
fs.writeFileSync(keyPath, normalizedKey, { mode: 0o600 });
|
|
232
|
+
|
|
233
|
+
return keyPath;
|
|
234
|
+
} catch (e) {
|
|
235
|
+
console.error('Error guardando SSH key:', e.message);
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Carga la SSH key guardada localmente (si existe)
|
|
242
|
+
*/
|
|
243
|
+
function loadLocalSshKey() {
|
|
244
|
+
try {
|
|
245
|
+
const fs = require('fs');
|
|
246
|
+
const keyPath = getDgpSshKeyPath();
|
|
247
|
+
if (fs.existsSync(keyPath)) {
|
|
248
|
+
return fs.readFileSync(keyPath, 'utf8');
|
|
249
|
+
}
|
|
250
|
+
return null;
|
|
251
|
+
} catch (e) {
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Verifica si hay una SSH key guardada localmente
|
|
258
|
+
*/
|
|
259
|
+
function hasLocalSshKey() {
|
|
260
|
+
const fs = require('fs');
|
|
261
|
+
return fs.existsSync(getDgpSshKeyPath());
|
|
262
|
+
}
|
|
263
|
+
|
|
205
264
|
// Versión del agente
|
|
206
|
-
const AGENT_VERSION = '1.4.
|
|
265
|
+
const AGENT_VERSION = '1.4.22';
|
|
207
266
|
let AGENT_MODE = 'smart'; // Siempre inteligente
|
|
208
267
|
|
|
209
268
|
// Configuración (prioridad: env vars > archivo config > platform config > defaults)
|
|
@@ -1260,10 +1319,32 @@ Analiza este error y sugiere soluciones.`;
|
|
|
1260
1319
|
*/
|
|
1261
1320
|
async function executeTestConnection(command) {
|
|
1262
1321
|
const { server_host, ssh_user, params } = command;
|
|
1263
|
-
|
|
1322
|
+
|
|
1323
|
+
// SSH Key: prioridad params > guardada localmente
|
|
1324
|
+
let sshPrivateKey = params?.ssh_private_key || null;
|
|
1325
|
+
let sshKeySource = 'ninguna';
|
|
1326
|
+
|
|
1327
|
+
// Si viene SSH key en params, guardarla localmente para futuro uso
|
|
1328
|
+
if (sshPrivateKey) {
|
|
1329
|
+
const savedPath = saveSshKeyLocally(sshPrivateKey);
|
|
1330
|
+
if (savedPath) {
|
|
1331
|
+
logSuccess(`SSH Key guardada en ${savedPath}`);
|
|
1332
|
+
sshKeySource = 'plataforma (guardada localmente)';
|
|
1333
|
+
} else {
|
|
1334
|
+
sshKeySource = 'plataforma';
|
|
1335
|
+
}
|
|
1336
|
+
} else if (hasLocalSshKey()) {
|
|
1337
|
+
// Si no viene en params pero hay una guardada, usarla
|
|
1338
|
+
sshPrivateKey = loadLocalSshKey();
|
|
1339
|
+
sshKeySource = `local (${getDgpSshKeyPath()})`;
|
|
1340
|
+
logInfo(`Usando SSH Key guardada: ${getDgpSshKeyPath()}`);
|
|
1341
|
+
}
|
|
1264
1342
|
|
|
1265
1343
|
logCommand(`=== Testing SSH Connection ===`);
|
|
1266
1344
|
logInfo(`Target: ${ssh_user || 'root'}@${server_host}`);
|
|
1345
|
+
if (sshKeySource !== 'ninguna') {
|
|
1346
|
+
logInfo(`SSH Key: ${sshKeySource}`);
|
|
1347
|
+
}
|
|
1267
1348
|
|
|
1268
1349
|
// Iniciar tracking de logs
|
|
1269
1350
|
currentCommandId = command.id;
|
|
@@ -1306,18 +1387,11 @@ async function executeTestConnection(command) {
|
|
|
1306
1387
|
// Step 2: SSH test
|
|
1307
1388
|
logInfo('Step 2: Testing SSH connection...');
|
|
1308
1389
|
|
|
1309
|
-
//
|
|
1390
|
+
// Usar la SSH key guardada en ~/.ssh/dgp_key (ya se guardó arriba si vino de params)
|
|
1310
1391
|
let sshKeyPath = null;
|
|
1311
|
-
if (sshPrivateKey) {
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
const path = await import('path');
|
|
1315
|
-
sshKeyPath = path.join(os.tmpdir(), `dgp_test_key_${Date.now()}`);
|
|
1316
|
-
// Normalizar saltos de línea (pueden venir como \n literal del JSON)
|
|
1317
|
-
const normalizedKey = sshPrivateKey.replace(/\\n/g, '\n').replace(/\r\n/g, '\n');
|
|
1318
|
-
fs.writeFileSync(sshKeyPath, normalizedKey, { mode: 0o600 });
|
|
1319
|
-
logInfo('SSH Key: Usando clave desde plataforma');
|
|
1320
|
-
await addCommandLog('info', 'Usando SSH key configurada en la plataforma');
|
|
1392
|
+
if (sshPrivateKey && hasLocalSshKey()) {
|
|
1393
|
+
sshKeyPath = getDgpSshKeyPath();
|
|
1394
|
+
await addCommandLog('info', `Usando SSH key: ${sshKeyPath}`);
|
|
1321
1395
|
}
|
|
1322
1396
|
|
|
1323
1397
|
const user = ssh_user || 'root';
|
|
@@ -1412,13 +1486,7 @@ async function executeTestConnection(command) {
|
|
|
1412
1486
|
}
|
|
1413
1487
|
}
|
|
1414
1488
|
|
|
1415
|
-
//
|
|
1416
|
-
if (sshKeyPath) {
|
|
1417
|
-
try {
|
|
1418
|
-
const fs = await import('fs');
|
|
1419
|
-
fs.unlinkSync(sshKeyPath);
|
|
1420
|
-
} catch (e) { /* ignorar */ }
|
|
1421
|
-
}
|
|
1489
|
+
// Nota: No limpiamos la SSH key porque está guardada permanentemente en ~/.ssh/dgp_key
|
|
1422
1490
|
|
|
1423
1491
|
// Determinar estado final
|
|
1424
1492
|
const overallSuccess = results.ssh.success;
|