140 lines
3.7 KiB
PHP
140 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Drivers\Caddy;
|
|
|
|
use App\Data\Operations\Plan;
|
|
use App\Data\Operations\PlannedStep;
|
|
use App\Drivers\Concerns\RendersCompose;
|
|
use App\Drivers\Concerns\SupportsSlices;
|
|
use App\Drivers\GatewayDriver;
|
|
use App\Enums\EnvironmentAttachmentRole;
|
|
use App\Enums\ServiceType;
|
|
use App\Models\Service;
|
|
use App\Models\ServiceSlice;
|
|
|
|
class Caddy2Driver extends GatewayDriver implements RendersCompose, SupportsSlices
|
|
{
|
|
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;
|
|
}
|
|
|
|
public function getOperationPlan(string $operationHash): Plan
|
|
{
|
|
return new Plan(steps: [
|
|
new PlannedStep(
|
|
name: 'Render Caddy Compose files',
|
|
script: "mkdir -p /home/keystone/gateway /home/keystone/services/{$this->service?->id}",
|
|
),
|
|
new PlannedStep(
|
|
name: 'Start Caddy gateway',
|
|
script: "docker compose -f /home/keystone/services/{$this->service?->id}/compose.yml up -d",
|
|
),
|
|
]);
|
|
}
|
|
|
|
public function serviceType(): ServiceType
|
|
{
|
|
return ServiceType::CADDY;
|
|
}
|
|
|
|
public function versionTrack(): string
|
|
{
|
|
return '2';
|
|
}
|
|
|
|
public function defaultImage(): string
|
|
{
|
|
return 'caddy:2';
|
|
}
|
|
|
|
public function defaultPorts(): array
|
|
{
|
|
return [80, 443];
|
|
}
|
|
|
|
public function firewallRules(): array
|
|
{
|
|
return ['80/tcp', '443/tcp'];
|
|
}
|
|
|
|
public function environmentSchema(): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function resourceDefaults(): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function updateBehavior(): string
|
|
{
|
|
return 'stateless_redeploy';
|
|
}
|
|
|
|
public function composeService(): array
|
|
{
|
|
return [
|
|
'image' => $this->service?->available_image_digest
|
|
?: $this->service?->current_image_digest
|
|
?: $this->defaultImage(),
|
|
'restart' => 'unless-stopped',
|
|
'ports' => ['80:80', '443:443'],
|
|
'volumes' => [
|
|
'/home/keystone/gateway/Caddyfile:/etc/caddy/Caddyfile:ro',
|
|
"keystone_service_{$this->service?->id}_caddy_data:/data",
|
|
"keystone_service_{$this->service?->id}_caddy_config:/config",
|
|
],
|
|
'healthcheck' => [
|
|
'test' => ['CMD', 'caddy', 'version'],
|
|
'interval' => '10s',
|
|
'timeout' => '5s',
|
|
'retries' => 5,
|
|
],
|
|
];
|
|
}
|
|
|
|
public function composeVolumes(): array
|
|
{
|
|
return [
|
|
"keystone_service_{$this->service?->id}_caddy_data" => null,
|
|
"keystone_service_{$this->service?->id}_caddy_config" => null,
|
|
];
|
|
}
|
|
|
|
public function environmentExports(): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function supportedSliceTypes(): array
|
|
{
|
|
return ['route'];
|
|
}
|
|
|
|
public function environmentExportsForSlice(ServiceSlice $slice, ?EnvironmentAttachmentRole $role = null): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function provisionSliceScript(ServiceSlice $slice): string
|
|
{
|
|
return implode("\n", [
|
|
'set -euo pipefail',
|
|
'mkdir -p /home/keystone/gateway/Caddyfile.d',
|
|
'test -f /home/keystone/gateway/Caddyfile || touch /home/keystone/gateway/Caddyfile',
|
|
"test ! -e /home/keystone/gateway/Caddyfile.d/{$slice->id}.caddy || true",
|
|
]);
|
|
}
|
|
}
|