#!/usr/bin/php
query('SELECT * FROM `builds` WHERE `status`="build/ready" ORDER BY `ctime` ASC LIMIT 1');
if ($r->rowCount()) {
$build=new sql_build($r->fetch(PDO::FETCH_ASSOC));
$build->start=time();
$build->status='build/running';
$build->write();
log_msg('Starting build id='.$build->id);
$file=null;
try {
$opts=$build->get_buildopts();
$module=$opts['backend_module'];
$build_proc=$module.'_build';
foreach (glob(BACKEND."/modules/$module/*.php") as $inc) {
require_once($inc);
}
// TODO check that build_proc exists
$workdir=WORK.'/build-'.$build->id;
fatal(log_status('Creating work directory '.$workdir, mkdir($workdir, 0700)));
$file=$build_proc($build, $opts, $workdir);
if (!$conf['debug']) {
execute_command('Delete work directory', 'rm -rf "'.$workdir.'"');
}
} catch (Exception $e) {
log_msg('Caught exception: '.$e->getMessage());
$build->status='finished/failed: '.$e->getMessage();
$owner=$build->get_owner();
xhtmlemail('"'.$owner->name.'" <'.$owner->email.'>', null, $conf['title'].' build failed', 'Your build has failed. You can find more information at '.url('logs/'.$build->id).'');
}
$build->finish=time();
log_msg('Finished with build id='.$build->id);
if (isset($file)) {
log_msg("Completed build successfully");
if ($conf['split_setup']) {
$build->status='finished/uploading';
$build->write();
$key=randstring(30);
$opt=new sql_buildopt($build->id, 'uploadkey', $key);
$opt->write();
$c=curl_init($conf['frontend_location'].'/backend/upload_image');
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, array(
'build' => $build->id,
'key' => $key,
'file' => "@$file"
));
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($c);
if ($conf['debug'] && is_string($result)) {
debug($result);
}
if ($result === false || strpos($result, 'Upload successful') === false) {
$build->status='finished/failed: upload failed'.($result === false?' - '.curl_error($c):'');
} else {
debug("Transferred $file... unlinking it");
unlink($file);
$build->status='finished/success';
}
} else {
$build->status='finished/success';
$base=basename($file);
$ext=substr($base, substr($base, strpos($base, '.')));
rename($file, COMPLETED.'/build-'.$build->id.$ext);
}
xhtmlemail('"'.$owner->name.'" <'.$owner->email.'>', null, $conf['title'].' build finished', 'Your build has completed successfully. You can find more information and download the completed image at '.url('logs/'.$build->id).'');
}
$build->write();
unset($build);
}
// log_msg('Sleeping...', false);
sleep(5);
// log_msg("done");
}
?>