Skip to content

Commit

Permalink
fix: #72 assign correct team when the service is accessed via api
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Jul 11, 2024
1 parent 05bc449 commit 0e7cfe4
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 9 deletions.
4 changes: 3 additions & 1 deletion app/Http/Controllers/NodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public function create()
*/
public function store(StoreNodeRequest $request)
{
$node = Node::create($request->validated());
$node = Node::make($request->validated());
$node->team_id = auth()->user()->current_team_id;
$node->save();

return to_route('nodes.show', ['node' => $node->id]);
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/ServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function store(StoreServiceRequest $request)
$deploymentData = DeploymentData::validateAndCreate($request->get('deploymentData'));

$service = Service::make($request->validated());
$service->team_id = auth()->user()->current_team_id;
DB::transaction(function () use ($service, $deploymentData) {
$service->save();

Expand Down
3 changes: 1 addition & 2 deletions app/Http/Controllers/SwarmController.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,11 @@ public function updateDockerRegistries(Swarm $swarm, Request $request)
'type' => NodeTaskGroupType::UpdateDockerRegistries,
'swarm_id' => $swarm->id,
'invoker_id' => auth()->user()->id,
'team_id' => auth()->user()->current_team_id,
]);

$taskGroup->tasks()->createMany($tasks);
}
});

