vito/app/ServerTypes/Database.php
2024-03-06 20:33:33 +01:00

131 lines
3.5 KiB
PHP
Executable File

<?php
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
{
return [
'database' => [
'required',
'in:'.implode(',', config('core.databases')),
],
];
}
public function data(array $input): array
{
return [];
}
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',
]);
}
}