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\Step; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class StepContainer |
13
|
|
|
* |
14
|
|
|
* @package Ktomk\Pipelines\Runner |
15
|
|
|
*/ |
16
|
|
|
class StepContainer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string id of the (running) container |
20
|
|
|
*/ |
21
|
|
|
private $id; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string name of the container |
25
|
|
|
*/ |
26
|
|
|
private $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Step |
30
|
|
|
*/ |
31
|
|
|
private $step; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Exec |
35
|
|
|
*/ |
36
|
|
|
private $exec; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Step $step |
40
|
|
|
* |
41
|
|
|
* @return StepContainer |
42
|
|
|
*/ |
43
|
2 |
|
public static function create(Step $step, Exec $exec = null) |
44
|
|
|
{ |
45
|
2 |
|
if (null === $exec) { |
46
|
2 |
|
$exec = new Exec(); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
return new self($step, $exec); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Step $step |
54
|
|
|
* @param string $prefix |
55
|
|
|
* @param string $project name |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
1 |
|
public static function createName(Step $step, $prefix, $project) |
60
|
|
|
{ |
61
|
1 |
|
return self::create($step)->generateName($prefix, $project); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* StepContainer constructor. |
66
|
|
|
* |
67
|
|
|
* @param Step $step |
68
|
|
|
* @param Exec $exec |
69
|
|
|
*/ |
70
|
6 |
|
public function __construct(Step $step, Exec $exec) |
71
|
|
|
{ |
72
|
6 |
|
$this->step = $step; |
73
|
6 |
|
$this->exec = $exec; |
74
|
6 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* generate step container name |
78
|
|
|
* |
79
|
|
|
* example: pipelines-1.pipeline-features-and-introspection.default.app |
80
|
|
|
* ^ `^` ^ ` ^ ` ^ |
81
|
|
|
* | | | | | |
82
|
|
|
* | step number step name pipeline id | |
83
|
|
|
* prefix project |
84
|
|
|
* |
85
|
|
|
* @param string $prefix for the name (normally "pipelines") |
86
|
|
|
* @param string $project name |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
4 |
|
public function generateName($prefix, $project) |
91
|
|
|
{ |
92
|
4 |
|
$step = $this->step; |
93
|
|
|
|
94
|
4 |
|
$idContainerSlug = preg_replace('([^a-zA-Z0-9_.-]+)', '-', $step->getPipeline()->getId()); |
95
|
4 |
|
if ('' === $idContainerSlug) { |
96
|
4 |
|
$idContainerSlug = 'null'; |
97
|
|
|
} |
98
|
4 |
|
$nameSlug = preg_replace(array('( )', '([^a-zA-Z0-9_.-]+)'), array('-', ''), $step->getName()); |
99
|
4 |
|
if ('' === $nameSlug) { |
100
|
4 |
|
$nameSlug = 'no-name'; |
101
|
|
|
} |
102
|
|
|
|
103
|
4 |
|
return $this->name = $prefix . '-' . implode( |
104
|
4 |
|
'.', |
105
|
|
|
array( |
106
|
4 |
|
$step->getIndex() + 1, |
107
|
4 |
|
$nameSlug, |
108
|
4 |
|
trim($idContainerSlug, '-'), |
109
|
4 |
|
$project, |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return null|string ID of (once) running container or null if not yet running |
116
|
|
|
*/ |
117
|
1 |
|
public function getId() |
118
|
|
|
{ |
119
|
1 |
|
return $this->id; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return null|string name of the container, NULL if no name generated yet |
124
|
|
|
*/ |
125
|
1 |
|
public function getName() |
126
|
|
|
{ |
127
|
1 |
|
return $this->name; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param bool $keep a container on true, kill on false (if it exists) |
132
|
|
|
* |
133
|
|
|
* @return null|string |
134
|
|
|
*/ |
135
|
2 |
|
public function keepOrKill($keep) |
136
|
|
|
{ |
137
|
2 |
|
$name = $this->name; |
138
|
2 |
|
if (null === $name) { |
|
|
|
|
139
|
1 |
|
throw new \BadMethodCallException('Container has no name yet'); |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
$processManager = Docker::create($this->exec)->getProcessManager(); |
143
|
|
|
|
144
|
1 |
|
if (false === $keep) { |
145
|
1 |
|
$processManager->zapContainersByName($name); |
146
|
|
|
|
147
|
1 |
|
return $this->id = null; |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
return $this->id = $processManager->findContainerIdByName($name); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|