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:
@ -2,9 +2,8 @@
|
||||
|
||||
namespace App\ServerTypes;
|
||||
|
||||
use App\Contracts\ServerType;
|
||||
use App\Enums\ServiceStatus;
|
||||
use App\Models\Server;
|
||||
use Closure;
|
||||
|
||||
abstract class AbstractType implements ServerType
|
||||
{
|
||||
@ -15,12 +14,110 @@ public function __construct(Server $server)
|
||||
$this->server = $server;
|
||||
}
|
||||
|
||||
protected function progress(int $percentage, ?string $step = null): Closure
|
||||
public function install(): void
|
||||
{
|
||||
return function () use ($percentage, $step) {
|
||||
$this->server->progress = $percentage;
|
||||
$this->server->progress_step = $step;
|
||||
$this->server->save();
|
||||
};
|
||||
$this->createUser();
|
||||
$this->progress(15, 'installing-updates');
|
||||
$this->server->os()->upgrade();
|
||||
$this->progress(25, 'installing-dependencies');
|
||||
$this->server->os()->installDependencies();
|
||||
$services = $this->server->services;
|
||||
$currentProgress = 45;
|
||||
$progressPerService = (100 - $currentProgress) / count($services);
|
||||
foreach ($services as $service) {
|
||||
$currentProgress += $progressPerService;
|
||||
$this->progress($currentProgress, 'installing- '.$service->name);
|
||||
$service->handler()->install();
|
||||
$service->update(['status' => ServiceStatus::READY]);
|
||||
if ($service->type == 'php') {
|
||||
$this->progress($currentProgress, 'installing-composer');
|
||||
$service->handler()->installComposer();
|
||||
}
|
||||
}
|
||||
$this->progress(100, 'finishing');
|
||||
}
|
||||
|
||||
protected function createUser(): void
|
||||
{
|
||||
$this->server->os()->createUser(
|
||||
$this->server->authentication['user'],
|
||||
$this->server->authentication['pass'],
|
||||
$this->server->sshKey()['public_key']
|
||||
);
|
||||
$this->server->ssh_user = config('core.ssh_user');
|
||||
$this->server->save();
|
||||
$this->server->refresh();
|
||||
$this->server->public_key = $this->server->os()->getPublicKey($this->server->getSshUser());
|
||||
$this->server->save();
|
||||
}
|
||||
|
||||
protected function progress(int $percentage, ?string $step = null): void
|
||||
{
|
||||
$this->server->progress = $percentage;
|
||||
$this->server->progress_step = $step;
|
||||
$this->server->save();
|
||||
}
|
||||
|
||||
protected function addWebserver(string $service): void
|
||||
{
|
||||
if ($service != 'none') {
|
||||
$this->server->services()->create([
|
||||
'type' => 'webserver',
|
||||
'name' => $service,
|
||||
'version' => 'latest',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function addDatabase(string $service): void
|
||||
{
|
||||
if ($service != 'none') {
|
||||
$this->server->services()->create([
|
||||
'type' => 'database',
|
||||
'name' => config('core.databases_name.'.$service),
|
||||
'version' => config('core.databases_version.'.$service),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function addPHP(string $version): void
|
||||
{
|
||||
if ($version != 'none') {
|
||||
$this->server->services()->create([
|
||||
'type' => 'php',
|
||||
'type_data' => [
|
||||
'extensions' => [],
|
||||
],
|
||||
'name' => 'php',
|
||||
'version' => $version,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function addSupervisor(): void
|
||||
{
|
||||
$this->server->services()->create([
|
||||
'type' => 'process_manager',
|
||||
'name' => 'supervisor',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
}
|
||||
|
||||
protected function addRedis(): void
|
||||
{
|
||||
$this->server->services()->create([
|
||||
'type' => 'memory_database',
|
||||
'name' => 'redis',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
}
|
||||
|
||||
protected function addUfw(): void
|
||||
{
|
||||
$this->server->services()->create([
|
||||
'type' => 'firewall',
|
||||
'name' => 'ufw',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -2,19 +2,9 @@
|
||||
|
||||
namespace App\ServerTypes;
|
||||
|
||||
use App\Facades\Notifier;
|
||||
use App\Jobs\Installation\Initialize;
|
||||
use App\Jobs\Installation\InstallRequirements;
|
||||
use App\Jobs\Installation\Upgrade;
|
||||
use App\Notifications\ServerInstallationFailed;
|
||||
use App\Notifications\ServerInstallationSucceed;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class Database extends AbstractType
|
||||
{
|
||||
public function createValidationRules(array $input): array
|
||||
public function createRules(array $input): array
|
||||
{
|
||||
return [
|
||||
'database' => [
|
||||
@ -32,99 +22,9 @@ public function data(array $input): array
|
||||
public function createServices(array $input): void
|
||||
{
|
||||
$this->server->services()->forceDelete();
|
||||
|
||||
$this->addDatabase($input['database']);
|
||||
$this->addSupervisor();
|
||||
$this->addRedis();
|
||||
$this->addUfw();
|
||||
}
|
||||
|
||||
public function install(): void
|
||||
{
|
||||
$jobs = [
|
||||
new Initialize($this->server, $this->server->ssh_user, $this->server->provider === 'custom'),
|
||||
$this->progress(15, 'Installing Updates'),
|
||||
new Upgrade($this->server),
|
||||
$this->progress(25, 'Installing Requirements'),
|
||||
new InstallRequirements($this->server),
|
||||
];
|
||||
|
||||
$services = $this->server->services;
|
||||
$currentProgress = 25;
|
||||
$progressPerService = (100 - $currentProgress) / count($services);
|
||||
foreach ($services as $service) {
|
||||
$currentProgress += $progressPerService;
|
||||
$jobs[] = $this->progress($currentProgress, 'Installing '.$service->name);
|
||||
$jobs[] = $service->installer();
|
||||
}
|
||||
|
||||
$jobs[] = function () {
|
||||
$this->server->update([
|
||||
'status' => 'ready',
|
||||
'progress' => 100,
|
||||
]);
|
||||
Notifier::send($this->server, new ServerInstallationSucceed($this->server));
|
||||
};
|
||||
|
||||
Bus::chain($jobs)
|
||||
->catch(function (Throwable $e) {
|
||||
$this->server->update([
|
||||
'status' => 'installation_failed',
|
||||
]);
|
||||
Notifier::send($this->server, new ServerInstallationFailed($this->server));
|
||||
Log::error('server-installation-error', [
|
||||
'error' => (string) $e,
|
||||
]);
|
||||
throw $e;
|
||||
})
|
||||
->onConnection('ssh-long')
|
||||
->dispatch();
|
||||
}
|
||||
|
||||
protected function addDatabase(string $service): void
|
||||
{
|
||||
if ($service != 'none') {
|
||||
$this->server->services()->create([
|
||||
'type' => 'database',
|
||||
'name' => config('core.databases_name.'.$service),
|
||||
'version' => config('core.databases_version.'.$service),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* add supervisor
|
||||
*/
|
||||
protected function addSupervisor(): void
|
||||
{
|
||||
$this->server->services()->create([
|
||||
'type' => 'process_manager',
|
||||
'name' => 'supervisor',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* add supervisor
|
||||
*/
|
||||
protected function addRedis(): void
|
||||
{
|
||||
$this->server->services()->create([
|
||||
'type' => 'memory_database',
|
||||
'name' => 'redis',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* add supervisor
|
||||
*/
|
||||
protected function addUfw(): void
|
||||
{
|
||||
$this->server->services()->create([
|
||||
'type' => 'firewall',
|
||||
'name' => 'ufw',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -2,23 +2,9 @@
|
||||
|
||||
namespace App\ServerTypes;
|
||||
|
||||
use App\Enums\ServerStatus;
|
||||
use App\Facades\Notifier;
|
||||
use App\Jobs\Installation\Initialize;
|
||||
use App\Jobs\Installation\InstallCertbot;
|
||||
use App\Jobs\Installation\InstallComposer;
|
||||
use App\Jobs\Installation\InstallNodejs;
|
||||
use App\Jobs\Installation\InstallRequirements;
|
||||
use App\Jobs\Installation\Upgrade;
|
||||
use App\Notifications\ServerInstallationFailed;
|
||||
use App\Notifications\ServerInstallationSucceed;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class Regular extends AbstractType
|
||||
{
|
||||
public function createValidationRules(array $input): array
|
||||
public function createRules(array $input): array
|
||||
{
|
||||
return [
|
||||
'webserver' => [
|
||||
@ -44,7 +30,6 @@ public function data(array $input): array
|
||||
public function createServices(array $input): void
|
||||
{
|
||||
$this->server->services()->forceDelete();
|
||||
|
||||
$this->addWebserver($input['webserver']);
|
||||
$this->addDatabase($input['database']);
|
||||
$this->addPHP($input['php']);
|
||||
@ -52,126 +37,4 @@ public function createServices(array $input): void
|
||||
$this->addRedis();
|
||||
$this->addUfw();
|
||||
}
|
||||
|
||||
public function install(): void
|
||||
{
|
||||
$jobs = [
|
||||
new Initialize($this->server, $this->server->ssh_user),
|
||||
$this->progress(15, 'Installing Updates'),
|
||||
new Upgrade($this->server),
|
||||
$this->progress(25, 'Installing Requirements'),
|
||||
new InstallRequirements($this->server),
|
||||
$this->progress(35, 'Installing Node.js'),
|
||||
new InstallNodejs($this->server),
|
||||
$this->progress(45, 'Installing Certbot'),
|
||||
new InstallCertbot($this->server),
|
||||
];
|
||||
|
||||
$services = $this->server->services;
|
||||
$currentProgress = 45;
|
||||
$progressPerService = (100 - $currentProgress) / count($services);
|
||||
foreach ($services as $service) {
|
||||
$currentProgress += $progressPerService;
|
||||
$jobs[] = $this->progress($currentProgress, 'Installing '.$service->name);
|
||||
$jobs[] = $service->installer();
|
||||
if ($service->type == 'php') {
|
||||
$jobs[] = $this->progress($currentProgress, 'Installing Composer');
|
||||
$jobs[] = new InstallComposer($this->server);
|
||||
}
|
||||
}
|
||||
|
||||
$jobs[] = function () {
|
||||
$this->server->update([
|
||||
'status' => ServerStatus::READY,
|
||||
'progress' => 100,
|
||||
]);
|
||||
Notifier::send($this->server, new ServerInstallationSucceed($this->server));
|
||||
};
|
||||
|
||||
Bus::chain($jobs)
|
||||
->catch(function (Throwable $e) {
|
||||
$this->server->update([
|
||||
'status' => 'installation_failed',
|
||||
]);
|
||||
Notifier::send($this->server, new ServerInstallationFailed($this->server));
|
||||
Log::error('server-installation-error', [
|
||||
'error' => (string) $e,
|
||||
]);
|
||||
throw $e;
|
||||
})
|
||||
->onConnection('ssh-long')
|
||||
->dispatch();
|
||||
}
|
||||
|
||||
protected function addWebserver(string $service): void
|
||||
{
|
||||
if ($service != 'none') {
|
||||
$this->server->services()->create([
|
||||
'type' => 'webserver',
|
||||
'name' => $service,
|
||||
'version' => 'latest',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function addDatabase(string $service): void
|
||||
{
|
||||
if ($service != 'none') {
|
||||
$this->server->services()->create([
|
||||
'type' => 'database',
|
||||
'name' => config('core.databases_name.'.$service),
|
||||
'version' => config('core.databases_version.'.$service),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function addPHP(string $version): void
|
||||
{
|
||||
if ($version != 'none') {
|
||||
$this->server->services()->create([
|
||||
'type' => 'php',
|
||||
'type_data' => [
|
||||
'extensions' => [],
|
||||
],
|
||||
'name' => 'php',
|
||||
'version' => $version,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* add supervisor
|
||||
*/
|
||||
protected function addSupervisor(): void
|
||||
{
|
||||
$this->server->services()->create([
|
||||
'type' => 'process_manager',
|
||||
'name' => 'supervisor',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* add redis
|
||||
*/
|
||||
protected function addRedis(): void
|
||||
{
|
||||
$this->server->services()->create([
|
||||
'type' => 'memory_database',
|
||||
'name' => 'redis',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* add ufw
|
||||
*/
|
||||
protected function addUfw(): void
|
||||
{
|
||||
$this->server->services()->create([
|
||||
'type' => 'firewall',
|
||||
'name' => 'ufw',
|
||||
'version' => 'latest',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
14
app/ServerTypes/ServerType.php
Executable file
14
app/ServerTypes/ServerType.php
Executable file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\ServerTypes;
|
||||
|
||||
interface ServerType
|
||||
{
|
||||
public function createRules(array $input): array;
|
||||
|
||||
public function data(array $input): array;
|
||||
|
||||
public function createServices(array $input): void;
|
||||
|
||||
public function install(): void;
|
||||
}
|
Reference in New Issue
Block a user