diff --git a/config/api_platform/CommandTask.yaml b/config/api_platform/CommandTask.yaml index d008252..9832433 100644 --- a/config/api_platform/CommandTask.yaml +++ b/config/api_platform/CommandTask.yaml @@ -24,6 +24,13 @@ resources: ApiPlatform\Metadata\Post: ~ ApiPlatform\Metadata\Delete: ~ + command_task_execute: + class: ApiPlatform\Metadata\Post + method: POST + input: App\Dto\Input\CommandTaskExecuteInput + uriTemplate: /command-tasks/{uuid}/execute + controller: App\Controller\CommandTaskExecuteAction + properties: App\Entity\CommandTask: id: diff --git a/src/Command/RunScheduledCommandTasksCommand.php b/src/Command/RunScheduledCommandTasksCommand.php index 675d0a9..c3b58b4 100644 --- a/src/Command/RunScheduledCommandTasksCommand.php +++ b/src/Command/RunScheduledCommandTasksCommand.php @@ -17,12 +17,10 @@ use App\Entity\CommandTask; use App\Entity\Image; use App\Entity\ImageImageRepository; use App\Entity\Partition; -use App\Entity\Trace; use App\Model\ClientStatus; -use App\Model\CommandTypes; -use App\Model\TraceStatus; use App\Repository\ClientRepository; use App\Repository\CommandTaskRepository; +use App\Service\CommandTaskTraceCreator; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; @@ -41,6 +39,7 @@ class RunScheduledCommandTasksCommand extends Command private readonly DeployImageAction $deployImageAction, private readonly CreateImageAction $createImageAction, private readonly PartitionAssistantAction $partitionAssistantAction, + private readonly CommandTaskTraceCreator $commandTaskTraceCreator, ) { parent::__construct(); @@ -78,15 +77,15 @@ class RunScheduledCommandTasksCommand extends Command if ($isDue && $notYetExecuted) { $output->writeln("Ejecutando tarea: " . $task->getName()); - $scripts = $task->getCommandTaskScripts()->toArray(); - usort($scripts, fn($a, $b) => $a->getExecutionOrder() <=> $b->getExecutionOrder()); - - foreach ($scripts as $script) { - $output->writeln(" - Creando traza para script de tipo {$script->getType()} con orden {$script->getExecutionOrder()}"); - - $this->createTraceForScript($script, $task, $output); + if ($task->getScope() !== 'clients') { + $ou = $task->getOrganizationalUnit(); + $clients = $ou ? $this->clientRepository->findClientsByOrganizationalUnitAndDescendants($ou->getId()) : []; + } else { + $clients = $task->getClients(); } + $this->commandTaskTraceCreator->createPendingTraces($task, $clients); + $task->setLastExecution(new \DateTimeImmutable('now', new \DateTimeZone('UTC'))); $task->setNextExecution($task->calculateNextExecutionDate()); @@ -98,127 +97,6 @@ class RunScheduledCommandTasksCommand extends Command return Command::SUCCESS; } - private function createTraceForScript($script, CommandTask $task, OutputInterface $output): void - { - $scriptParameters = $script->getParameters(); - - if ($task->getScope() !== 'clients') { - $ou = $task->getOrganizationalUnit(); - $clients = $ou ? $this->clientRepository->findClientsByOrganizationalUnitAndDescendants($ou->getId()) : []; - } else { - $clients = $task->getClients(); - } - - foreach ($clients as $client) { - $trace = new Trace(); - $trace->setClient($client); - $trace->setStatus(TraceStatus::PENDING); - $trace->setExecutedAt(new \DateTime()); - - switch ($script->getType()) { - case 'run-script': - $trace->setCommand(CommandTypes::RUN_SCRIPT); - $trace->setInput([ - 'script' => $script->getContent() - ]); - break; - - case 'deploy-image': - $trace->setCommand(CommandTypes::DEPLOY_IMAGE); - - $input = [ - 'imageImageRepository' => $scriptParameters['imageUuid'] - ]; - - if (isset($scriptParameters['method'])) { - $input['method'] = $scriptParameters['method']; - } - if (isset($scriptParameters['type'])) { - $input['type'] = $scriptParameters['type']; - } - if (isset($scriptParameters['diskNumber'])) { - $input['diskNumber'] = $scriptParameters['diskNumber']; - } - if (isset($scriptParameters['partitionNumber'])) { - $input['partitionNumber'] = $scriptParameters['partitionNumber']; - } - if (isset($scriptParameters['mcastMode'])) { - $input['mcastMode'] = $scriptParameters['mcastMode']; - } - if (isset($scriptParameters['mcastSpeed'])) { - $input['mcastSpeed'] = $scriptParameters['mcastSpeed']; - } - if (isset($scriptParameters['mcastPort'])) { - $input['mcastPort'] = $scriptParameters['mcastPort']; - } - if (isset($scriptParameters['mcastIp'])) { - $input['mcastIp'] = $scriptParameters['mcastIp']; - } - if (isset($scriptParameters['maxClients'])) { - $input['maxClients'] = $scriptParameters['maxClients']; - } - if (isset($scriptParameters['maxTime'])) { - $input['maxTime'] = $scriptParameters['maxTime']; - } - if (isset($scriptParameters['p2pMode'])) { - $input['p2pMode'] = $scriptParameters['p2pMode']; - } - if (isset($scriptParameters['p2pTime'])) { - $input['p2pTime'] = $scriptParameters['p2pTime']; - } - - $trace->setInput($input); - break; - - case 'create-image': - $trace->setCommand(CommandTypes::CREATE_IMAGE); - $trace->setInput([ - 'type' => $scriptParameters['type'], - 'imageName' => $scriptParameters['imageName'], - 'imageUuid' => $scriptParameters['imageUuid'] ?? null, - 'diskNumber' => $scriptParameters['diskNumber'] ?? null, - 'partitionNumber' => $scriptParameters['partitionNumber'] ?? null, - 'gitRepositoryName' => $scriptParameters['gitRepositoryName'] ?? null, - 'action' => $scriptParameters['action'] ?? 'create' - ]); - break; - - case 'partition-assistant': - $trace->setCommand(CommandTypes::PARTITION_AND_FORMAT); - $trace->setInput($scriptParameters); - break; - - case 'power-on': - $trace->setCommand(CommandTypes::POWER_ON); - $trace->setInput($scriptParameters ?? []); - break; - - case 'reboot': - $trace->setCommand(CommandTypes::REBOOT); - $trace->setInput($scriptParameters ?? []); - break; - - case 'shutdown': - $trace->setCommand(CommandTypes::SHUTDOWN); - $trace->setInput($scriptParameters ?? []); - break; - - case 'login': - $trace->setCommand(CommandTypes::LOGIN); - $trace->setInput($scriptParameters ?? []); - break; - - default: - $output->writeln(" - Tipo de script no soportado: {$script->getType()}"); - continue 2; - } - - $this->entityManager->persist($trace); - $output->writeln(" - Traza creada para cliente {$client->getUuid()} con comando {$trace->getCommand()}"); - } - } - - private function executePartitionAssistant($script, CommandTask $task, OutputInterface $output): void { $scriptParameters = $script->getParameters(); diff --git a/src/Controller/CommandTaskExecuteAction.php b/src/Controller/CommandTaskExecuteAction.php new file mode 100644 index 0000000..7650f08 --- /dev/null +++ b/src/Controller/CommandTaskExecuteAction.php @@ -0,0 +1,32 @@ + $client->getEntity(), $input->clients); + + $this->commandTaskTraceCreator->createPendingTraces($commandTask, $clients); + + $this->entityManager->flush(); + + return new JsonResponse(data: 'Command task executed successfully', status: Response::HTTP_OK); + } +} diff --git a/src/Dto/Input/CommandTaskExecuteInput.php b/src/Dto/Input/CommandTaskExecuteInput.php new file mode 100644 index 0000000..fc4aa58 --- /dev/null +++ b/src/Dto/Input/CommandTaskExecuteInput.php @@ -0,0 +1,22 @@ +getCommandTaskScripts()->toArray(); + usort($scripts, fn($a, $b) => $a->getExecutionOrder() <=> $b->getExecutionOrder()); + + foreach ($scripts as $script) { + foreach ($clients as $client) { + $this->createTraceForScript($script, $client); + } + } + } + + private function createTraceForScript($script, $client): void + { + $scriptParameters = $script->getParameters(); + + $trace = new Trace(); + $trace->setClient($client); + $trace->setStatus(TraceStatus::PENDING); + $trace->setExecutedAt(new \DateTime()); + + switch ($script->getType()) { + case 'run-script': + $trace->setCommand(CommandTypes::RUN_SCRIPT); + $trace->setInput([ + 'script' => $script->getContent() + ]); + break; + + case 'deploy-image': + $trace->setCommand(CommandTypes::DEPLOY_IMAGE); + + $input = [ + 'imageImageRepository' => $scriptParameters['imageUuid'] + ]; + + if (isset($scriptParameters['method'])) { + $input['method'] = $scriptParameters['method']; + } + if (isset($scriptParameters['type'])) { + $input['type'] = $scriptParameters['type']; + } + if (isset($scriptParameters['diskNumber'])) { + $input['diskNumber'] = $scriptParameters['diskNumber']; + } + if (isset($scriptParameters['partitionNumber'])) { + $input['partitionNumber'] = $scriptParameters['partitionNumber']; + } + if (isset($scriptParameters['mcastMode'])) { + $input['mcastMode'] = $scriptParameters['mcastMode']; + } + if (isset($scriptParameters['mcastSpeed'])) { + $input['mcastSpeed'] = $scriptParameters['mcastSpeed']; + } + if (isset($scriptParameters['mcastPort'])) { + $input['mcastPort'] = $scriptParameters['mcastPort']; + } + if (isset($scriptParameters['mcastIp'])) { + $input['mcastIp'] = $scriptParameters['mcastIp']; + } + if (isset($scriptParameters['maxClients'])) { + $input['maxClients'] = $scriptParameters['maxClients']; + } + if (isset($scriptParameters['maxTime'])) { + $input['maxTime'] = $scriptParameters['maxTime']; + } + if (isset($scriptParameters['p2pMode'])) { + $input['p2pMode'] = $scriptParameters['p2pMode']; + } + if (isset($scriptParameters['p2pTime'])) { + $input['p2pTime'] = $scriptParameters['p2pTime']; + } + + $trace->setInput($input); + break; + + case 'create-image': + $trace->setCommand(CommandTypes::CREATE_IMAGE); + $trace->setInput([ + 'type' => $scriptParameters['type'], + 'imageName' => $scriptParameters['imageName'], + 'imageUuid' => $scriptParameters['imageUuid'] ?? null, + 'diskNumber' => $scriptParameters['diskNumber'] ?? null, + 'partitionNumber' => $scriptParameters['partitionNumber'] ?? null, + 'gitRepositoryName' => $scriptParameters['gitRepositoryName'] ?? null, + 'action' => $scriptParameters['action'] ?? 'create' + ]); + break; + + case 'partition-assistant': + $trace->setCommand(CommandTypes::PARTITION_AND_FORMAT); + $trace->setInput($scriptParameters); + break; + + case 'power-on': + $trace->setCommand(CommandTypes::POWER_ON); + $trace->setInput($scriptParameters ?? []); + break; + + case 'reboot': + $trace->setCommand(CommandTypes::REBOOT); + $trace->setInput($scriptParameters ?? []); + break; + + case 'shutdown': + $trace->setCommand(CommandTypes::SHUTDOWN); + $trace->setInput($scriptParameters ?? []); + break; + + case 'login': + $trace->setCommand(CommandTypes::LOGIN); + $trace->setInput($scriptParameters ?? []); + break; + + default: + return; + } + + $this->entityManager->persist($trace); + } +}