1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SamIT\Yii2\PhpFpm\controllers; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Docker\API\Model\AuthConfig; |
8
|
|
|
use Docker\API\Model\BuildInfo; |
9
|
|
|
use Docker\API\Model\PushImageInfo; |
10
|
|
|
use Docker\Docker; |
11
|
|
|
use Docker\Stream\BuildStream; |
12
|
|
|
use Docker\Stream\PushStream; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use SamIT\Yii2\PhpFpm\Module; |
15
|
|
|
use yii\base\InvalidConfigException; |
16
|
|
|
use yii\console\Controller; |
17
|
|
|
use yii\helpers\Console; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class BuildController |
21
|
|
|
* @package SamIT\Yii2\PhpFpm\controllers |
22
|
|
|
* @property Module $module |
23
|
|
|
*/ |
24
|
|
|
class BuildController extends Controller |
25
|
|
|
{ |
26
|
|
|
public $defaultAction = 'build'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string The name of the created image |
30
|
|
|
* If not explicitly set will take its default from module config. |
31
|
|
|
*/ |
32
|
|
|
public $image; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string The tag of the created image |
36
|
|
|
* If not explicitly set will take its default from module config. |
37
|
|
|
*/ |
38
|
|
|
public $tag; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool whether to push the image after a successful build. |
42
|
|
|
* If not explicitly set will take its default from module config. |
43
|
|
|
*/ |
44
|
|
|
public $push; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Docker |
48
|
|
|
*/ |
49
|
|
|
protected $docker; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string the user to authenticate against the repository |
53
|
|
|
*/ |
54
|
|
|
public $user; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string the password to authenticate against the repository |
58
|
|
|
*/ |
59
|
|
|
public $password; |
60
|
|
|
|
61
|
3 |
|
public function init(): void |
62
|
|
|
{ |
63
|
3 |
|
parent::init(); |
64
|
3 |
|
$this->docker = Docker::create(); |
65
|
3 |
|
$this->push = $this->module->push; |
66
|
3 |
|
$this->image = $this->module->image; |
67
|
3 |
|
$this->tag = $this->module->tag; |
68
|
3 |
|
} |
69
|
|
|
|
70
|
2 |
|
public function actionBuild(): void |
71
|
|
|
{ |
72
|
2 |
|
if ($this->push && !isset($this->image, $this->user, $this->password)) { |
73
|
1 |
|
throw new InvalidConfigException("When using the push option, you must configure or provide user, password and image"); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$params = []; |
77
|
|
|
|
78
|
|
|
|
79
|
1 |
|
if (isset($this->image)) { |
80
|
1 |
|
$params['t'] = "{$this->image}:{$this->tag}"; |
81
|
|
|
} |
82
|
1 |
|
$buildStream = $this->createBuildStream($params); |
83
|
1 |
|
$this->color = true; |
84
|
1 |
|
$buildStream->onFrame(\Closure::fromCallable([$this, 'logBuildInfo'])); |
85
|
1 |
|
$buildStream->wait(); |
86
|
1 |
|
$this->stdout("Wait finished\n"); |
87
|
1 |
|
$buildStream->wait(); |
88
|
|
|
|
89
|
1 |
|
if ($this->push) { |
90
|
1 |
|
$this->pushImage(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
private function logBuildInfo(BuildInfo $buildInfo): void |
95
|
|
|
{ |
96
|
1 |
|
$this->stdout($buildInfo->getStream(), Console::FG_CYAN); |
97
|
1 |
|
$this->stdout($buildInfo->getProgress(), Console::FG_YELLOW); |
98
|
1 |
|
$this->stdout($buildInfo->getStatus(), Console::FG_RED); |
99
|
1 |
|
if (!empty($buildInfo->getProgressDetail())) { |
100
|
|
|
$this->stdout($buildInfo->getProgressDetail()->getMessage(), Console::FG_YELLOW); |
101
|
|
|
} |
102
|
1 |
|
if (!empty($buildInfo->getErrorDetail())) { |
103
|
|
|
$this->stdout($buildInfo->getErrorDetail()->getCode() . ':' . $buildInfo->getErrorDetail()->getMessage(), Console::FG_YELLOW); |
104
|
|
|
} |
105
|
1 |
|
if (!is_null($buildInfo->getError())) { |
106
|
|
|
throw new \Exception($buildInfo->getError() . ':' . $buildInfo->getErrorDetail()->getMessage()); |
107
|
|
|
} |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Push a docker container image |
112
|
|
|
* @param string $name |
|
|
|
|
113
|
|
|
* @throws \Exception |
114
|
|
|
*/ |
115
|
1 |
|
private function pushImage(): void |
116
|
|
|
{ |
117
|
1 |
|
if (!isset($this->image)) { |
118
|
|
|
throw new \InvalidArgumentException("Image name must be configured when pushing images"); |
119
|
|
|
} |
120
|
1 |
|
$name = "{$this->image}:{$this->tag}"; |
121
|
|
|
|
122
|
1 |
|
$authConfig = new AuthConfig(); |
123
|
1 |
|
$authConfig->setUsername($this->user); |
124
|
1 |
|
$authConfig->setPassword($this->password); |
125
|
|
|
$params = [ |
126
|
1 |
|
'X-Registry-Auth' => $authConfig |
127
|
|
|
]; |
128
|
|
|
/** @var PushStream $pushStream */ |
129
|
1 |
|
$pushStream = $this->docker->imagePush($name, [], $params ?? [], Docker::FETCH_OBJECT); |
130
|
|
|
|
131
|
1 |
|
if ($pushStream instanceof ResponseInterface) { |
132
|
|
|
throw new \Exception($pushStream->getReasonPhrase() . ':' . $pushStream->getBody()->getContents(), $pushStream->getStatusCode()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$pushStream->onFrame(function(PushImageInfo $pushImageInfo): void { |
136
|
1 |
|
if (!empty($pushImageInfo->getError())) { |
137
|
1 |
|
throw new \Exception($pushImageInfo->getError()); |
138
|
|
|
} |
139
|
1 |
|
$this->stdout($pushImageInfo->getProgress(), Console::FG_YELLOW); |
140
|
1 |
|
$this->stdout($pushImageInfo->getStatus(), Console::FG_RED); |
141
|
1 |
|
}); |
142
|
1 |
|
$pushStream->wait(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function actionTestClient(): void |
146
|
|
|
{ |
147
|
|
|
$this->stdout("It seems the console client works!\n", Console::FG_GREEN); |
148
|
|
|
} |
149
|
|
|
|
150
|
2 |
|
public function createBuildStream(array $params = []): BuildStream |
151
|
|
|
{ |
152
|
|
|
|
153
|
2 |
|
$context = $this->module->createBuildContext(); |
154
|
2 |
|
return $this->docker->imageBuild($context->toStream(), $params, [], Docker::FETCH_OBJECT); |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
public function options($actionID) |
158
|
|
|
{ |
159
|
|
|
|
160
|
1 |
|
$result = parent::options($actionID); |
161
|
|
|
switch ($actionID) { |
162
|
1 |
|
case 'build': |
163
|
1 |
|
$result[] = 'push'; |
164
|
1 |
|
$result[] = 'image'; |
165
|
1 |
|
$result[] = 'tag'; |
166
|
1 |
|
$result[] = 'user'; |
167
|
1 |
|
$result[] = 'password'; |
168
|
1 |
|
break; |
169
|
|
|
|
170
|
|
|
} |
171
|
1 |
|
return $result; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function optionAliases() |
175
|
|
|
{ |
176
|
|
|
$result = parent::optionAliases(); |
177
|
|
|
$result['p'] = 'push'; |
178
|
|
|
$result['t'] = 'tag'; |
179
|
|
|
$result['i'] = 'image'; |
180
|
|
|
$result['u'] = 'user'; |
181
|
|
|
$result['P'] = 'password'; |
182
|
|
|
return $result; |
183
|
|
|
} |
184
|
|
|
|
185
|
1 |
View Code Duplication |
public function stdout($string) |
|
|
|
|
186
|
|
|
{ |
187
|
1 |
|
if ($this->isColorEnabled()) { |
188
|
1 |
|
$args = \func_get_args(); |
189
|
1 |
|
\array_shift($args); |
190
|
1 |
|
$string = Console::ansiFormat($string, $args); |
191
|
|
|
} |
192
|
|
|
|
193
|
1 |
|
echo $string; |
194
|
1 |
|
return \strlen($string); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.