Plugins base (#613)

* wip

* wip

* cleanup

* notification channels

* phpstan

* services

* remove server types

* refactoring

* refactoring
This commit is contained in:
Saeed Vaziry
2025-06-14 14:35:18 +02:00
committed by GitHub
parent adc0653d15
commit 131b828807
311 changed files with 3976 additions and 2660 deletions

View File

@ -0,0 +1,101 @@
<?php
namespace App\Services\Webserver;
use App\Models\Site;
use App\Services\AbstractService;
use Closure;
use InvalidArgumentException;
abstract class AbstractWebserver extends AbstractService implements Webserver
{
public function creationRules(array $input): array
{
return [
'name' => [
'required',
function (string $attribute, mixed $value, Closure $fail): void {
$webserverExists = $this->service->server->webserver();
if ($webserverExists) {
$fail('You already have a webserver service on the server.');
}
},
],
];
}
public function deletionRules(): array
{
return [
'service' => [
function (string $attribute, mixed $value, Closure $fail): void {
$hasSite = $this->service->server->sites()
->exists();
if ($hasSite) {
$fail('Cannot uninstall webserver while you have websites using it.');
}
},
],
];
}
/**
* @param array<string, string> $replace replace blocks
* @param array<int, string> $regenerate regenerates the blocks
* @param array<string, string> $append appends to the blocks
*/
protected function getUpdatedVHost(Site $site, string $vhost, array $replace = [], array $regenerate = [], array $append = []): string
{
foreach ($replace as $block => $replacement) {
$vhost = preg_replace(
'/#\['.$block.'](.*?)#\[\/'.$block.']/s',
$replacement,
$vhost
);
}
foreach ($regenerate as $block) {
$vhost = preg_replace(
'/#\['.$block.'](.*?)#\[\/'.$block.']/s',
$this->generateVhost($site, $block),
$vhost
);
}
foreach ($append as $block => $content) {
/**
* #[block]
* content
* content
* content
* content
* --append-here--
* #[/block]
*/
$vhost = preg_replace(
'/(#\['.$block.'](.*?)\n)(?=#\[\/'.$block.'])/s',
"\$1$content\n",
$vhost
);
}
return $vhost;
}
protected function generateVhost(Site $site, ?string $block = null): string
{
$viewPath = 'ssh.services.webserver.'.$this::id().'.vhost-blocks.'.$block;
if ($block) {
if (! view()->exists($viewPath)) {
throw new InvalidArgumentException("View for block '{$block}' does not exist.");
}
$vhost = view($viewPath, [
'site' => $site,
]);
} else {
$vhost = $site->type()->vhost($this::id());
}
return format_nginx_config($vhost);
}
}

209
app/Services/Webserver/Caddy.php Executable file
View File

@ -0,0 +1,209 @@
<?php
namespace App\Services\Webserver;
use App\Exceptions\SSHError;
use App\Exceptions\SSLCreationException;
use App\Models\Site;
use App\Models\Ssl;
use Throwable;
class Caddy extends AbstractWebserver
{
public static function id(): string
{
return 'caddy';
}
public static function type(): string
{
return 'webserver';
}
public function unit(): string
{
return 'caddy';
}
/**
* @throws SSHError
*/
public function install(): void
{
$this->service->server->ssh()->exec(
view('ssh.services.webserver.caddy.install-caddy'),
'install-caddy'
);
$this->service->server->ssh()->write(
'/etc/caddy/Caddyfile',
view('ssh.services.webserver.caddy.caddy'),
'root'
);
$this->service->server->ssh()->write(
'/etc/systemd/system/caddy.service',
view('ssh.services.webserver.caddy.caddy-systemd'),
'root'
);
$this->service->server->systemd()->reload();
$this->service->server->systemd()->restart('caddy');
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function uninstall(): void
{
$this->service->server->ssh()->exec(
view('ssh.services.webserver.caddy.uninstall-caddy'),
'uninstall-caddy'
);
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function createVHost(Site $site): void
{
// We need to get the isolated user first, if the site is isolated
// otherwise, use the default ssh user
$ssh = $this->service->server->ssh($site->user);
$ssh->exec(
view('ssh.services.webserver.caddy.create-path', [
'path' => $site->path,
]),
'create-path',
$site->id
);
$this->service->server->ssh()->write(
'/etc/caddy/sites-available/'.$site->domain,
$this->generateVhost($site),
'root'
);
$this->service->server->ssh()->exec(
view('ssh.services.webserver.caddy.create-vhost', [
'domain' => $site->domain,
]),
'create-vhost',
$site->id
);
}
/**
* @throws SSHError
*/
public function updateVHost(Site $site, ?string $vhost = null, array $replace = [], array $regenerate = [], array $append = []): void
{
if (! $vhost) {
$vhost = $this->getVHost($site);
}
if (! $vhost || ! preg_match('/#\[main]/', $vhost)) {
$vhost = $this->generateVhost($site);
}
$this->service->server->ssh()->write(
'/etc/caddy/sites-available/'.$site->domain,
format_nginx_config($this->getUpdatedVHost($site, $vhost, $replace, $regenerate, $append)),
'root'
);
$this->service->server->systemd()->restart('caddy');
}
/**
* @throws SSHError
*/
public function getVHost(Site $site): string
{
return $this->service->server->ssh()->exec(
view('ssh.services.webserver.caddy.get-vhost', [
'domain' => $site->domain,
]),
);
}
/**
* @throws SSHError
*/
public function deleteSite(Site $site): void
{
$this->service->server->ssh()->exec(
view('ssh.services.webserver.caddy.delete-site', [
'domain' => $site->domain,
'path' => $site->path,
]),
'delete-vhost',
$site->id
);
$this->service->restart();
}
/**
* @throws SSHError
*/
public function changePHPVersion(Site $site, string $version): void
{
$this->service->server->ssh()->exec(
view('ssh.services.webserver.caddy.change-php-version', [
'domain' => $site->domain,
'oldVersion' => $site->php_version,
'newVersion' => $version,
]),
'change-php-version',
$site->id
);
}
/**
* @throws SSHError
*/
public function setupSSL(Ssl $ssl): void
{
if ($ssl->type == 'custom') {
$ssl->certificate_path = '/etc/ssl/'.$ssl->id.'/cert.pem';
$ssl->pk_path = '/etc/ssl/'.$ssl->id.'/privkey.pem';
$ssl->save();
$command = view('ssh.services.webserver.caddy.create-custom-ssl', [
'path' => dirname($ssl->certificate_path),
'certificate' => $ssl->certificate,
'pk' => $ssl->pk,
'certificatePath' => $ssl->certificate_path,
'pkPath' => $ssl->pk_path,
]);
$result = $this->service->server->ssh()->setLog($ssl->log)->exec(
$command,
'create-ssl',
$ssl->site_id
);
if (! $ssl->validateSetup($result)) {
throw new SSLCreationException;
}
}
}
/**
* @throws Throwable
*/
public function removeSSL(Ssl $ssl): void
{
if ($ssl->certificate_path) {
$this->service->server->ssh()->exec(
'sudo rm -rf '.dirname($ssl->certificate_path),
'remove-ssl',
$ssl->site_id
);
}
$this->updateVHost($ssl->site);
}
}

213
app/Services/Webserver/Nginx.php Executable file
View File

@ -0,0 +1,213 @@
<?php
namespace App\Services\Webserver;
use App\Exceptions\SSHError;
use App\Exceptions\SSLCreationException;
use App\Models\Site;
use App\Models\Ssl;
use Throwable;
class Nginx extends AbstractWebserver
{
public static function id(): string
{
return 'nginx';
}
public static function type(): string
{
return 'webserver';
}
public function unit(): string
{
return 'nginx';
}
/**
* @throws SSHError
*/
public function install(): void
{
$this->service->server->ssh()->exec(
view('ssh.services.webserver.nginx.install-nginx'),
'install-nginx'
);
$this->service->server->ssh()->write(
'/etc/nginx/nginx.conf',
view('ssh.services.webserver.nginx.nginx', [
'user' => $this->service->server->getSshUser(),
]),
'root'
);
$this->service->server->systemd()->restart('nginx');
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function uninstall(): void
{
$this->service->server->ssh()->exec(
view('ssh.services.webserver.nginx.uninstall-nginx'),
'uninstall-nginx'
);
$this->service->server->os()->cleanup();
}
/**
* @throws SSHError
*/
public function createVHost(Site $site): void
{
// We need to get the isolated user first, if the site is isolated
// otherwise, use the default ssh user
$ssh = $this->service->server->ssh($site->user);
$ssh->exec(
view('ssh.services.webserver.nginx.create-path', [
'path' => $site->path,
]),
'create-path',
$site->id
);
$this->service->server->ssh()->write(
'/etc/nginx/sites-available/'.$site->domain,
$this->generateVhost($site),
'root'
);
$this->service->server->ssh()->exec(
view('ssh.services.webserver.nginx.create-vhost', [
'domain' => $site->domain,
'vhost' => $this->generateVhost($site),
]),
'create-vhost',
$site->id
);
}
/**
* @throws SSHError
*/
public function updateVHost(Site $site, ?string $vhost = null, array $replace = [], array $regenerate = [], array $append = []): void
{
if (! $vhost) {
$vhost = $this->getVHost($site);
}
if (! $vhost || ! preg_match('/#\[header]/', $vhost) || ! preg_match('/#\[main]/', $vhost) || ! preg_match('/#\[footer]/', $vhost)) {
$vhost = $this->generateVhost($site);
}
$this->service->server->ssh()->write(
'/etc/nginx/sites-available/'.$site->domain,
format_nginx_config($this->getUpdatedVHost($site, $vhost, $replace, $regenerate, $append)),
'root'
);
$this->service->server->systemd()->restart('nginx');
}
/**
* @throws SSHError
*/
public function getVHost(Site $site): string
{
return $this->service->server->ssh()->exec(
view('ssh.services.webserver.nginx.get-vhost', [
'domain' => $site->domain,
]),
);
}
/**
* @throws SSHError
*/
public function deleteSite(Site $site): void
{
$this->service->server->ssh()->exec(
view('ssh.services.webserver.nginx.delete-site', [
'domain' => $site->domain,
'path' => $site->path,
]),
'delete-vhost',
$site->id
);
$this->service->restart();
}
/**
* @throws SSHError
*/
public function changePHPVersion(Site $site, string $version): void
{
$this->service->server->ssh()->exec(
view('ssh.services.webserver.nginx.change-php-version', [
'domain' => $site->domain,
'oldVersion' => $site->php_version,
'newVersion' => $version,
]),
'change-php-version',
$site->id
);
}
/**
* @throws SSHError
*/
public function setupSSL(Ssl $ssl): void
{
$domains = '';
foreach ($ssl->getDomains() as $domain) {
$domains .= ' -d '.$domain;
}
$command = view('ssh.services.webserver.nginx.create-letsencrypt-ssl', [
'email' => $ssl->email,
'name' => $ssl->id,
'domains' => $domains,
]);
if ($ssl->type == 'custom') {
$ssl->certificate_path = '/etc/ssl/'.$ssl->id.'/cert.pem';
$ssl->pk_path = '/etc/ssl/'.$ssl->id.'/privkey.pem';
$ssl->save();
$command = view('ssh.services.webserver.nginx.create-custom-ssl', [
'path' => dirname($ssl->certificate_path),
'certificate' => $ssl->certificate,
'pk' => $ssl->pk,
'certificatePath' => $ssl->certificate_path,
'pkPath' => $ssl->pk_path,
]);
}
$result = $this->service->server->ssh()->setLog($ssl->log)->exec(
$command,
'create-ssl',
$ssl->site_id
);
if (! $ssl->validateSetup($result)) {
throw new SSLCreationException;
}
}
/**
* @throws Throwable
*/
public function removeSSL(Ssl $ssl): void
{
if ($ssl->certificate_path) {
$this->service->server->ssh()->exec(
'sudo rm -rf '.dirname($ssl->certificate_path),
'remove-ssl',
$ssl->site_id
);
}
$this->updateVHost($ssl->site);
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Services\Webserver;
use App\Models\Site;
use App\Models\Ssl;
use App\Services\ServiceInterface;
interface Webserver extends ServiceInterface
{
public function createVHost(Site $site): void;
/**
* @param array<string, string> $replace replace blocks
* @param array<int, string> $regenerate regenerates the blocks
* @param array<string, string> $append appends to the blocks
*/
public function updateVHost(Site $site, ?string $vhost = null, array $replace = [], array $regenerate = [], array $append = []): void;
public function getVHost(Site $site): string;
public function deleteSite(Site $site): void;
public function changePHPVersion(Site $site, string $version): void;
public function setupSSL(Ssl $ssl): void;
public function removeSSL(Ssl $ssl): void;
}