-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathMasterProject.php
More file actions
182 lines (153 loc) · 5.15 KB
/
MasterProject.php
File metadata and controls
182 lines (153 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace TryLib\JenkinsRunner;
use TryLib\JenkinsRunner;
use TryLib\Util\AnsiColor;
use TryLib\Util\DisplayException as DisplayException;
class MasterProject extends JenkinsRunner{
protected $colors;
private $jobs;
private $excluded_jobs;
private $polling_time;
private $try_job_prefix;
public function __construct(
$jenkins_url,
$jenkins_cli,
$try_job_name,
$cmd_runner,
$try_job_prefix = null,
$polling_time = 20
) {
parent::__construct(
$jenkins_url,
$jenkins_cli,
$try_job_name,
$cmd_runner
);
$this->jobs = [];
$this->excluded_jobs = [];
try {
$this->colors = new AnsiColor();
} catch (DisplayException $e) {
$this->colors = false;
}
$this->polling_time = $polling_time;
if (is_null($try_job_prefix)) {
$try_job_prefix = $try_job_name;
}
$this->try_job_prefix = $try_job_prefix;
}
public function getBuildCommand() {
return 'build-master';
}
public function setSubJobs($jobs) {
$this->jobs = array_unique($jobs);
}
public function setExcludedSubJobs($jobs) {
$this->excluded_jobs = array_unique($jobs);
}
public function getJobsList() {
$tryjobs = [];
foreach ($this->jobs as $job) {
if ( !in_array($job, $this->excluded_jobs)) {
$tryjobs[] = $this->try_job_prefix . '-' . $job;
}
}
foreach ($this->excluded_jobs as $job) {
$tryjobs[] = '-' . $this->try_job_prefix . '-' . $job;
}
return $tryjobs;
}
/** For a master project, the extra arguments are a list of subjobs */
public function getBuildExtraArguments($show_results, $show_progress) {
return $this->getJobsList();
}
public function getJobOutput() {
if (is_string($this->try_base_url)) {
return file_get_contents($this->try_base_url . '/consoleText');
}
return null;
}
/**
* Poll for completion of try job and print results
*/
public function pollForCompletion($show_progress) {
$try_output = $this->cmd_runner->getOutput();
// Find job URL
if (!preg_match('|http[s]?://[^/]+/job/' . $this->try_job_name . '/\d+|m', $try_output, $matches)) {
$this->cmd_runner->terminate('Could not find ' . $this->try_job_name . ' URL' . PHP_EOL);
} else {
$this->try_base_url = $matches[0];
$prev_text = '';
// Poll job URL for completion
while (true) {
$prev_text = $this->processLogOuput($prev_text, $show_progress);
if (is_null($prev_text)) {
break;
}
sleep($this->polling_time);
}
}
}
public function processLogOuput($prev_text, $show_progress) {
$try_log = $this->getJobOutput();
$new_text = str_replace($prev_text, "", $try_log);
$prev_text = $try_log;
if ($show_progress) {
$this->printJobResults($new_text);
}
if (preg_match('|^Finished: .*$|m', $try_log, $matches)) {
$this->try_status = $matches[0];
$this->try_status = str_replace("Finished: ", "", $this->try_status);
$this->cmd_runner->info(
PHP_EOL .
sprintf('Try Status : %s (%s)',
$this->colorStatus($this->try_status),
$this->try_base_url
) .
PHP_EOL
);
return null;
}
if (!$show_progress) {
$this->cmd_runner->info('......... waiting for job to finish');
}
return $prev_text;
}
public function colorStatus($status) {
if ($this->colors) {
if ($status == 'SUCCESS') {
$status = $this->colors->green($status);
} else if ($status == 'UNSTABLE') {
$status = $this->colors->yellow($status);
} else {
$status = $this->colors->red($status);
}
}
return $status;
}
/**
* Given a string of the try logs, print the results from any individual
* job found in the text.
*
* @param string $log
* @access public
* @return boolean Returns true if any job results were printed, false otherwise
*/
public function printJobResults($log) {
if (preg_match_all('|^\[([^\]]+)\] (' . $this->try_job_prefix . '[^ ]+) (\(http[s]?://[^)]+\))$|m', $log, $matches)) {
$this->cmd_runner->info(PHP_EOL);
foreach ($matches[0] as $k => $_) {
$job_status = $matches[1][$k];
$status = sprintf(
"% 32s % -10s %s",
$matches[2][$k],
$this->colorStatus($job_status),
$matches[1][$k] !== 'SUCCESS' ? $matches[3][$k] : ''
);
$this->cmd_runner->info($status);
}
return true;
}
return false;
}
}