summaryrefslogtreecommitdiff
blob: 3d37ec32c113fce20889f1d8f019bbabda9e7a27 (plain)
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
<?php
class sql_build extends sql_row_obj {
	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
		),
		'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='<div class="build"><span class="name">'.(isset($this->name) && strlen($this->name)?htmlentities($this->name):'Unnamed Build').'</span> ';
		$status=explode('/', $this->status, 2);
		if ($status[0] == 'config') {
			$status[1]=substr($status[1], strpos($status[1], 'p')+1);
			$html.='<span class="status config">[Configuration step '.$status[1].']</span><br/><span class="links"><a href="'.url('create/'.$this->id).'">Continue configuring</a></span>';
		} 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.="<span class=\"status queued\">[Queued ($num/$total)]</span>";
			} elseif ($status[1]='running') {
				// Add link to regular log viewer?
				// Build stage X
				$html.='<span class="status building">[building]</span><br/><span class="links"><a href="'.url('logs/'.$this->id.'/live').'">Watch</a> &bull; <a href="'.url('logs/'.$this->id).'"> Build log</a></span>';
			} else {
				throw_exception('Unrecognized build status '.$this->status);
			}
		} elseif ($status[0] == 'finished') {
			$status=explode(': ', $status[1], 2);
			if ($status[0] == 'success') {
				$html.='<span class="status successful">[successful]</span><br/><span class="links"><a href="'.url('download/'.$this->id).'">Download image</a> &bull; <a href="'.url('logs/'.$this->id).'">Build log</a></span>';
			} elseif ($status[0] == 'failed') {
				$html.='<span class="status failed">[failed: '.htmlentities($status[1]).']</span><br/><span class="links"><a href="'.url('logs/'.$this->id.'/failure').'">View output of failed command</a> &bull; <a href="'.url('logs/'.$this->id).'">Build log</a></span>';
			} else {
				throw_exception('Unrecognized build status '.$this->status);
			}
		} else {
			throw_exception('Unrecognized build status '.$this->status);
		}
		if (isset($this->ctime)) {
			$html.='<div class="time">Submitted for build at: <span class="time">'.date($format, $this->ctime).' UTC</span><br/>';
			if (isset($this->start)) {
				$html.='Build started at: <span class="time">'.date($format, $this->start).' UTC</span><br/>';
				if (isset($this->finish)) {
					$html.='Build finished at: <span class="time">'.date($format, $this->finish).' UTC</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;
	}
}
?>