fix: add timeout for ssh commands

This commit is contained in:
Andras Bacsai 2023-09-13 13:00:16 +02:00
parent 0924070a13
commit 1efb1235b4
4 changed files with 15 additions and 10 deletions

View File

@ -10,9 +10,6 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Process; use Illuminate\Support\Facades\Process;
use Spatie\Activitylog\Models\Activity; use Spatie\Activitylog\Models\Activity;
const TIMEOUT = 3600;
const IDLE_TIMEOUT = 3600;
class RunRemoteProcess class RunRemoteProcess
{ {
public Activity $activity; public Activity $activity;
@ -76,8 +73,7 @@ class RunRemoteProcess
$this->time_start = hrtime(true); $this->time_start = hrtime(true);
$status = ProcessStatus::IN_PROGRESS; $status = ProcessStatus::IN_PROGRESS;
$processResult = Process::forever()->run($this->getCommand(), $this->handleOutput(...));
$processResult = Process::timeout(TIMEOUT)->idleTimeout(IDLE_TIMEOUT)->run($this->getCommand(), $this->handleOutput(...));
if ($this->activity->properties->get('status') === ProcessStatus::ERROR->value) { if ($this->activity->properties->get('status') === ProcessStatus::ERROR->value) {
$status = ProcessStatus::ERROR; $status = ProcessStatus::ERROR;

View File

@ -56,7 +56,7 @@ class Form extends Component
$this->uptime = $uptime; $this->uptime = $uptime;
$this->emit('success', 'Server is reachable!'); $this->emit('success', 'Server is reachable!');
} else { } else {
$this->emit('error', 'Server is not rachable'); $this->emit('error', 'Server is not reachable');
return; return;
} }
if ($dockerVersion) { if ($dockerVersion) {

View File

@ -71,8 +71,12 @@ function save_private_key_for_server(Server $server)
function generate_ssh_command(string $private_key_location, string $server_ip, string $user, string $port, string $command, bool $isMux = true) function generate_ssh_command(string $private_key_location, string $server_ip, string $user, string $port, string $command, bool $isMux = true)
{ {
$timeout = config('constants.ssh.command_timeout');
$connectionTimeout = config('constants.ssh.connection_timeout');
$serverInterval = config('constants.ssh.server_interval');
$delimiter = 'EOF-COOLIFY-SSH'; $delimiter = 'EOF-COOLIFY-SSH';
$ssh_command = "ssh "; $ssh_command = "timeout $timeout ssh ";
if ($isMux && config('coolify.mux_enabled')) { if ($isMux && config('coolify.mux_enabled')) {
$ssh_command .= '-o ControlMaster=auto -o ControlPersist=1m -o ControlPath=/var/www/html/storage/app/ssh/mux/%h_%p_%r '; $ssh_command .= '-o ControlMaster=auto -o ControlPersist=1m -o ControlPath=/var/www/html/storage/app/ssh/mux/%h_%p_%r ';
@ -81,8 +85,8 @@ function generate_ssh_command(string $private_key_location, string $server_ip, s
$ssh_command .= "-i {$private_key_location} " $ssh_command .= "-i {$private_key_location} "
. '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ' . '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
. '-o PasswordAuthentication=no ' . '-o PasswordAuthentication=no '
. '-o ConnectTimeout=3600 ' . "-o ConnectTimeout=$connectionTimeout "
. '-o ServerAliveInterval=20 ' . "-o ServerAliveInterval=$serverInterval "
. '-o RequestTTY=no ' . '-o RequestTTY=no '
. '-o LogLevel=ERROR ' . '-o LogLevel=ERROR '
. "-p {$port} " . "-p {$port} "
@ -90,7 +94,7 @@ function generate_ssh_command(string $private_key_location, string $server_ip, s
. " 'bash -se' << \\$delimiter" . PHP_EOL . " 'bash -se' << \\$delimiter" . PHP_EOL
. $command . PHP_EOL . $command . PHP_EOL
. $delimiter; . $delimiter;
ray($ssh_command);
return $ssh_command; return $ssh_command;
} }
function instantCommand(string $command, $throwError = true) { function instantCommand(string $command, $throwError = true) {

View File

@ -1,5 +1,10 @@
<?php <?php
return [ return [
'ssh' =>[
'connection_timeout' => 10,
'server_interval' => 20,
'command_timeout' => 7200,
],
'waitlist' => [ 'waitlist' => [
'expiration' => 10, 'expiration' => 10,
], ],