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