From afde59bd39e8ceb7571484e9d9c9af94ec79831f Mon Sep 17 00:00:00 2001 From: Harry Bayliss Date: Thu, 24 Apr 2025 19:55:39 +0100 Subject: [PATCH] wip caddy deployment --- app/Data/Slices/CaddySliceData.php | 12 +++++ app/Drivers/Caddy/Caddy2Driver.php | 66 +++++++++++++++++++++++ app/Drivers/DatabaseDriver.php | 5 ++ app/Drivers/Driver.php | 6 ++- app/Drivers/GatewayDriver.php | 18 +++++++ app/Drivers/Postgres/Postgres17Driver.php | 5 +- app/Models/Service.php | 12 +++-- config/keystone.php | 12 +++-- 8 files changed, 126 insertions(+), 10 deletions(-) create mode 100644 app/Data/Slices/CaddySliceData.php create mode 100644 app/Drivers/Caddy/Caddy2Driver.php create mode 100644 app/Drivers/GatewayDriver.php diff --git a/app/Data/Slices/CaddySliceData.php b/app/Data/Slices/CaddySliceData.php new file mode 100644 index 0000000..8fe7bb5 --- /dev/null +++ b/app/Data/Slices/CaddySliceData.php @@ -0,0 +1,12 @@ +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; + } +} diff --git a/app/Drivers/DatabaseDriver.php b/app/Drivers/DatabaseDriver.php index b04b0e0..6713a02 100644 --- a/app/Drivers/DatabaseDriver.php +++ b/app/Drivers/DatabaseDriver.php @@ -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; diff --git a/app/Drivers/Driver.php b/app/Drivers/Driver.php index 51085c6..27277c9 100644 --- a/app/Drivers/Driver.php +++ b/app/Drivers/Driver.php @@ -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; } diff --git a/app/Drivers/GatewayDriver.php b/app/Drivers/GatewayDriver.php new file mode 100644 index 0000000..c70f833 --- /dev/null +++ b/app/Drivers/GatewayDriver.php @@ -0,0 +1,18 @@ +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; } ), diff --git a/app/Models/Service.php b/app/Models/Service.php index c13200f..77d1166 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -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 + ); } } diff --git a/config/keystone.php b/config/keystone.php index 9f851bb..7f3e78c 100644 --- a/config/keystone.php +++ b/config/keystone.php @@ -1,5 +1,6 @@ [ '17' => Postgres17Driver::class, ], + 'caddy' => [ + '2' => Caddy2Driver::class, + ] ], 'internal_ip_base' => env('INTERNAL_IP_BASE', '192.168.2.'), @@ -53,10 +57,10 @@ return [ 'name' => ServiceType::CADDY, 'description' => 'Caddy', 'versions' => [ - '2.9' => [ - 'name' => 'Caddy 2.9', - 'description' => 'Caddy 2.9', - 'image' => 'caddy:2.9' + '2' => [ + 'name' => 'Caddy 2', + 'description' => 'Caddy 2', + 'image' => 'caddy:2' ], ], ],