fix create queue with root user (#409)

* fix create queue with root user

* fix

* fix queues for root user
This commit is contained in:
Saeed Vaziry 2024-12-30 02:33:00 -08:00 committed by GitHub
parent dfdd50beb7
commit 8b86ff23c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 68 additions and 35 deletions

View File

@ -3,15 +3,11 @@
namespace App\Actions\Queue; namespace App\Actions\Queue;
use App\Models\Queue; use App\Models\Queue;
use App\SSH\Services\ProcessManager\ProcessManager;
class DeleteQueue class DeleteQueue
{ {
public function delete(Queue $queue): void public function delete(Queue $queue): void
{ {
/** @var ProcessManager $processManager */
$processManager = $queue->server->processManager()->handler();
$processManager->delete($queue->id, $queue->site_id);
$queue->delete(); $queue->delete();
} }
} }

View File

@ -8,6 +8,6 @@ class GetQueueLogs
{ {
public function getLogs(Queue $queue): string public function getLogs(Queue $queue): string
{ {
return $queue->server->processManager()->handler()->getLogs($queue->getLogFile()); return $queue->server->processManager()->handler()->getLogs($queue->user, $queue->getLogFile());
} }
} }

View File

@ -9,7 +9,9 @@
use App\Models\Server; use App\Models\Server;
use App\Models\ServerLog; use App\Models\ServerLog;
use Exception; use Exception;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use phpseclib3\Crypt\Common\PrivateKey; use phpseclib3\Crypt\Common\PrivateKey;
use phpseclib3\Crypt\PublicKeyLoader; use phpseclib3\Crypt\PublicKeyLoader;
@ -106,7 +108,7 @@ public function exec(string $command, string $log = '', ?int $siteId = null, ?bo
} }
try { try {
if (! $this->connection) { if (! $this->connection instanceof SSH2) {
$this->connect(); $this->connect();
} }
} catch (Throwable $e) { } catch (Throwable $e) {
@ -158,7 +160,7 @@ public function upload(string $local, string $remote): void
{ {
$this->log = null; $this->log = null;
if (! $this->connection) { if (! $this->connection instanceof SFTP) {
$this->connect(true); $this->connect(true);
} }
@ -172,13 +174,43 @@ public function download(string $local, string $remote): void
{ {
$this->log = null; $this->log = null;
if (! $this->connection) { if (! $this->connection instanceof SFTP) {
$this->connect(true); $this->connect(true);
} }
$this->connection->get($remote, $local, SFTP::SOURCE_LOCAL_FILE); $this->connection->get($remote, $local, SFTP::SOURCE_LOCAL_FILE);
} }
/**
* @throws SSHError
*/
public function write(string $remotePath, string $content, bool $sudo = false): void
{
$tmpName = Str::random(10).strtotime('now');
try {
/** @var FilesystemAdapter $storageDisk */
$storageDisk = Storage::disk('local');
$storageDisk->put($tmpName, $content);
if ($sudo) {
$this->upload($storageDisk->path($tmpName), sprintf('/home/%s/%s', $this->server->ssh_user, $tmpName));
$this->exec(sprintf('sudo mv /home/%s/%s %s', $this->server->ssh_user, $tmpName, $remotePath));
} else {
$this->upload($storageDisk->path($tmpName), $remotePath);
}
} catch (Throwable $e) {
throw new SSHCommandError(
message: $e->getMessage()
);
} finally {
if (Storage::disk('local')->exists($tmpName)) {
Storage::disk('local')->delete($tmpName);
}
}
}
/** /**
* @throws Exception * @throws Exception
*/ */

View File

@ -5,6 +5,8 @@
use App\Enums\QueueStatus; use App\Enums\QueueStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Log;
use Throwable;
/** /**
* @property int $server_id * @property int $server_id
@ -62,7 +64,11 @@ public static function boot(): void
parent::boot(); parent::boot();
static::deleting(function (Queue $queue) { static::deleting(function (Queue $queue) {
try {
$queue->server->processManager()->handler()->delete($queue->id, $queue->site_id); $queue->server->processManager()->handler()->delete($queue->id, $queue->site_id);
} catch (Throwable $e) {
Log::error($e);
}
}); });
} }
@ -89,6 +95,10 @@ public function site(): BelongsTo
public function getLogDirectory(): string public function getLogDirectory(): string
{ {
if ($this->user === 'root') {
return '/root/.logs/workers';
}
return '/home/'.$this->user.'/.logs/workers'; return '/home/'.$this->user.'/.logs/workers';
} }

View File

@ -121,8 +121,12 @@ public static function boot(): void
DB::beginTransaction(); DB::beginTransaction();
try { try {
$server->sites()->each(function (Site $site) { $server->sites()->each(function (Site $site) {
$site->delete(); $site->queues()->delete();
$site->ssls()->delete();
$site->deployments()->delete();
$site->deploymentScript()->delete();
}); });
$server->sites()->delete();
$server->logs()->each(function (ServerLog $log) { $server->logs()->each(function (ServerLog $log) {
$log->delete(); $log->delete();
}); });

View File

@ -23,5 +23,5 @@ public function stop(int $id, ?int $siteId = null): void;
public function start(int $id, ?int $siteId = null): void; public function start(int $id, ?int $siteId = null): void;
public function getLogs(string $logPath): string; public function getLogs(string $user, string $logPath): string;
} }

View File

@ -40,10 +40,9 @@ public function create(
string $logFile, string $logFile,
?int $siteId = null ?int $siteId = null
): void { ): void {
$this->service->server->ssh($user)->exec( $this->service->server->ssh()->write(
$this->getScript('supervisor/create-worker.sh', [ "/etc/supervisor/conf.d/$id.conf",
'id' => $id, $this->generateConfigFile(
'config' => $this->generateConfigFile(
$id, $id,
$command, $command,
$user, $user,
@ -52,6 +51,11 @@ public function create(
$numprocs, $numprocs,
$logFile $logFile
), ),
true
);
$this->service->server->ssh($user)->exec(
$this->getScript('supervisor/create-worker.sh', [
'id' => $id,
]), ]),
'create-worker', 'create-worker',
$siteId $siteId
@ -117,9 +121,9 @@ public function start(int $id, ?int $siteId = null): void
/** /**
* @throws Throwable * @throws Throwable
*/ */
public function getLogs(string $logPath): string public function getLogs(string $user, string $logPath): string
{ {
return $this->service->server->ssh()->exec( return $this->service->server->ssh($user)->exec(
"tail -100 $logPath" "tail -100 $logPath"
); );
} }

View File

@ -4,10 +4,6 @@ mkdir -p ~/.logs/workers
touch ~/.logs/workers/__id__.log touch ~/.logs/workers/__id__.log
if ! echo '__config__' | sudo tee /etc/supervisor/conf.d/__id__.conf; then
echo 'VITO_SSH_ERROR' && exit 1
fi
if ! sudo supervisorctl reread; then if ! sudo supervisorctl reread; then
echo 'VITO_SSH_ERROR' && exit 1 echo 'VITO_SSH_ERROR' && exit 1
fi fi

View File

@ -111,15 +111,6 @@ public function getHeaderActions(): array
return $actions; return $actions;
} }
public function getSecondSubNavigation(): array
{
if ($this->site->isInstalling()) {
return [];
}
return parent::getSecondSubNavigation();
}
private function deployAction(): Action private function deployAction(): Action
{ {
return Action::make('deploy') return Action::make('deploy')