@deinossrl/dgp-agent 1.4.35 → 1.4.37

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.
Files changed (2) hide show
  1. package/index.mjs +63 -45
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -221,8 +221,14 @@ function saveSshKeyLocally(sshPrivateKey) {
221
221
  }
222
222
 
223
223
  const keyPath = getDgpSshKeyPath();
224
- // Normalizar saltos de línea
225
- let normalizedKey = sshPrivateKey.replace(/\\n/g, '\n').replace(/\r\n/g, '\n');
224
+
225
+ // Normalizar la key: los saltos de línea pueden venir ya interpretados o como \n literal
226
+ let normalizedKey = sshPrivateKey;
227
+
228
+ // Si no tiene saltos de línea reales pero tiene \n literal, convertirlos
229
+ if (!normalizedKey.includes('\n') && normalizedKey.includes('\\n')) {
230
+ normalizedKey = normalizedKey.replace(/\\n/g, '\n');
231
+ }
226
232
 
227
233
  // Verificar si la key tiene los delimitadores BEGIN/END
228
234
  if (!normalizedKey.includes('-----BEGIN')) {
@@ -346,7 +352,7 @@ function getPlinkPath() {
346
352
  }
347
353
 
348
354
  // Versión del agente
349
- const AGENT_VERSION = '1.4.35';
355
+ const AGENT_VERSION = '1.4.37';
350
356
  let AGENT_MODE = 'smart'; // Siempre inteligente
351
357
 
352
358
  // Configuración (prioridad: env vars > archivo config > platform config > defaults)
@@ -1179,9 +1185,31 @@ async function executeDeploy(command) {
1179
1185
  const fs = await import('fs');
1180
1186
  const path = await import('path');
1181
1187
  sshKeyPath = path.join(os.tmpdir(), `dgp_deploy_key_${Date.now()}`);
1182
- // Normalizar saltos de línea (pueden venir como \n literal del JSON)
1183
- const normalizedKey = sshPrivateKey.replace(/\\n/g, '\n').replace(/\r\n/g, '\n');
1188
+
1189
+ // Normalizar la key: los saltos de línea pueden venir ya interpretados o como \n literal
1190
+ let normalizedKey = sshPrivateKey;
1191
+
1192
+ // Si no tiene saltos de línea reales pero tiene \n literal, convertirlos
1193
+ if (!normalizedKey.includes('\n') && normalizedKey.includes('\\n')) {
1194
+ normalizedKey = normalizedKey.replace(/\\n/g, '\n');
1195
+ }
1196
+
1197
+ // Asegurar que termina con salto de línea
1198
+ if (!normalizedKey.endsWith('\n')) {
1199
+ normalizedKey += '\n';
1200
+ }
1201
+
1184
1202
  fs.writeFileSync(sshKeyPath, normalizedKey, { mode: 0o600 });
1203
+
1204
+ // En Windows, ajustar permisos con icacls
1205
+ if (process.platform === 'win32') {
1206
+ try {
1207
+ shellSync(`icacls "${sshKeyPath}" /inheritance:r /grant:r "%USERNAME%:(R,W)"`);
1208
+ } catch (e) {
1209
+ // Ignorar si falla
1210
+ }
1211
+ }
1212
+
1185
1213
  sshOptions = `-e "ssh -i ${sshKeyPath} -o StrictHostKeyChecking=no"`;
1186
1214
  }
1187
1215
 
@@ -1192,66 +1220,56 @@ async function executeDeploy(command) {
1192
1220
  // Mark as running
1193
1221
  await updateCommandStatus(id, 'running');
1194
1222
 
1195
- // Step 1: Git fetch and checkout
1196
- currentStep = 'git_checkout';
1197
- logCommand(`[1/5] Checkout branch ${branch}...`);
1198
- steps.push({ step: currentStep, status: 'running' });
1199
-
1200
- await shellAsync(`git fetch origin`);
1201
- await shellAsync(`git checkout ${branch}`);
1202
- await shellAsync(`git pull origin ${branch}`);
1223
+ // Determinar carpeta de proyecto en el servidor
1224
+ const projectFolder = params?.project_path || (environment === 'production'
1225
+ ? '/var/www/tenminuteia-prod/'
1226
+ : '/var/www/tenminuteia-staging/');
1203
1227
 
1204
- steps[steps.length - 1].status = 'success';
1205
- logSuccess(`Branch ${branch} updated`);
1228
+ // Construir base del comando SSH
1229
+ const sshBase = sshKeyPath
1230
+ ? `ssh -i ${sshKeyPath} -o StrictHostKeyChecking=no ${ssh_user}@${server_host}`
1231
+ : `ssh ${ssh_user}@${server_host}`;
1206
1232
 
1207
- // Step 2: Install dependencies
1208
- currentStep = 'npm_install';
1209
- logCommand(`[2/5] Installing dependencies...`);
1233
+ // Step 1: Git pull on server
1234
+ currentStep = 'git_pull_server';
1235
+ logCommand(`[1/4] Pulling latest code on server (${branch})...`);
1210
1236
  steps.push({ step: currentStep, status: 'running' });
1211
1237
 
1212
- await shellAsync(`npm ci`);
1238
+ const gitPullCmd = `${sshBase} "cd ${projectFolder} && git fetch origin && git checkout ${branch} && git pull origin ${branch}"`;
1239
+ await shellAsync(gitPullCmd);
1213
1240
 
1214
1241
  steps[steps.length - 1].status = 'success';
1215
- logSuccess(`Dependencies installed`);
1242
+ logSuccess(`Code updated on server`);
1216
1243
 
1217
- // Step 3: Build
1218
- currentStep = 'npm_build';
1219
- logCommand(`[3/5] Building application...`);
1244
+ // Step 2: Install dependencies on server
1245
+ currentStep = 'npm_install_server';
1246
+ logCommand(`[2/4] Installing dependencies on server...`);
1220
1247
  steps.push({ step: currentStep, status: 'running' });
1221
1248
 
1222
- await shellAsync(`npm run build`);
1249
+ const npmInstallCmd = `${sshBase} "cd ${projectFolder} && npm ci"`;
1250
+ await shellAsync(npmInstallCmd);
1223
1251
 
1224
1252
  steps[steps.length - 1].status = 'success';
1225
- logSuccess(`Build completed`);
1253
+ logSuccess(`Dependencies installed on server`);
1226
1254
 
1227
- // Step 4: Deploy via rsync
1228
- currentStep = 'rsync_deploy';
1229
- logCommand(`[4/5] Deploying to server...`);
1255
+ // Step 3: Build on server
1256
+ currentStep = 'npm_build_server';
1257
+ logCommand(`[3/4] Building application on server...`);
1230
1258
  steps.push({ step: currentStep, status: 'running' });
1231
1259
 
1232
- const deployFolder = environment === 'production'
1233
- ? '/var/www/tenminuteia-prod/'
1234
- : '/var/www/tenminuteia-staging/';
1235
-
1236
- // Rsync the dist folder (con SSH key si disponible)
1237
- const rsyncCmd = sshOptions
1238
- ? `rsync -avz --delete ${sshOptions} dist/ ${ssh_user}@${server_host}:${deployFolder}`
1239
- : `rsync -avz --delete dist/ ${ssh_user}@${server_host}:${deployFolder}`;
1240
- await shellAsync(rsyncCmd);
1260
+ const npmBuildCmd = `${sshBase} "cd ${projectFolder} && npm run build"`;
1261
+ await shellAsync(npmBuildCmd);
1241
1262
 
1242
1263
  steps[steps.length - 1].status = 'success';
1243
- logSuccess(`Files deployed to ${server_host}:${deployFolder}`);
1264
+ logSuccess(`Build completed on server`);
1244
1265
 
1245
- // Step 5: Reload nginx
1266
+ // Step 4: Reload nginx
1246
1267
  currentStep = 'reload_nginx';
1247
- logCommand(`[5/5] Reloading Nginx...`);
1268
+ logCommand(`[4/4] Reloading Nginx...`);
1248
1269
  steps.push({ step: currentStep, status: 'running' });
1249
1270
 
1250
- // SSH con key si disponible
1251
- const sshCmd = sshKeyPath
1252
- ? `ssh -i ${sshKeyPath} -o StrictHostKeyChecking=no ${ssh_user}@${server_host} "sudo nginx -t && sudo systemctl reload nginx"`
1253
- : `ssh ${ssh_user}@${server_host} "sudo nginx -t && sudo systemctl reload nginx"`;
1254
- await shellAsync(sshCmd);
1271
+ const reloadNginxCmd = `${sshBase} "sudo nginx -t && sudo systemctl reload nginx"`;
1272
+ await shellAsync(reloadNginxCmd);
1255
1273
 
1256
1274
  steps[steps.length - 1].status = 'success';
1257
1275
  logSuccess(`Nginx reloaded`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deinossrl/dgp-agent",
3
- "version": "1.4.35",
3
+ "version": "1.4.37",
4
4
  "description": "Agente local para Despliegue-GPT - Reporta el estado del repositorio Git a la plataforma TenMinute IA",
5
5
  "main": "index.mjs",
6
6
  "bin": {