add phpmyadmin

This commit is contained in:
Saeed Vaziry
2024-03-27 11:41:29 +01:00
parent a7ba095919
commit 9244e69fd8
34 changed files with 401 additions and 85 deletions

View File

@ -0,0 +1,55 @@
<?php
namespace App\Actions\Service;
use App\Enums\ServiceStatus;
use App\Models\Server;
use App\Models\Service;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
class Create
{
public function create(Server $server, array $input): Service
{
$this->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();
}
}

View File

@ -11,4 +11,6 @@ final class SiteType
const LARAVEL = 'laravel';
const WORDPRESS = 'wordpress';
const PHPMYADMIN = 'phpmyadmin';
}

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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
);
}
}

View File

@ -0,0 +1,3 @@
if ! wget __url__ -O __path__; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -0,0 +1,23 @@
<?php
namespace App\SSH\PHPMyAdmin;
use App\Models\Site;
use App\SSH\HasScripts;
class PHPMyAdmin
{
use HasScripts;
public function install(Site $site): void
{
$site->server->ssh()->exec(
$this->getScript('install.sh', [
'version' => $site->type_data['version'],
'path' => $site->path,
]),
'install-phpmyadmin',
$site->id
);
}
}

View File

@ -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!"

View File

@ -0,0 +1,18 @@
<?php
namespace App\SSH\Services\AddOnServices;
use App\SSH\Services\ServiceInterface;
abstract class AbstractAddOnService implements ServiceInterface
{
abstract public function creationRules(array $input): array;
abstract public function creationData(array $input): array;
abstract public function create(): void;
abstract public function delete(): void;
abstract public function data(): array;
}

49
app/SiteTypes/PHPMyAdmin.php Executable file
View File

@ -0,0 +1,49 @@
<?php
namespace App\SiteTypes;
use Illuminate\Validation\Rule;
class PHPMyAdmin extends PHPSite
{
public function supportedFeatures(): array
{
return [
//
];
}
public function createRules(array $input): array
{
return [
'php_version' => [
'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);
}
}