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