diff options
author | 2011-07-11 14:59:51 +0100 | |
---|---|---|
committer | 2011-07-11 14:59:51 +0100 | |
commit | cb7c822b0284d11d16b49023162820d1ffca6fdd (patch) | |
tree | 18102deaecf445568d30bb63187294b0cceb10f2 /daemon.php | |
parent | Added compress flag to build tool (diff) | |
download | gentoaster-cb7c822b0284d11d16b49023162820d1ffca6fdd.tar.gz gentoaster-cb7c822b0284d11d16b49023162820d1ffca6fdd.tar.bz2 gentoaster-cb7c822b0284d11d16b49023162820d1ffca6fdd.zip |
Fixed hostname setting and changed Gearman daemon/client to meet Zend coding standard
Diffstat (limited to 'daemon.php')
-rw-r--r-- | daemon.php | 158 |
1 files changed, 86 insertions, 72 deletions
@@ -1,76 +1,90 @@ <?php - // Gentoaster build daemon worker - // Licensed under GPL v3, see COPYING file + // Gentoaster build daemon worker + // Licensed under GPL v3, see COPYING file + + $configurationsPath = "/var/www/gentoaster"; + $gentoasterPath = "/usr/share/gentoaster"; + $toolName = "create_image.sh"; + + // DO NOT EDIT BELOW THIS LINE + + $progressMagic = 23; + + $worker = new GearmanWorker(); + $worker->addServer(); + $worker->addFunction("invoke_image_build", "image_build"); + while ($worker->work()); + + function update_result($handle, $returncode, $result) + { + $result = trim($result); + echo "A job finished with return code ".$returncode.": ".$result."\n"; + $db = mysql_connect("localhost", "gentoaster", ""); + if(!$db) die("Could not connect to database ".mysql_error()); + mysql_select_db("gentoaster"); + $result = mysql_real_escape_string($result); + $query = "UPDATE builds". + " SET result = '".$result."', returncode = '".$returncode + "' WHERE handle = '".mysql_real_escape_string($handle)."'"; + mysql_query($query); + return serialize(array($returncode, $result)); + } + + function image_build($job) + { + global $configurationsPath, $gentoasterPath, $toolName, $progressMagic; + + $handle = $job->handle(); + $handlehash = md5($handle); + + echo "Processing job handle hash ".$handlehash."\n"; + + $configurationString = $job->workload(); + $configurationArray = parse_ini_string($configurationString); + + if ($configurationArray !== FALSE) { + if (isset($configurationArray["BUILD_ID"])) { + $buildID = $configurationArray["BUILD_ID"]; + $buildPath = $configurationsPath."/".$buildID; + @mkdir($buildPath, 0777, true); + + if (is_writable($buildPath)) { + chdir($buildPath); + file_put_contents("config.ini", $configurationString); + $toolArgs = "--config config.ini --compress"; + $cmd = $gentoasterPath."/".$toolName." ".$toolArgs; + $processHandle = popen($cmd." 2>&1", "r"); + + $nonstatusOutput = ""; + + while (!feof($processHandle)) { + $progressLine = fgets($processHandle); + preg_match("/Step (.+):/", $progressLine, $matches); + if (sizeof($matches) > 0) { + $job->sendStatus($matches[1], $progressMagic); + } else { + $nonstatusOutput .= $progressLine; + } + } + + $returncode = pclose($processHandle); + + unlink("config.ini"); + + return update_result($handle, $returncode, $nonstatusOutput); + } else { + $error = "Configured build path is not writable"; + return update_result($handle, -2, $error); + } + } else { + $error = "Configuration file is incomplete"; + return update_result($handle, -3, $error); + } + } else { + $error = "Configuration string is not valid"; + return update_result($handle, -4, $error); + } + } - $configurations_path = "/var/www/gentoaster"; - $gentoaster_path = "/usr/share/gentoaster"; - $tool_name = "create_image.sh"; - // DO NOT EDIT BELOW THIS LINE - - $progress_magic = 23; - - $worker = new GearmanWorker(); - $worker->addServer(); - $worker->addFunction("invoke_image_build", "image_build"); - while ($worker->work()); - - function update_result($handle, $returncode, $result) { - $result = trim($result); - echo "A job finished with return code ".$returncode.": ".$result."\n"; - $db = mysql_connect("localhost","gentoaster",""); - if(!$db) die("Could not connect to database ".mysql_error()); - mysql_select_db("gentoaster"); - mysql_query("UPDATE builds SET result = '".mysql_real_escape_string($result)."', returncode = '".$returncode."' WHERE handle = '".mysql_real_escape_string($handle)."'"); - return serialize(array($returncode, $result)); - } - - function image_build($job) { - global $configurations_path, $gentoaster_path, $tool_name, $progress_magic; - - $handle = $job->handle(); - $handlehash = md5($handle); - - echo "Processing job handle hash ".$handlehash."\n"; - - $configuration_string = $job->workload(); - $configuration_array = parse_ini_string($configuration_string); - - if($configuration_array !== FALSE && isset($configuration_array["BUILD_ID"])) { - $build_id = $configuration_array["BUILD_ID"]; - $build_path = $configurations_path."/".$build_id; - @mkdir($build_path, 0777, true); - - if(is_writable($build_path)) { - chdir($build_path); - file_put_contents("config.ini", $configuration_string); - $tool_args = "--config config.ini --compress"; - $process_handle = popen($gentoaster_path."/".$tool_name." ".$tool_args." 2>&1", "r"); - - $nonstatus_output = ""; - - while(!feof($process_handle)) { - $progress_line = fgets($process_handle); - preg_match("/Step (.+):/", $progress_line, $matches); - if(sizeof($matches) > 0) { - $job->sendStatus($matches[1], $progress_magic); - } else { - $nonstatus_output .= $progress_line; - } - } - - $returncode = pclose($process_handle); - - unlink("config.ini"); - - return update_result($handle, $returncode, $nonstatus_output); - } else { - return update_result($handle, -2, "Configured build path is not writable"); - } - } else { - return update_result($handle, -3, "Configuration string is not valid"); - } - } - -?> |