diff --git a/src/Command/ExecutePendingTracesCommand.php b/src/Command/ExecutePendingTracesCommand.php index 7f37f43..e110749 100644 --- a/src/Command/ExecutePendingTracesCommand.php +++ b/src/Command/ExecutePendingTracesCommand.php @@ -4,26 +4,11 @@ declare(strict_types=1); namespace App\Command; -use App\Controller\OgAgent\CreateImageAction; -use App\Controller\DeployImageAction; -use App\Controller\OgAgent\PartitionAssistantAction; -use App\Controller\OgAgent\RunScriptAction; -use App\Dto\Input\CommandExecuteInput; -use App\Dto\Input\DeployImageInput; -use App\Dto\Input\PartitionInput; -use App\Dto\Input\PartitionPostInput; -use App\Dto\Input\MultipleClientsInput; -use App\Dto\Input\BootClientsInput; -use App\Dto\Output\ClientOutput; -use App\Dto\Output\PartitionOutput; use App\Entity\Client; use App\Model\ClientStatus; -use App\Entity\Image; -use App\Entity\ImageImageRepository; -use App\Entity\Partition; use App\Entity\Trace; -use App\Model\CommandTypes; use App\Model\TraceStatus; +use App\Service\PendingTraceExecutionService; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; @@ -37,13 +22,7 @@ class ExecutePendingTracesCommand extends Command { public function __construct( private readonly EntityManagerInterface $entityManager, - private readonly CreateImageAction $createImageAction, - private readonly DeployImageAction $deployImageAction, - private readonly PartitionAssistantAction $partitionAssistantAction, - private readonly RunScriptAction $runScriptAction, - private readonly \App\Controller\OgAgent\RebootAction $rebootAction, - private readonly \App\Controller\OgAgent\PowerOffAction $powerOffAction, - private readonly \App\Controller\OgAgent\LoginAction $loginAction, + private readonly PendingTraceExecutionService $pendingTraceExecutionService, private readonly LoggerInterface $logger, ) { parent::__construct(); @@ -87,7 +66,7 @@ class ExecutePendingTracesCommand extends Command $io->info("Processing trace {$trace->getUuid()} with command: {$trace->getCommand()}"); - $result = $this->executeTrace($trace); + $result = $this->pendingTraceExecutionService->executeTrace($trace); if ($result) { $processedCount++; @@ -99,11 +78,6 @@ class ExecutePendingTracesCommand extends Command } catch (\Exception $e) { $errorCount++; $io->error("Error processing trace {$trace->getUuid()}: " . $e->getMessage()); - - $trace->setStatus(TraceStatus::FAILED); - $trace->setOutput($e->getMessage()); - $trace->setFinishedAt(new \DateTime()); - $this->entityManager->persist($trace); } } @@ -115,439 +89,4 @@ class ExecutePendingTracesCommand extends Command return Command::SUCCESS; } - - private function executeTrace(Trace $trace): bool - { - $command = $trace->getCommand(); - $input = $trace->getInput() ?? []; - $client = $trace->getClient(); - - $this->logger->info('Executing trace', [ - 'command' => $command, - 'input' => $input, - 'client' => $client->getUuid() - ]); - - if (!$client) { - throw new \Exception("No client associated with trace"); - } - - $trace->setExecutedAt(new \DateTime()); - $this->entityManager->persist($trace); - $this->entityManager->flush(); - - try { - switch ($command) { - case CommandTypes::CREATE_IMAGE: - $this->logger->info('Executing create image trace', [ - 'input' => $input, - 'client' => $client->getUuid() - ]); - return $this->executeCreateImage($trace, $input); - - case CommandTypes::DEPLOY_IMAGE: - $this->logger->info('Executing deploy image trace', [ - 'input' => $input, - 'client' => $client->getUuid() - ]); - return $this->executeDeployImage($trace, $input); - - case CommandTypes::PARTITION_AND_FORMAT: - $this->logger->info('Executing partition and format trace', [ - 'input' => $input, - 'client' => $client->getUuid() - ]); - return $this->executePartitionAssistant($trace, $input); - - case CommandTypes::RUN_SCRIPT: - $this->logger->info('Executing run script trace', [ - 'input' => $input, - 'client' => $client->getUuid() - ]); - return $this->executeRunScript($trace, $input); - - case CommandTypes::POWER_ON: - $this->logger->info('Executing power on trace', [ - 'input' => $input, - 'client' => $client->getUuid() - ]); - return $this->executePowerOn($trace, $input); - - case CommandTypes::REBOOT: - $this->logger->info('Executing reboot trace', [ - 'input' => $input, - 'client' => $client->getUuid() - ]); - return $this->executeReboot($trace, $input); - - case CommandTypes::SHUTDOWN: - $this->logger->info('Executing shutdown trace', [ - 'input' => $input, - 'client' => $client->getUuid() - ]); - return $this->executeShutdown($trace, $input); - - case CommandTypes::LOGIN: - $this->logger->info('Executing login trace', [ - 'input' => $input, - 'client' => $client->getUuid() - ]); - return $this->executeLogin($trace, $input); - - - default: - throw new \Exception("Unsupported command type: $command"); - } - } catch (\Exception $e) { - $this->logger->error('Error executing trace', [ - 'command' => $command, - 'input' => $input, - 'client' => $client->getUuid(), - 'error' => $e->getMessage() - ]); - $trace->setStatus(TraceStatus::FAILED); - $trace->setOutput($e->getMessage()); - $trace->setFinishedAt(new \DateTime()); - $this->entityManager->persist($trace); - $this->entityManager->flush(); - - throw $e; - } - } - - private function executeCreateImage(Trace $trace, array $input): bool - { - $client = $trace->getClient(); - - $image = null; - - if (isset($input['action']) && $input['action'] === 'create') { - $image = new Image(); - $image->setName($input['imageName'] ?? $input['name']); - $image->setType($input['type'] ?? 'monolithic'); - $image->setRemotePc($input['remotePc'] ?? false); - $image->setIsGlobal($input['isGlobal'] ?? false); - $image->setClient($client); - $this->entityManager->persist($image); - } elseif (isset($input['action']) && $input['action'] === 'update') { - if (!isset($input['imageUuid'])) { - throw new \Exception("Image UUID not found in trace input for update operation"); - } - - $image = $this->entityManager->getRepository(Image::class) - ->findOneBy(['uuid' => $input['imageUuid']]); - - if (!$image) { - throw new \Exception("Image not found with UUID: {$input['imageUuid']} for update operation"); - } - } else { - throw new \Exception("Either 'create' or 'update' field must be set to true in trace input"); - } - - if (isset($input['diskNumber']) && isset($input['partitionNumber'])) { - $partitionInfo = [ - 'numDisk' => $input['diskNumber'], - 'numPartition' => $input['partitionNumber'], - 'partitionCode' => $input['partitionCode'] ?? 'LINUX', - 'filesystem' => $input['filesystem'] ?? 'EXT4' - ]; - $image->setPartitionInfo(json_encode($partitionInfo)); - } - - $partition = null; - if (isset($input['diskNumber']) && isset($input['partitionNumber'])) { - $partition = $this->entityManager->getRepository(Partition::class) - ->findOneBy([ - 'client' => $client, - 'diskNumber' => $input['diskNumber'], - 'partitionNumber' => $input['partitionNumber'] - ]); - - if (!$partition) { - throw new \Exception("Partition not found for client {$client->getUuid()} with disk {$input['diskNumber']} and partition {$input['partitionNumber']}"); - } - } - - $this->logger->info('Creating image', [ - 'image' => $image, - 'partition' => $partition, - 'client' => $client->getUuid() - ]); - - try { - $response = $this->createImageAction->__invoke( - queue: false, - image: $image, - partition: $partition, - client: $client, - gitRepositoryName: $input['gitRepositoryName'] ?? null, - existingTrace: $trace - ); - - if (json_decode($response->getContent(), true)['status'] === 200) { - $trace->setStatus(TraceStatus::IN_PROGRESS); - $trace->setFinishedAt(new \DateTime()); - $this->entityManager->persist($trace); - $this->entityManager->flush(); - return true; - } - - return false; - } catch (\Exception $e) { - - return false; - } - } - - private function executeDeployImage(Trace $trace, array $input): bool - { - $client = $trace->getClient(); - - if (!isset($input['imageImageRepository']) && !isset($input['image'])) { - throw new \Exception("ImageImageRepository UUID or image UUID not found in trace input for deploy image"); - } - - $imageImageRepository = $this->entityManager->getRepository(ImageImageRepository::class) - ->findOneBy(['uuid' => $input['imageImageRepository'] ?? $input['image']]); - - if (!$imageImageRepository) { - throw new \Exception("ImageImageRepository not found with UUID: {$input['imageImageRepository']}"); - } - - $diskNumber = isset($input['diskNumber']) ? $input['diskNumber'] : $input['numDisk']; - $partitionNumber = isset($input['partitionNumber']) ? $input['partitionNumber'] : $input['numPartition']; - - if (!$diskNumber || !$partitionNumber) { - throw new \Exception("Disk number or partition number not found in trace input for deploy image"); - } - - $deployInput = new DeployImageInput(); - $deployInput->method = $input['method'] ?? 'unicast'; - $deployInput->type = $input['type'] ?? 'monolithic'; - $deployInput->diskNumber = (int) $diskNumber; - $deployInput->partitionNumber = (int) $partitionNumber; - $deployInput->mcastMode = $input['mcastMode'] ?? null; - $deployInput->mcastSpeed = $input['mcastSpeed'] ?? null; - $deployInput->mcastPort = $input['mcastPort'] ?? null; - $deployInput->mcastIp = $input['mcastIp'] ?? null; - $deployInput->maxClients = $input['maxClients'] ?? null; - $deployInput->maxTime = $input['maxTime'] ?? null; - $deployInput->p2pMode = $input['p2pMode'] ?? null; - $deployInput->p2pTime = $input['p2pTime'] ?? null; - $deployInput->queue = $input['queue'] ?? false; - - $deployInput->clients = [new ClientOutput($client)]; - - $this->logger->info('Deploying image', [ - 'input' => $deployInput, - 'client' => $client->getUuid() - ]); - - $response = $this->deployImageAction->__invoke($deployInput, $imageImageRepository, $trace); - - $clientJobs = $response['data']; - - $clientKey = '/clients/' . $client->getUuid(); - $jobId = $clientJobs[$clientKey] ?? []; - - $trace->setJobId($jobId['jobId']); - $trace->setStatus(TraceStatus::IN_PROGRESS); - $trace->setFinishedAt(new \DateTime()); - $this->entityManager->persist($trace); - $this->entityManager->flush(); - - return true; - } - - private function executePartitionAssistant(Trace $trace, array $input): bool - { - $client = $trace->getClient(); - - $partitionInput = new PartitionPostInput(); - $partitionInput->clients = [new ClientOutput($client)]; - - $partitions = []; - - foreach ($input as $item) { - $partitionInputObj = new PartitionInput(); - $partitionInputObj->diskNumber = $item['diskNumber']; - $partitionInputObj->partitionNumber = (int)$item['partitionNumber']; - $partitionInputObj->partitionCode = $item['partitionCode'] ?? 'LINUX'; - $partitionInputObj->size = $item['size']; - $partitionInputObj->filesystem = $item['filesystem'] ?? 'EXT4'; - $partitionInputObj->format = $item['format'] ?? false; - $partitions[] = $partitionInputObj; - } - - $partitionInput->partitions = $partitions; - $partitionInput->queue = false; - - $this->logger->info('Executing partition assistant', [ - 'input' => $partitionInput, - 'client' => $client->getUuid() - ]); - - try { - $response = $this->partitionAssistantAction->__invoke($partitionInput, $trace); - - if (json_decode($response->getContent(), true)['status'] === 200) { - $trace->setStatus(TraceStatus::SUCCESS); - $trace->setFinishedAt(new \DateTime()); - $this->entityManager->persist($trace); - $this->entityManager->flush(); - return true; - } - - return false; - } catch (\Exception $e) { - - return false; - } - } - - private function executeRunScript(Trace $trace, array $input): bool - { - $client = $trace->getClient(); - - if (!isset($input['script'])) { - throw new \Exception("Script not found in trace input"); - } - - $commandExecuteInput = new CommandExecuteInput(); - $commandExecuteInput->clients = [new ClientOutput($client)]; - $commandExecuteInput->script = $input['script']; - $commandExecuteInput->queue = false; - - $this->logger->info('Executing run script', [ - 'input' => $commandExecuteInput, - 'client' => $client->getUuid() - ]); - - try { - $response = $this->runScriptAction->__invoke($commandExecuteInput, $trace); - - return $response->getStatusCode() === 200; - } catch (\Exception $e) { - $this->logger->error('Error executing run script', [ - 'input' => $commandExecuteInput, - 'client' => $client->getUuid(), - 'error' => $e->getMessage() - ]); - return false; - } - } - - private function executePowerOn(Trace $trace, array $input): bool - { - $trace->setStatus(TraceStatus::SUCCESS); - $trace->setFinishedAt(new \DateTime()); - $this->entityManager->persist($trace); - $this->entityManager->flush(); - return true; - } - - private function executeReboot(Trace $trace, array $input): bool - { - $client = $trace->getClient(); - - $multipleClientsInput = new MultipleClientsInput(); - $multipleClientsInput->clients = [new ClientOutput($client)]; - $multipleClientsInput->queue = false; - - try { - $response = $this->rebootAction->__invoke($multipleClientsInput); - - if ($response->getStatusCode() === 200) { - $trace->setStatus(TraceStatus::SUCCESS); - $trace->setFinishedAt(new \DateTime()); - $this->entityManager->persist($trace); - $this->entityManager->flush(); - return true; - } - - return false; - } catch (\Exception $e) { - return false; - } - } - - private function executeShutdown(Trace $trace, array $input): bool - { - $client = $trace->getClient(); - - $multipleClientsInput = new MultipleClientsInput(); - $multipleClientsInput->clients = [new ClientOutput($client)]; - $multipleClientsInput->queue = false; - - try { - $response = $this->powerOffAction->__invoke($multipleClientsInput); - - if ($response->getStatusCode() === 200) { - $trace->setStatus(TraceStatus::SUCCESS); - $trace->setFinishedAt(new \DateTime()); - $this->entityManager->persist($trace); - $this->entityManager->flush(); - return true; - } - - return false; - } catch (\Exception $e) { - return false; - } - } - - private function executeLogin(Trace $trace, array $input): bool - { - $client = $trace->getClient(); - - if ($client->getStatus() !== ClientStatus::OG_LIVE) { - $this->logger->info('Login trace deferred: client not in OGLive', [ - 'client' => $client->getUuid(), - 'status' => $client->getStatus(), - ]); - return false; - } - - if (isset($input['partition'])) { - $partition = $this->entityManager->getRepository(Partition::class) - ->findOneBy(['uuid' => $input['partition']]); - if (!$partition) { - throw new \Exception("Partition not found with UUID: {$input['partition']}"); - } - } elseif (isset($input['dsk']) && isset($input['par'])) { - $partition = $this->entityManager->getRepository(Partition::class) - ->findOneBy([ - 'client' => $client, - 'diskNumber' => $input['dsk'], - 'partitionNumber' => $input['par'], - ]); - if (!$partition) { - throw new \Exception("Partition not found for disk {$input['dsk']} partition {$input['par']}"); - } - } else { - throw new \Exception("Partition not found in trace input"); - } - - $bootClientsInput = new BootClientsInput(); - $bootClientsInput->clients = [new ClientOutput($client)]; - $bootClientsInput->partition = new PartitionOutput($partition); - - try { - $response = $this->loginAction->__invoke($bootClientsInput); - - if ($response->getStatusCode() === 200) { - $trace->setStatus(TraceStatus::SUCCESS); - $trace->setFinishedAt(new \DateTime()); - $this->entityManager->persist($trace); - $this->entityManager->flush(); - return true; - } - - return false; - } catch (\Exception $e) { - return false; - } - } - - -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/Controller/OgAgent/Webhook/StatusController.php b/src/Controller/OgAgent/Webhook/StatusController.php index 2174ff5..a2a0a22 100644 --- a/src/Controller/OgAgent/Webhook/StatusController.php +++ b/src/Controller/OgAgent/Webhook/StatusController.php @@ -20,6 +20,7 @@ use App\Model\SoftwareTypes; use App\Model\TraceStatus; use App\Service\CreatePartitionService; use App\Service\Trace\CreateService; +use App\Service\PendingTraceExecutionService; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -56,6 +57,7 @@ class StatusController extends AbstractController protected readonly LoggerInterface $logger, protected readonly CreateService $createService, protected readonly HttpClientInterface $httpClient, + protected readonly PendingTraceExecutionService $pendingTraceExecutionService, ) { } @@ -439,6 +441,16 @@ class StatusController extends AbstractController break; } } + if (isset($client) && $client) { + try { + $this->pendingTraceExecutionService->executeNextTraceForClient($client); + } catch (\Exception $e) { + $this->logger->error('Error proactively processing next trace for client', [ + 'ip' => $client->getIp(), + 'error' => $e->getMessage() + ]); + } + } return new JsonResponse(data: 'Webhook finished', status: Response::HTTP_OK); } diff --git a/src/Service/PendingTraceExecutionService.php b/src/Service/PendingTraceExecutionService.php new file mode 100644 index 0000000..332683d --- /dev/null +++ b/src/Service/PendingTraceExecutionService.php @@ -0,0 +1,447 @@ +getStatus() === ClientStatus::BUSY) { + return false; + } + + $trace = $this->entityManager->getRepository(Trace::class) + ->createQueryBuilder('t') + ->select('t') + ->where('t.status = :status') + ->andWhere('t.client = :client') + ->setParameter('status', TraceStatus::PENDING) + ->setParameter('client', $client) + ->orderBy('t.id', 'ASC') + ->setMaxResults(1) + ->getQuery() + ->getOneOrNullResult(); + + if (!$trace) { + return false; + } + + $this->logger->info("Proactively executing next pending trace for client {$client->getIp()}"); + return $this->executeTrace($trace); + } + + public function executeTrace(Trace $trace): bool + { + $command = $trace->getCommand(); + $input = $trace->getInput() ?? []; + $client = $trace->getClient(); + + $this->logger->info('Executing trace', [ + 'command' => $command, + 'input' => $input, + 'client' => $client->getUuid() + ]); + + if (!$client) { + throw new \Exception("No client associated with trace"); + } + + $trace->setExecutedAt(new \DateTime()); + $this->entityManager->persist($trace); + $this->entityManager->flush(); + + try { + switch ($command) { + case CommandTypes::CREATE_IMAGE: + return $this->executeCreateImage($trace, $input); + + case CommandTypes::DEPLOY_IMAGE: + return $this->executeDeployImage($trace, $input); + + case CommandTypes::PARTITION_AND_FORMAT: + return $this->executePartitionAssistant($trace, $input); + + case CommandTypes::RUN_SCRIPT: + return $this->executeRunScript($trace, $input); + + case CommandTypes::POWER_ON: + return $this->executePowerOn($trace, $input); + + case CommandTypes::REBOOT: + return $this->executeReboot($trace, $input); + + case CommandTypes::SHUTDOWN: + return $this->executeShutdown($trace, $input); + + case CommandTypes::LOGIN: + return $this->executeLogin($trace, $input); + + default: + throw new \Exception("Unsupported command type: $command"); + } + } catch (\Exception $e) { + $this->logger->error('Error executing trace', [ + 'command' => $command, + 'input' => $input, + 'client' => $client->getUuid(), + 'error' => $e->getMessage() + ]); + $trace->setStatus(TraceStatus::FAILED); + $trace->setOutput($e->getMessage()); + $trace->setFinishedAt(new \DateTime()); + $this->entityManager->persist($trace); + $this->entityManager->flush(); + + throw $e; + } + } + + private function executeCreateImage(Trace $trace, array $input): bool + { + $client = $trace->getClient(); + + $image = null; + + if (isset($input['action']) && $input['action'] === 'create') { + $image = new Image(); + $image->setName($input['imageName'] ?? $input['name']); + $image->setType($input['type'] ?? 'monolithic'); + $image->setRemotePc($input['remotePc'] ?? false); + $image->setIsGlobal($input['isGlobal'] ?? false); + $image->setClient($client); + $this->entityManager->persist($image); + } elseif (isset($input['action']) && $input['action'] === 'update') { + if (!isset($input['imageUuid'])) { + throw new \Exception("Image UUID not found in trace input for update operation"); + } + + $image = $this->entityManager->getRepository(Image::class) + ->findOneBy(['uuid' => $input['imageUuid']]); + + if (!$image) { + throw new \Exception("Image not found with UUID: {$input['imageUuid']} for update operation"); + } + } else { + throw new \Exception("Either 'create' or 'update' field must be set to true in trace input"); + } + + if (isset($input['diskNumber']) && isset($input['partitionNumber'])) { + $partitionInfo = [ + 'numDisk' => $input['diskNumber'], + 'numPartition' => $input['partitionNumber'], + 'partitionCode' => $input['partitionCode'] ?? 'LINUX', + 'filesystem' => $input['filesystem'] ?? 'EXT4' + ]; + $image->setPartitionInfo(json_encode($partitionInfo)); + } + + $partition = null; + if (isset($input['diskNumber']) && isset($input['partitionNumber'])) { + $partition = $this->entityManager->getRepository(Partition::class) + ->findOneBy([ + 'client' => $client, + 'diskNumber' => $input['diskNumber'], + 'partitionNumber' => $input['partitionNumber'] + ]); + + if (!$partition) { + throw new \Exception("Partition not found for client {$client->getUuid()} with disk {$input['diskNumber']} and partition {$input['partitionNumber']}"); + } + } + + try { + $response = $this->createImageAction->__invoke( + queue: false, + image: $image, + partition: $partition, + client: $client, + gitRepositoryName: $input['gitRepositoryName'] ?? null, + existingTrace: $trace + ); + + if (json_decode($response->getContent(), true)['status'] === 200) { + $trace->setStatus(TraceStatus::IN_PROGRESS); + $trace->setFinishedAt(new \DateTime()); + $this->entityManager->persist($trace); + $this->entityManager->flush(); + return true; + } + + return false; + } catch (\Exception $e) { + return false; + } + } + + private function executeDeployImage(Trace $trace, array $input): bool + { + $client = $trace->getClient(); + + if (!isset($input['imageImageRepository']) && !isset($input['image'])) { + throw new \Exception("ImageImageRepository UUID or image UUID not found in trace input for deploy image"); + } + + $imageImageRepository = $this->entityManager->getRepository(ImageImageRepository::class) + ->findOneBy(['uuid' => $input['imageImageRepository'] ?? $input['image']]); + + if (!$imageImageRepository) { + throw new \Exception("ImageImageRepository not found with UUID: {$input['imageImageRepository']}"); + } + + $diskNumber = isset($input['diskNumber']) ? $input['diskNumber'] : $input['numDisk']; + $partitionNumber = isset($input['partitionNumber']) ? $input['partitionNumber'] : $input['numPartition']; + + if (!$diskNumber || !$partitionNumber) { + throw new \Exception("Disk number or partition number not found in trace input for deploy image"); + } + + $deployInput = new DeployImageInput(); + $deployInput->method = $input['method'] ?? 'unicast'; + $deployInput->type = $input['type'] ?? 'monolithic'; + $deployInput->diskNumber = (int) $diskNumber; + $deployInput->partitionNumber = (int) $partitionNumber; + $deployInput->mcastMode = $input['mcastMode'] ?? null; + $deployInput->mcastSpeed = $input['mcastSpeed'] ?? null; + $deployInput->mcastPort = $input['mcastPort'] ?? null; + $deployInput->mcastIp = $input['mcastIp'] ?? null; + $deployInput->maxClients = $input['maxClients'] ?? null; + $deployInput->maxTime = $input['maxTime'] ?? null; + $deployInput->p2pMode = $input['p2pMode'] ?? null; + $deployInput->p2pTime = $input['p2pTime'] ?? null; + $deployInput->queue = $input['queue'] ?? false; + + $deployInput->clients = [new ClientOutput($client)]; + + $response = $this->deployImageAction->__invoke($deployInput, $imageImageRepository, $trace); + + $clientJobs = $response['data']; + + $clientKey = '/clients/' . $client->getUuid(); + $jobId = $clientJobs[$clientKey] ?? []; + + $trace->setJobId($jobId['jobId'] ?? null); + $trace->setStatus(TraceStatus::IN_PROGRESS); + $trace->setFinishedAt(new \DateTime()); + $this->entityManager->persist($trace); + $this->entityManager->flush(); + + return true; + } + + private function executePartitionAssistant(Trace $trace, array $input): bool + { + $client = $trace->getClient(); + + $partitionInput = new PartitionPostInput(); + $partitionInput->clients = [new ClientOutput($client)]; + + $partitions = []; + + foreach ($input as $item) { + $partitionInputObj = new PartitionInput(); + $partitionInputObj->diskNumber = $item['diskNumber']; + $partitionInputObj->partitionNumber = (int)$item['partitionNumber']; + $partitionInputObj->partitionCode = $item['partitionCode'] ?? 'LINUX'; + $partitionInputObj->size = $item['size']; + $partitionInputObj->filesystem = $item['filesystem'] ?? 'EXT4'; + $partitionInputObj->format = $item['format'] ?? false; + $partitions[] = $partitionInputObj; + } + + $partitionInput->partitions = $partitions; + $partitionInput->queue = false; + + try { + $response = $this->partitionAssistantAction->__invoke($partitionInput, $trace); + + if (json_decode($response->getContent(), true)['status'] === 200) { + $trace->setStatus(TraceStatus::SUCCESS); + $trace->setFinishedAt(new \DateTime()); + $this->entityManager->persist($trace); + $this->entityManager->flush(); + return true; + } + + return false; + } catch (\Exception $e) { + return false; + } + } + + private function executeRunScript(Trace $trace, array $input): bool + { + $client = $trace->getClient(); + + if (!isset($input['script'])) { + throw new \Exception("Script not found in trace input"); + } + + $commandExecuteInput = new CommandExecuteInput(); + $commandExecuteInput->clients = [new ClientOutput($client)]; + $commandExecuteInput->script = $input['script']; + $commandExecuteInput->queue = false; + $commandExecuteInput->runAsAdmin = true; + + try { + $response = $this->runScriptAction->__invoke($commandExecuteInput, $trace); + return $response->getStatusCode() === 200; + } catch (\Exception $e) { + $this->logger->error('Error executing run script', [ + 'error' => $e->getMessage() + ]); + return false; + } + } + + private function executePowerOn(Trace $trace, array $input): bool + { + $trace->setStatus(TraceStatus::SUCCESS); + $trace->setFinishedAt(new \DateTime()); + $this->entityManager->persist($trace); + $this->entityManager->flush(); + return true; + } + + private function executeReboot(Trace $trace, array $input): bool + { + $client = $trace->getClient(); + + $multipleClientsInput = new MultipleClientsInput(); + $multipleClientsInput->clients = [new ClientOutput($client)]; + $multipleClientsInput->queue = false; + + try { + $response = $this->rebootAction->__invoke($multipleClientsInput); + + if ($response->getStatusCode() === 200) { + $trace->setStatus(TraceStatus::SUCCESS); + $trace->setFinishedAt(new \DateTime()); + $this->entityManager->persist($trace); + $this->entityManager->flush(); + return true; + } + + return false; + } catch (\Exception $e) { + return false; + } + } + + private function executeShutdown(Trace $trace, array $input): bool + { + $client = $trace->getClient(); + + $multipleClientsInput = new MultipleClientsInput(); + $multipleClientsInput->clients = [new ClientOutput($client)]; + $multipleClientsInput->queue = false; + + try { + $response = $this->powerOffAction->__invoke($multipleClientsInput); + + if ($response->getStatusCode() === 200) { + $trace->setStatus(TraceStatus::SUCCESS); + $trace->setFinishedAt(new \DateTime()); + $this->entityManager->persist($trace); + $this->entityManager->flush(); + return true; + } + + return false; + } catch (\Exception $e) { + return false; + } + } + + private function executeLogin(Trace $trace, array $input): bool + { + $client = $trace->getClient(); + + if ($client->getStatus() !== ClientStatus::OG_LIVE) { + return false; + } + + if (isset($input['partition'])) { + $partition = $this->entityManager->getRepository(Partition::class) + ->findOneBy(['uuid' => $input['partition']]); + if (!$partition) { + throw new \Exception("Partition not found with UUID: {$input['partition']}"); + } + } elseif (isset($input['dsk']) && isset($input['par'])) { + $partition = $this->entityManager->getRepository(Partition::class) + ->findOneBy([ + 'client' => $client, + 'diskNumber' => $input['dsk'], + 'partitionNumber' => $input['par'], + ]); + if (!$partition) { + throw new \Exception("Partition not found for disk {$input['dsk']} partition {$input['par']}"); + } + } else { + throw new \Exception("Partition not found in trace input"); + } + + $bootClientsInput = new BootClientsInput(); + $bootClientsInput->clients = [new ClientOutput($client)]; + $bootClientsInput->partition = new PartitionOutput($partition); + + try { + $response = $this->loginAction->__invoke($bootClientsInput); + + if ($response->getStatusCode() === 200) { + $trace->setStatus(TraceStatus::SUCCESS); + $trace->setFinishedAt(new \DateTime()); + $this->entityManager->persist($trace); + $this->entityManager->flush(); + return true; + } + + return false; + } catch (\Exception $e) { + return false; + } + } +}