Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup SoftwareVersionService #704

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Console/Commands/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function handle(): void
{
$this->output->title('Version Information');
$this->table([], [
['Panel Version', $this->versionService->versionData()['version']],
['Latest Version', $this->versionService->getPanel()],
['Panel Version', $this->versionService->currentPanelVersion()],
['Latest Version', $this->versionService->latestPanelVersion()],
['Up-to-Date', $this->versionService->isLatestPanel() ? 'Yes' : $this->formatText('No', 'bg=red')],
], 'compact');

Expand Down
9 changes: 5 additions & 4 deletions app/Filament/Pages/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Pages;

use App\Filament\Resources\NodeResource\Pages\CreateNode;
use App\Filament\Resources\NodeResource\Pages\ListNodes;
use App\Models\Egg;
use App\Models\Node;
Expand Down Expand Up @@ -39,8 +40,8 @@ public function getViewData(): array
{
return [
'inDevelopment' => config('app.version') === 'canary',
'version' => $this->softwareVersionService->versionData()['version'],
'latestVersion' => $this->softwareVersionService->getPanel(),
'version' => $this->softwareVersionService->currentPanelVersion(),
'latestVersion' => $this->softwareVersionService->latestPanelVersion(),
'isLatest' => $this->softwareVersionService->isLatestPanel(),
'eggsCount' => Egg::query()->count(),
'nodesList' => ListNodes::getUrl(),
Expand All @@ -65,13 +66,13 @@ public function getViewData(): array
CreateAction::make()
->label(trans('dashboard/index.sections.intro-first-node.button_label'))
->icon('tabler-server-2')
->url(route('filament.admin.resources.nodes.create')),
->url(CreateNode::getUrl()),
],
'supportActions' => [
CreateAction::make()
->label(trans('dashboard/index.sections.intro-support.button_donate'))
->icon('tabler-cash')
->url($this->softwareVersionService->getDonations(), true)
->url('https://pelican.dev/donate', true)
->color('success'),
],
'helpActions' => [
Expand Down
3 changes: 2 additions & 1 deletion app/Filament/Resources/NodeResource/Pages/EditNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Filament\Resources\NodeResource;
use App\Models\Node;
use App\Services\Helpers\SoftwareVersionService;
use App\Services\Nodes\NodeAutoDeployService;
use App\Services\Nodes\NodeUpdateService;
use Filament\Actions;
Expand Down Expand Up @@ -55,7 +56,7 @@ public function form(Forms\Form $form): Forms\Form
->schema([
Placeholder::make('')
->label('Wings Version')
->content(fn (Node $node) => $node->systemInformation()['version'] ?? 'Unknown'),
->content(fn (Node $node, SoftwareVersionService $versionService) => ($node->systemInformation()['version'] ?? 'Unknown') . ' (Latest: ' . $versionService->latestWingsVersion() . ')'),
Placeholder::make('')
->label('CPU Threads')
->content(fn (Node $node) => $node->systemInformation()['cpu_count'] ?? 0),
Expand Down
135 changes: 30 additions & 105 deletions app/Services/Helpers/SoftwareVersionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,146 +2,71 @@

namespace App\Services\Helpers;

use GuzzleHttp\Client;
use Carbon\CarbonImmutable;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Arr;
use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Exception;
use Illuminate\Support\Facades\Http;

class SoftwareVersionService
{
public const VERSION_CACHE_KEY = 'panel:versioning_data';

private static array $result;

/**
* SoftwareVersionService constructor.
*/
public function __construct(
protected CacheRepository $cache,
protected Client $client
) {
self::$result = $this->cacheVersionData();
}

/**
* Get the latest version of the panel from the CDN servers.
*/
public function getPanel(): string
public function latestPanelVersion(): string
{
return Arr::get(self::$result, 'panel') ?? 'error';
}
return cache()->remember('wings:latest_version', now()->addMinutes(config('panel.cdn.cache_time', 60)), function () {
try {
$response = Http::timeout(5)->connectTimeout(1)->get('https://api.github.com/repos/pelican-dev/panel/releases/latest')->throw()->json();

/**
* Get the latest version of the daemon from the CDN servers.
*/
public function getDaemon(): string
{
return Arr::get(self::$result, 'daemon') ?? 'error';
return trim($response['tag_name'], 'v');
} catch (Exception) {
return 'error';
}
});
}

/**
* Get the URL to the discord server.
*/
public function getDiscord(): string
public function latestWingsVersion(): string
{
return Arr::get(self::$result, 'discord') ?? 'https://pelican.dev/discord';
}
return cache()->remember('wings:latest_version', now()->addMinutes(config('panel.cdn.cache_time', 60)), function () {
try {
$response = Http::timeout(5)->connectTimeout(1)->get('https://api.github.com/repos/pelican-dev/wings/releases/latest')->throw()->json();

/**
* Get the donation URL.
*/
public function getDonations(): string
{
return Arr::get(self::$result, 'donate') ?? 'https://pelican.dev/donate';
return trim($response['tag_name'], 'v');
} catch (Exception) {
return 'error';
}
});
}

/**
* Determine if the current version of the panel is the latest.
*/
public function isLatestPanel(): bool
{
if (config('app.version') === 'canary') {
return true;
}

return version_compare(config('app.version'), $this->getPanel()) >= 0;
return version_compare(config('app.version'), $this->latestPanelVersion()) >= 0;
}

/**
* Determine if a passed daemon version string is the latest.
*/
public function isLatestDaemon(string $version): bool
public function isLatestWings(string $version): bool
{
if ($version === 'develop') {
return true;
}

return version_compare($version, $this->getDaemon()) >= 0;
return version_compare($version, $this->latestWingsVersion()) >= 0;
}

/**
* Keeps the versioning cache up-to-date with the latest results from the CDN.
*/
protected function cacheVersionData(): array
public function currentPanelVersion(): string
{
return $this->cache->remember(self::VERSION_CACHE_KEY, CarbonImmutable::now()->addMinutes(config('panel.cdn.cache_time', 60)), function () {
$versionData = [];

try {
$response = $this->client->request('GET', 'https://api.github.com/repos/pelican-dev/panel/releases/latest',
[
'timeout' => config('panel.guzzle.timeout'),
'connect_timeout' => config('panel.guzzle.connect_timeout'),
]
);
if ($response->getStatusCode() === 200) {
$panelData = json_decode($response->getBody(), true);
$versionData['panel'] = trim($panelData['tag_name'], 'v');
}

$response = $this->client->request('GET', 'https://api.github.com/repos/pelican-dev/wings/releases/latest',
[
'timeout' => config('panel.guzzle.timeout'),
'connect_timeout' => config('panel.guzzle.connect_timeout'),
]
);
if ($response->getStatusCode() === 200) {
$wingsData = json_decode($response->getBody(), true);
$versionData['daemon'] = trim($wingsData['tag_name'], 'v');
}
} catch (GuzzleException $e) {
}

$versionData['discord'] = 'https://pelican.dev/discord';
$versionData['donate'] = 'https://pelican.dev/donate';

return $versionData;
});
}

public function versionData(): array
{
return cache()->remember('git-version', 5, function () {
return cache()->remember('panel:current_version', now()->addMinutes(5), function () {
if (file_exists(base_path('.git/HEAD'))) {
$head = explode(' ', file_get_contents(base_path('.git/HEAD')));

if (array_key_exists(1, $head)) {
$path = base_path('.git/' . trim($head[1]));
}
}

if (isset($path) && file_exists($path)) {
return [
'version' => 'canary (' . substr(file_get_contents($path), 0, 8) . ')',
'is_git' => true,
];
if (file_exists($path)) {
return 'canary (' . substr(file_get_contents($path), 0, 7) . ')';
}
}
}

return [
'version' => config('app.version'),
'is_git' => false,
];
return config('app.version');
});
}
}
6 changes: 3 additions & 3 deletions resources/views/admin/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@else
Your panel is <strong>not up-to-date!</strong> The latest version is
<a>
<code>{{ $version->getPanel() }}</code>
<code>{{ $version->latestPanelVersion() }}</code>
</a>
and you are currently running version <code>{{ config('app.version') }}</code>.
@endif
Expand All @@ -41,7 +41,7 @@
</div>
<div class="row">
<div class="col-xs-6 col-sm-3 text-center">
<a href="{{ $version->getDiscord() }}"><button class="btn btn-warning" style="width:100%;"><i class="fa fa-fw fa-support"></i> Get Help <small>(via Discord)</small></button></a>
<a href="https://pelican.dev/discord"><button class="btn btn-warning" style="width:100%;"><i class="fa fa-fw fa-support"></i> Get Help <small>(via Discord)</small></button></a>
</div>
<div class="col-xs-6 col-sm-3 text-center">
<a href="https://pelican.dev"><button class="btn btn-primary" style="width:100%;"><i class="fa fa-fw fa-link"></i> Documentation</button></a>
Expand All @@ -51,7 +51,7 @@
<a href="https://github.com/pelican-dev/panel"><button class="btn btn-primary" style="width:100%;"><i class="fa fa-fw fa-support"></i> Github</button></a>
</div>
<div class="col-xs-6 col-sm-3 text-center">
<a href="{{ $version->getDonations() }}"><button class="btn btn-success" style="width:100%;"><i class="fa fa-fw fa-money"></i> Support the Project</button></a>
<a href="https://pelican.dev/donate"><button class="btn btn-success" style="width:100%;"><i class="fa fa-fw fa-money"></i> Support the Project</button></a>
</div>
</div>
@endsection
2 changes: 1 addition & 1 deletion resources/views/admin/nodes/view/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<table class="table table-hover">
<tr>
<td>Daemon Version</td>
<td><code data-attr="info-version"><i class="fa fa-refresh fa-fw fa-spin"></i></code> (Latest: <code>{{ $version->getDaemon() }}</code>)</td>
<td><code data-attr="info-version"><i class="fa fa-refresh fa-fw fa-spin"></i></code> (Latest: <code>{{ $version->currentWingsVersion() }}</code>)</td>
</tr>
<tr>
<td>System Information</td>
Expand Down
Loading