From 4bd4b34d244b8174f3ec9a7cb0c9ba768b112de6 Mon Sep 17 00:00:00 2001
From: Saeed Vaziry <61919774+saeedvaziry@users.noreply.github.com>
Date: Tue, 10 Oct 2023 17:15:58 +0200
Subject: [PATCH] phpmyadmin (#66)
* add phpmyadmin
* add tests
---
.../InstallPHPMyAdmin.php | 23 ++++----
.../Livewire/Services/InstallPHPMyAdmin.php | 31 +++++++++++
app/Http/Livewire/Services/ServicesList.php | 14 +++++
app/Jobs/Installation/InstallPHPMyAdmin.php | 12 ++++-
app/Jobs/Installation/UninstallPHPMyAdmin.php | 2 +-
app/Models/Service.php | 2 +-
config/core.php | 1 -
public/static/images/phpmyadmin.svg | 43 +++++++++++++++
.../webserver/nginx/phpmyadmin-vhost.conf | 2 +-
.../services/install-phpmyadmin.blade.php | 33 ++++++++++++
.../livewire/services/services-list.blade.php | 54 +++++++++++++------
tests/Feature/Http/ServicesTest.php | 43 +++++++++++++++
12 files changed, 230 insertions(+), 30 deletions(-)
rename app/Actions/{Database => Service}/InstallPHPMyAdmin.php (70%)
create mode 100644 app/Http/Livewire/Services/InstallPHPMyAdmin.php
create mode 100644 public/static/images/phpmyadmin.svg
create mode 100644 resources/views/livewire/services/install-phpmyadmin.blade.php
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 @@
+
+
\ 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 @@
+