86 lines
3.0 KiB
PHP
86 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\OgAgent;
|
|
|
|
use App\Dto\Input\RemoveCacheImageInput;
|
|
use App\Entity\Client;
|
|
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\HttpKernel\Exception\BadRequestHttpException;
|
|
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(RemoveCacheImageInput $input): JsonResponse
|
|
{
|
|
if (!$input->imageName) {
|
|
throw new BadRequestHttpException('Image name is required');
|
|
}
|
|
|
|
foreach ($input->clients as $clientEntity) {
|
|
/** @var Client $client */
|
|
$client = $clientEntity->getEntity();
|
|
|
|
if (!$client->getIp()) {
|
|
throw new BadRequestHttpException('IP is required');
|
|
}
|
|
|
|
if ($client->getStatus() !== ClientStatus::OG_LIVE) {
|
|
$this->createService->__invoke($client, CommandTypes::REMOVE_CACHE_IMAGE, TraceStatus::PENDING, null, []);
|
|
continue;
|
|
}
|
|
|
|
$script = 'rm -r /opt/opengnsys/cache/opt/opengnsys/images/' . escapeshellarg($input->imageName) . '*';
|
|
|
|
$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) {
|
|
if ($input->queue) {
|
|
$this->createService->__invoke($client, CommandTypes::REMOVE_CACHE_IMAGE, TraceStatus::PENDING, null, []);
|
|
continue;
|
|
}
|
|
|
|
continue;
|
|
}
|
|
$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);
|
|
}
|
|
} |