-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhpEls.php
More file actions
149 lines (122 loc) · 3.96 KB
/
Copy pathPhpEls.php
File metadata and controls
149 lines (122 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace App\Vito\Plugins\Siteway\VitodeployPhpElsPlugin;
use App\Exceptions\SSHCommandError;
use App\Services\AbstractService;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
class PhpEls extends AbstractService
{
public static function id(): string
{
return 'php-els';
}
public static function type(): string
{
return 'php-els';
}
public function unit(): string
{
return 'alt-php'.$this->versionNumber().'-fpm';
}
public function creationRules(array $input): array
{
return [
'version' => [
'required',
Rule::in(config('service.services.php-els.versions')),
Rule::unique('services', 'version')
->where('type', 'php-els')
->where('server_id', $this->service->server_id),
],
];
}
public function install(): void
{
$server = $this->service->server;
$server->ssh()->exec(
view('php-els::ssh.install-els', [
'version' => $this->versionNumber(),
'user' => $server->getSshUser(),
]),
'install-php-els-'.$this->service->version
);
$server->os()->cleanup();
}
public function uninstall(): void
{
$server = $this->service->server;
$server->ssh()->exec(
view('php-els::ssh.uninstall-els', [
'version' => $this->versionNumber(),
]),
'uninstall-php-els-'.$this->service->version
);
$server->os()->cleanup();
}
/**
* @throws SSHCommandError
*/
public function installExtension(string $name): void
{
$version = $this->versionNumber();
$result = $this->service->server->ssh()->exec(
view('php-els::ssh.install-extension', [
'version' => $version,
'name' => $name,
]),
'install-php-els-extension-'.$name
);
$pos = strpos($result, '[PHP Modules]');
if ($pos === false) {
throw new SSHCommandError('Failed to install extension');
}
$result = Str::substr($result, $pos);
if (! Str::contains($result, $name)) {
throw new SSHCommandError('Failed to install extension');
}
}
public function version(): string
{
$version = $this->versionNumber();
$result = $this->service->server->ssh()->exec(
'/opt/alt/php'.$version.'/usr/bin/php -r \'echo PHP_VERSION;\' 2>/dev/null'
);
if (preg_match('/(\d+\.\d+\.\d+)/', $result, $matches)) {
return $matches[1];
}
return $this->service->version;
}
public function createFpmPool(string $user, string $version): void
{
$versionNumber = str_replace('.', '', $version);
$this->service->server->ssh()->exec(
"sudo mkdir -p /run/alt-php{$versionNumber}-fpm",
'create-php-els-fpm-socket-dir'
);
$this->service->server->ssh()->write(
"/opt/alt/php{$versionNumber}/etc/php-fpm.d/{$user}.conf",
view('php-els::ssh.fpm-pool-isolated', [
'user' => $user,
'version' => $versionNumber,
]),
'root'
);
$this->service->server->systemd()->restart($this->unit());
}
public function removeFpmPool(string $user, string $version, ?int $siteId): void
{
$versionNumber = str_replace('.', '', $version);
$this->service->server->ssh()->exec(
"sudo rm -f /opt/alt/php{$versionNumber}/etc/php-fpm.d/{$user}.conf",
'remove-php-els-fpm-pool'
);
$this->service->server->systemd()->restart($this->unit());
}
/**
* Get the version number without dots (e.g., "7.3" → "73").
*/
public function versionNumber(): string
{
return str_replace('.', '', $this->service->version);
}
}