From 42ff7b19a46352acaf19f6879a15f566517a0353 Mon Sep 17 00:00:00 2001
From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com>
Date: Tue, 17 Sep 2024 15:54:22 +0200
Subject: [PATCH 213/441] Fix: Few multiplexing things
---
app/Helpers/SshMultiplexingHelper.php | 63 +++++++----------
.../CleanupStaleMultiplexedConnections.php | 68 +++++++++++++++----
app/Models/Server.php | 2 +-
bootstrap/helpers/remoteProcess.php | 67 +++++-------------
config/constants.php | 5 +-
5 files changed, 100 insertions(+), 105 deletions(-)
diff --git a/app/Helpers/SshMultiplexingHelper.php b/app/Helpers/SshMultiplexingHelper.php
index b1507bd00..c5fe90168 100644
--- a/app/Helpers/SshMultiplexingHelper.php
+++ b/app/Helpers/SshMultiplexingHelper.php
@@ -14,7 +14,7 @@ class SshMultiplexingHelper
{
$privateKey = PrivateKey::findOrFail($server->private_key_id);
$sshKeyLocation = $privateKey->getKeyLocation();
- $muxFilename = '/var/www/html/storage/app/ssh/mux/' . $server->muxFilename();
+ $muxFilename = '/var/www/html/storage/app/ssh/mux/mux_' . $server->uuid;
return [
'sshKeyLocation' => $sshKeyLocation,
@@ -25,12 +25,12 @@ class SshMultiplexingHelper
public static function ensureMultiplexedConnection(Server $server)
{
if (!self::isMultiplexingEnabled()) {
- ray('SSH Multiplexing: DISABLED')->red();
+ // ray('SSH Multiplexing: DISABLED')->red();
return;
}
- ray('SSH Multiplexing: ENABLED')->green();
- ray('Ensuring multiplexed connection for server:', $server->id);
+ // ray('SSH Multiplexing: ENABLED')->green();
+ // ray('Ensuring multiplexed connection for server:', $server);
$sshConfig = self::serverSshConfiguration($server);
$muxSocket = $sshConfig['muxFilename'];
@@ -42,18 +42,16 @@ class SshMultiplexingHelper
$process = Process::run($checkCommand);
if ($process->exitCode() !== 0) {
- ray('SSH Multiplexing: Existing connection check failed or not found')->orange();
- ray('Establishing new connection');
+ // ray('SSH Multiplexing: Existing connection check failed or not found')->orange();
+ // ray('Establishing new connection');
self::establishNewMultiplexedConnection($server);
} else {
- ray('SSH Multiplexing: Existing connection is valid')->green();
+ // ray('SSH Multiplexing: Existing connection is valid')->green();
}
}
public static function establishNewMultiplexedConnection(Server $server)
{
- ray('SSH Multiplexing: Establishing new connection for server:', $server->id);
-
$sshConfig = self::serverSshConfiguration($server);
$sshKeyLocation = $sshConfig['sshKeyLocation'];
$muxSocket = $sshConfig['muxFilename'];
@@ -63,27 +61,20 @@ class SshMultiplexingHelper
$muxPersistTime = config('constants.ssh.mux_persist_time');
$establishCommand = "ssh -fNM -o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} "
- . "-i {$sshKeyLocation} "
- . '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
- . '-o PasswordAuthentication=no '
- . "-o ConnectTimeout=$connectionTimeout "
- . "-o ServerAliveInterval=$serverInterval "
- . '-o RequestTTY=no '
- . '-o LogLevel=ERROR '
- . "-p {$server->port} "
+ . self::getCommonSshOptions($server, $sshKeyLocation, $connectionTimeout, $serverInterval)
. "{$server->user}@{$server->ip}";
$establishProcess = Process::run($establishCommand);
if ($establishProcess->exitCode() !== 0) {
- ray('SSH Multiplexing: Failed to establish connection', $establishProcess->errorOutput())->red();
throw new \RuntimeException('Failed to establish multiplexed connection: ' . $establishProcess->errorOutput());
}
- ray('SSH Multiplexing: Connection established successfully')->green();
-
$muxContent = "Multiplexed connection established at " . now()->toDateTimeString();
- Storage::disk('ssh-mux')->put(basename($muxSocket), $muxContent);
+ $muxFilename = basename($muxSocket);
+ if (!Storage::disk('ssh-mux')->put($muxFilename, $muxContent)) {
+ throw new \RuntimeException('Failed to write mux file to disk: ' . $muxFilename);
+ }
}
public static function removeMuxFile(Server $server)
@@ -93,8 +84,6 @@ class SshMultiplexingHelper
$closeCommand = "ssh -O exit -o ControlPath=/var/www/html/storage/app/ssh/mux/{$muxFilename} {$server->user}@{$server->ip}";
Process::run($closeCommand);
-
- Storage::disk('ssh-mux')->delete($muxFilename);
}
public static function generateScpCommand(Server $server, string $source, string $dest)
@@ -104,29 +93,26 @@ class SshMultiplexingHelper
$muxSocket = $sshConfig['muxFilename'];
$timeout = config('constants.ssh.command_timeout');
- $connectionTimeout = config('constants.ssh.connection_timeout');
- $serverInterval = config('constants.ssh.server_interval');
$scp_command = "timeout $timeout scp ";
if (self::isMultiplexingEnabled()) {
- ray('SSH Multiplexing: Enabled for SCP command')->green();
+ // ray('SSH Multiplexing: Enabled for SCP command')->green();
$muxPersistTime = config('constants.ssh.mux_persist_time');
$scp_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} ";
self::ensureMultiplexedConnection($server);
- // Add this line to verify multiplexing is being used
- ray('SSH Multiplexing: Verifying usage')->blue();
+ // ray('SSH Multiplexing: Verifying usage')->blue();
$checkCommand = "ssh -O check -o ControlPath=$muxSocket {$server->user}@{$server->ip}";
$checkProcess = Process::run($checkCommand);
- ray('SSH Multiplexing: ' . ($checkProcess->exitCode() === 0 ? 'Active' : 'Not Active'))->color($checkProcess->exitCode() === 0 ? 'green' : 'red');
+ // ray('SSH Multiplexing: ' . ($checkProcess->exitCode() === 0 ? 'Active' : 'Not Active'))->color($checkProcess->exitCode() === 0 ? 'green' : 'red');
} else {
- ray('SSH Multiplexing: Disabled for SCP command')->orange();
+ // ray('SSH Multiplexing: Disabled for SCP command')->orange();
}
self::addCloudflareProxyCommand($scp_command, $server);
- $scp_command .= self::getCommonSshOptions($server, $sshKeyLocation, $connectionTimeout, $serverInterval);
+ $scp_command .= self::getCommonSshOptions($server, $sshKeyLocation, config('constants.ssh.connection_timeout'), config('constants.ssh.server_interval'));
$scp_command .= "{$source} {$server->user}@{$server->ip}:{$dest}";
return $scp_command;
@@ -143,29 +129,26 @@ class SshMultiplexingHelper
$muxSocket = $sshConfig['muxFilename'];
$timeout = config('constants.ssh.command_timeout');
- $connectionTimeout = config('constants.ssh.connection_timeout');
- $serverInterval = config('constants.ssh.server_interval');
$ssh_command = "timeout $timeout ssh ";
if (self::isMultiplexingEnabled()) {
- ray('SSH Multiplexing: Enabled for SSH command')->green();
+ // ray('SSH Multiplexing: Enabled for SSH command')->green();
$muxPersistTime = config('constants.ssh.mux_persist_time');
$ssh_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} ";
self::ensureMultiplexedConnection($server);
- // Add this line to verify multiplexing is being used
- ray('SSH Multiplexing: Verifying usage')->blue();
+ // ray('SSH Multiplexing: Verifying usage')->blue();
$checkCommand = "ssh -O check -o ControlPath=$muxSocket {$server->user}@{$server->ip}";
$checkProcess = Process::run($checkCommand);
- ray('SSH Multiplexing: ' . ($checkProcess->exitCode() === 0 ? 'Active' : 'Not Active'))->color($checkProcess->exitCode() === 0 ? 'green' : 'red');
+ // ray('SSH Multiplexing: ' . ($checkProcess->exitCode() === 0 ? 'Active' : 'Not Active'))->color($checkProcess->exitCode() === 0 ? 'green' : 'red');
} else {
- ray('SSH Multiplexing: Disabled for SSH command')->orange();
+ // ray('SSH Multiplexing: Disabled for SSH command')->orange();
}
self::addCloudflareProxyCommand($ssh_command, $server);
- $ssh_command .= self::getCommonSshOptions($server, $sshKeyLocation, $connectionTimeout, $serverInterval);
+ $ssh_command .= self::getCommonSshOptions($server, $sshKeyLocation, config('constants.ssh.connection_timeout'), config('constants.ssh.server_interval'));
$command = "PATH=\$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/host/usr/local/sbin:/host/usr/local/bin:/host/usr/sbin:/host/usr/bin:/host/sbin:/host/bin && $command";
$delimiter = Hash::make($command);
@@ -181,7 +164,7 @@ class SshMultiplexingHelper
private static function isMultiplexingEnabled(): bool
{
$isEnabled = config('constants.ssh.mux_enabled') && !config('coolify.is_windows_docker_desktop');
- ray('SSH Multiplexing Status:', $isEnabled ? 'ENABLED' : 'DISABLED')->color($isEnabled ? 'green' : 'red');
+ // ray('SSH Multiplexing Status:', $isEnabled ? 'ENABLED' : 'DISABLED')->color($isEnabled ? 'green' : 'red');
return $isEnabled;
}
diff --git a/app/Jobs/CleanupStaleMultiplexedConnections.php b/app/Jobs/CleanupStaleMultiplexedConnections.php
index bcca77c18..acb28c2f4 100644
--- a/app/Jobs/CleanupStaleMultiplexedConnections.php
+++ b/app/Jobs/CleanupStaleMultiplexedConnections.php
@@ -9,6 +9,8 @@ use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Process;
+use Illuminate\Support\Facades\Storage;
+use Carbon\Carbon;
class CleanupStaleMultiplexedConnections implements ShouldQueue
{
@@ -16,22 +18,64 @@ class CleanupStaleMultiplexedConnections implements ShouldQueue
public function handle()
{
- Server::chunk(100, function ($servers) {
- foreach ($servers as $server) {
- $this->cleanupStaleConnection($server);
- }
- });
+ $this->cleanupStaleConnections();
+ $this->cleanupNonExistentServerConnections();
}
- private function cleanupStaleConnection(Server $server)
+ private function cleanupStaleConnections()
{
- $muxSocket = "/tmp/mux_{$server->id}";
- $checkCommand = "ssh -O check -o ControlPath=$muxSocket {$server->user}@{$server->ip} 2>/dev/null";
- $checkProcess = Process::run($checkCommand);
+ $muxFiles = Storage::disk('ssh-mux')->files();
- if ($checkProcess->exitCode() !== 0) {
- $closeCommand = "ssh -O exit -o ControlPath=$muxSocket {$server->user}@{$server->ip} 2>/dev/null";
- Process::run($closeCommand);
+ foreach ($muxFiles as $muxFile) {
+ $serverUuid = $this->extractServerUuidFromMuxFile($muxFile);
+ $server = Server::where('uuid', $serverUuid)->first();
+
+ if (!$server) {
+ $this->removeMultiplexFile($muxFile);
+ continue;
+ }
+
+ $muxSocket = "/var/www/html/storage/app/ssh/mux/{$muxFile}";
+ $checkCommand = "ssh -O check -o ControlPath={$muxSocket} {$server->user}@{$server->ip} 2>/dev/null";
+ $checkProcess = Process::run($checkCommand);
+
+ if ($checkProcess->exitCode() !== 0) {
+ $this->removeMultiplexFile($muxFile);
+ } else {
+ $muxContent = Storage::disk('ssh-mux')->get($muxFile);
+ $establishedAt = Carbon::parse(substr($muxContent, 37));
+ $expirationTime = $establishedAt->addSeconds(config('constants.ssh.mux_persist_time'));
+
+ if (Carbon::now()->isAfter($expirationTime)) {
+ $this->removeMultiplexFile($muxFile);
+ }
+ }
}
}
+
+ private function cleanupNonExistentServerConnections()
+ {
+ $muxFiles = Storage::disk('ssh-mux')->files();
+ $existingServerUuids = Server::pluck('uuid')->toArray();
+
+ foreach ($muxFiles as $muxFile) {
+ $serverUuid = $this->extractServerUuidFromMuxFile($muxFile);
+ if (!in_array($serverUuid, $existingServerUuids)) {
+ $this->removeMultiplexFile($muxFile);
+ }
+ }
+ }
+
+ private function extractServerUuidFromMuxFile($muxFile)
+ {
+ return substr($muxFile, 4);
+ }
+
+ private function removeMultiplexFile($muxFile)
+ {
+ $muxSocket = "/var/www/html/storage/app/ssh/mux/{$muxFile}";
+ $closeCommand = "ssh -O exit -o ControlPath={$muxSocket} localhost 2>/dev/null";
+ Process::run($closeCommand);
+ Storage::disk('ssh-mux')->delete($muxFile);
+ }
}
diff --git a/app/Models/Server.php b/app/Models/Server.php
index 43045e1b0..363db3297 100644
--- a/app/Models/Server.php
+++ b/app/Models/Server.php
@@ -967,7 +967,7 @@ $schema://$host {
public function validateConnection($isManualCheck = true)
{
config()->set('constants.ssh.mux_enabled', !$isManualCheck);
- ray('Manual Check: ' . ($isManualCheck ? 'true' : 'false'));
+ // ray('Manual Check: ' . ($isManualCheck ? 'true' : 'false'));
$server = Server::find($this->id);
if (! $server) {
diff --git a/bootstrap/helpers/remoteProcess.php b/bootstrap/helpers/remoteProcess.php
index 5263ea970..ebc8420c6 100644
--- a/bootstrap/helpers/remoteProcess.php
+++ b/bootstrap/helpers/remoteProcess.php
@@ -60,40 +60,28 @@ function remote_process(
function instant_scp(string $source, string $dest, Server $server, $throwError = true)
{
- $timeout = config('constants.ssh.command_timeout');
$scp_command = SshMultiplexingHelper::generateScpCommand($server, $source, $dest);
- $process = Process::timeout($timeout)->run($scp_command);
+ $process = Process::timeout(config('constants.ssh.command_timeout'))->run($scp_command);
$output = trim($process->output());
$exitCode = $process->exitCode();
if ($exitCode !== 0) {
- if (! $throwError) {
- return null;
- }
-
- return excludeCertainErrors($process->errorOutput(), $exitCode);
+ return $throwError ? excludeCertainErrors($process->errorOutput(), $exitCode) : null;
}
- if ($output === 'null') {
- $output = null;
- }
-
- return $output;
+ return $output === 'null' ? null : $output;
}
function instant_remote_process(Collection|array $command, Server $server, bool $throwError = true, bool $no_sudo = false): ?string
{
- $timeout = config('constants.ssh.command_timeout');
- if ($command instanceof Collection) {
- $command = $command->toArray();
- }
- if ($server->isNonRoot() && ! $no_sudo) {
+ $command = $command instanceof Collection ? $command->toArray() : $command;
+ if ($server->isNonRoot() && !$no_sudo) {
$command = parseCommandsByLineForSudo(collect($command), $server);
}
$command_string = implode("\n", $command);
- $start_time = microtime(true);
+ // $start_time = microtime(true);
$sshCommand = SshMultiplexingHelper::generateSshCommand($server, $command_string);
- $process = Process::timeout($timeout)->run($sshCommand);
- $end_time = microtime(true);
+ $process = Process::timeout(config('constants.ssh.command_timeout'))->run($sshCommand);
+ // $end_time = microtime(true);
// $execution_time = ($end_time - $start_time) * 1000; // Convert to milliseconds
// ray('SSH command execution time:', $execution_time.' ms')->orange();
@@ -102,17 +90,9 @@ function instant_remote_process(Collection|array $command, Server $server, bool
$exitCode = $process->exitCode();
if ($exitCode !== 0) {
- if (! $throwError) {
- return null;
- }
-
- return excludeCertainErrors($process->errorOutput(), $exitCode);
+ return $throwError ? excludeCertainErrors($process->errorOutput(), $exitCode) : null;
}
- if ($output === 'null') {
- $output = null;
- }
-
- return $output;
+ return $output === 'null' ? null : $output;
}
function excludeCertainErrors(string $errorOutput, ?int $exitCode = null)
@@ -121,13 +101,7 @@ function excludeCertainErrors(string $errorOutput, ?int $exitCode = null)
'Permission denied (publickey',
'Could not resolve hostname',
]);
- $ignored = false;
- foreach ($ignoredErrors as $ignoredError) {
- if (Str::contains($errorOutput, $ignoredError)) {
- $ignored = true;
- break;
- }
- }
+ $ignored = $ignoredErrors->contains(fn($error) => Str::contains($errorOutput, $error));
if ($ignored) {
// TODO: Create new exception and disable in sentry
throw new \RuntimeException($errorOutput, $exitCode);
@@ -137,11 +111,11 @@ function excludeCertainErrors(string $errorOutput, ?int $exitCode = null)
function decode_remote_command_output(?ApplicationDeploymentQueue $application_deployment_queue = null): Collection
{
- $application = Application::find(data_get($application_deployment_queue, 'application_id'));
- $is_debug_enabled = data_get($application, 'settings.is_debug_enabled');
if (is_null($application_deployment_queue)) {
return collect([]);
}
+ $application = Application::find(data_get($application_deployment_queue, 'application_id'));
+ $is_debug_enabled = data_get($application, 'settings.is_debug_enabled');
try {
$decoded = json_decode(
data_get($application_deployment_queue, 'logs'),
@@ -153,20 +127,19 @@ function decode_remote_command_output(?ApplicationDeploymentQueue $application_d
}
$seenCommands = collect();
$formatted = collect($decoded);
- if (! $is_debug_enabled) {
+ if (!$is_debug_enabled) {
$formatted = $formatted->filter(fn ($i) => $i['hidden'] === false ?? false);
}
- $formatted = $formatted
+ return $formatted
->sortBy(fn ($i) => data_get($i, 'order'))
->map(function ($i) {
data_set($i, 'timestamp', Carbon::parse(data_get($i, 'timestamp'))->format('Y-M-d H:i:s.u'));
-
return $i;
})
->reduce(function ($deploymentLogLines, $logItem) use ($seenCommands) {
$command = data_get($logItem, 'command');
$isStderr = data_get($logItem, 'type') === 'stderr';
- $isNewCommand = ! is_null($command) && ! $seenCommands->first(function ($seenCommand) use ($logItem) {
+ $isNewCommand = !is_null($command) && !$seenCommands->first(function ($seenCommand) use ($logItem) {
return data_get($seenCommand, 'command') === data_get($logItem, 'command') && data_get($seenCommand, 'batch') === data_get($logItem, 'batch');
});
@@ -198,14 +171,11 @@ function decode_remote_command_output(?ApplicationDeploymentQueue $application_d
return $deploymentLogLines;
}, collect());
-
- return $formatted;
}
function remove_iip($text)
{
$text = preg_replace('/x-access-token:.*?(?=@)/', 'x-access-token:'.REDACTED, $text);
-
return preg_replace('/\x1b\[[0-9;]*m/', '', $text);
}
@@ -233,9 +203,8 @@ function checkRequiredCommands(Server $server)
break;
}
$commandFound = instant_remote_process(["docker run --rm --privileged --net=host --pid=host --ipc=host --volume /:/host busybox chroot /host bash -c 'command -v {$command}'"], $server, false);
- if ($commandFound) {
- continue;
+ if (!$commandFound) {
+ break;
}
- break;
}
}
diff --git a/config/constants.php b/config/constants.php
index 906ef3ba2..5792b358c 100644
--- a/config/constants.php
+++ b/config/constants.php
@@ -6,9 +6,8 @@ return [
'contact' => 'https://coolify.io/docs/contact',
],
'ssh' => [
- // Using MUX
- 'mux_enabled' => env('MUX_ENABLED', env('SSH_MUX_ENABLED', true), true),
- 'mux_persist_time' => env('SSH_MUX_PERSIST_TIME', '1h'),
+ 'mux_enabled' => env('MUX_ENABLED', env('SSH_MUX_ENABLED', true)),
+ 'mux_persist_time' => env('SSH_MUX_PERSIST_TIME', 3600),
'connection_timeout' => 10,
'server_interval' => 20,
'command_timeout' => 7200,
From d13e2c086541f8e2005ee5c2bc14da49baf419a2 Mon Sep 17 00:00:00 2001
From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com>
Date: Tue, 17 Sep 2024 15:57:57 +0200
Subject: [PATCH 214/441] Fix: Clear mux directory
---
..._16_170001_populate_ssh_keys_and_clear_mux_directory.php} | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
rename database/migrations/{2024_09_16_170001_populate_ssh_keys_directory.php => 2024_09_16_170001_populate_ssh_keys_and_clear_mux_directory.php} (71%)
diff --git a/database/migrations/2024_09_16_170001_populate_ssh_keys_directory.php b/database/migrations/2024_09_16_170001_populate_ssh_keys_and_clear_mux_directory.php
similarity index 71%
rename from database/migrations/2024_09_16_170001_populate_ssh_keys_directory.php
rename to database/migrations/2024_09_16_170001_populate_ssh_keys_and_clear_mux_directory.php
index 33a5e695f..944b00e13 100644
--- a/database/migrations/2024_09_16_170001_populate_ssh_keys_directory.php
+++ b/database/migrations/2024_09_16_170001_populate_ssh_keys_and_clear_mux_directory.php
@@ -4,13 +4,16 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Storage;
use App\Models\PrivateKey;
-class PopulateSshKeysDirectory extends Migration
+class PopulateSshKeysAndClearMuxDirectory extends Migration
{
public function up()
{
Storage::disk('ssh-keys')->deleteDirectory('');
Storage::disk('ssh-keys')->makeDirectory('');
+ Storage::disk('ssh-mux')->deleteDirectory('');
+ Storage::disk('ssh-mux')->makeDirectory('');
+
PrivateKey::chunk(100, function ($keys) {
foreach ($keys as $key) {
$key->storeInFileSystem();
From d9181bd00bf70b2db5550583c6ca60fb8a92b6ba Mon Sep 17 00:00:00 2001
From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com>
Date: Tue, 17 Sep 2024 16:22:53 +0200
Subject: [PATCH 215/441] Fix: Multiplexing do not write file manually
---
app/Helpers/SshMultiplexingHelper.php | 59 +++++++++++++++------------
app/Models/PrivateKey.php | 6 +--
2 files changed, 35 insertions(+), 30 deletions(-)
diff --git a/app/Helpers/SshMultiplexingHelper.php b/app/Helpers/SshMultiplexingHelper.php
index c5fe90168..57d4c88a4 100644
--- a/app/Helpers/SshMultiplexingHelper.php
+++ b/app/Helpers/SshMultiplexingHelper.php
@@ -5,7 +5,6 @@ namespace App\Helpers;
use App\Models\Server;
use App\Models\PrivateKey;
use Illuminate\Support\Facades\Process;
-use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Hash;
class SshMultiplexingHelper
@@ -56,6 +55,10 @@ class SshMultiplexingHelper
$sshKeyLocation = $sshConfig['sshKeyLocation'];
$muxSocket = $sshConfig['muxFilename'];
+ // ray('Establishing new multiplexed connection')->blue();
+ // ray('SSH Key Location:', $sshKeyLocation);
+ // ray('Mux Socket:', $muxSocket);
+
$connectionTimeout = config('constants.ssh.connection_timeout');
$serverInterval = config('constants.ssh.server_interval');
$muxPersistTime = config('constants.ssh.mux_persist_time');
@@ -64,26 +67,46 @@ class SshMultiplexingHelper
. self::getCommonSshOptions($server, $sshKeyLocation, $connectionTimeout, $serverInterval)
. "{$server->user}@{$server->ip}";
+ // ray('Establish Command:', $establishCommand);
+
$establishProcess = Process::run($establishCommand);
+ // ray('Establish Process Exit Code:', $establishProcess->exitCode());
+ // ray('Establish Process Output:', $establishProcess->output());
+ // ray('Establish Process Error Output:', $establishProcess->errorOutput());
+
if ($establishProcess->exitCode() !== 0) {
+ // ray('Failed to establish multiplexed connection')->red();
throw new \RuntimeException('Failed to establish multiplexed connection: ' . $establishProcess->errorOutput());
}
- $muxContent = "Multiplexed connection established at " . now()->toDateTimeString();
- $muxFilename = basename($muxSocket);
- if (!Storage::disk('ssh-mux')->put($muxFilename, $muxContent)) {
- throw new \RuntimeException('Failed to write mux file to disk: ' . $muxFilename);
+ // ray('Successfully established multiplexed connection')->green();
+
+ // Check if the mux socket file was created
+ if (!file_exists($muxSocket)) {
+ // ray('Mux socket file not found after connection establishment')->orange();
}
}
public static function removeMuxFile(Server $server)
{
$sshConfig = self::serverSshConfiguration($server);
- $muxFilename = basename($sshConfig['muxFilename']);
+ $muxSocket = $sshConfig['muxFilename'];
- $closeCommand = "ssh -O exit -o ControlPath=/var/www/html/storage/app/ssh/mux/{$muxFilename} {$server->user}@{$server->ip}";
- Process::run($closeCommand);
+ $closeCommand = "ssh -O exit -o ControlPath=$muxSocket {$server->user}@{$server->ip}";
+ $process = Process::run($closeCommand);
+
+ // ray('Closing multiplexed connection')->blue();
+ // ray('Close command:', $closeCommand);
+ // ray('Close process exit code:', $process->exitCode());
+ // ray('Close process output:', $process->output());
+ // ray('Close process error output:', $process->errorOutput());
+
+ if ($process->exitCode() !== 0) {
+ // ray('Failed to close multiplexed connection')->orange();
+ } else {
+ // ray('Successfully closed multiplexed connection')->green();
+ }
}
public static function generateScpCommand(Server $server, string $source, string $dest)
@@ -97,17 +120,9 @@ class SshMultiplexingHelper
$scp_command = "timeout $timeout scp ";
if (self::isMultiplexingEnabled()) {
- // ray('SSH Multiplexing: Enabled for SCP command')->green();
$muxPersistTime = config('constants.ssh.mux_persist_time');
$scp_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} ";
self::ensureMultiplexedConnection($server);
-
- // ray('SSH Multiplexing: Verifying usage')->blue();
- $checkCommand = "ssh -O check -o ControlPath=$muxSocket {$server->user}@{$server->ip}";
- $checkProcess = Process::run($checkCommand);
- // ray('SSH Multiplexing: ' . ($checkProcess->exitCode() === 0 ? 'Active' : 'Not Active'))->color($checkProcess->exitCode() === 0 ? 'green' : 'red');
- } else {
- // ray('SSH Multiplexing: Disabled for SCP command')->orange();
}
self::addCloudflareProxyCommand($scp_command, $server);
@@ -133,17 +148,9 @@ class SshMultiplexingHelper
$ssh_command = "timeout $timeout ssh ";
if (self::isMultiplexingEnabled()) {
- // ray('SSH Multiplexing: Enabled for SSH command')->green();
$muxPersistTime = config('constants.ssh.mux_persist_time');
$ssh_command .= "-o ControlMaster=auto -o ControlPath=$muxSocket -o ControlPersist={$muxPersistTime} ";
self::ensureMultiplexedConnection($server);
-
- // ray('SSH Multiplexing: Verifying usage')->blue();
- $checkCommand = "ssh -O check -o ControlPath=$muxSocket {$server->user}@{$server->ip}";
- $checkProcess = Process::run($checkCommand);
- // ray('SSH Multiplexing: ' . ($checkProcess->exitCode() === 0 ? 'Active' : 'Not Active'))->color($checkProcess->exitCode() === 0 ? 'green' : 'red');
- } else {
- // ray('SSH Multiplexing: Disabled for SSH command')->orange();
}
self::addCloudflareProxyCommand($ssh_command, $server);
@@ -163,9 +170,7 @@ class SshMultiplexingHelper
private static function isMultiplexingEnabled(): bool
{
- $isEnabled = config('constants.ssh.mux_enabled') && !config('coolify.is_windows_docker_desktop');
- // ray('SSH Multiplexing Status:', $isEnabled ? 'ENABLED' : 'DISABLED')->color($isEnabled ? 'green' : 'red');
- return $isEnabled;
+ return config('constants.ssh.mux_enabled') && !config('coolify.is_windows_docker_desktop');
}
private static function validateSshKey(string $sshKeyLocation): void
diff --git a/app/Models/PrivateKey.php b/app/Models/PrivateKey.php
index 1ad12cf36..6985ca536 100644
--- a/app/Models/PrivateKey.php
+++ b/app/Models/PrivateKey.php
@@ -137,20 +137,20 @@ class PrivateKey extends BaseModel
public function storeInFileSystem()
{
- $filename = "ssh@{$this->uuid}";
+ $filename = "ssh_key@{$this->uuid}";
Storage::disk('ssh-keys')->put($filename, $this->private_key);
return "/var/www/html/storage/app/ssh/keys/{$filename}";
}
public static function deleteFromStorage(self $privateKey)
{
- $filename = "ssh@{$privateKey->uuid}";
+ $filename = "ssh_key@{$privateKey->uuid}";
Storage::disk('ssh-keys')->delete($filename);
}
public function getKeyLocation()
{
- return "/var/www/html/storage/app/ssh/keys/ssh@{$this->uuid}";
+ return "/var/www/html/storage/app/ssh/keys/ssh_key@{$this->uuid}";
}
public function updatePrivateKey(array $data)
From d373815f989bdddbfb897454e906aa4b514fb380 Mon Sep 17 00:00:00 2001
From: Andras Bacsai
Date: Tue, 17 Sep 2024 16:28:28 +0200
Subject: [PATCH 216/441] refactor: Add authorization check in
ExecuteContainerCommand mount method
---
app/Livewire/Project/Shared/ExecuteContainerCommand.php | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/app/Livewire/Project/Shared/ExecuteContainerCommand.php b/app/Livewire/Project/Shared/ExecuteContainerCommand.php
index 79f32ab8b..d95443621 100644
--- a/app/Livewire/Project/Shared/ExecuteContainerCommand.php
+++ b/app/Livewire/Project/Shared/ExecuteContainerCommand.php
@@ -33,6 +33,9 @@ class ExecuteContainerCommand extends Component
public function mount()
{
+ if (! auth()->user()->isAdmin()) {
+ abort(403);
+ }
$this->parameters = get_route_parameters();
$this->containers = collect();
$this->servers = collect();
@@ -130,7 +133,6 @@ class ExecuteContainerCommand extends Component
{
try {
$container_name = data_get($this->container, 'container.Names');
- ray($this->container);
if (is_null($container_name)) {
throw new \RuntimeException('Container not found.');
}
From 07ed726c88c6da5069a47f597e23bc5d3f8e0c03 Mon Sep 17 00:00:00 2001
From: Andras Bacsai
Date: Tue, 17 Sep 2024 16:48:30 +0200
Subject: [PATCH 217/441] refactor: Remove unnecessary code in Terminal.php
---
app/Livewire/Project/Shared/Terminal.php | 7 -------
1 file changed, 7 deletions(-)
diff --git a/app/Livewire/Project/Shared/Terminal.php b/app/Livewire/Project/Shared/Terminal.php
index 7c23c291d..802e65a30 100644
--- a/app/Livewire/Project/Shared/Terminal.php
+++ b/app/Livewire/Project/Shared/Terminal.php
@@ -14,13 +14,6 @@ class Terminal extends Component
$server = Server::ownedByCurrentTeam()->whereUuid($serverUuid)->firstOrFail();
- // if (auth()->user()) {
- // $teams = auth()->user()->teams->pluck('id');
- // if (! $teams->contains($server->team_id) && ! $teams->contains(0)) {
- // throw new \Exception('User is not part of the team that owns this server');
- // }
- // }
-
if ($isContainer) {
$status = getContainerStatus($server, $identifier);
if ($status !== 'running') {
From 595a2414b133d66922e279abf87e5e2cb5a0e69c Mon Sep 17 00:00:00 2001
From: Andras Bacsai
Date: Tue, 17 Sep 2024 16:48:58 +0200
Subject: [PATCH 218/441] fix: if you exit a container manually, it should
close the underlying tty as well
---
resources/views/livewire/project/shared/terminal.blade.php | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/resources/views/livewire/project/shared/terminal.blade.php b/resources/views/livewire/project/shared/terminal.blade.php
index 31887120d..3c485c286 100644
--- a/resources/views/livewire/project/shared/terminal.blade.php
+++ b/resources/views/livewire/project/shared/terminal.blade.php
@@ -118,9 +118,8 @@
socket.send(JSON.stringify({
message: data
}));
-
// Type CTRL + D or exit in the terminal
- if (data === '\x04' || (data === '\r' && stripAnsiCommands(commandBuffer).trim() === 'exit')) {
+ if (data === '\x04' || (data === '\r' && stripAnsiCommands(commandBuffer).trim().includes('exit'))) {
checkIfProcessIsRunningAndKillIt();
setTimeout(() => {
$data.terminalActive = false;
@@ -215,8 +214,8 @@
term.resize(termWidth, termHeight);
socket.send(JSON.stringify({
resize: {
- cols: 600,
- rows: 600
+ cols: termWidth,
+ rows: termHeight
}
}));
}
From bf48b33e647f0cdb9f3883fbd09a0793ed3dd74f Mon Sep 17 00:00:00 2001
From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com>
Date: Tue, 17 Sep 2024 17:14:15 +0200
Subject: [PATCH 219/441] Update pull_request_template.md
---
.github/pull_request_template.md | 25 ++++++++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)
diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md
index 3ded74ce3..972329c11 100644
--- a/.github/pull_request_template.md
+++ b/.github/pull_request_template.md
@@ -1 +1,24 @@
-> Always use `next` branch as destination branch for PRs, not `main`
+> [!IMPORTANT]
+> - Always select the `next` branch as the destination for your PR, not `main`!
+> - Fill out the [Issues](#issues) (if applicable) and [Changes](#changes) sections below.
+> - List all changes using the following prefixes:
+> - fix: (for bug fixes)
+> - feat: (for new features)
+> - improvement: (for enhancing existing features)
+> - service: (for new services)
+> - chore: (for maintenance, refactoring, dependencies, etc.)
+> - Complete the [Checklist](#checklist) before submitting.
+> - Remove this note and any unused sections before submitting.
+
+### Issues
+- Fixes:
+
+### Changes
+-
+
+### Checklist
+- [ ] I have selected the `next` branch as the destination.
+- [ ] I have tested my changes.
+- [ ] I have listed all changes in the [Changes](#changes) section.
+- [ ] I have considered backwards compatibility.
+- [ ] I have removed the instruction note and any unused sections.
From e937d30545c4a16dd4e6a5c57e9f7d624a864aa2 Mon Sep 17 00:00:00 2001
From: Andras Bacsai
Date: Tue, 17 Sep 2024 17:15:34 +0200
Subject: [PATCH 220/441] fix: move terminal to separate view on services
---
app/Livewire/Project/Service/Navbar.php | 2 ++
.../livewire/project/service/configuration.blade.php | 7 -------
.../views/livewire/project/service/navbar.blade.php | 12 ++++++++----
.../shared/execute-container-command.blade.php | 2 +-
routes/web.php | 2 +-
5 files changed, 12 insertions(+), 13 deletions(-)
diff --git a/app/Livewire/Project/Service/Navbar.php b/app/Livewire/Project/Service/Navbar.php
index 674182df5..e6bb6d9bf 100644
--- a/app/Livewire/Project/Service/Navbar.php
+++ b/app/Livewire/Project/Service/Navbar.php
@@ -20,6 +20,8 @@ class Navbar extends Component
public $isDeploymentProgress = false;
+ public $title = 'Configuration';
+
public function mount()
{
if (str($this->service->status())->contains('running') && is_null($this->service->config_hash)) {
diff --git a/resources/views/livewire/project/service/configuration.blade.php b/resources/views/livewire/project/service/configuration.blade.php
index c5ac0412f..4b692f9a9 100644
--- a/resources/views/livewire/project/service/configuration.blade.php
+++ b/resources/views/livewire/project/service/configuration.blade.php
@@ -23,10 +23,6 @@
@click.prevent="activeTab = 'scheduled-tasks'; window.location.hash = 'scheduled-tasks'"
href="#">Scheduled Tasks
-
-