Skip to content

Commit

Permalink
feat: #12 watch after the new agent releases
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Jul 1, 2024
1 parent 24450eb commit 0037a61
Show file tree
Hide file tree
Showing 27 changed files with 454 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
APP_NAME=Laravel
APP_NAME=Ptah.sh
APP_ENV=local
APP_KEY=
APP_DEBUG=true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Events\NodeTasks\ConfirmAgentUpgrade;

use App\Events\NodeTasks\BaseTaskEvent;

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

namespace App\Events\NodeTasks\ConfirmAgentUpgrade;

use App\Events\NodeTasks\BaseTaskEvent;

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

namespace App\Events\NodeTasks\DownloadAgentUpgrade;

use App\Events\NodeTasks\BaseTaskEvent;

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

namespace App\Events\NodeTasks\DownloadAgentUpgrade;

use App\Events\NodeTasks\BaseTaskEvent;

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

namespace App\Events\NodeTasks\UpdateAgentSymlink;

use App\Events\NodeTasks\BaseTaskEvent;

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

namespace App\Events\NodeTasks\UpdateAgentSymlink;

use App\Events\NodeTasks\BaseTaskEvent;

class UpdateAgentSymlinkFailed extends BaseTaskEvent
{
}
27 changes: 26 additions & 1 deletion app/Http/Controllers/NodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use App\Http\Requests\StoreNodeRequest;
use App\Http\Requests\UpdateNodeRequest;
use App\Models\AgentRelease;
use App\Models\Node;
use App\Models\NodeTaskGroupType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;

class NodeController extends Controller
Expand Down Expand Up @@ -48,7 +51,16 @@ public function show(Node $node)
$initTaskGroup = null;
}

return Inertia::render('Nodes/Show', ['node' => $node, 'initTaskGroup' => $initTaskGroup]);
$lastAgentVersion = AgentRelease::latest()->sole()->tag_name;

$taskGroup = $node->actualTaskGroup(NodeTaskGroupType::SelfUpgrade);

return Inertia::render('Nodes/Show', [
'node' => $node,
'initTaskGroup' => $initTaskGroup,
'lastAgentVersion' => $lastAgentVersion,
'agentUpgradeTaskGroup' => $taskGroup?->is_completed ? null : $taskGroup,
]);
}

/**
Expand All @@ -74,4 +86,17 @@ public function destroy(Node $node)
{
//
}

public function upgradeAgent(Node $node, Request $request)
{
$req = $request->validate([
'targetVersion' => ['required', 'exists:agent_releases,tag_name'],
]);

DB::transaction(function () use ($node, $req) {
$node->upgradeAgent($req['targetVersion']);
});

return to_route('nodes.show', ['node' => $node->id]);
}
}
47 changes: 47 additions & 0 deletions app/Jobs/CheckAgentUpdates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Jobs;

use App\Models\AgentRelease;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;

class CheckAgentUpdates implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*/
public function __construct()
{
}

/**
* Execute the job.
*/
public function handle(): void
{
$json = Http::get("https://api.github.com/repos/ptah-sh/ptah-agent/releases/latest")->json();

foreach ($json['assets'] as $asset) {
preg_match('/^ptah-agent-(?<os>.+)-(?<arch>.+).bin$/', $asset['name'], $matches);
if (empty($matches)) {
continue;
}

$attrs = [
'tag_name' => $json['tag_name'],
'download_url' => $asset['browser_download_url'],
'os' => $matches['os'],
'arch' => $matches['arch'],
];

AgentRelease::firstOrCreate($attrs, $attrs);
}
}
}
18 changes: 18 additions & 0 deletions app/Models/AgentRelease.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class AgentRelease extends Model
{
use HasFactory;

protected $fillable = [
'tag_name',
'download_url',
'os',
'arch',
];
}
44 changes: 44 additions & 0 deletions app/Models/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,48 @@ public function actualTaskGroup($type): ?NodeTaskGroup

return $base->orderByDesc('id')->take(1)->get()[0] ?? null;
}

