From c023be2348fb7d3b3fd0a4006c61049f32bc9733 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 18 Sep 2023 13:01:01 +0200 Subject: [PATCH] fix: improve localhost boarding process --- app/Http/Livewire/Boarding/Index.php | 12 +++++++++++- app/Jobs/ApplicationDeploymentJob.php | 2 +- bootstrap/helpers/remoteProcess.php | 4 ++-- resources/views/livewire/boarding/index.blade.php | 12 ++++++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/app/Http/Livewire/Boarding/Index.php b/app/Http/Livewire/Boarding/Index.php index bc15c5579..fc1784383 100644 --- a/app/Http/Livewire/Boarding/Index.php +++ b/app/Http/Livewire/Boarding/Index.php @@ -39,6 +39,10 @@ class Index extends Component public ?Project $createdProject = null; public bool $dockerInstallationStarted = false; + + public string $localhostPublicKey; + public bool $localhostReachable = true; + public function mount() { $this->privateKeyName = generate_random_name(); @@ -94,6 +98,7 @@ uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA== if (!$this->createdServer) { return $this->emit('error', 'Localhost server is not found. Something went wrong during installation. Please try to reinstall or contact support.'); } + $this->localhostPublicKey = $this->createdServer->privateKey->publicKey(); return $this->validateServer('localhost'); } elseif ($this->selectedServerType === 'remote') { $this->privateKeys = PrivateKey::ownedByCurrentTeam(['name'])->where('id', '!=', 0)->get(); @@ -188,7 +193,7 @@ uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA== $this->createdServer->save(); $this->validateServer(); } - public function validateServer(?string $type = null) + public function validateServer() { try { $customErrorMessage = "Server is not reachable:"; @@ -199,7 +204,12 @@ uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA== $this->createdServer->settings->update([ 'is_reachable' => true, ]); + } catch (\Throwable $e) { + $this->localhostReachable = false; + return handleError(error: $e, customErrorMessage: $customErrorMessage, livewire: $this); + } + try { $dockerVersion = instant_remote_process(["docker version|head -2|grep -i version| awk '{print $2}'"], $this->createdServer, true); $dockerVersion = checkMinimumDockerEngineVersion($dockerVersion); if (is_null($dockerVersion)) { diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 484c8709e..1054c3f3b 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -89,7 +89,7 @@ class ApplicationDeploymentJob implements ShouldQueue, ShouldBeEncrypted $this->is_debug_enabled = $this->application->settings->is_debug_enabled; $this->container_name = generateApplicationContainerName($this->application->uuid, $this->pull_request_id); - addPrivateKeyToSshAgent($this->server); + savePrivateKeyToFs($this->server); $this->saved_outputs = collect(); // Set preview fqdn diff --git a/bootstrap/helpers/remoteProcess.php b/bootstrap/helpers/remoteProcess.php index 7017ead10..5982b46d8 100644 --- a/bootstrap/helpers/remoteProcess.php +++ b/bootstrap/helpers/remoteProcess.php @@ -53,7 +53,7 @@ function remote_process( // } // // processWithEnv()->run("echo '{$server->privateKey->private_key}' | ssh-add -d -"); // } -function addPrivateKeyToSshAgent(Server $server) +function savePrivateKeyToFs(Server $server) { if (data_get($server, 'privateKey.private_key') === null) { throw new \Exception("Server {$server->name} does not have a private key"); @@ -70,7 +70,7 @@ function generateSshCommand(Server $server, string $command, bool $isMux = true) { $user = $server->user; $port = $server->port; - $privateKeyLocation = addPrivateKeyToSshAgent($server); + $privateKeyLocation = savePrivateKeyToFs($server); $timeout = config('constants.ssh.command_timeout'); $connectionTimeout = config('constants.ssh.connection_timeout'); $serverInterval = config('constants.ssh.server_interval'); diff --git a/resources/views/livewire/boarding/index.blade.php b/resources/views/livewire/boarding/index.blade.php index 8cd3034eb..b86320612 100644 --- a/resources/views/livewire/boarding/index.blade.php +++ b/resources/views/livewire/boarding/index.blade.php @@ -46,9 +46,21 @@ Localhost + Remote Server + @if (!$localhostReachable) + Localhost is not reachable with the following public key. +

+ Please make sure you have the correct public key in your ~/.ssh/authorized_keys file for user + 'root' or skip the boarding process and add a new private key manually to Coolify and to the + server. + + Check again + + @endif

Servers are the main building blocks, as they will host your applications, databases,