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
),
'status' => array (
'type' => 'VARCHAR',
'length' => 255,
'not_null' => true,
'default' => ''
),
'ctime' => array (
'type' => 'INT',
'length' => 10,
'unsigned' => true
),
'start' => array (
'type' => 'INT',
'length' => 10,
'unsigned' => true
),
'finish' => array (
'type' => 'INT',
'length' => 10,
'unsigned' => true
)
);
// Generates a unique id and sets status to config/step0, writes self to db and returns id
public function init() {
global $S;
$this->owner=$S['user']->id;
$this->status='config/step1';
$fails=0;
while (true) {
$id=randstring(6);
debug("Trying id=$id...");
$r=$S['pdo']->query('SELECT `id` FROM `builds` WHERE `id`="'.$id.'"');
if ($r->rowCount() == 0) {
break;
}
if (++$fails == 10) {
throw_exception('Failed 10 times to find a unique build id... this shouldn\'t happen.');
}
}
$this->id=$id;
$this->write();
return $this->id;
}
// Fetches all available buildopts pertaining to this build in a nice array
function &get_buildopts() {
global $S;
$r=$S['pdo']->query('SELECT * FROM `buildopts` WHERE `build`="'.$this->id.'"');
$opts=array();
while ($opt=$r->fetch(PDO::FETCH_ASSOC)) {
$opt=new sql_buildopt($opt);
$opts[$opt->name]=$opt->value; // TODO maybe we should return the actual objects
}
return $opts;
}
// Returns HTML code describing this build's status (for human consumption)
function display() {
global $S;
$format='D j M Y G:i:s';
$html='
'.(isset($this->name) && strlen($this->name)?htmlentities($this->name):'Unnamed Build').' ';
$status=explode('/', $this->status, 2);
if ($status[0] == 'config') {
$status[1]=substr($status[1], strpos($status[1], 'p')+1);
$html.='
[Configuration step '.$status[1].']Continue configuring';
} elseif ($status[0] == 'build') {
if ($status[1] == 'ready') {
$total=$S['pdo']->query('SELECT COUNT(*) FROM `builds` WHERE `status`="build/ready"')->fetch(PDO::FETCH_COLUMN);
$num=$S['pdo']->query('SELECT COUNT(*) FROM `builds` WHERE `status`="build/ready" AND `ctime` <= '.$this->ctime)->fetch(PDO::FETCH_COLUMN);
$html.="
[Queued ($num/$total)]";
} elseif ($status[1]='running') {
// Add link to regular log viewer?
// Build stage X
$html.='
[building]Watch';
} else {
throw_exception('Unrecognized build status '.$this->status);
}
} elseif ($status[0] == 'finished') {
$status=explode(': ', $status[1], 2);
if ($status[0] == 'success') {
$html.='
[successful]Download image • Build log';
} elseif ($status[0] == 'failed') {
$html.='
[failed: '.htmlentities($status[1]).']View output of failed command • Build log';
} else {
throw_exception('Unrecognized build status '.$this->status);
}
} else {
throw_exception('Unrecognized build status '.$this->status);
}
if (isset($this->ctime)) {
$html.='
Submitted for build at: '.date($format, $this->ctime).' UTC
';
if (isset($this->start)) {
$html.='Build started at: '.date($format, $this->start).' UTC
';
if (isset($this->finish)) {
$html.='Build finished at: '.date($format, $this->finish).' UTC
Total build time: '.display_time($this->finish-$this->start).'';
} else {
$html.='Running for: '.display_time(time()-$this->start).'';
}
} else {
$html.='Queued for: '.display_time(time()-$this->ctime).'';
}
$html.='
';
}
$html.='
';
return $html;
}
}
?>