mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 22:46:16 +00:00
refactoring (#116)
- refactoring architecture - fix incomplete ssh logs - code editor for scripts in the app - remove Jobs and SSHCommands
This commit is contained in:
16
app/SSH/Services/ProcessManager/AbstractProcessManager.php
Normal file
16
app/SSH/Services/ProcessManager/AbstractProcessManager.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\SSH\Services\ProcessManager;
|
||||
|
||||
use App\Models\Service;
|
||||
use App\SSH\Services\ServiceInterface;
|
||||
|
||||
abstract class AbstractProcessManager implements ProcessManager, ServiceInterface
|
||||
{
|
||||
protected Service $service;
|
||||
|
||||
public function __construct(Service $service)
|
||||
{
|
||||
$this->service = $service;
|
||||
}
|
||||
}
|
27
app/SSH/Services/ProcessManager/ProcessManager.php
Executable file
27
app/SSH/Services/ProcessManager/ProcessManager.php
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\SSH\Services\ProcessManager;
|
||||
|
||||
interface ProcessManager
|
||||
{
|
||||
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 $logPath): string;
|
||||
}
|
136
app/SSH/Services/ProcessManager/Supervisor.php
Normal file
136
app/SSH/Services/ProcessManager/Supervisor.php
Normal file
@ -0,0 +1,136 @@
|
||||
<?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'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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($user)->exec(
|
||||
$this->getScript('supervisor/create-worker.sh', [
|
||||
'id' => $id,
|
||||
'config' => $this->generateConfigFile(
|
||||
$id,
|
||||
$command,
|
||||
$user,
|
||||
$autoStart,
|
||||
$autoRestart,
|
||||
$numprocs,
|
||||
$logFile
|
||||
),
|
||||
]),
|
||||
'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 $logPath): string
|
||||
{
|
||||
return $this->service->server->ssh()->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,
|
||||
]);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
mkdir -p ~/.logs
|
||||
|
||||
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
|
||||
|
||||
if ! sudo supervisorctl update; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! sudo supervisorctl start __id__:*; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
@ -0,0 +1,20 @@
|
||||
if ! sudo supervisorctl stop __id__:*; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! sudo rm -rf ~/.logs/workers/__id__.log; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
||||
if ! sudo rm -rf /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
|
||||
|
||||
if ! sudo supervisorctl update; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
||||
|
5
app/SSH/Services/ProcessManager/scripts/supervisor/install-supervisor.sh
Executable file
5
app/SSH/Services/ProcessManager/scripts/supervisor/install-supervisor.sh
Executable file
@ -0,0 +1,5 @@
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get install supervisor -y
|
||||
|
||||
sudo service supervisor enable
|
||||
|
||||
sudo service supervisor start
|
@ -0,0 +1,3 @@
|
||||
if ! sudo supervisorctl restart __id__:*; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
@ -0,0 +1,3 @@
|
||||
if ! sudo supervisorctl start __id__:*; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
@ -0,0 +1,3 @@
|
||||
if ! sudo supervisorctl stop __id__:*; then
|
||||
echo 'VITO_SSH_ERROR' && exit 1
|
||||
fi
|
@ -0,0 +1,10 @@
|
||||
[program:__name__]
|
||||
process_name=%(program_name)s_%(process_num)02d
|
||||
command=__command__
|
||||
autostart=__auto_start__
|
||||
autorestart=__auto_restart__
|
||||
user=__user__
|
||||
numprocs=__numprocs__
|
||||
redirect_stderr=true
|
||||
stdout_logfile=__log_file__
|
||||
stopwaitsecs=3600
|
Reference in New Issue
Block a user