summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbackend/backend.php1
-rw-r--r--backend/functions/build.php31
-rw-r--r--backend/functions/error_handling.php9
-rw-r--r--backend/functions/execution.php36
-rw-r--r--backend/functions/log.php20
-rw-r--r--depend2
6 files changed, 96 insertions, 3 deletions
diff --git a/backend/backend.php b/backend/backend.php
index d16497a..84af0db 100755
--- a/backend/backend.php
+++ b/backend/backend.php
@@ -2,7 +2,6 @@
<?php
require_once(dirname(__FILE__).'/../shared/include/paths.php'); // USE __DIR__ once 5.3.0 is out
require_once(SHARED.'/include/includes.php');
-pcntl_signal(SIGCHLD, SIG_IGN); // Avoid zombie processes - TODO get rid of this, we generally don't want to keep going with children in the background, or if we do, we'll set a real handler there.
$opts=getopt('f');
if (isset($opts['f'])) {
$f=pcntl_fork();
diff --git a/backend/functions/build.php b/backend/functions/build.php
index 5823398..6ef5fa2 100644
--- a/backend/functions/build.php
+++ b/backend/functions/build.php
@@ -1,11 +1,38 @@
<?php
+// Sample variables (will be in config file or user input)
+$conf['pkgdir']='/home/eitan/soc/tinderbox/default-linux-amd64';
+$conf['cflags']='-march=nocona -O2 -pipe';
+$conf['cxxflags']=$build['cflags'];
+$conf['chost']='x86_64-pc-linux-gnu';
+$conf['port_logdir']='$W/log';
+$conf['emerge_default_opts']='-K --color=y';
+$profile='/etc/make.profile';
// This is the main function that carries out a build from start to finish
function build() {
+ 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));
- mkdir(W, 0700);
- mkdir(W.'/etc/portage', 0700, true);
+ fatal(log_status('Creating work directory '.W, mkdir(W, 0700)));
+ fatal(log_status('Creating '.W.'/image', mkdir(W.'/image', 0700)));
+ define('I', W.'/image');
+ fatal(log_status('Creating '.W.'/config_root', mkdir(W.'/config_root', 0700)));
+ define('C', W.'/config_root');
+ fatal(log_status('Creating '.C.'/etc', mkdir(C.'/etc', 0700)));
+ fatal(log_status('Creating '.C.'/etc/portage', mkdir(C.'/etc/portage', 0700)));
+ fatal(log_status('Creating '.W.'/log', mkdir(W.'/log', 0700)));
+ $makeconf='W="'.W.'"'."\n";
+ foreach ($conf as $name => $val) {
+ $makeconf.=strtoupper($name).'="'.$val.'"'."\n";
+ }
+ fatal(log_status('Writing '.C.'/make.conf:'."\n".indent($makeconf), file_put_contents(C.'/etc/make.conf', $makeconf)));
+ unset($makeconf);
+ fatal(log_status('Making make.profile symlink to '.$profile, symlink($profile, C.'/etc/make.profile')));
+ $envstring='PORTAGE_CONFIGROOT='.C.' ROOT='.I.' ';
+ passthru($envstring.'emerge --info');
+ passthru($envstring.'emerge --color=y -pv system');
}
?>
diff --git a/backend/functions/error_handling.php b/backend/functions/error_handling.php
new file mode 100644
index 0000000..4439509
--- /dev/null
+++ b/backend/functions/error_handling.php
@@ -0,0 +1,9 @@
+<?php
+function fatal($status) {
+ if ($status === false) {
+ exit(1);
+ } else {
+ return $status;
+ }
+}
+?>
diff --git a/backend/functions/execution.php b/backend/functions/execution.php
new file mode 100644
index 0000000..75d9da8
--- /dev/null
+++ b/backend/functions/execution.php
@@ -0,0 +1,36 @@
+<?php
+function log_command($id, $command, $path=null, $env=null) {
+ $descriptorspec=array(
+ 0 => array('pipe', 'r'), // STDIN
+ 1 => array('pipe', 'w'), // STDOUT
+ 2 => array('pipe', 'w') // STDERR
+ );
+ $p=proc_open($command, $descriptorspec, $pipes, $path, $env);
+ foreach ($pipes as $pipe) {
+ stream_set_blocking($pipe, 0);
+ }
+ while (true) {
+ $status=proc_get_status($p);
+ $s=stream_select(array($pipes[1], $pipes[2]), array(), array(), 1);
+ if ($s) {
+ $c=stream_get_contents($pipes[2]);
+ // TODO this really needs to go to the DB and carry metadata
+ if ($c) {
+ log_msg($c); // STDERR
+ }
+ $c=stream_get_contents($pipes[1]);
+ if ($c) {
+ log_msg($c); // STDOUT
+ }
+ }
+ if ($status['running'] === false) {
+ break;
+ }
+ }
+ foreach ($pipes as $pipe) {
+ fclose($pipe);
+ }
+ $exit_status=proc_close($p);
+ // Handle end status
+}
+?>
diff --git a/backend/functions/log.php b/backend/functions/log.php
index acb6c35..c07fd73 100644
--- a/backend/functions/log.php
+++ b/backend/functions/log.php
@@ -1,2 +1,22 @@
<?php
+// In the future, this will log to the database and require a build ID, but for now it just prints
+function log_msg($msg, $nl=true) {
+ echo $msg.($nl?"\n":'');
+}
+function log_status($msg, $result) {
+ log_msg($msg."... ".($result?color('[success]', 'green'):color('[failure]', 'red')));
+ return $result;
+}
+function color($msg, $color) {
+ $r="\033[0m";
+ switch(strtolower($color)) {
+ case 'red':
+ return "\033[31m$msg$r";
+ case 'green':
+ return "\033[32m$msg$r";
+ }
+}
+function indent($msg, $tabs=1) {
+ return str_replace("\n", "\n".str_repeat("\t", $tabs), $msg);
+}
?>
diff --git a/depend b/depend
index 9d0fe3f..9309914 100644
--- a/depend
+++ b/depend
@@ -1 +1,3 @@
>=dev-lang/php-5.2.0 USE=pdo hash pcntl cli
+sys-apps/fakeroot
+sys-apps/portage