refactoring (#116)

- refactoring architecture
- fix incomplete ssh logs
- code editor for scripts in the app
- remove Jobs and SSHCommands
This commit is contained in:
Saeed Vaziry
2024-03-14 20:03:43 +01:00
committed by GitHub
parent cee4a70c3c
commit 428140b931
472 changed files with 24110 additions and 8159 deletions

View File

@ -2,17 +2,16 @@
namespace App\Models;
use App\Contracts\Database;
use App\Contracts\Firewall;
use App\Contracts\ProcessManager;
use App\Contracts\Webserver;
use App\Enums\ServiceStatus;
use App\Exceptions\InstallationFailed;
use App\Jobs\Service\Manage;
use App\ServiceHandlers\PHP;
use App\Actions\Service\Manage;
use App\Exceptions\ServiceInstallationFailed;
use App\SSH\Services\Database\Database as DatabaseHandler;
use App\SSH\Services\Firewall\Firewall as FirewallHandler;
use App\SSH\Services\PHP\PHP as PHPHandler;
use App\SSH\Services\ProcessManager\ProcessManager as ProcessManagerHandler;
use App\SSH\Services\Redis\Redis as RedisHandler;
use App\SSH\Services\Webserver\Webserver as WebserverHandler;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Str;
/**
@ -49,134 +48,49 @@ class Service extends AbstractModel
'is_default' => 'boolean',
];
public static function boot(): void
{
parent::boot();
static::creating(function (Service $service) {
$service->unit = config('core.service_units')[$service->name][$service->server->os][$service->version];
});
}
public function server(): BelongsTo
{
return $this->belongsTo(Server::class);
}
public function handler(): Database|Firewall|Webserver|PHP|ProcessManager
{
public function handler(
): PHPHandler|WebserverHandler|DatabaseHandler|FirewallHandler|ProcessManagerHandler|RedisHandler {
$handler = config('core.service_handlers')[$this->name];
return new $handler($this);
}
public function installer(): mixed
{
$installer = config('core.service_installers')[$this->name];
return new $installer($this);
}
public function uninstaller(): mixed
{
$uninstaller = config('core.service_uninstallers')[$this->name];
return new $uninstaller($this);
}
public function getUnitAttribute($value): ?string
{
if ($value) {
return $value;
}
if (isset(config('core.service_units')[$this->name])) {
$value = config('core.service_units')[$this->name][$this->server->os][$this->version];
if ($value) {
$this->fill(['unit' => $value]);
$this->save();
}
}
return $value;
}
public function install(): void
{
Bus::chain([
$this->installer(),
])->onConnection('ssh-long')->dispatch();
}
/**
* @throws InstallationFailed
* @throws ServiceInstallationFailed
*/
public function validateInstall($result): void
{
if (! Str::contains($result, 'Active: active')) {
throw new InstallationFailed();
throw new ServiceInstallationFailed();
}
}
public function uninstall(): void
{
$this->status = ServiceStatus::UNINSTALLING;
$this->save();
Bus::chain([
$this->uninstaller(),
function () {
$this->delete();
},
])->catch(function () {
$this->status = ServiceStatus::FAILED;
$this->save();
})->onConnection('ssh')->dispatch();
}
public function start(): void
{
$this->action(
'start',
ServiceStatus::STARTING,
ServiceStatus::READY,
ServiceStatus::STOPPED,
__('Failed to start')
);
app(Manage::class)->start($this);
}
public function stop(): void
{
$this->action(
'stop',
ServiceStatus::STOPPING,
ServiceStatus::STOPPED,
ServiceStatus::FAILED,
__('Failed to stop')
);
app(Manage::class)->stop($this);
}
public function restart(): void
{
$this->action(
'restart',
ServiceStatus::RESTARTING,
ServiceStatus::READY,
ServiceStatus::FAILED,
__('Failed to restart')
);
}
public function action(
string $type,
string $status,
string $successStatus,
string $failStatus,
string $failMessage
): void {
$this->status = $status;
$this->save();
dispatch(new Manage($this, $type, $successStatus, $failStatus, $failMessage))
->onConnection('ssh');
}
public function installedVersions(): array
{
$versions = [];
$services = $this->server->services()->where('type', $this->type)->get(['version']);
foreach ($services as $service) {
$versions[] = $service->version;
}
return $versions;
app(Manage::class)->restart($this);
}
}