Skip to content

Commit

Permalink
feat: #225 rework workers ui definition
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Oct 7, 2024
1 parent 870fbc4 commit 3482f7d
Show file tree
Hide file tree
Showing 21 changed files with 1,197 additions and 1,278 deletions.
39 changes: 22 additions & 17 deletions app/Actions/Nodes/InitCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,23 +195,29 @@ private function getCaddyProcessConfig(Node $node): array
return [
'name' => 'svc',
'placementNodeId' => $node->id,
'launchMode' => LaunchMode::Daemon->value,
'dockerRegistryId' => null,
'dockerImage' => 'ghcr.io/ptah-sh/ptah-caddy:latest',
'releaseCommand' => [
'command' => null,
],
'command' => 'sh /start.sh',
'healthcheck' => [
'command' => null,
'interval' => 10,
'timeout' => 5,
'retries' => 3,
'startPeriod' => 30,
'startInterval' => 5,
'workers' => [
[
'name' => 'main',
'dockerRegistryId' => null,
'dockerImage' => 'ghcr.io/ptah-sh/ptah-caddy:latest',
'dockerName' => 'caddy',
'command' => 'sh /start.sh',
'replicas' => 1,
'launchMode' => LaunchMode::Daemon,
'schedule' => null,
'releaseCommand' => [
'command' => null,
],
'healthcheck' => [
'command' => null,
'interval' => 10,
'timeout' => 5,
'retries' => 3,
'startPeriod' => 30,
'startInterval' => 5,
],
],
],
'backups' => [],
'workers' => [],
'envVars' => [
[
'name' => 'CADDY_ADMIN',
Expand Down Expand Up @@ -242,7 +248,6 @@ private function getCaddyProcessConfig(Node $node): array
'path' => '/config',
],
],
'replicas' => 1,
'ports' => [
['targetPort' => '80', 'publishedPort' => '80'],
['targetPort' => '443', 'publishedPort' => '443'],
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Services/StartDeployment.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function handle(User $user, Service $service, DeploymentData $deploymentD
'swarm_id' => $service->swarm_id,
'team_id' => $service->team_id,
'invoker_id' => $user->id,
'type' => $service->deployments()->exists() ? NodeTaskGroupType::UpdateService : NodeTaskGroupType::CreateService,
'type' => NodeTaskGroupType::LaunchService,
]);

$deployment = $service->deployments()->create([
Expand Down
12 changes: 12 additions & 0 deletions app/Http/Controllers/ServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use App\Http\Requests\UpdateServiceRequest;
use App\Models\DeploymentData;
use App\Models\DeploymentData\Process;
use App\Models\DeploymentData\Worker;
use App\Models\Service;
use App\Models\Swarm;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -52,6 +54,9 @@ public function create()
'networkName' => $networks->first()->name,
]);

$blankProcess = Process::make([]);
$blankWorker = Worker::make([]);

return Inertia::render('Services/Create', [
'networks' => $networks,
'nodes' => $nodes,
Expand All @@ -62,6 +67,8 @@ public function create()
'node' => [
'swarm' => $swarm,
],
'blankProcess' => $blankProcess,
'blankWorker' => $blankWorker,
]);
}

Expand All @@ -78,6 +85,9 @@ public function show(Service $service)
$s3Storages = $service->swarm->data->s3Storages;
$swarm = $service->swarm;

$blankProcess = Process::make([]);
$blankWorker = Worker::make([]);

return Inertia::render('Services/Show', [
'service' => $service,
'networks' => $networks,
Expand All @@ -87,6 +97,8 @@ public function show(Service $service)
'node' => [
'swarm' => $swarm,
],
'blankProcess' => $blankProcess,
'blankWorker' => $blankWorker,
]);
}

Expand Down
8 changes: 6 additions & 2 deletions app/Models/Deployment.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ public function asNodeTasks(): array
$tasks = [];

foreach ($data->processes as $process) {
$tasks = array_merge($tasks, $process->asNodeTasks($this));
$tasks = [
...$tasks,
...$process->asNodeTasks($this),
];
}

$previousProcesses = $this->previousDeployment()?->data->processes ?? [];
Expand All @@ -101,7 +104,8 @@ public function asNodeTasks(): array
}
}

// Why is this needed? :)
// Question: Why is this needed? :)
// Answer: process asNodeTasks() method makes modifications to the process object. "asNodeTasks" is not the best name.
$this->saveQuietly();

return $tasks;
Expand Down
33 changes: 2 additions & 31 deletions app/Models/DeploymentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace App\Models;

use App\Models\DeploymentData\Healthcheck;
use App\Models\DeploymentData\LaunchMode;
use App\Models\DeploymentData\Process;
use App\Models\DeploymentData\ReleaseCommand;
use App\Rules\UniqueInArray;
use App\Util\Arrays;
use Illuminate\Validation\ValidationException;
Expand All @@ -26,38 +23,12 @@ public function __construct(

public static function make(array $attributes): static
{
$processDefaults = [
'name' => 'svc',
'placementNodeId' => null,
'dockerRegistryId' => null,
'dockerImage' => '',
'releaseCommand' => ReleaseCommand::from([
'command' => null,
]),
'command' => '',
'healthcheck' => Healthcheck::from([
'command' => null,
]),
'backups' => [],
'workers' => [],
'launchMode' => LaunchMode::Daemon->value,
'envVars' => [],
'secretVars' => [],
'configFiles' => [],
'secretFiles' => [],
'volumes' => [],
'ports' => [],
'replicas' => 1,
'caddy' => [],
'fastCgi' => null,
'redirectRules' => [],
'rewriteRules' => [],
];
$processDefaults = Process::make([]);

$defaults = [
'networkName' => '',
'internalDomain' => '',
'processes' => empty($attributes['processes']) ? [$processDefaults] : $attributes['processes'],
'processes' => [$processDefaults],
];

return self::from([
Expand Down
17 changes: 14 additions & 3 deletions app/Models/DeploymentData/LaunchMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@
enum LaunchMode: string
{
case Daemon = 'daemon';
case Scheduled = 'scheduled';
case Backup = 'backup';
case Lifecycle = 'lifecycle';
case Cronjob = 'cronjob';
case BackupCreate = 'backup_create';
case BackupRestore = 'backup_restore';
case Manual = 'manual';

public function maxReplicas(): int
{
return match ($this) {
self::Daemon => PHP_INT_MAX,
self::Cronjob => 1,
self::BackupCreate => 1,
self::BackupRestore => 1,
self::Manual => 1,
};
}
}
Loading

0 comments on commit 3482f7d

Please sign in to comment.