// return back();
}
}
10 changes: 10 additions & 0 deletions app/Http/Controllers/SwarmTaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Http\Requests\NodeTask\InitClusterFormRequest;
use App\Models\Deployment;
use App\Models\DeploymentData;
use App\Models\DeploymentData\LaunchMode;
use App\Models\DeploymentData\ReleaseCommand;
Expand All @@ -14,6 +15,7 @@
use App\Models\NodeTasks\InitSwarm\InitSwarmMeta;
use App\Models\NodeTasks\UpdateCurrentNode\UpdateCurrentNodeMeta;
use App\Models\NodeTaskType;
use App\Models\Service;
use App\Models\Swarm;
use App\Models\SwarmData;
use Illuminate\Support\Facades\DB;
Expand All @@ -25,6 +27,7 @@ public function initCluster(InitClusterFormRequest $request)
DB::transaction(function () use ($request) {
$swarm = Swarm::create([
'name' => $request->name,
'team_id' => auth()->user()->current_team_id,
'data' => SwarmData::validateAndCreate([
'registriesRev' => 0,
'registries' => [],
Expand All @@ -38,6 +41,7 @@ public function initCluster(InitClusterFormRequest $request)

$network = Network::create([
'swarm_id' => $swarm->id,
'team_id' => auth()->user()->current_team_id,
'name' => dockerize_name('ptah-net'),
]);

Expand All @@ -46,6 +50,7 @@ public function initCluster(InitClusterFormRequest $request)
'swarm_id' => $swarm->id,
'node_id' => $request->node_id,
'invoker_id' => auth()->user()->id,
'team_id' => auth()->user()->current_team_id,
]);

$tasks = [
Expand Down Expand Up @@ -108,9 +113,11 @@ public function initCluster(InitClusterFormRequest $request)

$caddyService = $swarm->services()->create([
'name' => 'caddy',
'team_id' => auth()->user()->current_team_id,
]);

$deployment = $caddyService->deployments()->create([
'team_id' => auth()->user()->current_team_id,
'data' => DeploymentData::validateAndCreate([
'networkName' => $network->docker_name,
'internalDomain' => 'caddy.ptah.local',
Expand Down Expand Up @@ -173,10 +180,13 @@ public function initCluster(InitClusterFormRequest $request)
'replicas' => 1,
'caddy' => [],
'fastcgiVars' => null,
'redirectRules' => [],
],
],
]),
]);
$deployment->team_id = auth()->user()->current_team_id;
$deployment->save();

foreach ($deployment->asNodeTasks() as $task) {
$tasks[] = $task;
Expand Down
4 changes: 4 additions & 0 deletions app/Models/Deployment.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Deployment extends Model
use HasOwningTeam;

protected $fillable = [
'service_id',
'team_id',
'task_group_id',
'data',
];
Expand Down Expand Up @@ -124,6 +126,7 @@ public function asNodeTasks(): array
'response' => [
'set' => [
'X-Powered-By' => ['https://ptah.sh'],
'X-Ptah-Rule-Id' => [$caddy->id],
],
],
],
Expand Down Expand Up @@ -158,6 +161,7 @@ public function asNodeTasks(): array
'status_code' => (string) $redirectRule->statusCode,
'headers' => [
'X-Powered-By' => ['https://ptah.sh'],
'X-Ptah-Rule-Id' => [$redirectRule->id],
'Location' => ["{http.request.scheme}://{$redirectRule->domainTo}{$pathTo}"],
],
]
Expand Down
1 change: 1 addition & 0 deletions app/Models/DeploymentData/Caddy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class Caddy extends Data
{
public function __construct(
public string $id,
#[In(['http', 'fastcgi'])]
public string $targetProtocol,
#[Between(1, 65535)]
Expand Down
1 change: 1 addition & 0 deletions app/Models/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Network extends Model
protected $fillable = [
'swarm_id',
'name',
'team_id',
];

protected static function booted(): void
Expand Down
4 changes: 3 additions & 1 deletion app/Models/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ class Node extends Model

protected $fillable = [
'name',
'team_id',
];

protected $appends = [
'online',
];

protected static function booted()
protected static function booted(): void
{
self::creating(function (Node $node) {
$node->agent_token = Str::random(42);
Expand Down Expand Up @@ -75,6 +76,7 @@ public function upgradeAgent($targetVersion): void
'node_id' => $this->id,
'type' => NodeTaskGroupType::SelfUpgrade,
'invoker_id' => auth()->user()->id,
'team_id' => auth()->user()->current_team_id,
]);

$taskGroup->tasks()->createMany([
Expand Down
7 changes: 7 additions & 0 deletions app/Models/NodeTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class NodeTask extends Model
'formatted_result',
];

protected static function booted(): void
{
self::creating(function (NodeTask $nodeTask) {
$nodeTask->team_id = $nodeTask->taskGroup->team_id;
});
}

public function taskGroup(): BelongsTo
{
return $this->belongsTo(NodeTaskGroup::class);
Expand Down
2 changes: 2 additions & 0 deletions app/Models/NodeTaskGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class NodeTaskGroup extends Model
'swarm_id',
'node_id',
'invoker_id',
'team_id',
];

public function swarm(): BelongsTo
Expand Down Expand Up @@ -75,6 +76,7 @@ public function retry(Node|null $node): void
$taskGroup->forceFill(collect($this->attributes)->only([
'swarm_id',
'invoker_id',
'team_id',
])->toArray());
$taskGroup->save();

Expand Down
3 changes: 3 additions & 0 deletions app/Models/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Service extends Model
protected $fillable = [
'name',
'swarm_id',
'team_id',
];

protected static function booted()
Expand Down Expand Up @@ -87,11 +88,13 @@ public function deploy(DeploymentData $deploymentData): Deployment

$taskGroup = NodeTaskGroup::create([
'swarm_id' => $this->swarm_id,
'team_id' => $this->team_id,
'invoker_id' => auth()->id(),
'type' => $this->deployments()->exists() ? NodeTaskGroupType::UpdateService : NodeTaskGroupType::CreateService,
]);

$deployment = $this->deployments()->create([
'team_id' => $this->team_id,
'data' => $deploymentData,
]);

Expand Down
3 changes: 2 additions & 1 deletion app/Models/Swarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Swarm extends Model

protected $fillable = [
'name',
'data'
'data',
'team_id',
];

protected $casts = [
Expand Down
4 changes: 0 additions & 4 deletions app/Traits/HasOwningTeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ trait HasOwningTeam
protected static function bootHasOwningTeam(): void
{
static::addGlobalScope(new TeamScope());

static::creating(function ($model) {
$model->team_id = auth()->user()->currentTeam->id;
});
}

public function team()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::update("
UPDATE deployments
SET data = jsonb_set(
data,
'{processes}',
(
SELECT jsonb_agg(
jsonb_set(
process,
'{caddy}',
COALESCE(
(
SELECT jsonb_agg(
caddy_item || jsonb_build_object(
'id',
concat('caddy-', substr(md5(random()::text), 1, 11))
)
)
FROM jsonb_array_elements(process->'caddy') AS caddy_item
),
'[]'::jsonb
)
)
)
FROM jsonb_array_elements(data->'processes') AS process
)
);
");
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};

0 comments on commit 0e7cfe4

Please sign in to comment.