vito/app/SSH/Services/PHP/PHP.php
Richard Anderson c1ae58772c
Isolate Users (#431)
* WIP to isolate users

* Resolved issue with SSH AsUser

Updated Isolated User Script to use Server User for Team Access
Updated Path creation script to simplify for running as the isolated user

* Included the server user

* PHPMyAdmin script updated

Wordpress Script Updated
Updated Execute Script to support executing as isolated users

* Issue Resolution & Resolved Failing Unit Tests

* Fix for isolated_username vs user

* Run the deploy as the isolated user

* queue updates for isolated user

* Support isolated users in cronjobs

* script tests for isolated users

* Queue tests for isolated users

* Cronjob tests for isolated user

* Removed default queue command for laravel apps

* add default user to factory

* laravel pint fixes

* ensure echos are consistent

* removed unneeded parameter

* update

* fix queues for isolated users

* revert addslashes

---------

Co-authored-by: Saeed Vaziry <mr.saeedvaziry@gmail.com>
2025-01-18 01:17:48 +01:00

142 lines
4.0 KiB
PHP

<?php
namespace App\SSH\Services\PHP;
use App\Exceptions\SSHCommandError;
use App\SSH\HasScripts;
use App\SSH\Services\AbstractService;
use Closure;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
class PHP extends AbstractService
{
use HasScripts;
public function creationRules(array $input): array
{
return [
'version' => [
'required',
Rule::in(config('core.php_versions')),
Rule::notIn([\App\Enums\PHP::NONE]),
Rule::unique('services', 'version')
->where('type', 'php')
->where('server_id', $this->service->server_id),
],
];
}
public function deletionRules(): array
{
return [
'service' => [
function (string $attribute, mixed $value, Closure $fail) {
$hasSite = $this->service->server->sites()
->where('php_version', $this->service->version)
->exists();
if ($hasSite) {
$fail('Some sites are using this PHP version.');
}
},
],
];
}
public function install(): void
{
$server = $this->service->server;
$server->ssh()->exec(
$this->getScript('install-php.sh', [
'version' => $this->service->version,
'user' => $server->getSshUser(),
]),
'install-php-'.$this->service->version
);
$this->installComposer();
$this->service->server->os()->cleanup();
}
public function uninstall(): void
{
$this->service->server->ssh()->exec(
$this->getScript('uninstall-php.sh', [
'version' => $this->service->version,
]),
'uninstall-php-'.$this->service->version
);
$this->service->server->os()->cleanup();
}
public function setDefaultCli(): void
{
$this->service->server->ssh()->exec(
$this->getScript('change-default-php.sh', [
'version' => $this->service->version,
]),
'change-default-php'
);
}
/**
* @throws SSHCommandError
*/
public function installExtension($name): void
{
$result = $this->service->server->ssh()->exec(
$this->getScript('install-php-extension.sh', [
'version' => $this->service->version,
'name' => $name,
]),
'install-php-extension-'.$name
);
$result = Str::substr($result, strpos($result, '[PHP Modules]'));
if (! Str::contains($result, $name)) {
throw new SSHCommandError('Failed to install extension');
}
}
public function installComposer(): void
{
$this->service->server->ssh()->exec(
$this->getScript('install-composer.sh'),
'install-composer'
);
}
public function getPHPIni(string $type): string
{
return $this->service->server->os()->readFile(
sprintf('/etc/php/%s/%s/php.ini', $this->service->version, $type)
);
}
public function createFpmPool(string $user, string $version, $site_id): void
{
$this->service->server->ssh()->exec(
$this->getScript('create-fpm-pool.sh', [
'user' => $user,
'version' => $version,
'config' => $this->getScript('fpm-pool.conf', [
'user' => $user,
'version' => $version,
]),
]),
"create-{$version}fpm-pool-{$user}",
$site_id
);
}
public function removeFpmPool(string $user, string $version, $site_id): void
{
$this->service->server->ssh()->exec(
$this->getScript('remove-fpm-pool.sh', [
'user' => $user,
'version' => $version,
]),
"remove-{$version}fpm-pool-{$user}",
$site_id
);
}
}