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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
<?php
class sql_build extends conf_build_common {
protected $table='builds', $primary_key=array('id'), $columns=array(
'id' => array (
'type' => 'CHAR',
'length' => 6,
'not_null' => true,
'default' => ''
),
'owner' => array (
'type' => 'INT',
'length' => 10,
'unsigned' => true,
'not_null' => true,
'default' => 0,
'refers_to' => 'users.id'
),
'name' => array (
'type' => 'VARCHAR',
'length' => 255
),
'module' => array (
'type' => 'VARCHAR',
'length' => 255,
'not_null' => true,
'default' => ''
),
'bundler' => array (
'type' => 'VARCHAR',
'length' => 255,
'not_null' => true
),
'flags' => array (
'type' => 'VARCHAR',
'length' => 255,
'not_null' => true
),
'backend' => array (
'type' => 'VARCHAR',
'length' => 255
),
'status' => array (
'type' => 'ENUM',
'length' => '\'queued\',\'uploading\',\'cancel\',\'complete\',\'canceled\',\'building\',\'bundling\'',
'not_null' => true
),
'build_step' => array (
'type' => 'TINYINT',
'length' => 4,
'unsigned' => true
),
'num_steps' => array (
'type' => 'TINYINT',
'length' => 4,
'unsigned' => true
),
'ctime' => array (
'type' => 'INT',
'length' => 10,
'unsigned' => true
),
'start' => array (
'type' => 'INT',
'length' => 10,
'unsigned' => true
),
'finish' => array (
'type' => 'INT',
'length' => 10,
'unsigned' => true
)
);
// Returns HTML code describing this build's status (for human consumption)
public function display() {
global $S;
$format='D j M Y G:i:s T';
$perms=!$this->has_flag('p') || owner_or_admin($this->id);
$html='<div class="build"><span class="name">'.(isset($this->name) && strlen($this->name)?htmlentities($this->name):'Unnamed Build').'</span> ';
if ($this->has_flag('f'))
$html.='<span class="status failed">[failed]</span> ';
$links=array();
switch ($this->status) {
case 'queued':
$total=query('SELECT COUNT(*) FROM `builds` WHERE `status`="queued"')->fetch(PDO::FETCH_COLUMN);
$num=query('SELECT COUNT(*) FROM `builds` WHERE `status`="queued" AND `ctime` <= '.$this->ctime)->fetch(PDO::FETCH_COLUMN);
$html.="<span class=\"status queued\">[queued ($num/$total)]</span>";
break;
case 'uploading':
$html.='<span class="status successful">[uploading]</span>';
if ($perms) $links['Build log']="build/$this->id";
break;
case 'cancel':
$html.='<span class="status queued">[pending cancellation]</span>';
if ($perms) $links['Build log']="build/$this->id";
break;
case 'building':
case 'bundling':
$html.='<span class="status building">['.$this->status.' ('.$this->build_step.'/'.$this->num_steps.')]</span>';
if ($perms) {
//$links['Watch']="build/$this->id/live";
$links['Build Log']="build/$this->id";
}
break;
case 'complete':
$url="build/$this->id/history";
if ($perms && $S['request'] != $url) {
$r=query('SELECT COUNT(*) as `count`, MAX(`time`) as `time` FROM `downloads` WHERE `build`="'.$this->id.'"')->fetch(PDO::FETCH_ASSOC);
$d=($r['count']?'<a href="'.url($url).'">':'').$r['count'].' download'.($r['count'] != 1?'s':'').($r['count']?($perms?'</a>':'').'<br/><span class="time">(last at '.date($format, $r['time']).')</span>':'');
} else
$d='';
$html.='<span class="downloads">'.$d.'</span><span class="status successful">[successful]</span>';
$links['Download image']="build/$this->id/download";
if ($perms) $links['Build log']="build/$this->id";
break;
case 'canceled':
$html.='<span class="status failed">[canceled]</span>';
if ($perms) $links['Build log']="build/$this->id";
break;
default:
$html.='<span class="status failed">[UNKNOWN STATUS: '.$this->status.']</span>';
}
if ($perms) {
if ($this->status == 'canceled' || $this->status == 'queued' || $this->status == 'complete' || $this->has_flag('f'))
$links['Delete']="build/$this->id/delete";
elseif ($this->status != 'cancel')
$links['Cancel']="build/$this->id/cancel";
}
if ($links) {
foreach ($links as $label => $url) {
if ($S['request'] == $url)
unset($links[$label]);
else
$links[$label]='<a href="'.url($url).'">'.htmlentities($label).'</a>';
}
if ($links)
$html.='<br/><span class="links">'.implode(' • ', $links).'</span>';
}
if (isset($this->ctime)) {
$html.='<div class="time">Submitted for build at: <span class="time">'.date($format, $this->ctime).'</span><br/>';
if (isset($this->start)) {
$html.='Build started at: <span class="time">'.date($format, $this->start).'</span><br/>';
if (isset($this->finish)) {
$html.='Build finished at: <span class="time">'.date($format, $this->finish).'</span><br/>Total build time: <span class="time">'.display_time($this->finish-$this->start).'</span>';
} else {
$html.='Running for: <span class="time">'.display_time(time()-$this->start).'</span>';
}
} else {
$html.='Queued for: <span class="time">'.display_time(time()-$this->ctime).'</span>';
}
$html.='</div>';
}
$html.='</div>';
return $html;
}
public function delete() {
global $S;
query('DELETE FROM `buildlogs` WHERE `build`="'.$this->id.'"');
query('DELETE FROM `tasks` WHERE `build`="'.$this->id.'"');
query('DELETE FROM `buildopts` WHERE `build`="'.$this->id.'"');
query('DELETE FROM `downloads` WHERE `build`="'.$this->id.'"');
foreach (glob(COMPLETED."/build-.$this->id.*") as $file)
unlink($file);
parent::delete();
}
public function build($workdir) {
global $S;
try {
$opts=$this->get_opts();
$S['build_steps']=array();
if (!is_readable(BACKEND."/modules/$this->module/build.php"))
throw_exception("No build script for module $this->module");
$imagedir=require(BACKEND."/modules/$this->module/build.php");
switch ($this->status) {
case 'queued':
$this->status='building';
$this->build_step=0;
$this->num_steps=count($S['build_steps']);
$this->write();
case 'building':
$step=$this->build_step;
break;
default:
return $imagedir;
}
if (!is_dir($workdir))
log_status('Create work directory '.$workdir, mkdir($workdir, 0700));
while ($step < count($S['build_steps'])) {
require(BACKEND."/modules/$this->module/{$S['build_steps'][$step]}.php");
$step++;
$this->build_step=$step;
$this->write();
if ($this->is_canceled()) return false;
}
return $imagedir;
} catch(Exception $e) {
log_msg('Caught exception: '.$e->getMessage());
end_internal_task(1);
$this->failed();
return false;
}
}
public function bundle($imagedir, $workdir) {
global $S;
try {
$opts=$this->get_opts();
$bundler=$opts['bundler'];
if (!is_readable(BACKEND."/bundlers/$bundler/bundle.php"))
throw_exception("No bundle script for bundler $bundler");
$S['build_steps']=array();
$file=require(BACKEND."/bundlers/$bundler/bundle.php");
switch($this->status) {
case 'building':
$this->status='bundling';
$this->build_step=0;
$this->num_steps=count($S['build_steps']);
$this->write();
case 'bundling':
$step=$this->build_step;
break;
default:
return $file;
}
while ($step < count($S['build_steps'])) {
require(BACKEND."/bundlers/$bundler/{$S['build_steps'][$step]}.php");
$step++;
$this->build_step=$step;
$this->write();
if ($this->is_canceled()) return false;
}
return $file;
} catch (Exception $e) {
log_msg('Caught exception: '.$e->getMessage());
end_internal_task(1);
$this->failed();
return false;
}
}
public function upload($file) {
$this->status='uploading';
$this->write();
$key=randstring(30);
$this->set_opt('uploadkey', $key);
$c=curl_init(url('backend/upload_image'));
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, array(
'build' => $this->id,
'key' => $key,
'file' => "@$file"
));
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($c);
if (trim($result) == 'Upload successful') {
$this->status='complete';
$this->finish=time();
$this->write();
} else
$this->failed();
return $result;
}
private function failed() {
$this->set_flag('f');
$this->finish=time();
$this->write();
}
public function is_canceled() {
$this->load();
return ($this->status == 'cancel');
}
}
?>
|