enum case consistency, wip creating service

This commit is contained in:
2025-03-31 13:51:02 +00:00
parent 75f2ecb7bf
commit d150f57c8f
16 changed files with 150 additions and 113 deletions

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Actions\Services;
use App\Enums\ServiceCategory;
use App\Enums\ServiceStatus;
use App\Enums\ServiceType;
use App\Jobs\Services\InstallService;
use App\Models\Server;
use Illuminate\Support\Str;
class CreateService
{
public function execute(
Server $server,
string $name,
ServiceCategory $category,
ServiceType $type,
string $version,
string $driverName,
) {
$service = $server->services()->create([
'name' => $name,
'category' => $category,
'type' => $type,
'version' => $version, // 17
'driver_name' => $driverName, // postgres
'status' => ServiceStatus::NOT_INSTALLED,
]);
}
}

View File

@@ -4,8 +4,16 @@ namespace App\Data\Deployments;
class Step
{
public string $script;
public function __construct(
public string $name = 'Step',
string|callable $script = 'echo "Incomplete Step"',
) {
//
if (is_callable($script)) {
$this->script = $script();
} else {
$this->script = $script;
}
}
}

View File

@@ -4,5 +4,12 @@ namespace App\Drivers;
interface DatabaseDriver extends Driver
{
//
public string $defaultUser = 'keystone';
public string $defaultDb = 'keystone';
public function __construct(
public ?string $containerName = null,
public ?string $containerId = null,
public ?string $defaultPassword = null,
);
}

View File

@@ -2,7 +2,9 @@
namespace App\Drivers;
use App\Data\Deployments\Plan;
interface Driver
{
//
public Plan $deploymentPlan { get; }
}

View File

@@ -2,9 +2,54 @@
namespace App\Drivers\Postgres;
use App\Data\Deployments\Plan;
use App\Data\Deployments\Step;
use App\Drivers\DatabaseDriver;
class Postgres17Driver implements DatabaseDriver
{
// @todo
public Plan $deploymentPlan;
public string $defaultUser = 'keystone';
public string $defaultDb = 'keystone';
public function __construct(
public ?string $containerName = null,
public ?string $containerId = null,
public ?string $defaultPassword = null,
)
{
$this->deploymentPlan = new Plan(steps: [
new Step(
name: 'Check docker is installed',
script: 'command -v docker >/dev/null 2>&1 || { echo >&2 "Docker is not installed. Aborting."; exit 1; }',
),
new Step(
name: 'Run the docker image',
script: function () {
$script = collect();
if ($this->containerName) {
$script->push('docker stop ' . $this->containerName . ' || true');
} else if ($this->containerId) {
$script->push('docker stop ' . $this->containerId . ' || true');
}
$runCommand = "docker run -d";
if ($this->containerName) {
$runCommand .= " --name {$this->containerName}";
}
if ($this->defaultPassword) {
$runCommand .= " -e POSTGRES_PASSWORD={$this->defaultPassword}";
}
if ($this->defaultUser) {
$runCommand .= " -e POSTGRES_USER={$this->defaultUser}";
}
if ($this->defaultDb) {
$runCommand .= " -e POSTGRES_DB={$this->defaultDb}";
}
$runCommand .= " -p 5432:5432 postgres:17";
}
)
]);
}
}

View File

@@ -4,6 +4,6 @@ namespace App\Enums;
enum OrganisationRole: string
{
case Admin = 'admin';
case Member = 'member';
case ADMIN = 'admin';
case MEMBER = 'member';
}

View File

@@ -4,5 +4,5 @@ namespace App\Enums;
enum RepositoryType: string
{
case Git = 'git';
case GIT = 'git';
}

View File

@@ -4,6 +4,6 @@ namespace App\Enums;
enum ServerProvider: string
{
case Hetzner = 'hetzner';
case DigitalOcean = 'digital-ocean';
case HETZNER = 'hetzner';
case DIGITAL_OCEAN = 'digital-ocean';
}

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Enums;
enum ServiceCategory: string
{
case DATABASE = 'database';
case APPLICATION = 'application';
case GATEWAY = 'gateway';
case STORAGE = 'storage';
case CACHE = 'cache';
}

View File

@@ -4,6 +4,8 @@ namespace App\Enums;
enum ServiceStatus: string
{
case NOT_INSTALLED = 'not-installed';
case INSTALLING = 'installing';
case RUNNING = 'running';
case STOPPED = 'stopped';
case ERROR = 'error';

16
app/Enums/ServiceType.php Normal file
View File

@@ -0,0 +1,16 @@
<?php
namespace App\Enums;
enum ServiceType: string {
case FRANKENPHP = 'frankenphp';
case PHP_FPM = 'php-fpm';
case POSTGRES = 'postgres';
case CADDY = 'caddy';
case VALKEY = 'valkey';
// future?
case MYSQL = 'mysql';
case NGINX = 'nginx';
case REDIS = 'redis';
}

View File

@@ -3,7 +3,9 @@
namespace App\Models;
use App\Drivers\Driver;
use App\Enums\ServiceCategory;
use App\Enums\ServiceStatus;
use App\Enums\ServiceType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -16,6 +18,8 @@ class Service extends Model
{
return [
'status' => ServiceStatus::class,
'category' => ServiceCategory::class,
'type' => ServiceType::class,
];
}

View File

@@ -26,7 +26,7 @@ class User extends Authenticatable
'slug' => Organisation::createUniqueSlug($user->name),
'owner_id' => $user->id,
]);
$organisation->members()->attach($user, ['role' => OrganisationRole::Admin]);
$organisation->members()->attach($user, ['role' => OrganisationRole::ADMIN]);
});
}