Richard Anderson c1ae58772c
Isolate Users (#431)
* WIP to isolate users

* Resolved issue with SSH AsUser

Updated Isolated User Script to use Server User for Team Access
Updated Path creation script to simplify for running as the isolated user

* Included the server user

* PHPMyAdmin script updated

Wordpress Script Updated
Updated Execute Script to support executing as isolated users

* Issue Resolution & Resolved Failing Unit Tests

* Fix for isolated_username vs user

* Run the deploy as the isolated user

* queue updates for isolated user

* Support isolated users in cronjobs

* script tests for isolated users

* Queue tests for isolated users

* Cronjob tests for isolated user

* Removed default queue command for laravel apps

* add default user to factory

* laravel pint fixes

* ensure echos are consistent

* removed unneeded parameter

* update

* fix queues for isolated users

* revert addslashes

---------

Co-authored-by: Saeed Vaziry <mr.saeedvaziry@gmail.com>
2025-01-18 01:17:48 +01:00

153 lines
3.6 KiB
PHP

<?php
namespace App\SSH\Services\ProcessManager;
use App\SSH\HasScripts;
use Throwable;
class Supervisor extends AbstractProcessManager
{
use HasScripts;
public function install(): void
{
$this->service->server->ssh()->exec(
$this->getScript('supervisor/install-supervisor.sh'),
'install-supervisor'
);
$this->service->server->os()->cleanup();
}
public function uninstall(): void
{
$this->service->server->ssh()->exec(
$this->getScript('supervisor/uninstall-supervisor.sh'),
'uninstall-supervisor'
);
$this->service->server->os()->cleanup();
}
/**
* @throws Throwable
*/
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",
$this->generateConfigFile(
$id,
$command,
$user,
$autoStart,
$autoRestart,
$numprocs,
$logFile
),
true
);
$this->service->server->ssh()->exec(
$this->getScript('supervisor/create-worker.sh', [
'id' => $id,
'log_file' => $logFile,
'user' => $user,
]),
'create-worker',
$siteId
);
}
/**
* @throws Throwable
*/
public function delete(int $id, ?int $siteId = null): void
{
$this->service->server->ssh()->exec(
$this->getScript('supervisor/delete-worker.sh', [
'id' => $id,
]),
'delete-worker',
$siteId
);
}
/**
* @throws Throwable
*/
public function restart(int $id, ?int $siteId = null): void
{
$this->service->server->ssh()->exec(
$this->getScript('supervisor/restart-worker.sh', [
'id' => $id,
]),
'restart-worker',
$siteId
);
}
/**
* @throws Throwable
*/
public function stop(int $id, ?int $siteId = null): void
{
$this->service->server->ssh()->exec(
$this->getScript('supervisor/stop-worker.sh', [
'id' => $id,
]),
'stop-worker',
$siteId
);
}
/**
* @throws Throwable
*/
public function start(int $id, ?int $siteId = null): void
{
$this->service->server->ssh()->exec(
$this->getScript('supervisor/start-worker.sh', [
'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"
);
}
private function generateConfigFile(
int $id,
string $command,
string $user,
bool $autoStart,
bool $autoRestart,
int $numprocs,
string $logFile
): string {
return $this->getScript('supervisor/worker.conf', [
'name' => (string) $id,
'command' => $command,
'user' => $user,
'auto_start' => var_export($autoStart, true),
'auto_restart' => var_export($autoRestart, true),
'numprocs' => (string) $numprocs,
'log_file' => $logFile,
]);
}
}