1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
use App\Models\PhpVersion; |
6
|
|
|
use App\Models\Setting; |
7
|
|
|
use App\Support\Console\DockerCompose\CliCommandFactory; |
8
|
|
|
use App\Support\Console\DockerCompose\YamlBuilder; |
9
|
|
|
use App\Support\Contracts\Cli; |
10
|
|
|
use App\Support\Contracts\ImageRepository; |
11
|
|
|
use App\Support\Contracts\ImageSetRepository; |
12
|
|
|
|
13
|
|
|
class Porter |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The docker images sets used by Porter to serve sites. |
17
|
|
|
* |
18
|
|
|
* @var ImageSetRepository |
19
|
|
|
*/ |
20
|
|
|
protected $imageSets; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The CLI class that executes commands. |
24
|
|
|
* |
25
|
|
|
* @var Cli |
26
|
|
|
*/ |
27
|
|
|
protected $cli; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The Docker composer command factory. |
31
|
|
|
* |
32
|
|
|
* @var CliCommandFactory |
33
|
|
|
*/ |
34
|
|
|
protected $dockerCompose; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The DockerCompose YAML file builder. |
38
|
|
|
* |
39
|
|
|
* @var YamlBuilder |
40
|
|
|
*/ |
41
|
|
|
protected $yamlBuilder; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Porter constructor. |
45
|
|
|
* |
46
|
|
|
* @param ImageSetRepository $imageSets |
47
|
|
|
* @param Cli $cli |
48
|
|
|
* @param CliCommandFactory $commandFactory |
49
|
|
|
* @param YamlBuilder $yamlBuilder |
50
|
|
|
*/ |
51
|
134 |
|
public function __construct( |
52
|
|
|
ImageSetRepository $imageSets, |
53
|
|
|
Cli $cli, |
54
|
|
|
CliCommandFactory $commandFactory, |
55
|
|
|
YamlBuilder $yamlBuilder |
56
|
|
|
) { |
57
|
134 |
|
$this->imageSets = $imageSets; |
58
|
134 |
|
$this->cli = $cli; |
59
|
134 |
|
$this->dockerCompose = $commandFactory; |
60
|
134 |
|
$this->yamlBuilder = $yamlBuilder; |
61
|
134 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check if the Porter containers are running. |
65
|
|
|
* |
66
|
|
|
* @param string|null $service |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
10 |
|
public function isUp($service = null) |
71
|
|
|
{ |
72
|
10 |
|
return (bool) stristr($this->dockerCompose->command('ps')->perform(), "porter_{$service}"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Create the docker-compose.yaml file. |
77
|
|
|
*/ |
78
|
7 |
|
public function compose() |
79
|
|
|
{ |
80
|
7 |
|
$this->yamlBuilder->build($this->getDockerImageSet()); |
81
|
7 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Start Porter containers, optionally start a specific service, and force them to be recreated. |
85
|
|
|
* |
86
|
|
|
* @param string|null $service |
87
|
|
|
* @param bool $recreate |
88
|
|
|
*/ |
89
|
6 |
|
public function start($service = null, $recreate = false) |
90
|
|
|
{ |
91
|
6 |
|
$recreate = $recreate ? '--force-recreate ' : ''; |
92
|
|
|
|
93
|
6 |
|
$this->dockerCompose->command("up -d {$recreate}--remove-orphans {$service}")->realTime()->perform(); |
94
|
6 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Stop Porter containers. |
98
|
|
|
* |
99
|
|
|
* @param string|null $service |
100
|
|
|
*/ |
101
|
4 |
|
public function stop($service = null) |
102
|
|
|
{ |
103
|
4 |
|
if ($service) { |
104
|
3 |
|
$this->dockerCompose->command("stop {$service}")->realTime()->perform(); |
105
|
|
|
|
106
|
3 |
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
$this->dockerCompose->command('down --remove-orphans')->realTime()->perform(); |
110
|
1 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Restart Porter containers. |
114
|
|
|
* |
115
|
|
|
* @param string|null $service |
116
|
|
|
*/ |
117
|
4 |
|
public function restart($service = null) |
118
|
|
|
{ |
119
|
4 |
|
if ($this->isUp($service)) { |
120
|
|
|
$this->stop($service); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// If we're restarting something it's probably because config changed - so force recreation |
124
|
4 |
|
$this->start($service, true); |
125
|
4 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Restart serving, picking up changes in used PHP versions and NGiNX. |
129
|
|
|
*/ |
130
|
2 |
|
public function restartServing() |
131
|
|
|
{ |
132
|
|
|
// Build up docker-compose again - so we pick up any new PHP containers to be used |
133
|
2 |
|
$this->compose(); |
134
|
|
|
|
135
|
2 |
|
if (!$this->isUp()) { |
136
|
1 |
|
return; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
PhpVersion::active() |
140
|
1 |
|
->get() |
141
|
|
|
->reject(function ($phpVersion) { |
142
|
1 |
|
return $this->isUp($phpVersion->fpm_name); |
143
|
1 |
|
}) |
144
|
|
|
->each(function ($phpVersion) { |
145
|
1 |
|
$this->start($phpVersion->fpm_name); |
146
|
1 |
|
$this->start($phpVersion->cli_name); |
147
|
1 |
|
}); |
148
|
|
|
|
149
|
1 |
|
$this->restart('nginx'); |
150
|
1 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Turn a service on. |
154
|
|
|
* |
155
|
|
|
* @param string $service |
156
|
|
|
*/ |
157
|
2 |
|
public function turnOnService($service) |
158
|
|
|
{ |
159
|
2 |
|
if (setting("use_{$service}") == 'on') { |
160
|
1 |
|
return; |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
Setting::updateOrCreate("use_{$service}", 'on'); |
164
|
|
|
|
165
|
1 |
|
$this->compose(); |
166
|
|
|
|
167
|
1 |
|
if ($this->isUp()) { |
168
|
1 |
|
$this->start($service); |
169
|
|
|
} |
170
|
1 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Turn a service off. |
174
|
|
|
* |
175
|
|
|
* @param string $service |
176
|
|
|
*/ |
177
|
2 |
|
public function turnOffService($service) |
178
|
|
|
{ |
179
|
2 |
|
if (in_array(setting("use_{$service}"), [null, 'off'])) { |
180
|
1 |
|
return; |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
Setting::updateOrCreate("use_{$service}", 'off'); |
184
|
|
|
|
185
|
1 |
|
if ($this->isUp()) { |
186
|
1 |
|
$this->stop($service); |
187
|
|
|
} |
188
|
1 |
|
$this->compose(); |
189
|
1 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* (Re)build Porter containers. |
193
|
|
|
*/ |
194
|
1 |
|
public function build() |
195
|
|
|
{ |
196
|
1 |
|
$this->dockerCompose->command('build')->perform(); |
197
|
1 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get the current image set to use. |
201
|
|
|
* |
202
|
|
|
* @return ImageRepository |
203
|
|
|
*/ |
204
|
7 |
|
public function getDockerImageSet() |
205
|
|
|
{ |
206
|
7 |
|
return $this->imageSets->getImageRepository( |
207
|
7 |
|
setting('docker_image_set', config('porter.default-docker-image-set')) |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Show container status. |
213
|
|
|
*/ |
214
|
1 |
|
public function status() |
215
|
|
|
{ |
216
|
1 |
|
echo $this->dockerCompose->command('ps')->perform(); |
217
|
1 |
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Show container logs. |
221
|
|
|
* |
222
|
|
|
* @param string|null $service |
223
|
|
|
*/ |
224
|
1 |
|
public function logs($service = null) |
225
|
|
|
{ |
226
|
1 |
|
echo $this->dockerCompose->command("logs {$service}")->perform(); |
227
|
1 |
|
} |
228
|
|
|
} |
229
|
|
|
|