Plugins base (#613)

* wip

* wip

* cleanup

* notification channels

* phpstan

* services

* remove server types

* refactoring

* refactoring
This commit is contained in:
Saeed Vaziry
2025-06-14 14:35:18 +02:00
committed by GitHub
parent adc0653d15
commit 131b828807
311 changed files with 3976 additions and 2660 deletions

View File

@ -17,7 +17,12 @@ public function install(Server $server, array $input): Service
{
Validator::make($input, self::rules($input))->validate();
$input['type'] = config('core.service_types')[$input['name']];
$name = $input['name'];
$input['type'] = config("service.services.$name.type");
if (! $input['type']) {
throw new \InvalidArgumentException("Service type is not defined for $name");
}
$service = new Service([
'server_id' => $server->id,
@ -55,14 +60,14 @@ public static function rules(array $input): array
$rules = [
'name' => [
'required',
Rule::in(array_keys(config('core.service_types'))),
Rule::in(array_keys(config('service.services'))),
],
'version' => [
'required',
],
];
if (isset($input['name'])) {
$rules['version'][] = Rule::in(config("core.service_versions.{$input['name']}"));
$rules['version'][] = Rule::in(config("service.services.{$input['name']}.versions", []));
}
return $rules;

View File

@ -4,15 +4,17 @@
use App\Enums\ServiceStatus;
use App\Models\Service;
use Illuminate\Validation\ValidationException;
class Manage
{
public function start(Service $service): void
{
$this->validate($service);
$service->status = ServiceStatus::STARTING;
$service->save();
dispatch(function () use ($service): void {
$status = $service->server->systemd()->start($service->unit);
$status = $service->server->systemd()->start($service->handler()->unit());
if (str($status)->contains('Active: active')) {
$service->status = ServiceStatus::READY;
} else {
@ -24,10 +26,11 @@ public function start(Service $service): void
public function stop(Service $service): void
{
$this->validate($service);
$service->status = ServiceStatus::STOPPING;
$service->save();
dispatch(function () use ($service): void {
$status = $service->server->systemd()->stop($service->unit);
$status = $service->server->systemd()->stop($service->handler()->unit());
if (str($status)->contains('Active: inactive')) {
$service->status = ServiceStatus::STOPPED;
} else {
@ -39,10 +42,11 @@ public function stop(Service $service): void
public function restart(Service $service): void
{
$this->validate($service);
$service->status = ServiceStatus::RESTARTING;
$service->save();
dispatch(function () use ($service): void {
$status = $service->server->systemd()->restart($service->unit);
$status = $service->server->systemd()->restart($service->handler()->unit());
if (str($status)->contains('Active: active')) {
$service->status = ServiceStatus::READY;
} else {
@ -54,10 +58,11 @@ public function restart(Service $service): void
public function enable(Service $service): void
{
$this->validate($service);
$service->status = ServiceStatus::ENABLING;
$service->save();
dispatch(function () use ($service): void {
$status = $service->server->systemd()->enable($service->unit);
$status = $service->server->systemd()->enable($service->handler()->unit());
if (str($status)->contains('Active: active')) {
$service->status = ServiceStatus::READY;
} else {
@ -69,10 +74,11 @@ public function enable(Service $service): void
public function disable(Service $service): void
{
$this->validate($service);
$service->status = ServiceStatus::DISABLING;
$service->save();
dispatch(function () use ($service): void {
$status = $service->server->systemd()->disable($service->unit);
$status = $service->server->systemd()->disable($service->handler()->unit());
if (str($status)->contains('Active: inactive')) {
$service->status = ServiceStatus::DISABLED;
} else {
@ -81,4 +87,13 @@ public function disable(Service $service): void
$service->save();
})->onConnection('ssh');
}
private function validate(Service $service): void
{
if (! $service->handler()->unit()) {
throw ValidationException::withMessages([
'service' => __('This service does not have a systemd unit configured.'),
]);
}
}
}