Reviewed-on: #1 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\Dashboard;
|
||||
use App\Repository\ApplicationRepository;
|
||||
use App\Service\DockerService;
|
||||
|
||||
final readonly class DashboardProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private ApplicationRepository $applicationRepository,
|
||||
private DockerService $dockerService,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): Dashboard
|
||||
{
|
||||
$applications = $this->applicationRepository->findAll();
|
||||
|
||||
$dto = new Dashboard();
|
||||
|
||||
foreach ($applications as $app) {
|
||||
$envs = [];
|
||||
|
||||
foreach ($app->getEnvironments() as $env) {
|
||||
$containerStatus = $this->dockerService->getContainerStatus($env->getContainerName());
|
||||
|
||||
$envs[] = [
|
||||
'id' => $env->getId(),
|
||||
'name' => $env->getName(),
|
||||
'status' => $containerStatus['status'],
|
||||
'version' => $containerStatus['version'],
|
||||
];
|
||||
}
|
||||
|
||||
$dto->applications[] = [
|
||||
'name' => $app->getName(),
|
||||
'slug' => $app->getSlug(),
|
||||
'giteaUrl' => $app->getGiteaUrl(),
|
||||
'environments' => $envs,
|
||||
];
|
||||
}
|
||||
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\ApiResource\DeployResult;
|
||||
use App\Repository\EnvironmentRepository;
|
||||
use App\Service\DeployService;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
final readonly class DeployProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private EnvironmentRepository $environmentRepository,
|
||||
private DeployService $deployService,
|
||||
) {}
|
||||
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): DeployResult
|
||||
{
|
||||
$id = $uriVariables['id'] ?? null;
|
||||
$environment = $id ? $this->environmentRepository->find($id) : null;
|
||||
|
||||
if (null === $environment) {
|
||||
throw new NotFoundHttpException(sprintf('Environment "%s" not found.', $id));
|
||||
}
|
||||
|
||||
$requestData = $context['request']?->toArray() ?? [];
|
||||
$tag = $requestData['tag'] ?? null;
|
||||
|
||||
if (null === $tag || '' === $tag) {
|
||||
throw new BadRequestHttpException('The "tag" field is required.');
|
||||
}
|
||||
|
||||
$result = $this->deployService->deploy($environment, $tag);
|
||||
|
||||
$dto = new DeployResult();
|
||||
$dto->success = $result['success'];
|
||||
$dto->output = $result['output'];
|
||||
$dto->tag = $tag;
|
||||
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\EnvironmentHealth;
|
||||
use App\Repository\EnvironmentRepository;
|
||||
use App\Service\DockerService;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
final readonly class EnvironmentHealthProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private EnvironmentRepository $environmentRepository,
|
||||
private DockerService $dockerService,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): EnvironmentHealth
|
||||
{
|
||||
$id = $uriVariables['id'] ?? null;
|
||||
$environment = $id ? $this->environmentRepository->find($id) : null;
|
||||
|
||||
if (null === $environment) {
|
||||
throw new NotFoundHttpException(sprintf('Environment "%s" not found.', $id));
|
||||
}
|
||||
|
||||
$containerName = $environment->getContainerName();
|
||||
$status = $this->dockerService->getContainerStatus($containerName);
|
||||
$stats = $this->dockerService->getContainerStats($containerName);
|
||||
|
||||
$dto = new EnvironmentHealth();
|
||||
$dto->status = $status['status'];
|
||||
$dto->version = $status['version'];
|
||||
$dto->startedAt = $status['startedAt'];
|
||||
$dto->cpuPercent = $stats['cpuPercent'];
|
||||
$dto->memoryUsage = $stats['memoryUsage'];
|
||||
$dto->memoryLimit = $stats['memoryLimit'];
|
||||
$dto->memoryPercent = $stats['memoryPercent'];
|
||||
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
@@ -6,42 +6,26 @@ namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\ApiResource\ManagedApplication;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use App\Entity\Environment;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
|
||||
final readonly class MaintenanceToggleProcessor implements ProcessorInterface
|
||||
{
|
||||
/**
|
||||
* @param list<array{name: string, slug: string, maintenance_path: string}> $managedApplications
|
||||
* @param Environment $data
|
||||
*/
|
||||
public function __construct(
|
||||
#[Autowire('%app.managed_applications%')]
|
||||
private array $managedApplications,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param ManagedApplication $data
|
||||
*/
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): ManagedApplication
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Environment
|
||||
{
|
||||
$slug = $uriVariables['slug'] ?? '';
|
||||
$appConfig = null;
|
||||
$maintenancePath = $data->getMaintenanceFilePath();
|
||||
|
||||
foreach ($this->managedApplications as $app) {
|
||||
if ($app['slug'] === $slug) {
|
||||
$appConfig = $app;
|
||||
break;
|
||||
}
|
||||
if (null === $maintenancePath) {
|
||||
throw new BadRequestHttpException('Maintenance file path is not configured for this environment.');
|
||||
}
|
||||
|
||||
if (null === $appConfig) {
|
||||
throw new NotFoundHttpException(sprintf('Application "%s" not found.', $slug));
|
||||
}
|
||||
$requestData = $context['request']?->toArray() ?? [];
|
||||
$enableMaintenance = $requestData['maintenance'] ?? false;
|
||||
|
||||
$maintenancePath = $appConfig['maintenance_path'];
|
||||
|
||||
if ($data->maintenance) {
|
||||
if ($enableMaintenance) {
|
||||
$directory = dirname($maintenancePath);
|
||||
|
||||
if (!is_dir($directory) && !mkdir($directory, 0755, true)) {
|
||||
@@ -57,11 +41,6 @@ final readonly class MaintenanceToggleProcessor implements ProcessorInterface
|
||||
}
|
||||
}
|
||||
|
||||
$dto = new ManagedApplication();
|
||||
$dto->slug = $appConfig['slug'];
|
||||
$dto->name = $appConfig['name'];
|
||||
$dto->maintenance = file_exists($maintenancePath);
|
||||
|
||||
return $dto;
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\ManagedApplication;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
final readonly class ManagedApplicationProvider implements ProviderInterface
|
||||
{
|
||||
/**
|
||||
* @param list<array{name: string, slug: string, maintenance_path: string}> $managedApplications
|
||||
*/
|
||||
public function __construct(
|
||||
#[Autowire('%app.managed_applications%')]
|
||||
private array $managedApplications,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ManagedApplication|array
|
||||
{
|
||||
if ($operation instanceof GetCollection) {
|
||||
return array_map(
|
||||
fn (array $app) => $this->buildDto($app),
|
||||
$this->managedApplications,
|
||||
);
|
||||
}
|
||||
|
||||
$slug = $uriVariables['slug'] ?? '';
|
||||
|
||||
foreach ($this->managedApplications as $app) {
|
||||
if ($app['slug'] === $slug) {
|
||||
return $this->buildDto($app);
|
||||
}
|
||||
}
|
||||
|
||||
throw new NotFoundHttpException(sprintf('Application "%s" not found.', $slug));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{name: string, slug: string, maintenance_path: string} $app
|
||||
*/
|
||||
private function buildDto(array $app): ManagedApplication
|
||||
{
|
||||
$dto = new ManagedApplication();
|
||||
$dto->slug = $app['slug'];
|
||||
$dto->name = $app['name'];
|
||||
$dto->maintenance = file_exists($app['maintenance_path']);
|
||||
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\TagList;
|
||||
use App\Repository\ApplicationRepository;
|
||||
use App\Service\GiteaRegistryService;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
final readonly class TagListProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private ApplicationRepository $applicationRepository,
|
||||
private GiteaRegistryService $giteaRegistryService,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): TagList
|
||||
{
|
||||
$slug = $uriVariables['slug'] ?? '';
|
||||
$application = $this->applicationRepository->findOneBy(['slug' => $slug]);
|
||||
|
||||
if (null === $application) {
|
||||
throw new NotFoundHttpException(sprintf('Application "%s" not found.', $slug));
|
||||
}
|
||||
|
||||
$dto = new TagList();
|
||||
$dto->tags = $this->giteaRegistryService->listTags($application->getRegistryImage());
|
||||
|
||||
return $dto;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user