coolify/app/Livewire/Server/PrivateKey/Show.php

80 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2023-10-09 09:00:18 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Server\PrivateKey;
2023-10-09 09:00:18 +00:00
use App\Models\PrivateKey;
use App\Models\Server;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\DB;
2023-10-09 09:00:18 +00:00
use Livewire\Component;
class Show extends Component
{
use AuthorizesRequests;
2024-10-30 13:54:27 +00:00
public Server $server;
2024-06-10 20:43:34 +00:00
2023-10-09 09:00:18 +00:00
public $privateKeys = [];
2024-06-10 20:43:34 +00:00
2023-10-09 09:00:18 +00:00
public $parameters = [];
2024-06-10 20:43:34 +00:00
2024-10-30 13:54:27 +00:00
public function mount(string $server_uuid)
2023-10-09 09:00:18 +00:00
{
try {
2024-10-30 13:54:27 +00:00
$this->server = Server::ownedByCurrentTeam()->whereUuid($server_uuid)->firstOrFail();
2023-10-09 09:00:18 +00:00
$this->privateKeys = PrivateKey::ownedByCurrentTeam()->get()->where('is_git_related', false);
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
2024-10-30 13:54:27 +00:00
public function setPrivateKey($privateKeyId)
{
$ownedPrivateKey = PrivateKey::ownedByCurrentTeam()->find($privateKeyId);
if (is_null($ownedPrivateKey)) {
$this->dispatch('error', 'You are not allowed to use this private key.');
return;
}
try {
$this->authorize('update', $this->server);
DB::transaction(function () use ($ownedPrivateKey) {
$this->server->privateKey()->associate($ownedPrivateKey);
$this->server->save();
['uptime' => $uptime, 'error' => $error] = $this->server->validateConnection(justCheckingNewKey: true);
if (! $uptime) {
throw new \Exception($error);
}
});
$this->dispatch('success', 'Private key updated successfully.');
$this->dispatch('refreshServerShow');
2024-10-30 13:54:27 +00:00
} catch (\Exception $e) {
$this->server->refresh();
2024-10-30 13:54:27 +00:00
$this->server->validateConnection();
$this->dispatch('error', $e->getMessage());
}
}
public function checkConnection()
{
try {
['uptime' => $uptime, 'error' => $error] = $this->server->validateConnection();
if ($uptime) {
$this->dispatch('success', 'Server is reachable.');
$this->dispatch('refreshServerShow');
2024-10-30 13:54:27 +00:00
} else {
$this->dispatch('error', 'Server is not reachable.<br><br>Check this <a target="_blank" class="underline" href="https://coolify.io/docs/knowledge-base/server/openssh">documentation</a> for further help.<br><br>Error: '.$error);
return;
}
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2023-10-09 09:00:18 +00:00
public function render()
{
return view('livewire.server.private-key.show');
}
}