diff --git a/config/api_platform/Client.yaml b/config/api_platform/Client.yaml index fb4a8c3..3882d9c 100644 --- a/config/api_platform/Client.yaml +++ b/config/api_platform/Client.yaml @@ -32,7 +32,7 @@ resources: class: ApiPlatform\Metadata\Post method: POST input: App\Dto\Input\ChangeOrganizationalUnitInput - uriTemplate: /clients/change-organizational-units + uriTemplate: /clients/change-organizational-unit controller: App\Controller\ChangeOrganizationalUnitAction agent_status: @@ -57,6 +57,13 @@ resources: uriTemplate: /clients/server/boot-client controller: App\Controller\OgAgent\LoginAction + remove_cache_image: + class: ApiPlatform\Metadata\Post + method: POST + input: App\Dto\Input\BootClientsInput + uriTemplate: /clients/server/remove-cache-image + controller: App\Controller\OgAgent\RemoveCacheImageAction + reboot_client: class: ApiPlatform\Metadata\Post method: POST diff --git a/src/Controller/OgAgent/RemoveCacheImageAction.php b/src/Controller/OgAgent/RemoveCacheImageAction.php index b34e977..769453f 100644 --- a/src/Controller/OgAgent/RemoveCacheImageAction.php +++ b/src/Controller/OgAgent/RemoveCacheImageAction.php @@ -2,7 +2,84 @@ namespace App\Controller\OgAgent; -class RemoveCacheImageAction -{ +use App\Dto\Input\BootClientsInput; +use App\Entity\Client; +use App\Entity\Partition; +use App\Model\ClientStatus; +use App\Model\CommandTypes; +use App\Model\TraceStatus; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Validator\Exception\ValidatorException; +use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; +class RemoveCacheImageAction extends AbstractOgAgentController +{ + /** + * @throws TransportExceptionInterface + * @throws ServerExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ClientExceptionInterface + */ + public function __invoke(BootClientsInput $input): JsonResponse + { + /** @var Partition $partition */ + $partition = $input->partition->getEntity(); + + foreach ($input->clients as $clientEntity) { + /** @var Client $client */ + $client = $clientEntity->getEntity(); + + if (!$partition->getImage()) { + throw new ValidatorException('Image is required'); + } + + if (!$client->getIp()) { + throw new ValidatorException('IP is required'); + } + + if ($client->getStatus() !== ClientStatus::OG_LIVE) { + throw new ValidatorException('Client is not in OG_LIVE status'); + } + + $script = `rm%20-r%20/opt/opengnsys/cache/opt/opengnsys/images/{$partition->getImage()->getName()}.*@'`; + + $data = [ + 'nfn' => 'EjecutarScript', + 'scp' => base64_encode($script), + 'ids' => '0' + ]; + + $response = $this->createRequest( + method: 'POST', + url: 'https://'.$client->getIp().':8000/opengnsys/EjecutarScript', + params: [ + 'json' => $data, + ], + token: $client->getToken(), + ); + + if (isset($response['error']) && $response['code'] === Response::HTTP_INTERNAL_SERVER_ERROR) { + throw new ValidatorException('Error logging in: '.$response['error']); + } + + $this->logger->info('Login client', ['client' => $client->getId()]); + + $jobId = $response['job_id']; + + $this->entityManager->persist($client); + $this->entityManager->flush(); + + $inputData = [ + 'script' => $script, + ]; + + $this->createService->__invoke($client, CommandTypes::RUN_SCRIPT, TraceStatus::SUCCESS, $jobId, $inputData); + } + + return new JsonResponse(data: [], status: Response::HTTP_OK); + } } \ No newline at end of file