1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SamIT\Yii2\PhpFpm\controllers; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Docker\API\Model\BuildInfo; |
8
|
|
|
use Docker\Context\Context; |
9
|
|
|
use Docker\Context\ContextBuilder; |
10
|
|
|
use Docker\Context\ContextInterface; |
11
|
|
|
use Docker\Docker; |
12
|
|
|
use Docker\Stream\BuildStream; |
13
|
|
|
use SamIT\Yii2\PhpFpm\Module; |
14
|
|
|
use yii\console\Controller; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class BuildController |
18
|
|
|
* @package SamIT\Yii2\PhpFpm\controllers |
19
|
|
|
* @property Module $module |
20
|
|
|
*/ |
21
|
|
|
class BuildController extends Controller |
22
|
|
|
{ |
23
|
|
|
public $defaultAction = 'build'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string The name of the created image |
27
|
|
|
* If not explicitly set will take its default from module config. |
28
|
|
|
*/ |
29
|
|
|
public $image; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string The tag of the created image |
33
|
|
|
* If not explicitly set will take its default from module config. |
34
|
|
|
*/ |
35
|
|
|
public $tag; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var bool whether to push the image after a successful build. |
39
|
|
|
* If not explicitly set will take its default from module config. |
40
|
|
|
*/ |
41
|
|
|
public $push; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Docker |
45
|
|
|
*/ |
46
|
|
|
protected $docker; |
47
|
|
|
|
48
|
|
|
public function init(): void |
49
|
|
|
{ |
50
|
|
|
parent::init(); |
51
|
|
|
$this->docker = Docker::create(); |
52
|
|
|
$this->push = $this->module->push; |
53
|
|
|
$this->image = $this->module->image; |
54
|
|
|
$this->tag = $this->module->tag; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function actionBuild(): void |
58
|
|
|
{ |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
$params = []; |
62
|
|
|
if (isset($this->image)) { |
63
|
|
|
$name = "{$this->image}:{$this->tag}"; |
64
|
|
|
$params['t'] = $name; |
65
|
|
|
} |
66
|
|
|
$buildStream = $this->createBuildStream($params); |
67
|
|
|
|
68
|
|
|
$buildStream->onFrame(function(BuildInfo $buildInfo): void { |
69
|
|
|
echo $buildInfo->getStream(); |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
$buildStream->wait(); |
73
|
|
|
echo "Wait finished\n"; |
74
|
|
|
$buildStream->wait(); |
75
|
|
|
|
76
|
|
|
if ($this->push && isset($name)) { |
77
|
|
|
$this->docker->imagePush($name); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function createBuildStream(array $params = []): BuildStream |
82
|
|
|
{ |
83
|
|
|
|
84
|
|
|
$context = $this->module->createBuildContext(); |
85
|
|
|
|
86
|
|
|
/** @var BuildStream $buildStream */ |
87
|
|
|
$buildStream = $this->docker->imageBuild($context->toStream(), $params, Docker::FETCH_STREAM); |
88
|
|
|
return $buildStream; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function options($actionID) |
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
$result = parent::options($actionID); |
95
|
|
|
switch ($actionID) { |
96
|
|
|
case 'build': |
97
|
|
|
$result[] = 'push'; |
98
|
|
|
$result[] = 'image'; |
99
|
|
|
$result[] = 'tag'; |
100
|
|
|
break; |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
return $result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function optionAliases() |
107
|
|
|
{ |
108
|
|
|
$result = parent::optionAliases(); |
109
|
|
|
$result['p'] = 'push'; |
110
|
|
|
$result['t'] = 'tag'; |
111
|
|
|
$result['i'] = 'image'; |
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
} |