1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SamIT\Yii2\PhpFpm\controllers; |
5
|
|
|
|
6
|
|
|
use SamIT\Yii2\PhpFpm\helpers\Docker; |
7
|
|
|
use SamIT\Yii2\PhpFpm\Module; |
8
|
|
|
use yii\base\InvalidConfigException; |
9
|
|
|
use yii\console\Controller; |
10
|
|
|
use yii\helpers\Console; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class BuildController |
14
|
|
|
* @package SamIT\Yii2\PhpFpm\controllers |
15
|
|
|
* @property Module $module |
16
|
|
|
*/ |
17
|
|
|
class BuildController extends Controller |
18
|
|
|
{ |
19
|
|
|
public $defaultAction = 'build'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string The name of the created image |
23
|
|
|
* If not explicitly set will take its default from module config. |
24
|
|
|
*/ |
25
|
|
|
public $image; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string The tag of the created image |
29
|
|
|
* If not explicitly set will take its default from module config. |
30
|
|
|
*/ |
31
|
|
|
public $tag; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool whether to push the image after a successful build. |
35
|
|
|
* If not explicitly set will take its default from module config. |
36
|
|
|
*/ |
37
|
|
|
public $push; |
38
|
|
|
|
39
|
|
|
public function init(): void |
40
|
|
|
{ |
41
|
|
|
parent::init(); |
42
|
|
|
$this->push = $this->module->push; |
43
|
|
|
$this->image = $this->module->image; |
44
|
|
|
$this->tag = $this->module->tag; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function actionBuild(): void |
48
|
|
|
{ |
49
|
|
|
if ($this->push && !isset($this->image)) { |
50
|
|
|
throw new InvalidConfigException("When using the push option, you must configure or provide image"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$params = []; |
54
|
|
|
|
55
|
|
|
if (isset($this->image)) { |
56
|
|
|
$params['t'] = "{$this->image}:{$this->tag}"; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$context = $this->module->createBuildContext($this->tag); |
60
|
|
|
|
61
|
|
|
$docker = new Docker(); |
62
|
|
|
$docker->build($context, "{$this->image}:{$this->tag}"); |
63
|
|
|
|
64
|
|
|
if ($this->push) { |
65
|
|
|
$docker->push("{$this->image}:{$this->tag}"); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function actionTestClient(): void |
70
|
|
|
{ |
71
|
|
|
$this->stdout("It seems the console client works!\n", Console::FG_GREEN); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
public function options($actionID) |
76
|
|
|
{ |
77
|
|
|
|
78
|
|
|
$result = parent::options($actionID); |
79
|
|
|
switch ($actionID) { |
80
|
|
|
case 'build': |
81
|
|
|
$result[] = 'push'; |
82
|
|
|
$result[] = 'image'; |
83
|
|
|
$result[] = 'tag'; |
84
|
|
|
break; |
85
|
|
|
} |
86
|
|
|
return $result; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function optionAliases() |
90
|
|
|
{ |
91
|
|
|
$result = parent::optionAliases(); |
92
|
|
|
$result['p'] = 'push'; |
93
|
|
|
$result['t'] = 'tag'; |
94
|
|
|
$result['i'] = 'image'; |
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function stdout($string) |
99
|
|
|
{ |
100
|
|
|
if ($this->isColorEnabled()) { |
101
|
|
|
$args = \func_get_args(); |
102
|
|
|
\array_shift($args); |
103
|
|
|
$string = Console::ansiFormat($string, $args); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
echo $string; |
107
|
|
|
return \strlen($string); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|