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

View File

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

View File

@@ -50,12 +50,18 @@ class Service extends Model
return $this->morphMany(Deployment::class, 'target'); return $this->morphMany(Deployment::class, 'target');
} }
public function driver(): Driver { public function driver(): Driver
{
$class = config("keystone.drivers.{$this->driver_name}"); $class = config("keystone.drivers.{$this->driver_name}");
if (! class_exists($class)) { if (! class_exists($class)) {
throw new \Exception("Driver class {$class} not found"); 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
);
} }
} }

View File

@@ -1,5 +1,6 @@
<?php <?php
use App\Drivers\Caddy\Caddy2Driver;
use App\Drivers\Postgres\Postgres17Driver; use App\Drivers\Postgres\Postgres17Driver;
use App\Enums\ServiceCategory; use App\Enums\ServiceCategory;
use App\Enums\ServiceType; use App\Enums\ServiceType;
@@ -9,6 +10,9 @@ return [
'postgres' => [ 'postgres' => [
'17' => Postgres17Driver::class, '17' => Postgres17Driver::class,
], ],
'caddy' => [
'2' => Caddy2Driver::class,
]
], ],
'internal_ip_base' => env('INTERNAL_IP_BASE', '192.168.2.'), 'internal_ip_base' => env('INTERNAL_IP_BASE', '192.168.2.'),
@@ -53,10 +57,10 @@ return [
'name' => ServiceType::CADDY, 'name' => ServiceType::CADDY,
'description' => 'Caddy', 'description' => 'Caddy',
'versions' => [ 'versions' => [
'2.9' => [ '2' => [
'name' => 'Caddy 2.9', 'name' => 'Caddy 2',
'description' => 'Caddy 2.9', 'description' => 'Caddy 2',
'image' => 'caddy:2.9' 'image' => 'caddy:2'
], ],
], ],
], ],