Skip to content

Commit

Permalink
phpmyadmin (#66)
Browse files Browse the repository at this point in the history
* add phpmyadmin

* add tests
  • Loading branch information
saeedvaziry authored Oct 10, 2023
1 parent 7c5505b commit 4bd4b34
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace App\Actions\Database;
namespace App\Actions\Service;

use App\Enums\ServiceStatus;
use App\Models\Server;
use App\Models\Service;
use Illuminate\Support\Facades\Validator;
Expand All @@ -18,23 +19,21 @@ public function install(Server $server, array $input): Service

$phpMyAdmin = $server->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();
Expand All @@ -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();
}
}
31 changes: 31 additions & 0 deletions app/Http/Livewire/Services/InstallPHPMyAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Livewire\Services;

use App\Actions\Service\InstallPHPMyAdmin as InstallPHPMyAdminAction;
use App\Models\Server;
use Illuminate\Contracts\View\View;
use Livewire\Component;

class InstallPHPMyAdmin extends Component
{
public Server $server;

public string $allowed_ip;

public string $port = '5433';

public function install(): void
{
app(InstallPHPMyAdminAction::class)->install($this->server, $this->all());

$this->dispatchBrowserEvent('started', true);

$this->emitTo(ServicesList::class, '$refresh');
}

public function render(): View
{
return view('livewire.services.install-phpmyadmin');
}
}
14 changes: 14 additions & 0 deletions app/Http/Livewire/Services/ServicesList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -33,13 +36,24 @@ 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();

$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', [
Expand Down
12 changes: 10 additions & 2 deletions app/Jobs/Installation/InstallPHPMyAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,6 +33,9 @@ public function handle(): void
$this->downloadSource();
$this->setUpVHost();
$this->restartPHP();
$this->service->update([
'status' => ServiceStatus::READY,
]);
}

/**
Expand All @@ -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'];
Expand All @@ -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',
]
Expand All @@ -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'
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion app/Jobs/Installation/UninstallPHPMyAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion config/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@
'https' => 443,
'mysql' => 3306,
'ftp' => 21,
'phpmyadmin' => 54331,
'tcp' => '',
'udp' => '',
],
Expand Down
43 changes: 43 additions & 0 deletions public/static/images/phpmyadmin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion resources/commands/webserver/nginx/phpmyadmin-vhost.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server {
listen 54331;
listen __port__;
server_name _;
root /home/vito/phpmyadmin;

Expand Down
33 changes: 33 additions & 0 deletions resources/views/livewire/services/install-phpmyadmin.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<x-modal name="install-phpmyadmin">
<form wire:submit.prevent="install" class="p-6">
<h2 class="text-lg font-medium text-gray-900 dark:text-gray-100">
{{ __('Install PHPMyAdmin') }}
</h2>

<div class="mt-6">
<x-input-label for="allowed_ip" :value="__('Allowed IP')" />
<x-text-input wire:model.defer="allowed_ip" id="allowed_ip" name="allowed_ip" class="mt-1 w-full" />
@error('allowed_ip')
<x-input-error class="mt-2" :messages="$message" />
@enderror
</div>

<div class="mt-6">
<x-input-label for="port" :value="__('Port')" />
<x-text-input wire:model.defer="port" id="port" name="port" class="mt-1 w-full" />
@error('port')
<x-input-error class="mt-2" :messages="$message" />
@enderror
</div>

<div class="mt-6 flex justify-end">
<x-secondary-button type="button" x-on:click="$dispatch('close')">
{{ __('Cancel') }}
</x-secondary-button>

<x-primary-button class="ml-3" @started.window="$dispatch('close')">
{{ __('Install') }}
</x-primary-button>
</div>
</form>
</x-modal>
52 changes: 38 additions & 14 deletions resources/views/livewire/services/services-list.blade.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
<div>
<x-card-header>
<x-slot name="title">{{ __("Services") }}</x-slot>
<x-slot name="description">{{ __("All services that we installed on your server are here") }}</x-slot>
<x-slot name="title">{{ __('Services') }}</x-slot>
<x-slot name="description">{{ __('All services that we installed on your server are here') }}</x-slot>
<x-slot name="aside">
<x-dropdown>
<x-slot name="trigger">
<x-primary-button>
{{ __('Install Service') }}
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4 ml-1">
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5" />
</svg>
</x-primary-button>
</x-slot>
<x-slot name="content">
<x-dropdown-link class="cursor-pointer" x-on:click="$dispatch('open-modal', 'install-phpmyadmin')">
{{ __('PHPMyAdmin') }}
</x-dropdown-link>
</x-slot>
</x-dropdown>
<livewire:services.install-p-h-p-my-admin :server="$server" />
</x-slot>
</x-card-header>

<div class="space-y-3">
@foreach($services as $service)
@foreach ($services as $service)
<x-item-card>
<div class="flex-none">
<img src="{{ asset('static/images/' . $service->name . '.svg') }}" class="h-10 w-10" alt="">
Expand All @@ -20,24 +38,30 @@
<x-dropdown>
<x-slot name="trigger">
<x-secondary-button>
{{ __("Actions") }}
{{ __('Actions') }}
</x-secondary-button>
</x-slot>

<x-slot name="content">
@if($service->status == \App\Enums\ServiceStatus::STOPPED)
<x-dropdown-link class="cursor-pointer" wire:click="start({{ $service->id }})">
{{ __("Start") }}
@if($service->unit)
@if ($service->status == \App\Enums\ServiceStatus::STOPPED)
<x-dropdown-link class="cursor-pointer" wire:click="start({{ $service->id }})">
{{ __('Start') }}
</x-dropdown-link>
@endif
@if ($service->status == \App\Enums\ServiceStatus::READY)
<x-dropdown-link class="cursor-pointer" wire:click="stop({{ $service->id }})">
{{ __('Stop') }}
</x-dropdown-link>
@endif
<x-dropdown-link class="cursor-pointer" wire:click="restart({{ $service->id }})">
{{ __('Restart') }}
</x-dropdown-link>
@endif
@if($service->status == \App\Enums\ServiceStatus::READY)
<x-dropdown-link class="cursor-pointer" wire:click="stop({{ $service->id }})">
{{ __("Stop") }}
@else
<x-dropdown-link class="cursor-pointer" wire:click="uninstall({{ $service->id }})">
{{ __('Uninstall') }}
</x-dropdown-link>
@endif
<x-dropdown-link class="cursor-pointer" wire:click="restart({{ $service->id }})">
{{ __("Restart") }}
</x-dropdown-link>
</x-slot>
</x-dropdown>
</div>
Expand Down
Loading

0 comments on commit 4bd4b34

Please sign in to comment.