219169ba9f
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
76 lines
3.9 KiB
JavaScript
76 lines
3.9 KiB
JavaScript
// Genera en el navegador el instalador (.bat) del agente GLPI con el servidor y el token ya
|
|
// puestos. No pasa por el backend: el token en claro solo existe en la pantalla (en BD queda su hash).
|
|
export const GLPI_AGENT_VERSION = '1.21';
|
|
|
|
export function buildInstallerBat({ serverUrl, token, tag }) {
|
|
const ver = GLPI_AGENT_VERSION;
|
|
const msiName = `GLPI-Agent-${ver}-x64.msi`;
|
|
const safeTag = String(tag || '').replace(/[^\w .-]/g, '').trim();
|
|
const lines = [
|
|
'@echo off',
|
|
'setlocal',
|
|
`title Instalador GLPI Agent ${ver} - Project`,
|
|
`rem Generado por Project (${serverUrl}). Si el .msi esta junto a este archivo se usa; si no, se descarga.`,
|
|
'net session >nul 2>&1',
|
|
'if %errorlevel% neq 0 (',
|
|
' echo Solicitando permisos de administrador...',
|
|
` powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"`,
|
|
' exit /b',
|
|
')',
|
|
// Verifica el token ANTES de instalar: 400 = valido (llego al servidor y fallo solo el cuerpo de prueba),
|
|
// 401 = token invalido, 000 = sin conexion. Asi un token malo nunca deja el agente mal configurado.
|
|
'echo Verificando el token con el servidor...',
|
|
'set "CODE="',
|
|
`for /f %%C in ('curl.exe -s -o nul -w "%%{http_code}" -u project:${token} -X POST "${serverUrl}" -d x 2^>nul') do set "CODE=%%C"`,
|
|
'if "%CODE%"=="401" (',
|
|
' echo ERROR: el token no es valido en este servidor. Genere uno en Equipos - Agentes y descargue el instalador de nuevo.',
|
|
' pause',
|
|
' exit /b 2',
|
|
')',
|
|
'if "%CODE%"=="000" (',
|
|
` echo ERROR: no hay conexion con ${serverUrl}. Revise la red o internet del equipo.`,
|
|
' pause',
|
|
' exit /b 3',
|
|
')',
|
|
'if not defined CODE echo Aviso: no se pudo verificar el token ^(curl no disponible^). Se continua con la instalacion.',
|
|
`set "MSI=%~dp0${msiName}"`,
|
|
'if exist "%MSI%" goto instalar',
|
|
`set "MSI=%TEMP%\\${msiName}"`,
|
|
'if exist "%MSI%" goto instalar',
|
|
`echo Descargando GLPI Agent ${ver}...`,
|
|
`powershell -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -UseBasicParsing 'https://github.com/glpi-project/glpi-agent/releases/download/${ver}/${msiName}' -OutFile '%MSI%'"`,
|
|
'if errorlevel 1 (',
|
|
` echo ERROR: no se pudo descargar. Copie ${msiName} junto a este archivo y ejecute de nuevo.`,
|
|
' pause',
|
|
' exit /b 1',
|
|
')',
|
|
':instalar',
|
|
'echo Instalando y configurando el agente...',
|
|
`start /wait msiexec /i "%MSI%" /qn /norestart SERVER="${serverUrl}" USER="project" PASSWORD="${token}"${safeTag ? ` TAG="${safeTag}"` : ''} RUNNOW=1`,
|
|
'set RC=%errorlevel%',
|
|
'if %RC% neq 0 if %RC% neq 3010 (',
|
|
' echo ERROR: la instalacion fallo ^(codigo %RC%^).',
|
|
' pause',
|
|
' exit /b %RC%',
|
|
')',
|
|
// Si el agente ya estaba instalado, msiexec no reescribe su configuracion: se fuerza en el registro.
|
|
'echo Aplicando configuracion...',
|
|
`powershell -NoProfile -ExecutionPolicy Bypass -Command "$k='HKLM:\\SOFTWARE\\GLPI-Agent'; Set-ItemProperty -Path $k -Name server -Value '${serverUrl}'; Set-ItemProperty -Path $k -Name user -Value 'project'; Set-ItemProperty -Path $k -Name password -Value '${token}';${safeTag ? ` Set-ItemProperty -Path $k -Name tag -Value '${safeTag}';` : ''} Restart-Service glpi-agent"`,
|
|
'echo.',
|
|
'sc query glpi-agent | find "RUNNING" >nul && (echo Listo: el agente esta instalado y en ejecucion. El equipo aparecera en Project en unos minutos.) || (echo Instalado, pero el servicio no esta en ejecucion. Revise services.msc.)',
|
|
'pause',
|
|
];
|
|
return lines.join('\r\n') + '\r\n';
|
|
}
|
|
|
|
export function downloadTextFile(filename, content) {
|
|
const url = URL.createObjectURL(new Blob([content], { type: 'application/octet-stream' }));
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = filename;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
URL.revokeObjectURL(url);
|
|
}
|