|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Joli\JoliCi; |
|
4
|
|
|
|
|
5
|
|
|
use Docker\API\Model\BuildInfo; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
|
|
8
|
|
|
class LoggerCallback |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var \Closure |
|
12
|
|
|
*/ |
|
13
|
|
|
private $buildCallback; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var \Closure |
|
17
|
|
|
*/ |
|
18
|
|
|
private $runStdoutCallback; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \Closure |
|
22
|
|
|
*/ |
|
23
|
|
|
private $runStderrCallback; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var LoggerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $logger; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(LoggerInterface $logger) |
|
31
|
|
|
{ |
|
32
|
|
|
$build = new \ReflectionMethod($this, 'buildCallback'); |
|
33
|
|
|
$runStdout = new \ReflectionMethod($this, 'runStdoutCallback'); |
|
34
|
|
|
$runStderr = new \ReflectionMethod($this, 'runStderrCallback'); |
|
35
|
|
|
|
|
36
|
|
|
$this->buildCallback = $build->getClosure($this); |
|
37
|
|
|
$this->runStdoutCallback = $runStdout->getClosure($this); |
|
38
|
|
|
$this->runStderrCallback = $runStderr->getClosure($this); |
|
39
|
|
|
$this->logger = $logger; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get the build log callback when building / pulling an image |
|
44
|
|
|
* |
|
45
|
|
|
* @return callable |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getBuildCallback() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->buildCallback; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the run stdout callback for docker |
|
54
|
|
|
* |
|
55
|
|
|
* @return callable |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getRunStdoutCallback() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->runStdoutCallback; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get the run stderr callback for docker |
|
64
|
|
|
* |
|
65
|
|
|
* @return callable |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getRunStderrCallback() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->runStderrCallback; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Clear static log buffer |
|
74
|
|
|
*/ |
|
75
|
|
|
public function clearStatic() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->logger->debug("", array('clear-static' => true)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* The build callback when creating a image, useful to see what happens during building |
|
82
|
|
|
* |
|
83
|
|
|
* @param BuildInfo $output An encoded json string from docker daemon |
|
84
|
|
|
*/ |
|
85
|
|
|
private function buildCallback(BuildInfo $output) |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
$message = ""; |
|
88
|
|
|
|
|
89
|
|
|
if ($output->getError()) { |
|
90
|
|
|
$this->logger->error(sprintf("Error when creating job: %s\n", $output->getError()), array('static' => false, 'static-id' => null)); |
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($output->getStream()) { |
|
95
|
|
|
$message = $output->getStream(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($output->getStatus()) { |
|
99
|
|
|
$message = $output->getStatus(); |
|
100
|
|
|
|
|
101
|
|
|
if ($output->getProgress()) { |
|
102
|
|
|
$message .= " " . $output->getProgress(); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// Force new line |
|
107
|
|
|
if (!$output->getId() && !preg_match('#\n#', $message)) { |
|
108
|
|
|
$message .= "\n"; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$this->logger->debug($message, array( |
|
112
|
|
|
'static' => $output->getId() !== null, |
|
113
|
|
|
'static-id' => $output->getId(), |
|
114
|
|
|
)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Run callback to catch stdout logs of test running |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $output Output from run (stdout) |
|
121
|
|
|
*/ |
|
122
|
|
|
private function runStdoutCallback($output) |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
$this->logger->info($output); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Run callback to catch stderr logs of test running |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $output Output from run (stderr) |
|
131
|
|
|
*/ |
|
132
|
|
|
private function runStderrCallback($output) |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
$this->logger->error($output); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|