Plugins base (#613)

* wip

* wip

* cleanup

* notification channels

* phpstan

* services

* remove server types

* refactoring

* refactoring
This commit is contained in:
Saeed Vaziry
2025-06-14 14:35:18 +02:00
committed by GitHub
parent adc0653d15
commit 131b828807
311 changed files with 3976 additions and 2660 deletions

View File

@ -0,0 +1,38 @@
<?php
namespace App\Services\ProcessManager;
use App\Services\AbstractService;
use Closure;
abstract class AbstractProcessManager extends AbstractService implements ProcessManager
{
public function creationRules(array $input): array
{
return [
'type' => [
'required',
function (string $attribute, mixed $value, Closure $fail): void {
$processManagerExists = $this->service->server->processManager();
if ($processManagerExists) {
$fail('You already have a process manager service on the server.');
}
},
],
];
}
public function deletionRules(): array
{
return [
'service' => [
function (string $attribute, mixed $value, Closure $fail): void {
$hasWorker = $this->service->server->workers()->exists();
if ($hasWorker) {
$fail('You have worker(s) on the server.');
}
},
],
];
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Services\ProcessManager;
use App\Services\ServiceInterface;
interface ProcessManager extends ServiceInterface
{
public function create(
int $id,
string $command,
string $user,
bool $autoStart,
bool $autoRestart,
int $numprocs,
string $logFile,
?int $siteId = null
): void;
public function delete(int $id, ?int $siteId = null): void;
public function restart(int $id, ?int $siteId = null): void;
public function stop(int $id, ?int $siteId = null): void;
public function start(int $id, ?int $siteId = null): void;
public function getLogs(string $user, string $logPath): string;
}

View File

@ -0,0 +1,152 @@
<?php
namespace App\Services\ProcessManager;
use App\Exceptions\SSHError;
use Throwable;
class Supervisor extends AbstractProcessManager
{
public static function id(): string
{
return 'supervisor';
}
public static function type(): string
{
return 'process_manager';
}
public function unit(): string
{
return 'supervisor';
}
/**
* @throws SSHError
*/
public function install(): void
{
$this->service->server->ssh()->exec(
view('ssh.services.process-manager.supervisor.install-supervisor'),
'install-supervisor'
);
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function uninstall(): void
{
$this->service->server->ssh()->exec(
view('ssh.services.process-manager.supervisor.uninstall-supervisor'),
'uninstall-supervisor'
);
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function create(
int $id,
string $command,
string $user,
bool $autoStart,
bool $autoRestart,
int $numprocs,
string $logFile,
?int $siteId = null
): void {
$this->service->server->ssh()->write(
"/etc/supervisor/conf.d/$id.conf",
view('ssh.services.process-manager.supervisor.worker', [
'name' => (string) $id,
'command' => $command,
'user' => $user,
'autoStart' => var_export($autoStart, true),
'autoRestart' => var_export($autoRestart, true),
'numprocs' => (string) $numprocs,
'logFile' => $logFile,
]),
'root'
);
$this->service->server->ssh()->exec(
view('ssh.services.process-manager.supervisor.create-worker', [
'id' => $id,
'logFile' => $logFile,
'user' => $user,
]),
'create-worker',
$siteId
);
}
/**
* @throws Throwable
*/
public function delete(int $id, ?int $siteId = null): void
{
$this->service->server->ssh()->exec(
view('ssh.services.process-manager.supervisor.delete-worker', [
'id' => $id,
]),
'delete-worker',
$siteId
);
}
/**
* @throws Throwable
*/
public function restart(int $id, ?int $siteId = null): void
{
$this->service->server->ssh()->exec(
view('ssh.services.process-manager.supervisor.restart-worker', [
'id' => $id,
]),
'restart-worker',
$siteId
);
}
/**
* @throws Throwable
*/
public function stop(int $id, ?int $siteId = null): void
{
$this->service->server->ssh()->exec(
view('ssh.services.process-manager.supervisor.stop-worker', [
'id' => $id,
]),
'stop-worker',
$siteId
);
}
/**
* @throws Throwable
*/
public function start(int $id, ?int $siteId = null): void
{
$this->service->server->ssh()->exec(
view('ssh.services.process-manager.supervisor.start-worker', [
'id' => $id,
]),
'start-worker',
$siteId
);
}
/**
* @throws Throwable
*/
public function getLogs(string $user, string $logPath): string
{
return $this->service->server->ssh($user)->exec(
"tail -100 $logPath"
);
}
}