From 9244e69fd88db65ad1b892cb1752685fc36170e8 Mon Sep 17 00:00:00 2001 From: Saeed Vaziry Date: Wed, 27 Mar 2024 11:41:29 +0100 Subject: [PATCH] add phpmyadmin --- README.md | 1 + app/Actions/Service/Create.php | 55 +++++++++++ app/Enums/SiteType.php | 2 + app/Http/Controllers/ServiceController.php | 12 +++ app/Models/Service.php | 17 ++-- app/SSH/OS/OS.php | 17 ++++ app/SSH/OS/scripts/download.sh | 3 + app/SSH/PHPMyAdmin/PHPMyAdmin.php | 23 +++++ app/SSH/PHPMyAdmin/scripts/install.sh | 25 +++++ .../AddOnServices/AbstractAddOnService.php | 18 ++++ app/SiteTypes/PHPMyAdmin.php | 49 ++++++++++ config/core.php | 6 ++ public/static/images/supervisor.svg | 10 +- public/static/images/ufw.svg | 10 +- .../application/phpmyadmin-app.blade.php | 6 ++ .../views/application/wordpress-app.blade.php | 2 +- .../heroicons/o-no-symbol.blade.php | 14 +++ .../views/components/icon-button.blade.php | 5 +- .../views/components/item-card.blade.php | 2 +- resources/views/components/live.blade.php | 5 +- .../components/secondary-button.blade.php | 2 +- resources/views/services/index.blade.php | 2 + .../services/partials/actions/nginx.blade.php | 1 + .../services/partials/actions/php.blade.php | 1 + .../partials/actions/postgresql.blade.php | 1 + .../services/partials/actions/redis.blade.php | 1 + .../partials/actions/supervisor.blade.php | 1 + .../services/partials/actions/ufw.blade.php | 1 + .../views/services/partials/add-ons.blade.php | 33 +++++++ .../services/partials/services-list.blade.php | 96 ++++++------------- .../services/partials/unit-actions.blade.php | 42 ++++++++ .../partials/create/phpmyadmin.blade.php | 13 +++ routes/server.php | 1 + tests/Feature/SitesTest.php | 9 ++ 34 files changed, 401 insertions(+), 85 deletions(-) create mode 100644 app/Actions/Service/Create.php create mode 100644 app/SSH/OS/scripts/download.sh create mode 100644 app/SSH/PHPMyAdmin/PHPMyAdmin.php create mode 100755 app/SSH/PHPMyAdmin/scripts/install.sh create mode 100644 app/SSH/Services/AddOnServices/AbstractAddOnService.php create mode 100755 app/SiteTypes/PHPMyAdmin.php create mode 100644 resources/views/application/phpmyadmin-app.blade.php create mode 100644 resources/views/components/heroicons/o-no-symbol.blade.php create mode 100644 resources/views/services/partials/actions/nginx.blade.php create mode 100644 resources/views/services/partials/actions/php.blade.php create mode 100644 resources/views/services/partials/actions/postgresql.blade.php create mode 100644 resources/views/services/partials/actions/redis.blade.php create mode 100644 resources/views/services/partials/actions/supervisor.blade.php create mode 100644 resources/views/services/partials/actions/ufw.blade.php create mode 100644 resources/views/services/partials/add-ons.blade.php create mode 100644 resources/views/services/partials/unit-actions.blade.php create mode 100644 resources/views/sites/partials/create/phpmyadmin.blade.php diff --git a/README.md b/README.md index 970203c..e90cb56 100644 --- a/README.md +++ b/README.md @@ -54,3 +54,4 @@ ## Credits - Prettier - Postcss - Flowbite +- svgrepo.com diff --git a/app/Actions/Service/Create.php b/app/Actions/Service/Create.php new file mode 100644 index 0000000..bfb75cc --- /dev/null +++ b/app/Actions/Service/Create.php @@ -0,0 +1,55 @@ +validate($server, $input); + + $service = new Service([ + 'name' => $input['type'], + 'type' => $input['type'], + 'version' => $input['version'], + 'status' => ServiceStatus::INSTALLING, + ]); + + Validator::make($input, $service->handler()->creationRules($input))->validate(); + + $service->type_data = $service->handler()->creationData($input); + + $service->save(); + + $service->handler()->create(); + + dispatch(function () use ($service) { + $service->handler()->install(); + $service->status = ServiceStatus::READY; + $service->save(); + })->catch(function () use ($service) { + $service->handler()->delete(); + $service->delete(); + })->onConnection('ssh'); + + return $service; + } + + private function validate(Server $server, array $input): void + { + Validator::make($input, [ + 'type' => [ + 'required', + Rule::in(config('core.add_on_services')), + Rule::unique('services', 'type')->where('server_id', $server->id), + ], + 'version' => 'required', + ])->validate(); + } +} diff --git a/app/Enums/SiteType.php b/app/Enums/SiteType.php index 128d3f6..21d4a36 100644 --- a/app/Enums/SiteType.php +++ b/app/Enums/SiteType.php @@ -11,4 +11,6 @@ final class SiteType const LARAVEL = 'laravel'; const WORDPRESS = 'wordpress'; + + const PHPMYADMIN = 'phpmyadmin'; } diff --git a/app/Http/Controllers/ServiceController.php b/app/Http/Controllers/ServiceController.php index 6a5e7ce..6969126 100644 --- a/app/Http/Controllers/ServiceController.php +++ b/app/Http/Controllers/ServiceController.php @@ -2,11 +2,14 @@ namespace App\Http\Controllers; +use App\Actions\Service\Create; use App\Facades\Toast; +use App\Helpers\HtmxResponse; use App\Models\Server; use App\Models\Service; use Illuminate\Contracts\View\View; use Illuminate\Http\RedirectResponse; +use Illuminate\Http\Request; class ServiceController extends Controller { @@ -62,4 +65,13 @@ public function disable(Server $server, Service $service): RedirectResponse return back(); } + + public function install(Server $server, Request $request): HtmxResponse + { + app(Create::class)->create($server, $request->input()); + + Toast::success('Service is being uninstalled!'); + + return htmx()->back(); + } } diff --git a/app/Models/Service.php b/app/Models/Service.php index 6c30d88..4516ae2 100755 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -4,6 +4,7 @@ use App\Actions\Service\Manage; use App\Exceptions\ServiceInstallationFailed; +use App\SSH\Services\AddOnServices\AbstractAddOnService; use App\SSH\Services\Database\Database as DatabaseHandler; use App\SSH\Services\Firewall\Firewall as FirewallHandler; use App\SSH\Services\PHP\PHP as PHPHandler; @@ -53,7 +54,9 @@ public static function boot(): void parent::boot(); static::creating(function (Service $service) { - $service->unit = config('core.service_units')[$service->name][$service->server->os][$service->version]; + if (array_key_exists($service->name, config('core.service_units'))) { + $service->unit = config('core.service_units')[$service->name][$service->server->os][$service->version]; + } }); } @@ -63,7 +66,7 @@ public function server(): BelongsTo } public function handler( - ): PHPHandler|WebserverHandler|DatabaseHandler|FirewallHandler|ProcessManagerHandler|RedisHandler { + ): PHPHandler|WebserverHandler|DatabaseHandler|FirewallHandler|ProcessManagerHandler|RedisHandler|AbstractAddOnService { $handler = config('core.service_handlers')[$this->name]; return new $handler($this); @@ -81,26 +84,26 @@ public function validateInstall($result): void public function start(): void { - app(Manage::class)->start($this); + $this->unit && app(Manage::class)->start($this); } public function stop(): void { - app(Manage::class)->stop($this); + $this->unit && app(Manage::class)->stop($this); } public function restart(): void { - app(Manage::class)->restart($this); + $this->unit && app(Manage::class)->restart($this); } public function enable(): void { - app(Manage::class)->enable($this); + $this->unit && app(Manage::class)->enable($this); } public function disable(): void { - app(Manage::class)->disable($this); + $this->unit && app(Manage::class)->disable($this); } } diff --git a/app/SSH/OS/OS.php b/app/SSH/OS/OS.php index fe0befb..87dc0ac 100644 --- a/app/SSH/OS/OS.php +++ b/app/SSH/OS/OS.php @@ -131,4 +131,21 @@ public function runScript(string $path, string $script, ?int $siteId = null): Se return $ssh->log; } + + public function download(string $url, string $path): string + { + return $this->server->ssh()->exec( + $this->getScript('download.sh', [ + 'url' => $url, + 'path' => $path, + ]) + ); + } + + public function unzip(string $path): string + { + return $this->server->ssh()->exec( + 'unzip '.$path + ); + } } diff --git a/app/SSH/OS/scripts/download.sh b/app/SSH/OS/scripts/download.sh new file mode 100644 index 0000000..c5a7c7b --- /dev/null +++ b/app/SSH/OS/scripts/download.sh @@ -0,0 +1,3 @@ +if ! wget __url__ -O __path__; then + echo 'VITO_SSH_ERROR' && exit 1 +fi diff --git a/app/SSH/PHPMyAdmin/PHPMyAdmin.php b/app/SSH/PHPMyAdmin/PHPMyAdmin.php new file mode 100644 index 0000000..a8160fd --- /dev/null +++ b/app/SSH/PHPMyAdmin/PHPMyAdmin.php @@ -0,0 +1,23 @@ +server->ssh()->exec( + $this->getScript('install.sh', [ + 'version' => $site->type_data['version'], + 'path' => $site->path, + ]), + 'install-phpmyadmin', + $site->id + ); + } +} diff --git a/app/SSH/PHPMyAdmin/scripts/install.sh b/app/SSH/PHPMyAdmin/scripts/install.sh new file mode 100755 index 0000000..34b7cf0 --- /dev/null +++ b/app/SSH/PHPMyAdmin/scripts/install.sh @@ -0,0 +1,25 @@ +sudo rm -rf phpmyadmin + +sudo rm -rf __path__ + +if ! wget https://files.phpmyadmin.net/phpMyAdmin/__version__/phpMyAdmin-__version__-all-languages.zip; then + echo 'VITO_SSH_ERROR' && exit 1 +fi + +if ! unzip phpMyAdmin-__version__-all-languages.zip; then + echo 'VITO_SSH_ERROR' && exit 1 +fi + +if ! rm -rf phpMyAdmin-__version__-all-languages.zip; then + echo 'VITO_SSH_ERROR' && exit 1 +fi + +if ! mv phpMyAdmin-__version__-all-languages __path__; then + echo 'VITO_SSH_ERROR' && exit 1 +fi + +if ! mv __path__/config.sample.inc.php __path__/config.inc.php; then + echo 'VITO_SSH_ERROR' && exit 1 +fi + +echo "PHPMyAdmin installed!" diff --git a/app/SSH/Services/AddOnServices/AbstractAddOnService.php b/app/SSH/Services/AddOnServices/AbstractAddOnService.php new file mode 100644 index 0000000..442ae16 --- /dev/null +++ b/app/SSH/Services/AddOnServices/AbstractAddOnService.php @@ -0,0 +1,18 @@ + [ + 'required', + Rule::in($this->site->server->installedPHPVersions()), + ], + 'version' => 'required|string', + ]; + } + + public function createFields(array $input): array + { + return [ + 'web_directory' => '', + 'php_version' => $input['php_version'] ?? '', + ]; + } + + public function data(array $input): array + { + return [ + 'version' => $input['version'], + ]; + } + + public function install(): void + { + $this->site->server->webserver()->handler()->createVHost($this->site); + $this->progress(30); + app(\App\SSH\PHPMyAdmin\PHPMyAdmin::class)->install($this->site); + $this->progress(65); + } +} diff --git a/config/core.php b/config/core.php index 4068070..02843c8 100755 --- a/config/core.php +++ b/config/core.php @@ -13,6 +13,7 @@ use App\ServerProviders\Vultr; use App\SiteTypes\Laravel; use App\SiteTypes\PHPBlank; +use App\SiteTypes\PHPMyAdmin; use App\SiteTypes\PHPSite; use App\SiteTypes\Wordpress; use App\SourceControlProviders\Bitbucket; @@ -177,6 +178,9 @@ 'ufw' => Ufw::class, 'supervisor' => Supervisor::class, ], + 'add_on_services' => [ + // add-on services + ], 'service_units' => [ 'nginx' => [ 'ubuntu_18' => [ @@ -320,12 +324,14 @@ \App\Enums\SiteType::PHP_BLANK, \App\Enums\SiteType::LARAVEL, \App\Enums\SiteType::WORDPRESS, + \App\Enums\SiteType::PHPMYADMIN, ], 'site_types_class' => [ \App\Enums\SiteType::PHP => PHPSite::class, \App\Enums\SiteType::PHP_BLANK => PHPBlank::class, \App\Enums\SiteType::LARAVEL => Laravel::class, \App\Enums\SiteType::WORDPRESS => Wordpress::class, + \App\Enums\SiteType::PHPMYADMIN => PHPMyAdmin::class, ], /* diff --git a/public/static/images/supervisor.svg b/public/static/images/supervisor.svg index 40866cd..3203fe5 100644 --- a/public/static/images/supervisor.svg +++ b/public/static/images/supervisor.svg @@ -1 +1,9 @@ - \ No newline at end of file + + + + + + + + \ No newline at end of file diff --git a/public/static/images/ufw.svg b/public/static/images/ufw.svg index 0cf1d56..2843aa3 100644 --- a/public/static/images/ufw.svg +++ b/public/static/images/ufw.svg @@ -1 +1,9 @@ - \ No newline at end of file + + + \ No newline at end of file diff --git a/resources/views/application/phpmyadmin-app.blade.php b/resources/views/application/phpmyadmin-app.blade.php new file mode 100644 index 0000000..48133ac --- /dev/null +++ b/resources/views/application/phpmyadmin-app.blade.php @@ -0,0 +1,6 @@ +
+ + PHPMyAdmin is installed and ready to use! + Open + +
diff --git a/resources/views/application/wordpress-app.blade.php b/resources/views/application/wordpress-app.blade.php index 7cb80a6..eb08473 100644 --- a/resources/views/application/wordpress-app.blade.php +++ b/resources/views/application/wordpress-app.blade.php @@ -3,7 +3,7 @@ {{ __("Your Wordpress site is installed and ready to use! ") }} - + {{ __("Open Website") }} diff --git a/resources/views/components/heroicons/o-no-symbol.blade.php b/resources/views/components/heroicons/o-no-symbol.blade.php new file mode 100644 index 0000000..bc8cf23 --- /dev/null +++ b/resources/views/components/heroicons/o-no-symbol.blade.php @@ -0,0 +1,14 @@ + + + diff --git a/resources/views/components/icon-button.blade.php b/resources/views/components/icon-button.blade.php index 433ba86..7fe0ca3 100644 --- a/resources/views/components/icon-button.blade.php +++ b/resources/views/components/icon-button.blade.php @@ -1,5 +1,6 @@ @props([ "href", + "disabled" => false, ]) @php @@ -7,12 +8,12 @@ "inline-flex w-max items-center justify-center px-2 py-1 font-semibold capitalize outline-0 transition hover:opacity-50 focus:ring focus:ring-primary-200 disabled:opacity-25 dark:focus:ring-primary-700 dark:focus:ring-opacity-40"; @endphp -@if (isset($href)) +@if (isset($href) && ! $disabled) merge(["class" => $class]) }}> {{ $slot }} @else - @endif diff --git a/resources/views/components/item-card.blade.php b/resources/views/components/item-card.blade.php index bf1fcf1..c6b57b5 100644 --- a/resources/views/components/item-card.blade.php +++ b/resources/views/components/item-card.blade.php @@ -1,5 +1,5 @@
merge(["class" => "flex h-20 items-center justify-between rounded-b-md rounded-t-md border border-gray-200 bg-white p-7 text-center dark:border-gray-700 dark:bg-gray-800"]) }} > {{ $slot }}
diff --git a/resources/views/components/live.blade.php b/resources/views/components/live.blade.php index 0f4998b..a7ddb1c 100644 --- a/resources/views/components/live.blade.php +++ b/resources/views/components/live.blade.php @@ -5,16 +5,13 @@ ])
merge(["interval" => $interval, "id" => $id, "hx-get" => request()->getUri(), "hx-trigger" => "every " . $interval, "hx-swap" => "outerHTML"]) }} @if ($target) hx-target="{{ $target }}" hx-select="{{ $target }}" @else hx-select="#{{ $id }}" @endif - hx-swap="outerHTML" > {{ $slot }}
diff --git a/resources/views/components/secondary-button.blade.php b/resources/views/components/secondary-button.blade.php index 95f8fca..77ffb86 100644 --- a/resources/views/components/secondary-button.blade.php +++ b/resources/views/components/secondary-button.blade.php @@ -7,7 +7,7 @@ @php $class = - "inline-flex h-9 min-w-max items-center rounded-md border border-gray-300 bg-white px-4 py-1 font-semibold text-gray-700 shadow-sm transition duration-150 ease-in-out hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-gray-500 focus:ring-offset-2 disabled:opacity-25 dark:border-gray-500 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700 dark:focus:ring-offset-gray-800"; + "inline-flex h-9 w-max min-w-max items-center justify-center rounded-md border border-gray-300 bg-white px-4 py-1 font-semibold text-gray-700 shadow-sm transition duration-150 ease-in-out hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-gray-500 focus:ring-offset-2 disabled:opacity-25 dark:border-gray-500 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700 dark:focus:ring-offset-gray-800"; @endphp @if (isset($href)) diff --git a/resources/views/services/index.blade.php b/resources/views/services/index.blade.php index 1daf857..cadcb47 100644 --- a/resources/views/services/index.blade.php +++ b/resources/views/services/index.blade.php @@ -2,4 +2,6 @@ {{ __("Services") }} @include("services.partials.services-list") + + {{-- @include("services.partials.add-ons") --}} diff --git a/resources/views/services/partials/actions/nginx.blade.php b/resources/views/services/partials/actions/nginx.blade.php new file mode 100644 index 0000000..36d3048 --- /dev/null +++ b/resources/views/services/partials/actions/nginx.blade.php @@ -0,0 +1 @@ +@include("services.partials.unit-actions") diff --git a/resources/views/services/partials/actions/php.blade.php b/resources/views/services/partials/actions/php.blade.php new file mode 100644 index 0000000..36d3048 --- /dev/null +++ b/resources/views/services/partials/actions/php.blade.php @@ -0,0 +1 @@ +@include("services.partials.unit-actions") diff --git a/resources/views/services/partials/actions/postgresql.blade.php b/resources/views/services/partials/actions/postgresql.blade.php new file mode 100644 index 0000000..36d3048 --- /dev/null +++ b/resources/views/services/partials/actions/postgresql.blade.php @@ -0,0 +1 @@ +@include("services.partials.unit-actions") diff --git a/resources/views/services/partials/actions/redis.blade.php b/resources/views/services/partials/actions/redis.blade.php new file mode 100644 index 0000000..36d3048 --- /dev/null +++ b/resources/views/services/partials/actions/redis.blade.php @@ -0,0 +1 @@ +@include("services.partials.unit-actions") diff --git a/resources/views/services/partials/actions/supervisor.blade.php b/resources/views/services/partials/actions/supervisor.blade.php new file mode 100644 index 0000000..36d3048 --- /dev/null +++ b/resources/views/services/partials/actions/supervisor.blade.php @@ -0,0 +1 @@ +@include("services.partials.unit-actions") diff --git a/resources/views/services/partials/actions/ufw.blade.php b/resources/views/services/partials/actions/ufw.blade.php new file mode 100644 index 0000000..36d3048 --- /dev/null +++ b/resources/views/services/partials/actions/ufw.blade.php @@ -0,0 +1 @@ +@include("services.partials.unit-actions") diff --git a/resources/views/services/partials/add-ons.blade.php b/resources/views/services/partials/add-ons.blade.php new file mode 100644 index 0000000..4b70a93 --- /dev/null +++ b/resources/views/services/partials/add-ons.blade.php @@ -0,0 +1,33 @@ +
+ + Supported Services + Here you can find the supported services to install + + + +
+ @foreach (config("core.add_on_services") as $addOn) +
+
+
+ +
+
+
+
+ {{ $addOn }} +
+
+
+
+
+ @include("services.partials.add-on-installers." . $addOn) +
+
+ @endforeach +
+
diff --git a/resources/views/services/partials/services-list.blade.php b/resources/views/services/partials/services-list.blade.php index dab4f6a..46a3691 100644 --- a/resources/views/services/partials/services-list.blade.php +++ b/resources/views/services/partials/services-list.blade.php @@ -1,80 +1,42 @@
- {{ __("Services") }} - - {{ __("All services that we installed on your server are here") }} - + Installed Services + All services that we installed on your server are here - -
+ +
@foreach ($services as $service) - -
- name . ".svg") }}" class="h-10 w-10" alt="" /> +
+
+ @include("services.partials.status", ["status" => $service->status])
-
-
-
{{ $service->name }}:{{ $service->version }}
- @include("services.partials.status", ["status" => $service->status]) +
+
+ name . ".svg") }}" + class="h-20 w-20" + alt="" + /> +
+
+
+
+ {{ $service->name }} + {{ $service->version }} +
+
-
- - - - {{ __("Actions") }} - - - - - @if ($service->unit) - @if ($service->status == \App\Enums\ServiceStatus::STOPPED) - - {{ __("Start") }} - - @endif - - @if ($service->status == \App\Enums\ServiceStatus::READY) - - {{ __("Stop") }} - - @endif - - - {{ __("Restart") }} - - - @if ($service->status == \App\Enums\ServiceStatus::DISABLED) - - {{ __("Enable") }} - - @endif - - - {{ __("Disable") }} - - @endif - - +
+ @include("services.partials.actions." . $service->name)
- +
@endforeach
diff --git a/resources/views/services/partials/unit-actions.blade.php b/resources/views/services/partials/unit-actions.blade.php new file mode 100644 index 0000000..b7d03ea --- /dev/null +++ b/resources/views/services/partials/unit-actions.blade.php @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + diff --git a/resources/views/sites/partials/create/phpmyadmin.blade.php b/resources/views/sites/partials/create/phpmyadmin.blade.php new file mode 100644 index 0000000..911e8c0 --- /dev/null +++ b/resources/views/sites/partials/create/phpmyadmin.blade.php @@ -0,0 +1,13 @@ +@include("sites.partials.create.fields.php-version") + +
+ + + + + + + @error("version") + + @enderror +
diff --git a/routes/server.php b/routes/server.php index 7cc3c7c..20a5b0f 100644 --- a/routes/server.php +++ b/routes/server.php @@ -124,6 +124,7 @@ 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'); + Route::post('/{server}/services/install', [ServiceController::class, 'install'])->name('servers.services.install'); // console Route::get('/{server}/console', [ConsoleController::class, 'index'])->name('servers.console'); diff --git a/tests/Feature/SitesTest.php b/tests/Feature/SitesTest.php index 22ec47a..4de43e4 100644 --- a/tests/Feature/SitesTest.php +++ b/tests/Feature/SitesTest.php @@ -158,6 +158,15 @@ public static function create_data(): array 'web_directory' => 'public', ], ], + [ + [ + 'type' => SiteType::PHPMYADMIN, + 'domain' => 'example.com', + 'alias' => 'www.example.com', + 'php_version' => '8.2', + 'version' => '5.1.2', + ], + ], ]; }