@deinossrl/dgp-agent 1.4.36 → 1.4.38

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 +72 -11
  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.36';
355
+ const AGENT_VERSION = '1.4.38';
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
 
@@ -1202,16 +1230,49 @@ async function executeDeploy(command) {
1202
1230
  ? `ssh -i ${sshKeyPath} -o StrictHostKeyChecking=no ${ssh_user}@${server_host}`
1203
1231
  : `ssh ${ssh_user}@${server_host}`;
1204
1232
 
1205
- // Step 1: Git pull on server
1206
- currentStep = 'git_pull_server';
1207
- logCommand(`[1/4] Pulling latest code on server (${branch})...`);
1233
+ // Step 1: Git clone/pull on server
1234
+ currentStep = 'git_setup_server';
1235
+ logCommand(`[1/4] Setting up repository on server...`);
1208
1236
  steps.push({ step: currentStep, status: 'running' });
1209
1237
 
1210
- const gitPullCmd = `${sshBase} "cd ${projectFolder} && git fetch origin && git checkout ${branch} && git pull origin ${branch}"`;
1211
- await shellAsync(gitPullCmd);
1238
+ // Verificar si el directorio existe
1239
+ const checkDirCmd = `${sshBase} "test -d ${projectFolder} && echo EXISTS || echo NOT_EXISTS"`;
1240
+ const dirCheck = await shellAsync(checkDirCmd);
1241
+ const dirExists = dirCheck.stdout.trim() === 'EXISTS';
1242
+
1243
+ if (!dirExists) {
1244
+ // El directorio no existe - clonar el repositorio
1245
+ logInfo(`Repository directory doesn't exist, cloning...`);
1246
+
1247
+ // Obtener la URL del repositorio desde el remoto local
1248
+ let repoUrl = '';
1249
+ try {
1250
+ repoUrl = shellSync('git config --get remote.origin.url').trim();
1251
+ } catch (e) {
1252
+ throw new Error('Cannot get repository URL from local git config');
1253
+ }
1254
+
1255
+ logInfo(`Cloning from: ${repoUrl}`);
1256
+
1257
+ // Crear directorio padre y clonar
1258
+ const parentDir = projectFolder.substring(0, projectFolder.lastIndexOf('/'));
1259
+ const cloneCmd = `${sshBase} "mkdir -p ${parentDir} && git clone ${repoUrl} ${projectFolder}"`;
1260
+ await shellAsync(cloneCmd);
1261
+
1262
+ // Checkout de la rama correcta
1263
+ const checkoutCmd = `${sshBase} "cd ${projectFolder} && git checkout ${branch}"`;
1264
+ await shellAsync(checkoutCmd);
1265
+
1266
+ logSuccess(`Repository cloned successfully`);
1267
+ } else {
1268
+ // El directorio existe - hacer pull
1269
+ logInfo(`Repository exists, pulling latest changes...`);
1270
+ const gitPullCmd = `${sshBase} "cd ${projectFolder} && git fetch origin && git checkout ${branch} && git pull origin ${branch}"`;
1271
+ await shellAsync(gitPullCmd);
1272
+ logSuccess(`Code updated on server`);
1273
+ }
1212
1274
 
1213
1275
  steps[steps.length - 1].status = 'success';
1214
- logSuccess(`Code updated on server`);
1215
1276
 
1216
1277
  // Step 2: Install dependencies on server
1217
1278
  currentStep = 'npm_install_server';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deinossrl/dgp-agent",
3
- "version": "1.4.36",
3
+ "version": "1.4.38",
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": {