1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Runner; |
6
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\Cli\Docker; |
8
|
|
|
use Ktomk\Pipelines\Cli\Exec; |
9
|
|
|
use Ktomk\Pipelines\File\Pipeline\Step; |
10
|
|
|
use Ktomk\Pipelines\Runner\Containers\NameBuilder; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class StepContainer |
14
|
|
|
* |
15
|
|
|
* @package Ktomk\Pipelines\Runner |
16
|
|
|
*/ |
17
|
|
|
class StepContainer |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var null|string id of the (running) container |
21
|
|
|
*/ |
22
|
|
|
private $id; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var null|string name of the container |
26
|
|
|
*/ |
27
|
|
|
private $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Step |
31
|
|
|
*/ |
32
|
|
|
private $step; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Exec |
36
|
|
|
*/ |
37
|
|
|
private $exec; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Step $step |
41
|
|
|
* @param null|Exec $exec |
42
|
|
|
* |
43
|
|
|
* @return StepContainer |
44
|
|
|
*/ |
45
|
6 |
|
public static function create(Step $step, Exec $exec = null) |
46
|
|
|
{ |
47
|
6 |
|
if (null === $exec) { |
48
|
2 |
|
$exec = new Exec(); |
49
|
|
|
} |
50
|
|
|
|
51
|
6 |
|
return new self($step, $exec); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Step $step |
56
|
|
|
* @param string $prefix |
57
|
|
|
* @param string $project name |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
1 |
|
public static function createName(Step $step, $prefix, $project) |
62
|
|
|
{ |
63
|
1 |
|
return self::create($step)->generateName($prefix, $project); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* StepContainer constructor. |
68
|
|
|
* |
69
|
|
|
* @param Step $step |
70
|
|
|
* @param Exec $exec |
71
|
|
|
*/ |
72
|
14 |
|
public function __construct(Step $step, Exec $exec) |
73
|
|
|
{ |
74
|
14 |
|
$this->step = $step; |
75
|
14 |
|
$this->exec = $exec; |
76
|
14 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* generate step container name |
80
|
|
|
* |
81
|
|
|
* @param string $prefix for the name (normally "pipelines") |
82
|
|
|
* @param string $projectName name |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
9 |
|
public function generateName($prefix, $projectName) |
87
|
|
|
{ |
88
|
9 |
|
return $this->name = NameBuilder::stepContainerNameByStep($this->step, $prefix, $projectName); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* the display id |
93
|
|
|
* |
94
|
|
|
* side-effect: if id is null, this signals a dry-run which is made |
95
|
|
|
* visible by the string "*dry-run*" |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
8 |
|
public function getDisplayId() |
100
|
|
|
{ |
101
|
8 |
|
return isset($this->id) ? $this->id : '*dry-run*'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return null|string ID of (once) running container or null if not yet running |
106
|
|
|
*/ |
107
|
3 |
|
public function getId() |
108
|
|
|
{ |
109
|
3 |
|
return $this->id; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return null|string name of the container, NULL if no name generated yet |
114
|
|
|
*/ |
115
|
3 |
|
public function getName() |
116
|
|
|
{ |
117
|
3 |
|
return $this->name; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param bool $keep a container on true, kill on false (if it exists) |
122
|
|
|
* |
123
|
|
|
* @return null|string |
124
|
|
|
*/ |
125
|
7 |
|
public function keepOrKill($keep) |
126
|
|
|
{ |
127
|
7 |
|
$name = $this->name; |
128
|
7 |
|
if (null === $name) { |
129
|
1 |
|
throw new \BadMethodCallException('Container has no name yet'); |
130
|
|
|
} |
131
|
|
|
|
132
|
6 |
|
$processManager = Docker::create($this->exec)->getProcessManager(); |
133
|
|
|
|
134
|
6 |
|
if (false === $keep) { |
135
|
2 |
|
$processManager->zapContainersByName($name); |
136
|
|
|
|
137
|
2 |
|
return $this->id = null; |
138
|
|
|
} |
139
|
|
|
|
140
|
5 |
|
return $this->id = $processManager->findContainerIdByName($name); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param bool $kill |
145
|
|
|
* @param bool $remove |
146
|
|
|
* |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
2 |
|
public function killAndRemove($kill, $remove) |
150
|
|
|
{ |
151
|
2 |
|
$id = $this->getDisplayId(); |
152
|
|
|
|
153
|
2 |
|
StepContainers::execKillAndRemove($this->exec, $id, $kill, $remove); |
154
|
2 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param array $args |
158
|
|
|
* |
159
|
|
|
* @return array array(int $status, string $out, string $err) |
160
|
|
|
*/ |
161
|
4 |
|
public function run(array $args) |
162
|
|
|
{ |
163
|
4 |
|
$execRun = StepContainers::execRun($this->exec, $args); |
164
|
4 |
|
$this->id = array_pop($execRun); |
165
|
|
|
|
166
|
4 |
|
return $execRun; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|