wip caddy deployment

This commit is contained in:
2025-04-24 19:55:39 +01:00
parent 46a76f7bce
commit afde59bd39
8 changed files with 126 additions and 10 deletions

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Data\Slices;
class CaddySliceData
{
public function __construct(
public string $domain,
public string $type,
public array $targets
) {}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Drivers\Caddy;
use App\Drivers\GatewayDriver;
use App\Data\Deployments\Plan;
use App\Data\Deployments\PlannedStep as Step;
use App\Models\Service;
class Caddy2Driver extends GatewayDriver
{
public ?string $containerName;
public ?string $containerId;
public function __construct(
?string $containerName = null,
?string $containerId = null,
public ?Service $service = null,
) {
$this->containerName = $containerName;
$this->containerId = $containerId;
$this->service = $service;
$this->deploymentPlan = new Plan(steps: [
new Step(
name: 'Generate Caddyfile',
script: function () {
$script = collect();
$script->push('cd ~');
$script->push('test -d services || mkdir services');
$script->push('cd services');
$script->push("test -d {$this->service->id} || mkdir {$this->service->id}");
$script->push("cd {$this->service->id}");
}
),
new Step(
name: 'Run the docker image',
script: function () {
$script = collect();
if ($this->containerName) {
$script->push('docker stop '.$this->containerName.' || true');
} elseif ($this->containerId) {
$script->push('docker stop '.$this->containerId.' || true');
}
$runCommand = 'docker run -d';
if ($this->containerName) {
$runCommand .= " --name {$this->containerName}";
}
$runCommand .= ' -p 80:80 -p 443:443 caddy:2';
return $runCommand;
}
),
]);
}
public function buildCaddyfile(): string
{
$caddyfile = "http://{$this->service->name} {\n";
$caddyfile .= " reverse_proxy {$this->service->credentials['backend']}\n";
$caddyfile .= "}\n";
return $caddyfile;
}
}

View File

@@ -2,6 +2,8 @@
namespace App\Drivers;
use App\Models\Service;
abstract class DatabaseDriver extends Driver
{
public ?string $containerName;
@@ -13,9 +15,12 @@ abstract class DatabaseDriver extends Driver
abstract public function __construct(
?string $containerName = null,
?string $containerId = null,
?Service $service = null,
?array $credentials = null,
);
abstract public function defaultCredentials(): array;
abstract public function createUser(string $user, string $password): string;
// abstract public function createDatabase(string $db, string $user): string;

View File

@@ -3,9 +3,12 @@
namespace App\Drivers;
use App\Data\Deployments\Plan;
use App\Models\Service;
abstract class Driver
{
public ?Service $service;
public Plan $deploymentPlan;
public ?string $containerName;
@@ -15,7 +18,6 @@ abstract class Driver
abstract public function __construct(
?string $containerName = null,
?string $containerId = null,
?int $service = null,
);
abstract public function defaultCredentials(): array;
}

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Drivers;
use App\Models\Service;
abstract class GatewayDriver extends Driver
{
public ?string $containerName;
public ?string $containerId;
abstract public function __construct(
?string $containerName = null,
?string $containerId = null,
?Service $service = null,
);
}

View File

@@ -5,6 +5,7 @@ namespace App\Drivers\Postgres;
use App\Data\Deployments\Plan;
use App\Data\Deployments\PlannedStep as Step;
use App\Drivers\DatabaseDriver;
use App\Models\Service;
use Illuminate\Support\Str;
class Postgres17Driver extends DatabaseDriver
@@ -14,6 +15,7 @@ class Postgres17Driver extends DatabaseDriver
public function __construct(
public ?string $containerName = null,
public ?string $containerId = null,
public ?Service $service = null,
public ?array $credentials = null,
) {
$credentials = $credentials ?? $this->defaultCredentials();
@@ -48,9 +50,10 @@ class Postgres17Driver extends DatabaseDriver
if ($db) {
$runCommand .= " -e POSTGRES_DB={$db}";
}
$runCommand .= ' -p 5432:5432 postgres:17';
$script->push($runCommand);
return $runCommand;
}
),

View File

@@ -31,7 +31,7 @@ class Service extends Model
public function folderName(): Attribute
{
return new Attribute(
get: fn () => $this->name . '-' . $this->id,
get: fn() => $this->name . '-' . $this->id,
);
}
@@ -50,12 +50,18 @@ class Service extends Model
return $this->morphMany(Deployment::class, 'target');
}
public function driver(): Driver {
public function driver(): Driver
{
$class = config("keystone.drivers.{$this->driver_name}");
if (! class_exists($class)) {
throw new \Exception("Driver class {$class} not found");
}
return new $class($this->container_name, $this->container_id, credentials: $this->credentials);
return new $class(
containerName: $this->container_name,
containerId: $this->container_id,
serviceId: $this->id,
credentials: $this->credentials
);
}
}