Skip to content

Commit

Permalink
fix create queue with root user (#409)
Browse files Browse the repository at this point in the history
* fix create queue with root user

* fix

* fix queues for root user
  • Loading branch information
saeedvaziry authored Dec 30, 2024
1 parent dfdd50b commit 8b86ff2
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 35 deletions.
4 changes: 0 additions & 4 deletions app/Actions/Queue/DeleteQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion app/Actions/Queue/GetQueueLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
38 changes: 35 additions & 3 deletions app/Helpers/SSH.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
*/
Expand Down
12 changes: 11 additions & 1 deletion app/Models/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
});
}

Expand All @@ -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';
}

Expand Down
6 changes: 5 additions & 1 deletion app/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion app/SSH/Services/ProcessManager/ProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
26 changes: 15 additions & 11 deletions app/SSH/Services/ProcessManager/Supervisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions app/Web/Pages/Servers/Sites/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 8b86ff2

Please sign in to comment.