diff --git a/app/Actions/Database/InstallPHPMyAdmin.php b/app/Actions/Service/InstallPHPMyAdmin.php similarity index 70% rename from app/Actions/Database/InstallPHPMyAdmin.php rename to app/Actions/Service/InstallPHPMyAdmin.php index 34c1b68..c71aa28 100644 --- a/app/Actions/Database/InstallPHPMyAdmin.php +++ b/app/Actions/Service/InstallPHPMyAdmin.php @@ -1,7 +1,8 @@ defaultService('phpmyadmin'); if ($phpMyAdmin) { - if ($phpMyAdmin->status === 'ready') { - throw ValidationException::withMessages([ - 'install' => __('Already installed'), - ])->errorBag('installPHPMyAdmin'); - } - $phpMyAdmin->delete(); + throw ValidationException::withMessages([ + 'allowed_ip' => __('Already installed'), + ]); } $phpMyAdmin = new Service([ 'server_id' => $server->id, 'type' => 'phpmyadmin', 'type_data' => [ 'allowed_ip' => $input['allowed_ip'], + 'port' => $input['port'], 'php' => $server->defaultService('php')->version, ], 'name' => 'phpmyadmin', 'version' => '5.1.2', - 'status' => 'installing', + 'status' => ServiceStatus::INSTALLING, 'is_default' => 1, ]); $phpMyAdmin->save(); @@ -50,6 +49,12 @@ private function validate(array $input): void { Validator::make($input, [ 'allowed_ip' => 'required', - ])->validateWithBag('installPHPMyAdmin'); + 'port' => [ + 'required', + 'numeric', + 'min:1', + 'max:65535', + ], + ])->validate(); } } diff --git a/app/Http/Livewire/Services/InstallPHPMyAdmin.php b/app/Http/Livewire/Services/InstallPHPMyAdmin.php new file mode 100644 index 0000000..7629c1f --- /dev/null +++ b/app/Http/Livewire/Services/InstallPHPMyAdmin.php @@ -0,0 +1,31 @@ +install($this->server, $this->all()); + + $this->dispatchBrowserEvent('started', true); + + $this->emitTo(ServicesList::class, '$refresh'); + } + + public function render(): View + { + return view('livewire.services.install-phpmyadmin'); + } +} diff --git a/app/Http/Livewire/Services/ServicesList.php b/app/Http/Livewire/Services/ServicesList.php index fe09460..4cc98f3 100644 --- a/app/Http/Livewire/Services/ServicesList.php +++ b/app/Http/Livewire/Services/ServicesList.php @@ -3,6 +3,7 @@ namespace App\Http\Livewire\Services; use App\Models\Server; +use App\Models\Service; use App\Traits\RefreshComponentOnBroadcast; use Illuminate\Contracts\View\View; use Livewire\Component; @@ -15,6 +16,7 @@ class ServicesList extends Component public function stop(int $id): void { + /** @var Service $service */ $service = $this->server->services()->where('id', $id)->firstOrFail(); $service->stop(); @@ -24,6 +26,7 @@ public function stop(int $id): void public function start(int $id): void { + /** @var Service $service */ $service = $this->server->services()->where('id', $id)->firstOrFail(); $service->start(); @@ -33,6 +36,7 @@ public function start(int $id): void public function restart(int $id): void { + /** @var Service $service */ $service = $this->server->services()->where('id', $id)->firstOrFail(); $service->restart(); @@ -40,6 +44,16 @@ public function restart(int $id): void $this->refreshComponent([]); } + public function uninstall(int $id): void + { + /** @var Service $service */ + $service = $this->server->services()->where('id', $id)->firstOrFail(); + + $service->uninstall(); + + $this->refreshComponent([]); + } + public function render(): View { return view('livewire.services.services-list', [ diff --git a/app/Jobs/Installation/InstallPHPMyAdmin.php b/app/Jobs/Installation/InstallPHPMyAdmin.php index 9082771..b9fb301 100644 --- a/app/Jobs/Installation/InstallPHPMyAdmin.php +++ b/app/Jobs/Installation/InstallPHPMyAdmin.php @@ -3,6 +3,7 @@ namespace App\Jobs\Installation; use App\Actions\FirewallRule\CreateRule; +use App\Enums\ServiceStatus; use App\Jobs\Job; use App\Models\FirewallRule; use App\Models\Service; @@ -32,6 +33,9 @@ public function handle(): void $this->downloadSource(); $this->setUpVHost(); $this->restartPHP(); + $this->service->update([ + 'status' => ServiceStatus::READY, + ]); } /** @@ -41,7 +45,7 @@ private function setUpFirewall(): void { $this->firewallRule = FirewallRule::query() ->where('server_id', $this->service->server_id) - ->where('port', '54331') + ->where('port', $this->service->type_data['port']) ->first(); if ($this->firewallRule) { $this->firewallRule->source = $this->service->type_data['allowed_ip']; @@ -52,7 +56,7 @@ private function setUpFirewall(): void [ 'type' => 'allow', 'protocol' => 'tcp', - 'port' => '54331', + 'port' => $this->service->type_data['port'], 'source' => $this->service->type_data['allowed_ip'], 'mask' => '0', ] @@ -78,6 +82,7 @@ private function setUpVHost(): void { $vhost = File::get(resource_path('commands/webserver/nginx/phpmyadmin-vhost.conf')); $vhost = Str::replace('__php_version__', $this->service->server->defaultService('php')->version, $vhost); + $vhost = Str::replace('__port__', $this->service->type_data['port'], $vhost); $this->service->server->ssh()->exec( new CreateNginxPHPMyAdminVHostCommand($vhost), 'create-phpmyadmin-vhost' @@ -98,6 +103,9 @@ private function restartPHP(): void public function failed(Throwable $throwable): Throwable { $this->firewallRule?->removeFromServer(); + $this->service->update([ + 'status' => ServiceStatus::INSTALLATION_FAILED, + ]); throw $throwable; } } diff --git a/app/Jobs/Installation/UninstallPHPMyAdmin.php b/app/Jobs/Installation/UninstallPHPMyAdmin.php index 91d1d98..5f11aae 100644 --- a/app/Jobs/Installation/UninstallPHPMyAdmin.php +++ b/app/Jobs/Installation/UninstallPHPMyAdmin.php @@ -37,7 +37,7 @@ private function removeFirewallRule(): void /** @var ?FirewallRule $rule */ $rule = FirewallRule::query() ->where('server_id', $this->service->server_id) - ->where('port', '54331') + ->where('port', $this->service->type_data['port']) ->first(); $rule?->removeFromServer(); } diff --git a/app/Models/Service.php b/app/Models/Service.php index 58d05c5..e747ad2 100755 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -76,7 +76,7 @@ public function uninstaller(): mixed return new $uninstaller($this); } - public function getUnitAttribute($value): string + public function getUnitAttribute($value): ?string { if ($value) { return $value; diff --git a/config/core.php b/config/core.php index 93d6d89..4ecfaa7 100755 --- a/config/core.php +++ b/config/core.php @@ -324,7 +324,6 @@ 'https' => 443, 'mysql' => 3306, 'ftp' => 21, - 'phpmyadmin' => 54331, 'tcp' => '', 'udp' => '', ], diff --git a/public/static/images/phpmyadmin.svg b/public/static/images/phpmyadmin.svg new file mode 100644 index 0000000..95d919f --- /dev/null +++ b/public/static/images/phpmyadmin.svg @@ -0,0 +1,43 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/resources/commands/webserver/nginx/phpmyadmin-vhost.conf b/resources/commands/webserver/nginx/phpmyadmin-vhost.conf index 490743a..bd7050d 100755 --- a/resources/commands/webserver/nginx/phpmyadmin-vhost.conf +++ b/resources/commands/webserver/nginx/phpmyadmin-vhost.conf @@ -1,5 +1,5 @@ server { - listen 54331; + listen __port__; server_name _; root /home/vito/phpmyadmin; diff --git a/resources/views/livewire/services/install-phpmyadmin.blade.php b/resources/views/livewire/services/install-phpmyadmin.blade.php new file mode 100644 index 0000000..1295e2a --- /dev/null +++ b/resources/views/livewire/services/install-phpmyadmin.blade.php @@ -0,0 +1,33 @@ + +
+

+ {{ __('Install PHPMyAdmin') }} +

+ +
+ + + @error('allowed_ip') + + @enderror +
+ +
+ + + @error('port') + + @enderror +
+ +
+ + {{ __('Cancel') }} + + + + {{ __('Install') }} + +
+
+
diff --git a/resources/views/livewire/services/services-list.blade.php b/resources/views/livewire/services/services-list.blade.php index 1fb79ea..77fa07c 100644 --- a/resources/views/livewire/services/services-list.blade.php +++ b/resources/views/livewire/services/services-list.blade.php @@ -1,11 +1,29 @@
- {{ __("Services") }} - {{ __("All services that we installed on your server are here") }} + {{ __('Services') }} + {{ __('All services that we installed on your server are here') }} + + + + + {{ __('Install Service') }} + + + + + + + + {{ __('PHPMyAdmin') }} + + + + +
- @foreach($services as $service) + @foreach ($services as $service)
@@ -20,24 +38,30 @@ - {{ __("Actions") }} + {{ __('Actions') }} - @if($service->status == \App\Enums\ServiceStatus::STOPPED) - - {{ __("Start") }} + @if($service->unit) + @if ($service->status == \App\Enums\ServiceStatus::STOPPED) + + {{ __('Start') }} + + @endif + @if ($service->status == \App\Enums\ServiceStatus::READY) + + {{ __('Stop') }} + + @endif + + {{ __('Restart') }} + + @else + + {{ __('Uninstall') }} @endif - @if($service->status == \App\Enums\ServiceStatus::READY) - - {{ __("Stop") }} - - @endif - - {{ __("Restart") }} -
diff --git a/tests/Feature/Http/ServicesTest.php b/tests/Feature/Http/ServicesTest.php index 853e0d4..848cc43 100644 --- a/tests/Feature/Http/ServicesTest.php +++ b/tests/Feature/Http/ServicesTest.php @@ -3,8 +3,12 @@ namespace Tests\Feature\Http; use App\Enums\ServiceStatus; +use App\Http\Livewire\Services\InstallPHPMyAdmin; use App\Http\Livewire\Services\ServicesList; +use App\Jobs\Installation\InstallPHPMyAdmin as InstallationInstallPHPMyAdmin; +use App\Jobs\Installation\UninstallPHPMyAdmin; use App\Jobs\Service\Manage; +use App\Models\Service; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Bus; use Livewire\Livewire; @@ -80,6 +84,45 @@ public function test_start_service(string $name): void Bus::assertDispatched(Manage::class); } + public function test_install_phpmyadmin(): void + { + Bus::fake(); + + Livewire::test(InstallPHPMyAdmin::class, ['server' => $this->server]) + ->set('allowed_ip', '0.0.0.0') + ->set('port', 5433) + ->call('install') + ->assertSuccessful(); + + Bus::assertDispatched(InstallationInstallPHPMyAdmin::class); + } + + public function test_uninstall_phpmyadmin(): void + { + $service = Service::factory()->create([ + 'server_id' => $this->server->id, + 'type' => 'phpmyadmin', + 'type_data' => [ + 'allowed_ip' => '0.0.0.0', + 'port' => '5433', + 'php' => '8.1', + ], + 'name' => 'phpmyadmin', + 'version' => '5.1.2', + 'status' => ServiceStatus::READY, + 'is_default' => 1, + + ]); + + Bus::fake(); + + Livewire::test(ServicesList::class, ['server' => $this->server]) + ->call('uninstall', $service->id) + ->assertSuccessful(); + + Bus::assertDispatched(UninstallPHPMyAdmin::class); + } + public static function data(): array { return [