#!/usr/bin/php
query('LOCK TABLES `builds` WRITE');
$r=$S['pdo']->query('SELECT * FROM `builds` WHERE `status`=-128 ORDER BY `ctime` ASC LIMIT 1');
if ($r->rowCount()) {
$build=new sql_build($r->fetch(PDO::FETCH_ASSOC));
$build->start=time();
$build->status=-1;
$build->write();
$S['pdo']->query('UNLOCK TABLES');
debug('Starting build id='.$build->id);
$file=null;
try {
if ($conf['split_setup']) {
$opt=new sql_buildopt($build->id, 'backend', $conf['backend_id']);
$opt->write();
unset($opt);
}
$opts=$build->get_opts();
$build_proc=$build->module.'_build';
require_once(BACKEND."/modules/$build->module/build.php");
// TODO check that build_proc exists
$workdir=WORK.'/build-'.$build->id;
log_status('Creating work directory '.$workdir, mkdir($workdir, 0700));
$image=$build_proc($build, $opts, $workdir);
require_once(BACKEND."/bundlers/{$opts['bundler']}.php");
$proc='bundle_'.$opts['bundler'];
$file=$proc($image, $workdir, $opts);
if (!$conf['debug']) {
execute_command('Delete work directory', 'rm -rf "'.$workdir.'"');
}
} catch (Exception $e) {
end_internal_task(1);
log_msg('Caught exception: '.$e->getMessage());
$build->status=126;
$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();
debug('Finished with build id='.$build->id);
if (isset($file)) {
debug("Completed build successfully");
if ($conf['split_setup']) {
$build->status=-127;
$build->write();
$key=randstring(30);
$opt=new sql_buildopt($build->id, 'uploadkey', $key);
$opt->write();
$c=curl_init(url('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=127;
} else {
debug("Transferred $file... unlinking it");
unlink($file);
$build->status=0;
}
} else {
$build->status=0;
$base=basename($file);
$ext=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);
} else {
$S['pdo']->query('UNLOCK TABLES');
}
// log_msg('Sleeping...', false);
sleep(5);
// log_msg("done");
}
?>