curlRequestService = $curlRequestService; #$this->httpClient = $httpClient; $this->httpClient = HttpClient::create([ 'verify_peer' => false, // Ignorar la verificación del certificado SSL 'verify_host' => false, // Ignorar la verificación del nombre del host ]); $this->logger = $logger; // Añadimos el logger } protected function configure() { $this ->setDescription('Instala un ogLive en segundo plano') ->addArgument('isoUrl', InputArgument::REQUIRED, 'URL del ISO de ogLive') ->addArgument('transactionId', InputArgument::REQUIRED, 'ID de la transacción'); } protected function execute(InputInterface $input, OutputInterface $output) { ini_set('memory_limit', '-1'); ini_set('max_execution_time', '3000'); $isoUrl = $input->getArgument('isoUrl'); $transactionId = $input->getArgument('transactionId'); // Iniciamos los logs para rastrear el proceso $this->logger->info('Starting ogLive installation', ['transactionId' => $transactionId]); try { // Log: comenzando la descarga del ISO $this->logger->info('Starting download', ['isoUrl' => $isoUrl]); // Llamar al servicio para iniciar la instalación $installResult = $this->curlRequestService->callOgLive("download " . escapeshellarg($isoUrl)); // Log: descarga finalizada $this->logger->info('Download finished', ['transactionId' => $transactionId, 'installResult' => $installResult]); // Verificar el resultado de la instalación basado en el output del comando $status = 'success'; $messageText = 'ogLive client installed successfully'; $exitCode = $installResult['exitCode']; if (isset($installResult['output']['error'])) { // Si hay un error, asignar el estado y mensaje adecuado $status = $installResult['output']['error']; $messageText = $installResult['output']['message']; // Log: la instalación falló $this->logger->error('Installation failed', ['transactionId' => $transactionId, 'error' => $messageText]); } elseif ($installResult['exitCode'] !== 0) { // Si hubo un exitCode distinto de 0, manejar como error desconocido $status = 'UNKNOWN_ERROR'; $messageText = 'An unknown error occurred during the installation process.'; $this->logger->error('Unknown installation error', ['transactionId' => $transactionId, 'exitCode' => $installResult['exitCode']]); } else { // Log: instalación completada con éxito $messageText = $installResult['output']['message']; $this->logger->info('Installation completed successfully', ['transactionId' => $transactionId]); } // Preparar los datos para el webhook según el resultado $webhookData = [ 'ogCoreId' => $transactionId, 'status' => $status, 'code' => ($exitCode === 0) ? 200 : $exitCode, // Cambiar a 200 si es éxito 'message' => $messageText , ]; $this->logger->info('Installation completed with details', ['installResult' => $installResult]); $this->logger->info('Webhook data to be sent: ', ['webhookData' => $webhookData]); #$webhookUrl = "https://172.17.8.90:8443/og-lives/install/webhook"; $webhookUrl = "{$this->ogCoreApiUrl}/og-lives/install/webhook"; // Log: enviando datos al webhook $this->logger->info('Sending data to webhook', ['webhookUrl' => $webhookUrl, 'webhookData' => $webhookData]); // Llamar al webhook para notificar el resultado $this->notifyWebhook($webhookUrl, $webhookData); $this->logger->info('Notify webhook con exito'); // Log: notificación al webhook finalizada $this->logger->info('Webhook notification sent', ['transactionId' => $transactionId]); } catch (Exception $e) { // Log: error en la instalación $this->logger->error('Failed to complete ogLive installation', ['transactionId' => $transactionId, 'exception' => $e->getMessage()]); // Manejar errores y enviar notificación de fallo al webhook $webhookData = [ 'ogCoreId' => $transactionId, 'status' => 'failure', 'code' => 500, 'message' => $e->getMessage() ]; $webhookUrl = "https://172.17.8.90:8443/og-lives/install/webhook"; $this->notifyWebhook($webhookUrl, $webhookData); $this->logger->info('Notify webhook terminado con errores'); // Log: notificación de error enviada al webhook $this->logger->info('Failure notification sent to webhook', ['transactionId' => $transactionId]); } // Log: proceso de instalación terminado $this->logger->info('Installation process ended', ['transactionId' => $transactionId]); return Command::SUCCESS; } /** * Enviar el resultado al webhook */ private function notifyWebhook(string $webhookUrl, array $webhookData): void { try { $this->logger->info('Enter notify webhook'); $this->logger->info('Data to be sent', ['webhookData' => $webhookData]); $this->httpClient->request('POST', $webhookUrl, [ 'headers' => [ 'accept' => 'application/json', 'Content-Type' => 'application/json', ], 'body' => json_encode(['webhookData' => $webhookData]), ]); // Log: éxito al enviar la notificación $this->logger->info('Webhook data sent successfully', ['webhookUrl' => $webhookUrl, 'webhookData' => $webhookData]); } catch (Exception $e) { // Log: error al enviar al webhook $this->logger->error('Error sending webhook notification', ['webhookUrl' => $webhookUrl, 'exception' => $e->getMessage()]); } } }