コード例 #1
0
ファイル: Build.php プロジェクト: rasismeiro/cintient
 public function init()
 {
     //
     // Get the ID, first and foremost. Also this is the start datetime
     //
     $this->setDate($this->getNowDatetime());
     if (!$this->_save(true)) {
         return false;
     }
     $project = $this->getPtrProject();
     // Easier handling
     //
     // A few more sanity checks
     //
     $integrationBuilder = $project->getIntegrationBuilder();
     if (!$integrationBuilder instanceof Build_BuilderElement_Project) {
         SystemEvent::raise(SystemEvent::DEBUG, "No valid integration builder specified. [PROJECTID={$this->getProjectId()}]", __METHOD__);
         return false;
     }
     if ($integrationBuilder->isEmpty()) {
         SystemEvent::raise(SystemEvent::DEBUG, "Empty integration builder. [PROJECTID={$this->getProjectId()}]", __METHOD__);
         return false;
     }
     //
     // Check for special tasks and run pre build actions
     //
     $specialTasks = $this->getSpecialTasks();
     if (!empty($specialTasks)) {
         foreach ($specialTasks as $task) {
             if (!class_exists($task)) {
                 SystemEvent::raise(SystemEvent::ERROR, "Unexisting special task. [PID={$project->getId()}] [BUILD={$this->getId()}] [TASK={$task}]", __METHOD__);
                 return false;
             }
             $o = new $task($this);
             if (!$o->preBuild()) {
                 SystemEvent::raise(SystemEvent::ERROR, "Special task's pre build execution aborted. [PID={$project->getId()}] [BUILD={$this->getId()}] [TASK={$task}]", __METHOD__);
                 return false;
             }
         }
     }
     //
     // Export and execute the builder's source code
     //
     $builderCode = $integrationBuilder->toPhp();
     SystemEvent::raise(SystemEvent::DEBUG, "Integration builder source code:" . PHP_EOL . print_r($builderCode, true), __METHOD__);
     // Execute as an external process in order to have a clean sandboxed
     // environment. eval($builderCode) is no more.
     $builderProcess = new Framework_Process($GLOBALS['settings'][SystemSettings::EXECUTABLE_PHP]);
     $builderProcess->setStdin($builderCode);
     $builderProcess->run();
     $this->setDuration(time() - strtotime($this->getDate()));
     // Setup the first finish time. If we get to the end of this method, update again at the end
     //
     // Import back into Cintient space the external builder's output
     // TODO: we should probably have this somewhere better than
     // $GLOBALS['result']...
     //
     // Also check BuilderElement_Project for the expected
     // $GLOBALS['result'] vars...
     //
     $output = explode("\n", str_replace("\r", "", $builderProcess->getStdout()));
     $GLOBALS['result'] = array();
     $GLOBALS['result']['ok'] = false;
     foreach ($output as $line) {
         $line = trim($line);
         $neck = strpos($line, '=');
         $key = substr($line, 0, $neck);
         $value = substr($line, $neck + 1);
         $value = str_replace(CINTIENT_NEWLINE_TOKEN, "\n", $value);
         if (!empty($key)) {
             $GLOBALS['result'][$key] = $value;
         }
     }
     if (!empty($GLOBALS['result']['stacktrace'])) {
         $this->addToOutput($GLOBALS['result']['stacktrace']);
     }
     if (!$builderProcess->emptyStderr()) {
         $this->addToOutput($builderProcess->getStderr());
     }
     if ($GLOBALS['result']['ok'] != true) {
         if (!empty($GLOBALS['result']['task'])) {
             SystemEvent::raise(SystemEvent::INFO, "Failed executing task {$GLOBALS['result']['task']}.", __METHOD__);
         } else {
             SystemEvent::raise(SystemEvent::INFO, "Failed for unknown reasons.", __METHOD__);
         }
         SystemEvent::raise(SystemEvent::DEBUG, "Possible stacktrace: " . print_r($GLOBALS['result'], true), __METHOD__);
         return false;
     }
     $this->setDuration(time() - strtotime($this->getDate()));
     //
     // Create this build's report dir, backing up an existing one, just in case
     //
     if (is_dir($this->getBuildDir())) {
         $backupOldBuildDir = rtrim($this->getBuildDir(), '/') . '_old_' . uniqid() . '/';
         if (!@rename($this->getBuildDir(), $backupOldBuildDir)) {
             SystemEvent::raise(SystemEvent::ERROR, "Couldn't create backup of existing build dir found. [PID={$project->getId()}] [DIR={$this->getBuildDir()}] [BUILD={$this->getId()}]", __METHOD__);
             return false;
         }
     }
     if (!@mkdir($this->getBuildDir(), DEFAULT_DIR_MASK, true)) {
         SystemEvent::raise(SystemEvent::ERROR, "Couldn't create build dir. [PID={$project->getId()}] [DIR={$this->getBuildDir()}] [BUILD={$this->getId()}]", __METHOD__);
         return false;
     }
     //
     // Run post build actions
     //
     reset($specialTasks);
     $result = true;
     if (!empty($specialTasks)) {
         foreach ($specialTasks as $task) {
             if (!class_exists($task)) {
                 SystemEvent::raise(SystemEvent::ERROR, "Unexisting special task. [PID={$project->getId()}] [BUILD={$this->getId()}] [TASK={$task}]", __METHOD__);
                 continue;
             }
             $o = new $task($this);
             $result = $result & $o->postBuild();
         }
     }
     if (!$result) {
         // Don't abort, since this is just the post build actions, not the build itself.
         SystemEvent::raise(SystemEvent::ERROR, "Special task's post build execution had problems. [PID={$project->getId()}] [BUILD={$this->getId()}] [TASK={$task}]", __METHOD__);
     }
     $this->setStatus(self::STATUS_OK_WITHOUT_PACKAGE);
     $this->setDuration(time() - strtotime($this->getDate()));
     // Final duration time refresh
     return true;
 }