1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DockerCompose\Manager; |
4
|
|
|
|
5
|
|
|
use DockerCompose\Exception\ComposeFileNotFoundException; |
6
|
|
|
use DockerCompose\Exception\DockerHostConnexionErrorException; |
7
|
|
|
use DockerCompose\Exception\DockerInstallationMissingException; |
8
|
|
|
use DockerCompose\Exception\NoSuchServiceException; |
9
|
|
|
use DockerCompose\ComposeFileCollection; |
10
|
|
|
use mikehaertl\shellcommand\Command; |
11
|
|
|
use Exception; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* DockerCompose\Manager\ComposeManager |
15
|
|
|
*/ |
16
|
|
|
class ComposeManager |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Start service containers |
21
|
|
|
* |
22
|
|
|
* @param mixed $composeFiles The compose files names |
23
|
|
|
*/ |
24
|
|
|
public function start($composeFiles = array()) |
25
|
|
|
{ |
26
|
|
|
return $this->processResult( |
27
|
|
|
$this->execute( |
28
|
|
|
$this->formatCommand('up -d', $this->createComposeFileCollection($composeFiles)) |
29
|
|
|
) |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Stop service containers |
35
|
|
|
* |
36
|
|
|
* @param mixed $composeFiles The compose files names |
37
|
|
|
*/ |
38
|
|
|
public function stop($composeFiles = array()) |
39
|
|
|
{ |
40
|
|
|
return $this->processResult( |
41
|
|
|
$this->execute( |
42
|
|
|
$this->formatCommand('stop', $this->createComposeFileCollection($composeFiles)) |
43
|
|
|
) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Stop service containers |
49
|
|
|
* |
50
|
|
|
* @param mixed $composeFiles The compose files names |
51
|
|
|
* @param boolean $force If the remove need to be force (default=false) |
52
|
|
|
* @param boolean $removeVolumes If we need to remove the volumes (default=false) |
53
|
|
|
*/ |
54
|
|
|
public function remove($composeFiles = array(), $force = false, $removeVolumes = false) |
55
|
|
|
{ |
56
|
|
|
$command = 'rm'; |
57
|
|
|
if ($force) { |
58
|
|
|
$command .= ' --force'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($removeVolumes) { |
62
|
|
|
$command .= ' -v'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->processResult( |
66
|
|
|
$this->execute( |
67
|
|
|
$this->formatCommand($command, $this->createComposeFileCollection($composeFiles)) |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Build service images |
74
|
|
|
*/ |
75
|
|
|
public function build($composeFiles = array()) |
76
|
|
|
{ |
77
|
|
|
return $this->processResult( |
78
|
|
|
$this->execute( |
79
|
|
|
$this->formatCommand('build', $this->createComposeFileCollection($composeFiles)) |
80
|
|
|
) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Run service with command |
86
|
|
|
* |
87
|
|
|
* @param string $service Service name |
88
|
|
|
* @param string $command Command to pass to service |
89
|
|
|
* @param mixed $composeFiles The compose files names |
90
|
|
|
*/ |
91
|
|
|
public function run($service, $command, $composeFiles = array()) |
92
|
|
|
{ |
93
|
|
|
$command = 'run --rm ' . $service . ' ' . $command; |
94
|
|
|
$result = $this->execute( |
95
|
|
|
$this->formatCommand($command, $this->createComposeFileCollection($composeFiles)) |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
if ($result['code'] == 1 && strpos($result['output'], 'No such service') != false) { |
|
|
|
|
99
|
|
|
throw new NoSuchServiceException($result['output']); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->processResult($result); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Process result with returned code and output |
107
|
|
|
* |
108
|
|
|
* @param array $result The result of command with output and returnCode |
109
|
|
|
* |
110
|
|
|
* @throws DockerInstallationMissingException When returned code is 127 |
111
|
|
|
* @throws ComposeFileNotFoundException When no compose file precise and docker-compose.yml not found |
112
|
|
|
* @throws DockerHostConnexionErrorException When we can't connect to docker host |
113
|
|
|
* @throws \Exception When an unknown error is returned |
114
|
|
|
*/ |
115
|
|
|
private function processResult($result) |
116
|
|
|
{ |
117
|
|
|
if ($result['code'] === 127) { |
118
|
|
|
throw new DockerInstallationMissingException(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($result['code'] === 1) { |
122
|
|
|
if (!strpos($result['output'], 'DOCKER_HOST')) { |
123
|
|
|
if (!strpos($result['output'], 'docker-compose.yml')) { |
124
|
|
|
throw new Exception($result['output']); |
125
|
|
|
} else { |
126
|
|
|
throw new ComposeFileNotFoundException(); |
127
|
|
|
} |
128
|
|
|
} else { |
129
|
|
|
throw new DockerHostConnexionErrorException(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $result['output']; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Create the composeFileCollection from the type of value given |
138
|
|
|
* |
139
|
|
|
* @param mixed $composeFiles The docker-compose files (can be array, string or ComposeFile) |
140
|
|
|
* |
141
|
|
|
* @return ComposeFileCollection |
142
|
|
|
*/ |
143
|
|
|
private function createComposeFileCollection($composeFiles) |
144
|
|
|
{ |
145
|
|
|
if (!$composeFiles instanceof ComposeFileCollection) { |
146
|
|
|
if (!is_array($composeFiles)) { |
147
|
|
|
return new ComposeFileCollection([$composeFiles]); |
148
|
|
|
} else { |
149
|
|
|
return new ComposeFileCollection($composeFiles); |
150
|
|
|
} |
151
|
|
|
} else { |
152
|
|
|
return $composeFiles; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Format the command to execute |
158
|
|
|
* |
159
|
|
|
* @param string $subcommand The subcommand to pass to docker-compose command |
160
|
|
|
* @param ComposeFileCollection $composeFiles The compose files to precise in the command |
161
|
|
|
*/ |
162
|
|
|
private function formatCommand($subcommand, ComposeFileCollection $composeFiles) |
163
|
|
|
{ |
164
|
|
|
$project = ''; |
165
|
|
|
$networking = ''; |
166
|
|
|
$networkDriver = ''; |
167
|
|
|
|
168
|
|
|
# Add project name, and network options |
169
|
|
|
if ($composeFiles->getProjectName() != null) { |
170
|
|
|
$project = ' --project-name ' . $composeFiles->getProjectName(); |
171
|
|
|
if ($composeFiles->isNetworking()) { |
172
|
|
|
$networking = ' --x-networking'; |
173
|
|
|
if ($composeFiles->getNetworkDriver() != null) { |
174
|
|
|
$networkDriver = ' --x-network-driver ' . $composeFiles->getNetworkDriver(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
# Add files names |
180
|
|
|
$preciseFiles = ''; |
181
|
|
|
foreach ($composeFiles->getAll() as $composeFile) { |
182
|
|
|
$preciseFiles .= ' -f ' . $composeFile->getFileName(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$command = 'docker-compose' . $preciseFiles . $networking . $networkDriver . $project . ' ' . $subcommand; |
186
|
|
|
|
187
|
|
|
return $command; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Execute docker-compose commande |
192
|
|
|
* @codeCoverageIgnore |
193
|
|
|
* @param string $command The command to execute |
194
|
|
|
*/ |
195
|
|
|
protected function execute($command) |
196
|
|
|
{ |
197
|
|
|
$exec = new Command($command); |
198
|
|
|
|
199
|
|
|
if ($exec->execute()) { |
200
|
|
|
$output = $exec->getOutput(); |
201
|
|
|
} else { |
202
|
|
|
$output = $exec->getError(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return array( |
206
|
|
|
'output' => $output, |
207
|
|
|
'code' => $exec->getExitCode() |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|