1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Runner\Containers; |
6
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\Cli\Docker; |
8
|
|
|
use Ktomk\Pipelines\File\Pipeline\Step; |
9
|
|
|
use Ktomk\Pipelines\Runner\Containers; |
10
|
|
|
use Ktomk\Pipelines\Runner\Runner; |
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 Runner |
36
|
|
|
*/ |
37
|
|
|
private $runner; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var StepServiceContainers |
41
|
|
|
*/ |
42
|
|
|
private $serviceContainers; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* StepContainer constructor. |
46
|
|
|
* |
47
|
|
|
* @param string $name |
48
|
|
|
* @param Step $step |
49
|
|
|
* @param Runner $runner |
50
|
|
|
*/ |
51
|
12 |
|
public function __construct($name, Step $step, Runner $runner) |
52
|
|
|
{ |
53
|
12 |
|
$this->name = $name; |
54
|
12 |
|
$this->step = $step; |
55
|
12 |
|
$this->runner = $runner; |
56
|
12 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* the display id |
60
|
|
|
* |
61
|
|
|
* side-effect: if id is null, this signals a dry-run which is made |
62
|
|
|
* visible by the string "*dry-run*" |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
8 |
|
public function getDisplayId() |
67
|
|
|
{ |
68
|
8 |
|
return isset($this->id) ? $this->id : '*dry-run*'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return null|string ID of (once) running container or null if not yet running |
73
|
|
|
*/ |
74
|
3 |
|
public function getId() |
75
|
|
|
{ |
76
|
3 |
|
return $this->id; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return null|string name of the container, NULL if no name generated yet |
81
|
|
|
*/ |
82
|
5 |
|
public function getName() |
83
|
|
|
{ |
84
|
5 |
|
return $this->name; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return StepServiceContainers |
89
|
|
|
*/ |
90
|
4 |
|
public function getServiceContainers() |
91
|
|
|
{ |
92
|
4 |
|
return $this->serviceContainers |
93
|
4 |
|
= $this->serviceContainers ?: new StepServiceContainers($this->step, $this->runner); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param null|bool $keep a container on true, kill on false (if it exists) |
98
|
|
|
* |
99
|
|
|
* @return null|string |
100
|
|
|
*/ |
101
|
7 |
|
public function keepOrKill($keep = null) |
102
|
|
|
{ |
103
|
7 |
|
$keep = null === $keep ? $this->runner->getFlags()->reuseContainer() : $keep; |
104
|
|
|
|
105
|
7 |
|
$name = $this->name; |
106
|
7 |
|
if (null === $name) { |
107
|
1 |
|
throw new \BadMethodCallException('Container has no name yet'); |
108
|
|
|
} |
109
|
|
|
|
110
|
6 |
|
$processManager = Docker::create($this->runner->getExec())->getProcessManager(); |
111
|
|
|
|
112
|
6 |
|
if (false === $keep) { |
113
|
2 |
|
$processManager->zapContainersByName($name); |
114
|
|
|
|
115
|
2 |
|
return $this->id = null; |
116
|
|
|
} |
117
|
|
|
|
118
|
5 |
|
return $this->id = $processManager->findContainerIdByName($name); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param bool $kill |
123
|
|
|
* @param bool $remove |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
2 |
|
public function killAndRemove($kill, $remove) |
128
|
|
|
{ |
129
|
2 |
|
$id = $this->getDisplayId(); |
130
|
|
|
|
131
|
2 |
|
Containers::execKillAndRemove($this->runner->getExec(), $id, $kill, $remove); |
132
|
2 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param array $args |
136
|
|
|
* |
137
|
|
|
* @return array array(int $status, string $out, string $err) |
138
|
|
|
*/ |
139
|
4 |
|
public function run(array $args) |
140
|
|
|
{ |
141
|
4 |
|
$execRun = Containers::execRun($this->runner->getExec(), $args); |
142
|
4 |
|
$this->id = array_pop($execRun); |
143
|
|
|
|
144
|
4 |
|
return $execRun; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param int $status |
149
|
|
|
* |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
4 |
|
public function shutdown($status) |
153
|
|
|
{ |
154
|
4 |
|
$id = $this->getDisplayId(); |
155
|
|
|
|
156
|
4 |
|
$message = sprintf( |
157
|
4 |
|
'keeping container id %s', |
158
|
4 |
|
(string)substr($id, 0, 12) |
159
|
|
|
); |
160
|
|
|
|
161
|
4 |
|
Containers::execShutdownContainer( |
162
|
4 |
|
$this->runner->getExec(), |
163
|
4 |
|
$this->runner->getStreams(), |
164
|
4 |
|
$id, |
165
|
4 |
|
$status, |
166
|
4 |
|
$this->runner->getFlags(), |
167
|
4 |
|
$message |
168
|
|
|
); |
169
|
|
|
|
170
|
4 |
|
$this->getServiceContainers()->shutdown($status); |
171
|
4 |
|
} |
172
|
|
|
} |
173
|
|
|
|