public function upgradeAgent($targetVersion): void
{
$release = AgentRelease::where('tag_name', $targetVersion)->sole();

$taskGroup = NodeTaskGroup::create([
'swarm_id' => $this->swarm_id,
'node_id' => $this->id,
'type' => NodeTaskGroupType::SelfUpgrade,
'invoker_id' => auth()->user()->id,
]);

$taskGroup->tasks()->createMany([
[
'type' => NodeTaskType::DownloadAgentUpgrade,
'meta' => [
'targetVersion' => $targetVersion,
'downloadUrl' => $release->download_url,
],
'payload' => [
'TargetVersion' => $targetVersion,
'DownloadUrl' => $release->download_url,
]
],
[
'type' => NodeTaskType::UpdateAgentSymlink,
'meta' => [
'targetVersion' => $targetVersion,
],
'payload' => [
'TargetVersion' => $targetVersion,
]
],
[
'type' => NodeTaskType::ConfirmAgentUpgrade,
'meta' => [
'targetVersion' => $targetVersion,
],
'payload' => [
'TargetVersion' => $targetVersion,
]
]
]);
}
}
1 change: 1 addition & 0 deletions app/Models/NodeTaskGroupType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ enum NodeTaskGroupType: int
case CreateService = 1;
case UpdateService = 2;
case DeleteService = 3;
case SelfUpgrade = 4;
}
27 changes: 27 additions & 0 deletions app/Models/NodeTaskType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Models;

use App\Events\NodeTasks\ConfirmAgentUpgrade\ConfirmAgentUpgradeCompleted;
use App\Events\NodeTasks\ConfirmAgentUpgrade\ConfirmAgentUpgradeFailed;
use App\Events\NodeTasks\CreateConfig\CreateConfigCompleted;
use App\Events\NodeTasks\CreateConfig\CreateConfigFailed;
use App\Events\NodeTasks\CreateNetwork\CreateNetworkCompleted;
Expand All @@ -12,14 +14,20 @@
use App\Events\NodeTasks\CreateService\CreateServiceFailed;
use App\Events\NodeTasks\DeleteService\DeleteServiceCompleted;
use App\Events\NodeTasks\DeleteService\DeleteServiceFailed;
use App\Events\NodeTasks\DownloadAgentUpgrade\DownloadAgentUpgradeCompleted;
use App\Events\NodeTasks\DownloadAgentUpgrade\DownloadAgentUpgradeFailed;
use App\Events\NodeTasks\InitSwarm\InitSwarmCompleted;
use App\Events\NodeTasks\InitSwarm\InitSwarmFailed;
use App\Events\NodeTasks\RebuildCaddyConfig\ApplyCaddyConfigCompleted;
use App\Events\NodeTasks\RebuildCaddyConfig\ApplyCaddyConfigFailed;
use App\Events\NodeTasks\UpdateAgentSymlink\UpdateAgentSymlinkCompleted;
use App\Events\NodeTasks\UpdateAgentSymlink\UpdateAgentSymlinkFailed;
use App\Events\NodeTasks\UpdateNode\UpdateCurrentNodeCompleted;
use App\Events\NodeTasks\UpdateNode\UpdateCurrentNodeFailed;
use App\Events\NodeTasks\UpdateService\UpdateServiceCompleted;
use App\Events\NodeTasks\UpdateService\UpdateServiceFailed;
use App\Models\NodeTasks\ConfirmAgentUpgrade\ConfirmAgentUpgradeMeta;
use App\Models\NodeTasks\ConfirmAgentUpgrade\ConfirmAgentUpgradeResult;
use App\Models\NodeTasks\CreateConfig\CreateConfigMeta;
use App\Models\NodeTasks\CreateConfig\CreateConfigResult;
use App\Models\NodeTasks\CreateNetwork\CreateNetworkMeta;
Expand All @@ -30,10 +38,14 @@
use App\Models\NodeTasks\CreateService\CreateServiceResult;
use App\Models\NodeTasks\DeleteService\DeleteServiceMeta;
use App\Models\NodeTasks\DeleteService\DeleteServiceResult;
use App\Models\NodeTasks\DownloadAgentUpgrade\DownloadAgentUpgradeMeta;
use App\Models\NodeTasks\DownloadAgentUpgrade\DownloadAgentUpgradeResult;
use App\Models\NodeTasks\InitSwarm\InitSwarmMeta;
use App\Models\NodeTasks\InitSwarm\InitSwarmResult;
use App\Models\NodeTasks\ApplyCaddyConfig\ApplyCaddyConfigMeta;
use App\Models\NodeTasks\ApplyCaddyConfig\ApplyCaddyConfigResult;
use App\Models\NodeTasks\UpdateAgentSymlink\UpdateAgentSymlinkMeta;
use App\Models\NodeTasks\UpdateAgentSymlink\UpdateAgentSymlinkResult;
use App\Models\NodeTasks\UpdateCurrentNode\UpdateCurrentNodeMeta;
use App\Models\NodeTasks\UpdateCurrentNode\UpdateCurrentNodeResult;
use App\Models\NodeTasks\UpdateService\UpdateServiceMeta;
Expand All @@ -51,6 +63,9 @@ enum NodeTaskType: int
case UpdateService = 6;
case UpdateCurrentNode = 7;
case DeleteService = 8;
case DownloadAgentUpgrade = 9;
case UpdateAgentSymlink = 10;
case ConfirmAgentUpgrade = 11;

