Skip to content

Commit

Permalink
fix: #138 use dird to get the real ip of end-users
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Aug 25, 2024
1 parent 524aaf9 commit 6ca2ccc
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 0 deletions.
39 changes: 39 additions & 0 deletions api-nodes/Http/Controllers/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

use ApiNodes\Models\AgentStartedEventData;
use App\Models\Node;
use App\Models\NodeTaskGroup;
use App\Models\NodeTaskGroupType;
use App\Models\NodeTasks\UpdateDirdConfig\UpdateDirdConfigMeta;
use App\Models\NodeTaskType;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class EventController
{
Expand All @@ -20,6 +25,40 @@ public function started(Node $node, AgentStartedEventData $data)
$swarm->data->joinTokens = $data->swarm->joinTokens;
$swarm->data->managerNodes = $data->swarm->managerNodes;

$nodeAddresses = $swarm->nodes->pluck('data.address')->toArray();
$dockerServices = collect($swarm->services)
->filter(function ($service) {
return Str::contains($service->docker_name, 'caddy');
})
->pluck('docker_name')
->toArray();

$swarm->nodes->each(function (Node $node) use ($swarm, $nodeAddresses, $dockerServices) {
$taskGroup = NodeTaskGroup::create([
'type' => NodeTaskGroupType::UpdateDirdConfig,
'swarm_id' => $swarm->id,
'team_id' => $node->team_id,
'node_id' => $node->id,
'invoker_id' => $swarm->team->user_id,
]);

$meta = new UpdateDirdConfigMeta(
nodeAddresses: $nodeAddresses,
dockerServices: $dockerServices,
nodePorts: ['tcp/80', 'tcp/443']
);

$taskGroup->tasks()->create([
'type' => NodeTaskType::UpdateDirdConfig,
'meta' => $meta,
'payload' => [
'NodeAddresses' => $nodeAddresses,
'DockerServices' => $dockerServices,
'NodePorts' => ['tcp/80', 'tcp/443'],
],
]);
});

$swarm->save();
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\Events\NodeTasks\UpdateDirdConfig;

use App\Events\NodeTasks\BaseTaskEvent;

class UpdateDirdConfigCompleted extends BaseTaskEvent {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\Events\NodeTasks\UpdateDirdConfig;

use App\Events\NodeTasks\BaseTaskEvent;

class UpdateDirdConfigFailed extends BaseTaskEvent {}
1 change: 1 addition & 0 deletions app/Models/NodeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public function __construct(
public DockerData $docker,
public HostData $host,
public NodeRole $role,
public string $address,
) {}
}
1 change: 1 addition & 0 deletions app/Models/NodeTaskGroupType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ enum NodeTaskGroupType: int
case UpdateS3Storages = 6;
case CreateBackup = 7;
case JoinSwarm = 8;
case UpdateDirdConfig = 9;
}
9 changes: 9 additions & 0 deletions app/Models/NodeTaskType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
use App\Events\NodeTasks\ServiceExec\ServiceExecFailed;
use App\Events\NodeTasks\UpdateAgentSymlink\UpdateAgentSymlinkCompleted;
use App\Events\NodeTasks\UpdateAgentSymlink\UpdateAgentSymlinkFailed;
use App\Events\NodeTasks\UpdateDirdConfig\UpdateDirdConfigCompleted;
use App\Events\NodeTasks\UpdateDirdConfig\UpdateDirdConfigFailed;
use App\Events\NodeTasks\UpdateNode\UpdateCurrentNodeCompleted;
use App\Events\NodeTasks\UpdateNode\UpdateCurrentNodeFailed;
use App\Events\NodeTasks\UpdateService\UpdateServiceCompleted;
Expand Down Expand Up @@ -78,6 +80,8 @@
use App\Models\NodeTasks\UpdateAgentSymlink\UpdateAgentSymlinkResult;
use App\Models\NodeTasks\UpdateCurrentNode\UpdateCurrentNodeMeta;
use App\Models\NodeTasks\UpdateCurrentNode\UpdateCurrentNodeResult;
use App\Models\NodeTasks\UpdateDirdConfig\UpdateDirdConfigMeta;
use App\Models\NodeTasks\UpdateDirdConfig\UpdateDirdConfigResult;
use App\Models\NodeTasks\UpdateService\UpdateServiceMeta;
use App\Models\NodeTasks\UpdateService\UpdateServiceResult;
use App\Models\NodeTasks\UploadS3File\UploadS3FileMeta;
Expand Down Expand Up @@ -106,6 +110,7 @@ enum NodeTaskType: int
case ServiceExec = 17;
case UploadS3File = 18;
case JoinSwarm = 19;
case UpdateDirdConfig = 20;

