Skip to content

Commit 3482f7d

Browse files
committed
feat: #225 rework workers ui definition
1 parent 870fbc4 commit 3482f7d

21 files changed

+1197
-1278
lines changed

Diff for: app/Actions/Nodes/InitCluster.php

+22-17
Original file line numberDiff line numberDiff line change
@@ -195,23 +195,29 @@ private function getCaddyProcessConfig(Node $node): array
195195
return [
196196
'name' => 'svc',
197197
'placementNodeId' => $node->id,
198-
'launchMode' => LaunchMode::Daemon->value,
199-
'dockerRegistryId' => null,
200-
'dockerImage' => 'ghcr.io/ptah-sh/ptah-caddy:latest',
201-
'releaseCommand' => [
202-
'command' => null,
203-
],
204-
'command' => 'sh /start.sh',
205-
'healthcheck' => [
206-
'command' => null,
207-
'interval' => 10,
208-
'timeout' => 5,
209-
'retries' => 3,
210-
'startPeriod' => 30,
211-
'startInterval' => 5,
198+
'workers' => [
199+
[
200+
'name' => 'main',
201+
'dockerRegistryId' => null,
202+
'dockerImage' => 'ghcr.io/ptah-sh/ptah-caddy:latest',
203+
'dockerName' => 'caddy',
204+
'command' => 'sh /start.sh',
205+
'replicas' => 1,
206+
'launchMode' => LaunchMode::Daemon,
207+
'schedule' => null,
208+
'releaseCommand' => [
209+
'command' => null,
210+
],
211+
'healthcheck' => [
212+
'command' => null,
213+
'interval' => 10,
214+
'timeout' => 5,
215+
'retries' => 3,
216+
'startPeriod' => 30,
217+
'startInterval' => 5,
218+
],
219+
],
212220
],
213-
'backups' => [],
214-
'workers' => [],
215221
'envVars' => [
216222
[
217223
'name' => 'CADDY_ADMIN',
@@ -242,7 +248,6 @@ private function getCaddyProcessConfig(Node $node): array
242248
'path' => '/config',
243249
],
244250
],
245-
'replicas' => 1,
246251
'ports' => [
247252
['targetPort' => '80', 'publishedPort' => '80'],
248253
['targetPort' => '443', 'publishedPort' => '443'],

Diff for: app/Actions/Services/StartDeployment.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function handle(User $user, Service $service, DeploymentData $deploymentD
3939
'swarm_id' => $service->swarm_id,
4040
'team_id' => $service->team_id,
4141
'invoker_id' => $user->id,
42-
'type' => $service->deployments()->exists() ? NodeTaskGroupType::UpdateService : NodeTaskGroupType::CreateService,
42+
'type' => NodeTaskGroupType::LaunchService,
4343
]);
4444

4545
$deployment = $service->deployments()->create([

Diff for: app/Http/Controllers/ServiceController.php

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use App\Http\Requests\UpdateServiceRequest;
66
use App\Models\DeploymentData;
7+
use App\Models\DeploymentData\Process;
8+
use App\Models\DeploymentData\Worker;
79
use App\Models\Service;
810
use App\Models\Swarm;
911
use Illuminate\Support\Facades\DB;
@@ -52,6 +54,9 @@ public function create()
5254
'networkName' => $networks->first()->name,
5355
]);
5456

57+
$blankProcess = Process::make([]);
58+
$blankWorker = Worker::make([]);
59+
5560
return Inertia::render('Services/Create', [
5661
'networks' => $networks,
5762
'nodes' => $nodes,
@@ -62,6 +67,8 @@ public function create()
6267
'node' => [
6368
'swarm' => $swarm,
6469
],
70+
'blankProcess' => $blankProcess,
71+
'blankWorker' => $blankWorker,
6572
]);
6673
}
6774

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

88+
$blankProcess = Process::make([]);
89+
$blankWorker = Worker::make([]);
90+
8191
return Inertia::render('Services/Show', [
8292
'service' => $service,
8393
'networks' => $networks,
@@ -87,6 +97,8 @@ public function show(Service $service)
8797
'node' => [
8898
'swarm' => $swarm,
8999
],
100+
'blankProcess' => $blankProcess,
101+
'blankWorker' => $blankWorker,
90102
]);
91103
}
92104

Diff for: app/Models/Deployment.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ public function asNodeTasks(): array
8484
$tasks = [];
8585

8686
foreach ($data->processes as $process) {
87-
$tasks = array_merge($tasks, $process->asNodeTasks($this));
87+
$tasks = [
88+
...$tasks,
89+
...$process->asNodeTasks($this),
90+
];
8891
}
8992

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

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

107111
return $tasks;

Diff for: app/Models/DeploymentData.php

+2-31
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
namespace App\Models;
44

5-
use App\Models\DeploymentData\Healthcheck;
6-
use App\Models\DeploymentData\LaunchMode;
75
use App\Models\DeploymentData\Process;
8-
use App\Models\DeploymentData\ReleaseCommand;
96
use App\Rules\UniqueInArray;
107
use App\Util\Arrays;
118
use Illuminate\Validation\ValidationException;
@@ -26,38 +23,12 @@ public function __construct(
2623

2724
public static function make(array $attributes): static
2825
{
29-
$processDefaults = [
30-
'name' => 'svc',
31-
'placementNodeId' => null,
32-
'dockerRegistryId' => null,
33-
'dockerImage' => '',
34-
'releaseCommand' => ReleaseCommand::from([
35-
'command' => null,
36-
]),
37-
'command' => '',
38-
'healthcheck' => Healthcheck::from([
39-
'command' => null,
40-
]),
41-
'backups' => [],
42-
'workers' => [],
43-
'launchMode' => LaunchMode::Daemon->value,
44-
'envVars' => [],
45-
'secretVars' => [],
46-
'configFiles' => [],
47-
'secretFiles' => [],
48-
'volumes' => [],
49-
'ports' => [],
50-
'replicas' => 1,
51-
'caddy' => [],
52-
'fastCgi' => null,
53-
'redirectRules' => [],
54-
'rewriteRules' => [],
55-
];
26+
$processDefaults = Process::make([]);
5627

5728
$defaults = [
5829
'networkName' => '',
5930
'internalDomain' => '',
60-
'processes' => empty($attributes['processes']) ? [$processDefaults] : $attributes['processes'],
31+
'processes' => [$processDefaults],
6132
];
6233

6334
return self::from([

Diff for: app/Models/DeploymentData/LaunchMode.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,19 @@
55
enum LaunchMode: string
66
{
77
case Daemon = 'daemon';
8-
case Scheduled = 'scheduled';
9-
case Backup = 'backup';
10-
case Lifecycle = 'lifecycle';
8+
case Cronjob = 'cronjob';
9+
case BackupCreate = 'backup_create';
10+
case BackupRestore = 'backup_restore';
1111
case Manual = 'manual';
12+
13+
public function maxReplicas(): int
14+
{
15+
return match ($this) {
16+
self::Daemon => PHP_INT_MAX,
17+
self::Cronjob => 1,
18+
self::BackupCreate => 1,
19+
self::BackupRestore => 1,
20+
self::Manual => 1,
21+
};
22+
}
1223
}

0 commit comments

Comments
 (0)