mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-07 17:02:34 +00:00
Plugins base (#613)
* wip * wip * cleanup * notification channels * phpstan * services * remove server types * refactoring * refactoring
This commit is contained in:
@ -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.'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user