1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Runner; |
6
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\Cli\Exec; |
8
|
|
|
use Ktomk\Pipelines\Cli\Streams; |
9
|
|
|
use Ktomk\Pipelines\File\Pipeline; |
10
|
|
|
use Ktomk\Pipelines\File\Step; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Pipeline runner with docker under the hood |
14
|
|
|
*/ |
15
|
|
|
class Runner |
16
|
|
|
{ |
17
|
|
|
const STATUS_NO_STEPS = 1; |
18
|
|
|
const STATUS_RECURSION_DETECTED = 127; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $prefix; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Directories |
27
|
|
|
*/ |
28
|
|
|
private $directories; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Exec |
32
|
|
|
*/ |
33
|
|
|
private $exec; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Flags |
37
|
|
|
*/ |
38
|
|
|
private $flags; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Env |
42
|
|
|
*/ |
43
|
|
|
private $env; |
44
|
|
|
/** |
45
|
|
|
* @var Streams |
46
|
|
|
*/ |
47
|
|
|
private $streams; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* DockerSession constructor. |
51
|
|
|
* |
52
|
|
|
* @param string $prefix |
53
|
|
|
* @param Directories $directories source repository root directory based directories object |
54
|
|
|
* @param Exec $exec |
55
|
|
|
* @param Flags $flags [optional] |
56
|
|
|
* @param Env $env [optional] |
57
|
|
|
* @param Streams $streams [optional] |
58
|
|
|
*/ |
59
|
|
|
public function __construct( |
60
|
|
|
$prefix, |
61
|
|
|
Directories $directories, |
62
|
|
|
Exec $exec, |
63
|
|
|
Flags $flags = null, |
64
|
|
|
Env $env = null, |
65
|
|
|
Streams $streams = null |
66
|
|
|
) |
67
|
|
|
{ |
68
|
|
|
$this->prefix = $prefix; |
69
|
|
|
$this->directories = $directories; |
70
|
|
|
$this->exec = $exec; |
71
|
|
|
$this->flags = null === $flags ? new Flags() : $flags; |
72
|
|
|
$this->env = null === $env ? Env::create() : $env; |
73
|
17 |
|
$this->streams = null === $streams ? Streams::create() : $streams; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Pipeline $pipeline |
78
|
|
|
* @throws \RuntimeException |
79
|
|
|
* @return int status (as in exit status, 0 OK, !0 NOK) |
80
|
|
|
*/ |
81
|
|
|
public function run(Pipeline $pipeline) |
82
|
17 |
|
{ |
83
|
17 |
|
$hasId = $this->env->setPipelinesId($pipeline->getId()); # TODO give Env an addPipeline() method (compare addReference) |
84
|
17 |
|
if ($hasId) { |
85
|
17 |
|
$this->streams->err(sprintf( |
86
|
17 |
|
"pipelines: won't start pipeline '%s'; pipeline inside pipelines recursion detected\n", |
87
|
17 |
|
$pipeline->getId() |
88
|
17 |
|
)); |
89
|
|
|
|
90
|
|
|
return self::STATUS_RECURSION_DETECTED; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
foreach ($pipeline->getSteps() as $step) { |
94
|
|
|
$status = $this->runStep($step); |
95
|
15 |
|
if (0 !== $status) { |
96
|
|
|
return $status; |
97
|
15 |
|
} |
98
|
15 |
|
} |
99
|
1 |
|
|
100
|
1 |
|
if (!isset($status)) { |
101
|
1 |
|
$this->streams->err("pipelines: pipeline with no step to execute\n"); |
102
|
|
|
|
103
|
|
|
return self::STATUS_NO_STEPS; |
104
|
1 |
|
} |
105
|
|
|
|
106
|
|
|
return $status; |
107
|
14 |
|
} |
108
|
13 |
|
|
109
|
13 |
|
/** |
110
|
5 |
|
* @param Step $step |
111
|
|
|
* @return int status (as in exit status, 0 OK, !0 NOK) |
112
|
|
|
*/ |
113
|
|
|
public function runStep(Step $step) { |
114
|
9 |
|
$stepRunner = new StepRunner( |
115
|
1 |
|
$this->prefix, |
116
|
|
|
$this->directories, |
117
|
1 |
|
$this->exec, |
118
|
|
|
$this->flags, |
119
|
|
|
$this->env, |
120
|
8 |
|
$this->streams |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
return $stepRunner->runStep($step); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|