Skip to content

Commit

Permalink
feat: #41 move placementNodeId to processes and require it's presence
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Sep 8, 2024
1 parent cf5d8e5 commit 5897cc9
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 33 deletions.
6 changes: 5 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
node_modules/
vendor/
vendor/
.github/
.gitignore
.dockerignore
.gitattributes
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ RUN php composer.phar install \
&& npm run build \
&& php artisan optimize \
&& php artisan data:cache-structures \
&& rm -rf node_modules \
&& apt-get -y remove npm unzip \
&& apt-get -y clean \
&& apt-get -y autoremove \
Expand Down
6 changes: 3 additions & 3 deletions app/Actions/Nodes/InitCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,17 @@ private function createCaddyDeploymentData(Network $network, Node $node): Deploy
return DeploymentData::validateAndCreate([
'networkName' => $network->docker_name,
'internalDomain' => 'caddy.ptah.local',
'placementNodeId' => $node->id,
'processes' => [
$this->getCaddyProcessConfig(),
$this->getCaddyProcessConfig($node),
],
]);
}

private function getCaddyProcessConfig(): array
private function getCaddyProcessConfig(Node $node): array
{
return [
'name' => 'svc',
'placementNodeId' => $node->id,
'launchMode' => LaunchMode::Daemon->value,
'dockerRegistryId' => null,
'dockerImage' => 'caddy:2.8-alpine',
Expand Down
3 changes: 0 additions & 3 deletions app/Actions/Services/StartDeployment.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public function authorize(ActionRequest $request): bool
public function handle(User $user, Service $service, DeploymentData $deploymentData): Deployment
{
return DB::transaction(function () use ($service, $deploymentData, $user) {
$service->placement_node_id = $deploymentData->placementNodeId;
$service->saveQuietly();

$taskGroup = NodeTaskGroup::create([
'swarm_id' => $service->swarm_id,
'team_id' => $service->team_id,
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/DispatchProcessBackupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function dispatchBackupTask(): void
throw new Exception("Could not find backup command {$this->option('backup-cmd-id')} in process {$process->name}.");
}

$node = Node::findOrFail($deployment->data->placementNodeId);
$node = Node::findOrFail($process->placementNodeId);

$taskGroup = $node->taskGroups()->create([
'swarm_id' => $node->swarm_id,
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/DispatchVolumeBackupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function dispatchBackupTask(): void
throw new Exception("Could not find volume {$this->option('volume')} in process {$process->name}.");
}

$node = Node::withoutGlobalScope(TeamScope::class)->findOrFail($deployment->data->placementNodeId);
$node = Node::withoutGlobalScope(TeamScope::class)->findOrFail($process->placementNodeId);

$taskGroup = $node->taskGroups()->create([
'swarm_id' => $node->swarm_id,
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/SelfHostPtah.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ public function handle()
StartDeployment::run($user, $service, DeploymentData::validateAndCreate([
'networkName' => $network->docker_name,
'internalDomain' => 'server.ptah.local',
'placementNodeId' => $node->id,
'processes' => [
[
'name' => 'pg',
'placementNodeId' => $node->id,
'launchMode' => LaunchMode::Daemon->value,
'dockerRegistryId' => null,
'dockerImage' => 'bitnami/postgresql:16',
Expand Down
5 changes: 1 addition & 4 deletions app/Models/DeploymentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use App\Util\Arrays;
use Illuminate\Validation\ValidationException;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Attributes\Validation\Exists;
use Spatie\LaravelData\Attributes\Validation\Rule;
use Spatie\LaravelData\Data;

Expand All @@ -19,8 +18,6 @@ class DeploymentData extends Data
public function __construct(
public string $networkName,
public string $internalDomain,
#[Exists(Node::class, 'id')]
public ?int $placementNodeId,
#[DataCollectionOf(Process::class)]
#[Rule(new UniqueInArray('name'))]
/* @var Process[] */
Expand All @@ -31,6 +28,7 @@ public static function make(array $attributes): static
{
$processDefaults = [
'name' => 'svc',
'placementNodeId' => null,
'dockerRegistryId' => null,
'dockerImage' => '',
'releaseCommand' => ReleaseCommand::from([
Expand Down Expand Up @@ -58,7 +56,6 @@ public static function make(array $attributes): static
$defaults = [
'networkName' => '',
'internalDomain' => '',
'placementNodeId' => null,
'processes' => empty($attributes['processes']) ? [$processDefaults] : $attributes['processes'],
];

Expand Down
10 changes: 8 additions & 2 deletions app/Models/DeploymentData/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models\DeploymentData;

use App\Models\Deployment;
use App\Models\Node;
use App\Models\NodeTasks\CreateConfig\CreateConfigMeta;
use App\Models\NodeTasks\CreateSecret\CreateSecretMeta;
use App\Models\NodeTasks\CreateService\CreateServiceMeta;
Expand All @@ -15,13 +16,18 @@
use Exception;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Attributes\Validation\Enum;
use Spatie\LaravelData\Attributes\Validation\Exists;
use Spatie\LaravelData\Attributes\Validation\RequiredWith;
use Spatie\LaravelData\Attributes\Validation\Rule;
use Spatie\LaravelData\Data;

class Process extends Data
{
public function __construct(
public string $name,
#[Exists(Node::class, 'id')]
#[RequiredWith('volumes')]
public ?int $placementNodeId,
public ?string $dockerName,
public ?string $dockerRegistryId,
public string $dockerImage,
Expand Down Expand Up @@ -330,9 +336,9 @@ public function asNodeTasks(Deployment $deployment): array
],
'ConfigName' => $configFile->dockerName,
])->values()->toArray(),
'Placement' => $deployment->data->placementNodeId ? [
'Placement' => $this->placementNodeId ? [
'Constraints' => [
"node.labels.sh.ptah.node.id=={$deployment->data->placementNodeId}",
"node.labels.sh.ptah.node.id=={$this->placementNodeId}",
],
] : [],
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::statement("
UPDATE deployments
SET data = jsonb_set(
data,
'{processes}',
(
SELECT jsonb_agg(
process || jsonb_build_object('placementNodeId', data->'placementNodeId')
)
FROM jsonb_array_elements(data -> 'processes') AS process
)
) - 'placementNodeId'
");
}

/**
* Reverse the migrations.
*/
public function down(): void
{
DB::statement("
UPDATE deployments
SET data = jsonb_set(
data,
'{placementNodeId}',
(data -> 'processes' -> 0 -> 'placementNodeId')
) || jsonb_set(
data,
'{processes}',
(
SELECT jsonb_agg(process - 'placementNodeId')
FROM jsonb_array_elements(data -> 'processes') AS process
)
)
");
}
};
45 changes: 28 additions & 17 deletions resources/js/Pages/Services/Partials/DeploymentData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const addProcess = () => {
model.value.processes.push({
id: makeId("process"),
name: "process_" + newIndex,
placementNodeId: null,
dockerRegistryId: null,
dockerImage: "",
releaseCommand: {
Expand Down Expand Up @@ -330,20 +331,6 @@ const extractFieldErrors = (basePath) => {
</option>
</Select>
</FormField>

<FormField
:error="props.errors['placementNodeId']"
class="col-span-2"
>
<template #label>Placement Node</template>

<Select v-model="model.placementNodeId">
<option :value="null">Run on all Nodes</option>
<option v-for="node in $page.props.nodes" :value="node.id">
{{ node.name }}
</option>
</Select>
</FormField>
</template>
</ActionSection>

Expand Down Expand Up @@ -537,7 +524,7 @@ const extractFieldErrors = (basePath) => {
}}.{{ model.internalDomain }}</span
>
</FormField>

<!--
<FormField
class="col-span-2"
:error="
Expand All @@ -555,8 +542,32 @@ const extractFieldErrors = (basePath) => {
"
>
<option value="daemon">Daemon / Worker</option>
<!-- <option :value="false">Schedule (crontab)</option>-->
<!-- <option>Lifecycle Hook (before deploy / after deploy to, for example run migrations and/or upload static files)</option>-->
<option :value="false">Schedule (crontab)</option>
<option>Lifecycle Hook (before deploy / after deploy to, for example run migrations and/or upload static files)</option>
</Select>
</FormField>
-->

<FormField
:error="
props.errors[
`processes.${state.selectedProcessIndex['processes']}.placementNodeId`
]
"
class="col-span-2"
>
<template #label>Placement Node</template>

<Select
v-model="
model.processes[state.selectedProcessIndex['processes']]
.placementNodeId
"
>
<option :value="null">Run on all Nodes</option>
<option v-for="node in $page.props.nodes" :value="node.id">
{{ node.name }}
</option>
</Select>
</FormField>

Expand Down

0 comments on commit 5897cc9

Please sign in to comment.