Skip to content

Commit

Permalink
refactor: Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Jul 22, 2024
1 parent 22a5685 commit 1c16e9e
Show file tree
Hide file tree
Showing 22 changed files with 1,547 additions and 879 deletions.
50 changes: 30 additions & 20 deletions app/Actions/RemoteServer/CheckRemoteServerConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

namespace App\Actions\RemoteServer;

use App\Enums\ConnectionType;
use App\Events\RemoteServerConnectivityStatusChanged;
use App\Exceptions\ServerConnectionException;
use App\Factories\ServerConnectionFactory;
use App\Facades\ServerConnection;
use App\Models\RemoteServer;
use App\Support\ServerConnection\Exceptions\ConnectionException;
use Exception;
use Illuminate\Support\Facades\Log;
use RuntimeException;
Expand All @@ -23,20 +22,13 @@ class CheckRemoteServerConnection
{
private const int CONNECTION_TIMEOUT = 3;

/**
* @param ServerConnectionFactory $serverConnectionFactory Factory for creating server connections
*/
public function __construct(
private readonly ServerConnectionFactory $serverConnectionFactory
) {}

/**
* Check connection status of a remote server by its ID
*
* Retrieves the server by ID and initiates a connection check.
*
* @param int $remoteServerId The ID of the remote server to check
* @return array<string, mixed> Connection check results
* @return array{status: string, connectivity_status: string, message?: string, error?: string} Connection check results
*
* @throws Exception If the server cannot be found or connection fails unexpectedly
*/
Expand All @@ -55,7 +47,7 @@ public function byRemoteServerId(int $remoteServerId): array
* Creates a temporary RemoteServer instance and initiates a connection check.
*
* @param array<string, mixed> $remoteServerConnectionDetails Connection details including host, port, and username
* @return array<string, mixed> Connection check results
* @return array{status: string, connectivity_status: string, message?: string, error?: string} Connection check results
*
* @throws Exception If required connection details are missing or connection fails unexpectedly
*/
Expand All @@ -82,25 +74,42 @@ public function byRemoteServerConnectionDetails(array $remoteServerConnectionDet
* Attempts to establish an SSH connection to the server and updates its status accordingly.
*
* @param RemoteServer $remoteServer The server to check
* @return array<string, mixed> Connection check results
* @return array{status: string, connectivity_status: string, message?: string, error?: string} Connection check results
*
* @throws Exception If connection fails unexpectedly
*/
private function checkServerConnection(RemoteServer $remoteServer): array
{
try {
$connection = $this->serverConnectionFactory->makeFromModel($remoteServer, ConnectionType::SSH, self::CONNECTION_TIMEOUT);
$connection->connect();
$connection = ServerConnection::connectFromModel($remoteServer)
->timeout(self::CONNECTION_TIMEOUT)
->establish();

$this->updateServerStatus($remoteServer, RemoteServer::STATUS_ONLINE);
if ($connection->connected()) {
$this->updateServerStatus($remoteServer, RemoteServer::STATUS_ONLINE);

Log::debug('[Server Connection Check] Successfully connected to remote server');
Log::debug('[Server Connection Check] Successfully connected to remote server');

return [
'status' => 'success',
'connectivity_status' => RemoteServer::STATUS_ONLINE,
'message' => 'Successfully connected to remote server',
];
}

// If we reach here, the connection was established but not explicitly connected
$this->updateServerStatus($remoteServer, RemoteServer::STATUS_OFFLINE);

Log::debug('[Server Connection Check] Connection established but not explicitly connected');

return [
'connectivity_status' => RemoteServer::STATUS_ONLINE,
'status' => 'error',
'connectivity_status' => RemoteServer::STATUS_OFFLINE,
'message' => 'Connection established but not explicitly connected',
'error' => 'Connection not fully established',
];

} catch (ServerConnectionException $exception) {
} catch (ConnectionException $exception) {
Log::info('[Server Connection Check] Unable to connect to remote server (offline)', [
'error' => $exception->getMessage(),
'server_id' => $remoteServer->getAttribute('id'),
Expand All @@ -109,10 +118,11 @@ private function checkServerConnection(RemoteServer $remoteServer): array
$this->updateServerStatus($remoteServer, RemoteServer::STATUS_OFFLINE);

return [
'status' => 'error',
'connectivity_status' => RemoteServer::STATUS_OFFLINE,
'message' => 'Server is offline or unreachable',
'error' => $exception->getMessage(),
];

} finally {
if (isset($connection)) {
$connection->disconnect();
Expand Down
73 changes: 0 additions & 73 deletions app/Contracts/ServerConnectionInterface.php

This file was deleted.

11 changes: 0 additions & 11 deletions app/Enums/ConnectionType.php

This file was deleted.

12 changes: 0 additions & 12 deletions app/Exceptions/ServerConnectionException.php

This file was deleted.

39 changes: 20 additions & 19 deletions app/Facades/ServerConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,43 @@

namespace App\Facades;

use App\Testing\ServerConnectionFake;
use App\Models\RemoteServer;
use App\Support\ServerConnection\Fakes\ServerConnectionFake;
use App\Support\ServerConnection\PendingConnection;
use App\Support\ServerConnection\ServerConnectionManager;
use Illuminate\Support\Facades\Facade;

/**
* Facade for ServerConnection operations.
* Facade for ServerConnectionManager.
*
* This facade provides a static interface to server connection operations
* and testing utilities.
* This facade provides a static interface to the ServerConnectionManager,
* allowing for easy access to server connection functionality throughout the application.
*
* @method static PendingConnection connect(string $host = '', int $port = 22, string $username = 'root')
* @method static PendingConnection connectFromModel(RemoteServer $server)
* @method static ServerConnectionFake fake()
* @method static void assertConnected()
* @method static void assertDisconnected()
* @method static void assertNotConnected()
* @method static void assertConnectedTo(callable $callback)
* @method static void assertCommandExecuted(string $command)
* @method static void assertCommandRan(string $command)
* @method static void assertFileUploaded(string $localPath, string $remotePath)
* @method static void assertFileDownloaded(string $remotePath, string $localPath)
* @method static void assertOutput(string $output)
* @method static void assertConnectionAttempted(array $connectionDetails)
* @method static void defaultPrivateKey(string $path)
* @method static void defaultPassphrase(string $passphrase)
* @method static ServerConnectionFake shouldConnect()
* @method static ServerConnectionFake shouldNotConnect()*
*
* @see ServerConnectionFake
* @see ServerConnection
* @see \App\Support\ServerConnection\ServerConnectionManager
*/
class ServerConnection extends Facade
{
/**
* Replace the bound instance with a fake for testing.
*/
public static function fake(): ServerConnectionFake
{
static::swap($serverConnectionFake = new ServerConnectionFake);

return $serverConnectionFake;
}

/**
* Get the registered name of the component.
*/
protected static function getFacadeAccessor(): string
{
return 'server.connection';
return ServerConnectionManager::class;
}
}
82 changes: 0 additions & 82 deletions app/Factories/ServerConnectionFactory.php

This file was deleted.

12 changes: 9 additions & 3 deletions app/Livewire/RemoteServers/CreateRemoteServerForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,22 @@ public function usingServerProvider(string $provider): void
private function connectionAttempt(): bool
{
$checkRemoteServerConnection = new CheckRemoteServerConnection;

$response = $checkRemoteServerConnection->byRemoteServerConnectionDetails([
'host' => $this->host,
'port' => $this->port,
'username' => $this->username,
]);

if ($response['status'] === 'error') {
$this->connectionError = $response['error'];
$status = $response['status'] ?? 'error';
$error = $response['error'] ?? $response['message'] ?? 'Unknown error occurred';

if ($status === 'error') {
$this->connectionError = $error;

return false;
}

return $response['status'] === 'success';
return true;
}
}
Loading

0 comments on commit 1c16e9e

Please sign in to comment.