add enable/disable services

This commit is contained in:
Saeed Vaziry 2024-03-16 14:02:28 +01:00
parent 7a6dcb5654
commit 77d6914cd9
17 changed files with 219 additions and 24 deletions

View File

@ -51,4 +51,34 @@ public function restart(Service $service): void
$service->save();
})->onConnection('ssh');
}
public function enable(Service $service): void
{
$service->status = ServiceStatus::ENABLING;
$service->save();
dispatch(function () use ($service) {
$status = $service->server->systemd()->enable($service->unit);
if (str($status)->contains('Active: active')) {
$service->status = ServiceStatus::READY;
} else {
$service->status = ServiceStatus::FAILED;
}
$service->save();
})->onConnection('ssh');
}
public function disable(Service $service): void
{
$service->status = ServiceStatus::DISABLING;
$service->save();
dispatch(function () use ($service) {
$status = $service->server->systemd()->disable($service->unit);
if (str($status)->contains('Active: inactive')) {
$service->status = ServiceStatus::DISABLED;
} else {
$service->status = ServiceStatus::FAILED;
}
$service->save();
})->onConnection('ssh');
}
}

View File

@ -23,4 +23,10 @@ final class ServiceStatus extends Enum
const RESTARTING = 'restarting';
const STOPPED = 'stopped';
const ENABLING = 'enabling';
const DISABLING = 'disabling';
const DISABLED = 'disabled';
}

View File

@ -44,4 +44,22 @@ public function restart(Server $server, Service $service): RedirectResponse
return back();
}
public function enable(Server $server, Service $service): RedirectResponse
{
$service->enable();
Toast::success('Service is being enabled!');
return back();
}
public function disable(Server $server, Service $service): RedirectResponse
{
$service->disable();
Toast::success('Service is being disabled!');
return back();
}
}

View File

@ -93,4 +93,14 @@ public function restart(): void
{
app(Manage::class)->restart($this);
}
public function enable(): void
{
app(Manage::class)->enable($this);
}
public function disable(): void
{
app(Manage::class)->disable($this);
}
}

View File

@ -13,7 +13,7 @@ public function __construct(protected Server $server)
public function status(string $unit): string
{
$command = <<<EOD
sudo service $unit status | cat
sudo systemctl status $unit | cat
EOD;
return $this->server->ssh()->exec($command, sprintf('status-%s', $unit));
@ -22,8 +22,8 @@ public function status(string $unit): string
public function start(string $unit): string
{
$command = <<<EOD
sudo service $unit start
sudo service $unit status | cat
sudo systemctl start $unit
sudo systemctl status $unit | cat
EOD;
return $this->server->ssh()->exec($command, sprintf('start-%s', $unit));
@ -32,8 +32,8 @@ public function start(string $unit): string
public function stop(string $unit): string
{
$command = <<<EOD
sudo service $unit stop
sudo service $unit status | cat
sudo systemctl stop $unit
sudo systemctl status $unit | cat
EOD;
return $this->server->ssh()->exec($command, sprintf('stop-%s', $unit));
@ -42,10 +42,32 @@ public function stop(string $unit): string
public function restart(string $unit): string
{
$command = <<<EOD
sudo service $unit restart
sudo service $unit status | cat
sudo systemctl restart $unit
sudo systemctl status $unit | cat
EOD;
return $this->server->ssh()->exec($command, sprintf('restart-%s', $unit));
}
public function enable(string $unit): string
{
$command = <<<EOD
sudo systemctl start $unit
sudo systemctl enable $unit
sudo systemctl status $unit | cat
EOD;
return $this->server->ssh()->exec($command, sprintf('enable-%s', $unit));
}
public function disable(string $unit): string
{
$command = <<<EOD
sudo systemctl stop $unit
sudo systemctl disable $unit
sudo systemctl status $unit | cat
EOD;
return $this->server->ssh()->exec($command, sprintf('disable-%s', $unit));
}
}

View File

@ -16,7 +16,7 @@ public function up(): void
$table->json('type_data')->nullable();
$table->string('name');
$table->string('version');
$table->enum('status', ServiceStatus::getValues())->default(ServiceStatus::INSTALLING);
$table->string('status')->default(ServiceStatus::INSTALLING);
$table->boolean('is_default')->default(1);
$table->string('unit')->nullable();
$table->timestamps();

View File

@ -1,6 +1,5 @@
<?php
use App\Enums\DeploymentStatus;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@ -16,7 +15,7 @@ public function up(): void
$table->unsignedInteger('log_id')->nullable();
$table->json('commit_data')->nullable();
$table->string('commit_id')->nullable();
$table->enum('status', DeploymentStatus::getValues());
$table->string('status');
$table->timestamps();
});
}

View File

@ -13,7 +13,7 @@ public function up(): void
$table->id();
$table->unsignedBigInteger('server_id');
$table->string('name');
$table->enum('status', DatabaseStatus::getValues())->default(DatabaseStatus::CREATING);
$table->string('status')->default(DatabaseStatus::CREATING);
$table->timestamps();
});
}

View File

@ -16,7 +16,7 @@ public function up(): void
$table->longText('password')->nullable();
$table->json('databases')->nullable();
$table->string('host')->default('localhost');
$table->enum('status', DatabaseUserStatus::getValues())->default(DatabaseUserStatus::CREATING);
$table->string('status')->default(DatabaseUserStatus::CREATING);
$table->timestamps();
});
}

View File

