refactoring (#116)

- refactoring architecture
- fix incomplete ssh logs
- code editor for scripts in the app
- remove Jobs and SSHCommands
This commit is contained in:
Saeed Vaziry
2024-03-14 20:03:43 +01:00
committed by GitHub
parent cee4a70c3c
commit 428140b931
472 changed files with 24110 additions and 8159 deletions

View File

@ -2,7 +2,6 @@
namespace App\Helpers;
use App\Contracts\SSHCommand;
use App\Exceptions\SSHAuthenticationError;
use App\Exceptions\SSHCommandError;
use App\Exceptions\SSHConnectionError;
@ -23,7 +22,7 @@ class SSH
public ?ServerLog $log;
protected SSH2|SFTP|null $connection;
protected SSH2|SFTP|null $connection = null;
protected ?string $user;
@ -39,8 +38,8 @@ public function init(Server $server, ?string $asUser = null): self
$this->log = null;
$this->asUser = null;
$this->server = $server->refresh();
$this->user = $server->ssh_user;
if ($asUser && $asUser != $server->ssh_user) {
$this->user = $server->getSshUser();
if ($asUser && $asUser != $server->getSshUser()) {
$this->user = $asUser;
$this->asUser = $asUser;
}
@ -51,7 +50,7 @@ public function init(Server $server, ?string $asUser = null): self
return $this;
}
public function setLog(string $logType, $siteId = null): void
public function setLog(string $logType, $siteId = null): self
{
$this->log = $this->server->logs()->create([
'site_id' => $siteId,
@ -59,6 +58,8 @@ public function setLog(string $logType, $siteId = null): void
'type' => $logType,
'disk' => config('core.logs_disk'),
]);
return $this;
}
/**
@ -66,6 +67,7 @@ public function setLog(string $logType, $siteId = null): void
*/
public function connect(bool $sftp = false): void
{
// If the IP is an IPv6 address, we need to wrap it in square brackets
$ip = $this->server->ip;
if (str($ip)->contains(':')) {
$ip = '['.$ip.']';
@ -91,9 +93,10 @@ public function connect(bool $sftp = false): void
}
/**
* @throws Throwable
* @throws SSHCommandError
* @throws SSHConnectionError
*/
public function exec(string|array|SSHCommand $commands, string $log = '', ?int $siteId = null): string
public function exec(string|array $commands, string $log = '', ?int $siteId = null): string
{
if ($log) {
$this->setLog($log, $siteId);
@ -141,19 +144,15 @@ public function upload(string $local, string $remote): void
/**
* @throws Exception
*/
protected function executeCommand(string|SSHCommand $command): string
protected function executeCommand(string $command): string
{
if ($command instanceof SSHCommand) {
$commandContent = $command->content();
} else {
$commandContent = $command;
}
if ($this->asUser) {
$commandContent = 'sudo su - '.$this->asUser.' -c '.'"'.addslashes($commandContent).'"';
$command = 'sudo su - '.$this->asUser.' -c '.'"'.addslashes($command).'"';
}
$output = $this->connection->exec($commandContent);
$this->connection->setTimeout(0);
$output = $this->connection->exec($command);
$this->log?->write($output);
@ -174,4 +173,12 @@ public function disconnect(): void
$this->connection = null;
}
}
/**
* @throws Exception
*/
public function __destruct()
{
$this->disconnect();
}
}