mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-05 16:02:34 +00:00
Plugins base (#613)
* wip * wip * cleanup * notification channels * phpstan * services * remove server types * refactoring * refactoring
This commit is contained in:
209
app/Services/Webserver/Caddy.php
Executable file
209
app/Services/Webserver/Caddy.php
Executable 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user