@ -1,6 +1,5 @@
<?php
use App\Enums\CronjobStatus;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@ -16,7 +15,7 @@ public function up(): void
$table->string('user');
$table->string('frequency');
$table->boolean('hidden')->default(0);
$table->enum('status', CronjobStatus::getValues());
$table->string('status');
$table->timestamps();
});
}

View File

@ -1,6 +1,5 @@
<?php
use App\Enums\SslStatus;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@ -18,7 +17,7 @@ public function up(): void
$table->longText('pk')->nullable();
$table->longText('ca')->nullable();
$table->timestamp('expires_at');
$table->enum('status', SslStatus::getValues());
$table->string('status');
$table->timestamps();
});
}

View File

@ -1,6 +1,5 @@
<?php
use App\Enums\QueueStatus;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@ -20,7 +19,7 @@ public function up(): void
$table->tinyInteger('numprocs')->default(8);
$table->boolean('redirect_stderr')->default(1);
$table->string('stdout_logfile')->nullable();
$table->enum('status', QueueStatus::getValues());
$table->string('status');
$table->timestamps();
});
}

View File

@ -1,6 +1,5 @@
<?php
use App\Enums\SshKeyStatus;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@ -13,7 +12,7 @@ public function up(): void
$table->id();
$table->unsignedBigInteger('server_id');
$table->unsignedBigInteger('ssh_key_id');
$table->enum('status', SshKeyStatus::getValues());
$table->string('status');
$table->timestamps();
});
}

View File

@ -54,6 +54,22 @@ class="cursor-pointer"
>
{{ __("Restart") }}
</x-dropdown-link>
@if ($service->status == \App\Enums\ServiceStatus::DISABLED)
<x-dropdown-link
class="cursor-pointer"
href="{{ route('servers.services.enable', ['server' => $server, 'service' => $service]) }}"
>
{{ __("Enable") }}
</x-dropdown-link>
@endif
<x-dropdown-link
class="cursor-pointer"
href="{{ route('servers.services.disable', ['server' => $server, 'service' => $service]) }}"
>
{{ __("Disable") }}
</x-dropdown-link>
@endif
</x-slot>
</x-dropdown>

View File

@ -33,3 +33,15 @@
@if ($status == \App\Enums\ServiceStatus::STOPPED)
<x-status status="danger">{{ $status }}</x-status>
@endif
@if ($status == \App\Enums\ServiceStatus::ENABLING)
<x-status status="warning">{{ $status }}</x-status>
@endif
@if ($status == \App\Enums\ServiceStatus::DISABLING)
<x-status status="warning">{{ $status }}</x-status>
@endif
@if ($status == \App\Enums\ServiceStatus::DISABLED)
<x-status status="disabled">{{ $status }}</x-status>
@endif

View File

@ -119,6 +119,8 @@
Route::get('/{server}/services/{service}/start', [ServiceController::class, 'start'])->name('servers.services.start');
Route::get('/{server}/services/{service}/stop', [ServiceController::class, 'stop'])->name('servers.services.stop');
Route::get('/{server}/services/{service}/restart', [ServiceController::class, 'restart'])->name('servers.services.restart');
Route::get('/{server}/services/{service}/enable', [ServiceController::class, 'enable'])->name('servers.services.enable');
Route::get('/{server}/services/{service}/disable', [ServiceController::class, 'disable'])->name('servers.services.disable');
});
// settings

View File

@ -44,6 +44,27 @@ public function test_restart_service(string $name): void
$this->assertEquals(ServiceStatus::READY, $service->status);
}
/**
* @dataProvider data
*/
public function test_failed_to_restart_service(string $name): void
{
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
SSH::fake('Active: inactive');
$this->get(route('servers.services.restart', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
$service->refresh();
$this->assertEquals(ServiceStatus::FAILED, $service->status);
}
/**
* @dataProvider data
*/
@ -65,6 +86,27 @@ public function test_stop_service(string $name): void
$this->assertEquals(ServiceStatus::STOPPED, $service->status);
}
/**
* @dataProvider data
*/
public function test_failed_to_stop_service(string $name): void
{
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
SSH::fake('Active: active');
$this->get(route('servers.services.stop', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
$service->refresh();
$this->assertEquals(ServiceStatus::FAILED, $service->status);
}
/**
* @dataProvider data
*/
@ -110,7 +152,28 @@ public function test_failed_to_start_service(string $name): void
/**
* @dataProvider data
*/
public function test_failed_to_restart_service(string $name): void
public function test_enable_service(string $name): void
{
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
SSH::fake('Active: active');
$this->get(route('servers.services.enable', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
$service->refresh();
$this->assertEquals(ServiceStatus::READY, $service->status);
}
/**
* @dataProvider data
*/
public function test_failed_to_enable_service(string $name): void
{
$this->actingAs($this->user);
@ -118,7 +181,7 @@ public function test_failed_to_restart_service(string $name): void
SSH::fake('Active: inactive');
$this->get(route('servers.services.restart', [
$this->get(route('servers.services.enable', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
@ -131,7 +194,28 @@ public function test_failed_to_restart_service(string $name): void
/**
* @dataProvider data
*/
public function test_failed_to_stop_service(string $name): void
public function test_disable_service(string $name): void
{
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
SSH::fake('Active: inactive');
$this->get(route('servers.services.disable', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
$service->refresh();
$this->assertEquals(ServiceStatus::DISABLED, $service->status);
}
/**
* @dataProvider data
*/
public function test_failed_to_disable_service(string $name): void
{
$this->actingAs($this->user);
@ -139,7 +223,7 @@ public function test_failed_to_stop_service(string $name): void
SSH::fake('Active: active');
$this->get(route('servers.services.stop', [
$this->get(route('servers.services.disable', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();