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;
use App\Models\Queue;
use App\SSH\Services\ProcessManager\ProcessManager;
class DeleteQueue
{
public function delete(Queue $queue): void
{
/** @var ProcessManager $processManager */
$processManager = $queue->server->processManager()->handler();
$processManager->delete($queue->id, $queue->site_id);
$queue->delete();
}
}

View File

@ -8,6 +8,6 @@ class GetQueueLogs
{
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\ServerLog;
use Exception;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use phpseclib3\Crypt\Common\PrivateKey;
use phpseclib3\Crypt\PublicKeyLoader;
@ -106,7 +108,7 @@ public function exec(string $command, string $log = '', ?int $siteId = null, ?bo
}
try {
if (! $this->connection) {
if (! $this->connection instanceof SSH2) {
$this->connect();
}
} catch (Throwable $e) {
@ -158,7 +160,7 @@ public function upload(string $local, string $remote): void
{
$this->log = null;
if (! $this->connection) {
if (! $this->connection instanceof SFTP) {
$this->connect(true);
}
@ -172,13 +174,43 @@ public function download(string $local, string $remote): void
{
$this->log = null;
if (! $this->connection) {
if (! $this->connection instanceof SFTP) {
$this->connect(true);
}
$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
*/

View File

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

View File

@ -121,8 +121,12 @@ public static function boot(): void
DB::beginTransaction();
try {
$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) {
$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 getLogs(string $logPath): string;
public function getLogs(string $user, string $logPath): string;
}

View File

@ -40,18 +40,22 @@ public function create(
string $logFile,
?int $siteId = null
): void {
$this->service->server->ssh()->write(
"/etc/supervisor/conf.d/$id.conf",
$this->generateConfigFile(
$id,
$command,
$user,
$autoStart,
$autoRestart,
$numprocs,
$logFile
),
true
);
$this->service->server->ssh($user)->exec(
$this->getScript('supervisor/create-worker.sh', [
'id' => $id,
'config' => $this->generateConfigFile(
$id,
$command,
$user,
$autoStart,
$autoRestart,
$numprocs,
$logFile
),
]),
'create-worker',
$siteId
@ -117,9 +121,9 @@ public function start(int $id, ?int $siteId = null): void
/**
* @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"
);
}

View File

@ -4,10 +4,6 @@ mkdir -p ~/.logs/workers
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
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

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