public function meta(): string
{
Expand All @@ -64,6 +79,9 @@ public function meta(): string
self::UpdateService => UpdateServiceMeta::class,
self::UpdateCurrentNode => UpdateCurrentNodeMeta::class,
self::DeleteService => DeleteServiceMeta::class,
self::DownloadAgentUpgrade => DownloadAgentUpgradeMeta::class,
self::UpdateAgentSymlink => UpdateAgentSymlinkMeta::class,
self::ConfirmAgentUpgrade => ConfirmAgentUpgradeMeta::class,
};
}

Expand All @@ -79,6 +97,9 @@ public function result(): string
self::UpdateService => UpdateServiceResult::class,
self::UpdateCurrentNode => UpdateCurrentNodeResult::class,
self::DeleteService => DeleteServiceResult::class,
self::DownloadAgentUpgrade => DownloadAgentUpgradeResult::class,
self::UpdateAgentSymlink => UpdateAgentSymlinkResult::class,
self::ConfirmAgentUpgrade => ConfirmAgentUpgradeResult::class,
};
}

Expand All @@ -94,6 +115,9 @@ public function completed(): string
self::UpdateService => UpdateServiceCompleted::class,
self::UpdateCurrentNode => UpdateCurrentNodeCompleted::class,
self::DeleteService => DeleteServiceCompleted::class,
self::DownloadAgentUpgrade => DownloadAgentUpgradeCompleted::class,
self::UpdateAgentSymlink => UpdateAgentSymlinkCompleted::class,
self::ConfirmAgentUpgrade => ConfirmAgentUpgradeCompleted::class,
};
}

Expand All @@ -109,6 +133,9 @@ public function failed(): string
self::UpdateService => UpdateServiceFailed::class,
self::UpdateCurrentNode => UpdateCurrentNodeFailed::class,
self::DeleteService => DeleteServiceFailed::class,
self::DownloadAgentUpgrade => DownloadAgentUpgradeFailed::class,
self::UpdateAgentSymlink => UpdateAgentSymlinkFailed::class,
self::ConfirmAgentUpgrade => ConfirmAgentUpgradeFailed::class,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Models\NodeTasks\ConfirmAgentUpgrade;

use App\Models\NodeTasks\AbstractTaskMeta;

class ConfirmAgentUpgradeMeta extends AbstractTaskMeta
{
public function __construct(
public string $targetVersion
)
{
//
}

public function formattedHtml(): string
{
return "Confirm agent upgrade to <code>{$this->targetVersion}</code>";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Models\NodeTasks\ConfirmAgentUpgrade;

use App\Models\NodeTasks\AbstractTaskResult;

class ConfirmAgentUpgradeResult 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\DownloadAgentUpgrade;

use App\Models\NodeTasks\AbstractTaskMeta;

class DownloadAgentUpgradeMeta extends AbstractTaskMeta
{
public function __construct(
public string $targetVersion,
public string $downloadUrl
)
{
//
}

public function formattedHtml(): string
{
return "Download agent upgrade to <code>{$this->targetVersion}</code>";
}
}
Loading

0 comments on commit 0037a61

Please sign in to comment.