diff options
author | Eudyptula <eitan@mosenkis.net> | 2009-06-18 13:59:21 -0400 |
---|---|---|
committer | Eudyptula <eitan@mosenkis.net> | 2009-06-18 13:59:21 -0400 |
commit | 9bd3740a82f096d69253b487ead88d5761ea4a3f (patch) | |
tree | bfad07611eae7bce81d47ed8a3456d35c2c67175 | |
parent | Created class from SQL table frontend, generated sql_builds class (diff) | |
download | ingenue-9bd3740a82f096d69253b487ead88d5761ea4a3f.tar.gz ingenue-9bd3740a82f096d69253b487ead88d5761ea4a3f.tar.bz2 ingenue-9bd3740a82f096d69253b487ead88d5761ea4a3f.zip |
Plugged in sql_build to backend and frontend log viewer
-rwxr-xr-x | backend/backend.php | 31 | ||||
-rw-r--r-- | backend/functions/build.php | 10 | ||||
-rw-r--r-- | backend/functions/execution.php | 6 | ||||
-rw-r--r-- | frontend/include/footer.php | 2 | ||||
-rw-r--r-- | frontend/index.php | 2 | ||||
-rw-r--r-- | frontend/pages/logview.php | 86 | ||||
-rw-r--r-- | frontend/pages/newclass.php | 2 | ||||
-rw-r--r-- | frontend/routing.csv | 1 | ||||
-rw-r--r-- | shared/classes/0sql_row_obj.php | 5 | ||||
-rw-r--r-- | shared/classes/build.php | 9 | ||||
-rw-r--r-- | shared/classes/buildlog_entry.php | 17 |
11 files changed, 108 insertions, 63 deletions
diff --git a/backend/backend.php b/backend/backend.php index 2f70b6e..5e7d657 100755 --- a/backend/backend.php +++ b/backend/backend.php @@ -16,5 +16,34 @@ if (isset($opts['f'])) { echo "I am the child!\n"; } } -build(); +// TODO this should be in the frontend +$fails=0; +while (true) { + $id=randstring(6); + log_msg("Trying id=$id...", false); + // Maybe we should query the DB, not see if insert fails? + $build=new sql_build($id, 0, 'Test build', 'pre-build', null, null); + try { + if ($build->write()) { + if (is_dir(WORK.'/build-'.$id)) { + log_msg('failed... directory already exists (this is bad - it should be in the database)'); + $build->delete(); + } else { + break; + } + } + } catch (Exception $e) { + if (get_class($e) != 'PDOException' || !isset($e->errorInfo) || !isset($e->errorInfo[0]) || $e->errorInfo[0] != 23000) { + log_msg(''); // For \n + throw ($e); + } else { + log_msg('already taken in the database'); + } + } + if (++$fails >= 10) { + log_msg('Something is wrong - failed 10 times to choose a unique build id'); + die; + } +} +build($id); ?> diff --git a/backend/functions/build.php b/backend/functions/build.php index 41cfb76..39f2c48 100644 --- a/backend/functions/build.php +++ b/backend/functions/build.php @@ -9,14 +9,10 @@ $conf['emerge_default_opts']='-t -K --color=n'; $conf['portage_tmpdir']='$W/tmp'; $profile='/etc/make.profile'; // This is the main function that carries out a build from start to finish -function build() { +function build($id) { global $conf, $profile; - // TODO assigning IDs should probably be in the frontend (there will be status info even before build() is run) - do { - $id=randstring(6); - log_msg("Trying id=$id"); - define('W', WORK.'/build-'.$id); - } while (is_dir(W)); + $build=new sql_build($id); + define('W', WORK.'/build-'.$id); fatal(log_status('Creating work directory '.W, mkdir(W, 0700))); fatal(log_status('Creating '.W.'/image', mkdir(W.'/image', 0700))); define('I', W.'/image'); diff --git a/backend/functions/execution.php b/backend/functions/execution.php index eac464a..a06e962 100644 --- a/backend/functions/execution.php +++ b/backend/functions/execution.php @@ -11,26 +11,26 @@ function log_command($id, $command, $path=null, $env=null) { foreach ($pipes as $pipe) { stream_set_blocking($pipe, 0); } + $msg=0; while (true) { $status=proc_get_status($p); // We have to set these all to variables because stream_select requires pass by reference $null=null; $outs=array($pipes[1], $pipes[2]); $s=stream_select($outs, $null, $null, 1); - $msg=0; if ($s) { $c=stream_get_contents($pipes[2]); // TODO this really needs to go to the DB and carry metadata if ($c) { // STDERR - $entry=new sql_buildlog_entry($task->id, $msg++, 'stderr', $c); + $entry=new sql_buildlog_entry($task->id, $msg++, time(), 'stderr', $c); $entry->write(); //log_msg($c, false); } $c=stream_get_contents($pipes[1]); if ($c) { // STDOUT - $entry=new sql_buildlog_entry($task->id, $msg++, 'stdout', $c); + $entry=new sql_buildlog_entry($task->id, $msg++, time(), 'stdout', $c); $entry->write(); //log_msg($c, false); } diff --git a/frontend/include/footer.php b/frontend/include/footer.php index fd904af..1f2f241 100644 --- a/frontend/include/footer.php +++ b/frontend/include/footer.php @@ -13,7 +13,7 @@ if ($conf['debug']) { toggledebugbox();'."\n"; foreach ($S['debug'] as $row) { list($type, $text)=$row; - $text=str_replace('"', '\"', str_replace("\r", '\\r', str_replace("\n",'\\n',$text))); + $text=str_replace(array('"', "\r", "\n", "\t"), array('\"', '\\r', '\\n', '\\t'), $text); // $text used to have htmlentities() run on it, but that killed the SQL output tables if ($type == null) { echo 'debug("'.$text.'");'."\n"; diff --git a/frontend/index.php b/frontend/index.php index d3bf0c8..149bee8 100644 --- a/frontend/index.php +++ b/frontend/index.php @@ -5,8 +5,8 @@ require_once('../shared/include/includes.php'); require_once('include/error_handling.php'); require_once('include/constants.php'); require_once(SHARED.'/config.php'); -register_shutdown_function('onshutdown', realpath('include/footer.php'), realpath('include/header.php')); require_once('include/setup.php'); +register_shutdown_function('onshutdown', realpath('include/footer.php'), realpath('include/header.php')); foreach ($_REQUEST as $key => $value) { $request[$key]=get_magic_quotes_gpc()?stripslashes($value):$value; } diff --git a/frontend/pages/logview.php b/frontend/pages/logview.php index f721b60..9ef4ecd 100644 --- a/frontend/pages/logview.php +++ b/frontend/pages/logview.php @@ -1,54 +1,64 @@ <?php +// TODO Fix up the main builds and tasks views to display more info (especially tasks - builds will probably get removed) function init_logview() { global $S; $S['title']='Log Viewer'; } function body_logview() { global $S, $request; - $q=$S['pdo']->query('SELECT * FROM FROM `builds`'); - while (true) { - $build=$q->fetch(PDO::FETCH_ASSOC); - if ($build === false) { - break; + if (isset($request['task']) && is_numeric($request['task'])) { + $r=$S['pdo']->query('SELECT * FROM `tasks` WHERE `id`='.$request['task']); + if ($r->rowCount() == 0) { + echo print_error('Not found', 'Task #'.$request['task'].' was not found.'); + return; } - echo '<h2>Build '.$build['build'].'</h2>'; - $r=$S['pdo']->query('SELECT * FROM `tasks` WHERE `build`="'.$build['build'].'"'.(isset($request['task']) && is_numeric($request['task'])?' AND `id`='.$request['task']:'')); - while (true) { - $task=$r->fetch(PDO::FETCH_ASSOC); - if ($task === false) { - break; + $task=new sql_task($r->fetch(PDO::FETCH_ASSOC)); + echo '<h3>Task '.$task->id.': '.$task->command.' '; + if (isset($task->exit)) { + if ($task->exit == 0) { + echo '<span style="color: green">[completed]</span>'; + } else { + echo '<span style="color: red">[exit status '.$task->exit.']</span>'; } - $task=new sql_task($task); - echo '<h3><a href="'.url('logs/task'.$task->id).'#end">Task '.$task->id.'</a>: '.$task->command.' '; - if (isset($task->exit)) { - if ($task->exit == 0) { - echo '<span style="color: green">[completed]</span>'; - } else { - echo '<span style="color: red">[exit status '.$task->exit.']</span>'; + } else { + echo '<span style="color: yellow">[running]</span>'; + } + echo '</h3>'; + if (isset($request['task']) && is_numeric($request['task'])) { + $s=$S['pdo']->query('SELECT * FROM `buildlogs` WHERE `task`='.$task->id.' ORDER BY `order` ASC'); + if ($s->rowCount()) { + echo '<code>'; + while ($entry=$s->fetch(PDO::FETCH_ASSOC)) { + $entry=new sql_buildlog_entry($entry); + $text=str_replace("\n", "<br/>\n", htmlentities($entry->text)); + echo '<a name="entry_'.$task->id.'_'.$entry->order.'"'.($entry->stream=='stderr'?' style="color: red" ':'').' title="'.strtoupper($entry->stream).', entry #'.$entry->order.' @ '.date('D j M Y @ H:i:s', $entry->timestamp).' UTC">'.$text.'</a>'; } + echo '<a name="end"> </a>'; + echo '</code>'; } else { - echo '<span style="color: yellow">[running]</span>'; + echo '<b>No output</b>'; } - echo '</h3>'; - if (isset($request['task']) && is_numeric($request['task'])) { - $s=$S['pdo']->query('SELECT * FROM `buildlogs` WHERE `task`='.$task->id.' ORDER BY `order` ASC'); - if ($s->rowCount()) { - echo '<code>'; - while (true) { - $entry=$s->fetch(PDO::FETCH_ASSOC); - if ($entry === false) { - break; - } - $entry=new sql_buildlog_entry($entry); - $text=str_replace("\n", "<br/>\n", htmlentities($entry->text)); - echo '<a name="entry_'.$task->id.'_'.$entry->order.'"'.($entry->stream=='stderr'?' style="color: red" ':'').'>'.$text.'</a>'; - } - echo '<a name="end"> </a>'; - echo '</code>'; - } else { - echo '<b>No output</b>'; - } + } + } elseif (isset($request['build']) && preg_match('/[a-z0-9]{6}/', $request['build'])) { + $r=$S['pdo']->query('SELECT * FROM `tasks` WHERE `build`="'.$request['build'].'" ORDER BY `id` ASC'); + $i=0; + while ($task=$r->fetch(PDO::FETCH_ASSOC)) { + $task=new sql_task($task); + echo '<a href="'.url('logs/task'.$task->id).'#end">Task #'.++$i.'</a>: '.htmlentities($task->command).'<br/>'; + } + } else { + $r=$S['pdo']->query('SELECT * FROM `builds` ORDER BY `id` ASC'); + if ($r->rowCount() == 0) { + echo print_warning('No builds found.'); + } + while ($build=$r->fetch(PDO::FETCH_ASSOC)) { + $build=new sql_build($build); + debug('<pre>'.print_r($build, true).'</pre>'); + echo '<a href="'.url('logs/build'.$build->id).'">Build '.$build->id.'</a>: '; + if (isset($build->name)) { + echo htmlentities($build->name); } + echo '<br/>'; } } } diff --git a/frontend/pages/newclass.php b/frontend/pages/newclass.php index 44d1bf7..f56fa9c 100644 --- a/frontend/pages/newclass.php +++ b/frontend/pages/newclass.php @@ -9,7 +9,7 @@ function body_newclass() { $table=$request['table']; eval("class $class extends sql_row_obj {\nvar \$table='$table';\n}\n"); $o=new $class(); - echo '<pre>'.highlight_string('<?php'."\n".$o->to_php().'?>', true),'</pre>'; + echo '<pre>'.str_replace(str_repeat(' ', 4), "\t", highlight_string('<?php'."\n".$o->to_php().'?>', true)),'</pre>'; } else { echo '<form action="'.url('newclass').'">Class name: <input name="class" /><br/>Table name: <input name="table" /><br/><input type="submit" value="Submit" /></form>'; } diff --git a/frontend/routing.csv b/frontend/routing.csv index 96bfbdc..c8426f1 100644 --- a/frontend/routing.csv +++ b/frontend/routing.csv @@ -12,6 +12,7 @@ # Home ^$ welcome ^logs$ logview +^logs/build([a-z0-9]{6})$ logview build ^logs/task([0-9]+)$ logview task ^hardreset$ hardreset ^newclass$ newclass diff --git a/shared/classes/0sql_row_obj.php b/shared/classes/0sql_row_obj.php index 69b534e..edafa4d 100644 --- a/shared/classes/0sql_row_obj.php +++ b/shared/classes/0sql_row_obj.php @@ -35,7 +35,7 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up private $db_values=array(), $values, $ref_cache; // Sets the PDO object to use public static function set_pdo_obj(&$obj) { - $obj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); + $obj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$pdo=$obj; } // Makes an SQL query using $sql and returns the resulting object @@ -292,13 +292,14 @@ abstract class sql_row_obj { // If the name of this class changes, it must be up if ($this->is_in_db()) { $q.=' WHERE '.$this->sql_id(); } - self::sql_query($q); + $r=self::sql_query($q); // Fill auto-increment column if it was null before the query if (isset($this->auto_increment) && $this->__get($this->auto_increment) === null) { $this->__set($this->auto_increment, self::$pdo->lastInsertId()); } // We've just written the current values to the db, so it stands to reason they are now the values in the db $this->values_to_db_values(); + return $r; } // (Re-)Loads data from the database function load() { diff --git a/shared/classes/build.php b/shared/classes/build.php index f2fb443..305dd8e 100644 --- a/shared/classes/build.php +++ b/shared/classes/build.php @@ -4,7 +4,8 @@ class sql_build extends sql_row_obj { 'id' => array ( 'type' => 'CHAR', 'length' => 6, - 'not_null' => true + 'not_null' => true, + 'unique' => true ), 'owner' => array ( 'type' => 'INT', @@ -24,14 +25,12 @@ class sql_build extends sql_row_obj { 'start' => array ( 'type' => 'INT', 'length' => 10, - 'unsigned' => true, - 'not_null' => true + 'unsigned' => true ), 'finish' => array ( 'type' => 'INT', 'length' => 10, - 'unsigned' => true, - 'not_null' => true + 'unsigned' => true ) ); diff --git a/shared/classes/buildlog_entry.php b/shared/classes/buildlog_entry.php index d3dfb9c..f060e9e 100644 --- a/shared/classes/buildlog_entry.php +++ b/shared/classes/buildlog_entry.php @@ -5,23 +5,32 @@ class sql_buildlog_entry extends sql_row_obj { 'type' => 'INT', 'length' => 10, 'unsigned' => true, - 'not null' => true, + 'not_null' => true, + 'default' => 0 ), 'order' => array ( 'type' => 'INT', 'length' => 10, 'unsigned' => true, - 'not null' => true, + 'not_null' => true, + 'default' => 0 + ), + 'timestamp' => array ( + 'type' => 'INT', + 'length' => 10, + 'unsigned' => true, + 'not_null' => true ), 'stream' => array ( 'type' => 'ENUM', 'length' => '\'stdout\',\'stderr\',\'system\'', - 'not null' => true, + 'not_null' => true ), 'text' => array ( 'type' => 'TEXT', - 'not null' => true, + 'not_null' => true ) + ); } ?> |