public function meta(): string
{
Expand All @@ -130,6 +135,7 @@ public function meta(): string
self::ServiceExec => ServiceExecMeta::class,
self::UploadS3File => UploadS3FileMeta::class,
self::JoinSwarm => JoinSwarmMeta::class,
self::UpdateDirdConfig => UpdateDirdConfigMeta::class,
};
}

Expand All @@ -156,6 +162,7 @@ public function result(): string
self::ServiceExec => ServiceExecResult::class,
self::UploadS3File => UploadS3FileResult::class,
self::JoinSwarm => JoinSwarmResult::class,
self::UpdateDirdConfig => UpdateDirdConfigResult::class,
};
}

Expand All @@ -182,6 +189,7 @@ public function completed(): string
self::ServiceExec => ServiceExecCompleted::class,
self::UploadS3File => UploadS3FileCompleted::class,
self::JoinSwarm => JoinSwarmCompleted::class,
self::UpdateDirdConfig => UpdateDirdConfigCompleted::class,
};
}

Expand All @@ -208,6 +216,7 @@ public function failed(): string
self::ServiceExec => ServiceExecFailed::class,
self::UploadS3File => UploadS3FileFailed::class,
self::JoinSwarm => JoinSwarmFailed::class,
self::UpdateDirdConfig => UpdateDirdConfigFailed::class,
};
}
}
19 changes: 19 additions & 0 deletions app/Models/NodeTasks/UpdateDirdConfig/UpdateDirdConfigMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Models\NodeTasks\UpdateDirdConfig;

use App\Models\NodeTasks\AbstractTaskMeta;

class UpdateDirdConfigMeta extends AbstractTaskMeta
{
public function __construct(
public array $nodeAddresses,
public array $dockerServices,
public array $nodePorts,
) {}

public function formattedHtml(): string
{
return 'Update DIRD Config';
}
}
18 changes: 18 additions & 0 deletions app/Models/NodeTasks/UpdateDirdConfig/UpdateDirdConfigResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Models\NodeTasks\UpdateDirdConfig;

use App\Models\NodeTasks\AbstractTaskResult;

class UpdateDirdConfigResult extends AbstractTaskResult
{
public function __construct(
) {
//
}

public function formattedHtml(): string
{
return 'Success.';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Models\NodeTasks\UpdateDirdConfiguration;

use App\Models\NodeTasks\AbstractTaskMeta;

class UpdateDirdConfigurationMeta extends AbstractTaskMeta
{
public function __construct(
public array $nodeAddresses,
public array $dockerServices,
public array $nodePorts,
) {
//
}

public function formattedHtml(): string
{
return 'Update DIRD Configuration';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Models\NodeTasks\UpdateDirdConfiguration;

use App\Models\NodeTasks\AbstractTaskResult;

class UpdateDirdConfigurationResult extends AbstractTaskResult
{
public function __construct(
) {
//
}

public function formattedHtml(): string
{
return 'Success.';
}
}
31 changes: 31 additions & 0 deletions database/migrations/2024_08_25_130825_update_node_data_address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

class UpdateNodeDataAddress extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::statement("
UPDATE nodes
SET data = data || '{\"address\": \"10.0.0.2\"}'::jsonb
WHERE (data->>'address') IS NULL OR (data->>'address') = ''
");
}

/**
* Reverse the migrations.
*/
public function down(): void
{
DB::statement("
UPDATE nodes
SET data = data - 'address'
WHERE data ? 'address'
");
}
}

0 comments on commit 6ca2ccc

Please sign in to comment.