Implement Keystone environment deployments
This commit is contained in:
@@ -2,11 +2,14 @@
|
||||
|
||||
namespace App\Actions\Services;
|
||||
|
||||
use App\Enums\DeployPolicy;
|
||||
use App\Enums\ServiceCategory;
|
||||
use App\Enums\ServiceStatus;
|
||||
use App\Enums\ServiceType;
|
||||
use App\Jobs\Services\DeployService;
|
||||
use App\Models\Server;
|
||||
use App\Models\Service;
|
||||
use RuntimeException;
|
||||
|
||||
class CreateService
|
||||
{
|
||||
@@ -16,15 +19,25 @@ class CreateService
|
||||
ServiceCategory $category,
|
||||
ServiceType $type,
|
||||
string $version,
|
||||
) {
|
||||
): Service {
|
||||
if ($category === ServiceCategory::GATEWAY && $server->services()->where('category', ServiceCategory::GATEWAY)->exists()) {
|
||||
throw new RuntimeException('This server already has a gateway service.');
|
||||
}
|
||||
|
||||
$driverName = "{$type->value}.{$version}";
|
||||
$service = $server->services()->create([
|
||||
'organisation_id' => $server->organisation_id,
|
||||
'name' => $name,
|
||||
'category' => $category,
|
||||
'type' => $type, // postgres
|
||||
'version' => $version, // 17
|
||||
'driver_name' => $driverName, // postgres.17
|
||||
'type' => $type,
|
||||
'version' => $version,
|
||||
'version_track' => $version,
|
||||
'driver_name' => $driverName,
|
||||
'status' => ServiceStatus::NOT_INSTALLED,
|
||||
'deploy_policy' => $this->defaultDeployPolicy($category, $type),
|
||||
'process_roles' => [],
|
||||
'desired_replicas' => 1,
|
||||
'config' => [],
|
||||
]);
|
||||
|
||||
if (method_exists($service->driver(), 'defaultCredentials')) {
|
||||
@@ -32,8 +45,40 @@ class CreateService
|
||||
$service->save();
|
||||
}
|
||||
|
||||
$service->replicas()->create([
|
||||
'server_id' => $server->id,
|
||||
'container_name' => "keystone-service-{$service->id}-1",
|
||||
'internal_host' => "keystone-service-{$service->id}",
|
||||
'internal_port' => $this->defaultInternalPort($type),
|
||||
'status' => 'pending',
|
||||
'health_status' => 'unknown',
|
||||
'config' => [],
|
||||
]);
|
||||
|
||||
dispatch(new DeployService($service));
|
||||
|
||||
return $service;
|
||||
}
|
||||
|
||||
private function defaultDeployPolicy(ServiceCategory $category, ServiceType $type): DeployPolicy
|
||||
{
|
||||
return match (true) {
|
||||
$category === ServiceCategory::APPLICATION => DeployPolicy::WITH_ENVIRONMENT,
|
||||
$category === ServiceCategory::DATABASE,
|
||||
$category === ServiceCategory::CACHE,
|
||||
$category === ServiceCategory::STORAGE => DeployPolicy::DEPENDENCY_ONLY,
|
||||
$category === ServiceCategory::GATEWAY => DeployPolicy::MANUAL_OR_ON_ROUTE_CHANGE,
|
||||
default => DeployPolicy::MANUAL,
|
||||
};
|
||||
}
|
||||
|
||||
private function defaultInternalPort(ServiceType $type): int
|
||||
{
|
||||
return match ($type) {
|
||||
ServiceType::POSTGRES => 5432,
|
||||
ServiceType::VALKEY => 6379,
|
||||
ServiceType::CADDY,
|
||||
ServiceType::LARAVEL => 80,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user