Esempio n. 1
0
 public function handle(Pusher $pusher)
 {
     $timeStarted = time();
     $key = $this->writePrivateKey();
     try {
         $this->release->update(['status' => Release::RUNNING, 'started_at' => new \DateTime()]);
         $ansible = new Ansible($this->release->path(), Release::INVENTORY_FILENAME, Release::PLAYBOOK_FILENAME, $this->release->inventory->params + ['private_key' => $key]);
         $process = $ansible->play();
         $process->start();
         $lastOut = $process->getOutput();
         while ($process->isRunning() && !$this->release->isCancelled()) {
             $out = $process->getOutput();
             if ($lastOut != $out) {
                 $this->updateRelease($process);
                 $pusher->trigger(['releases'], "release-" . $this->release->id, $this->release->toArray());
                 $lastOut = $out;
             }
             sleep(1);
         }
         if ($process->isRunning()) {
             $process->stop(0);
         }
         $this->fs()->delete($key);
         $this->updateRelease($process);
         if ($this->release->status == Release::CANCELLED) {
             $pusher->trigger(['releases'], "release-" . $this->release->id, $this->release->toArray());
         } elseif ($process->getExitCode() == 0) {
             $this->notifier()->notifySuccess($this->release);
             $this->release->update(['status' => Release::COMPLETED, 'time' => time() - $timeStarted]);
             $pusher->trigger(['releases'], "release-" . $this->release->id, $this->release->toArray());
         } else {
             $this->notifier()->notifyFailure($this->release, $process->getErrorOutput());
             throw new AnsibleException($this->release, $ansible, $process->getErrorOutput());
         }
     } catch (\Exception $e) {
         $this->release->update(['status' => Release::ERROR, 'time' => time() - $timeStarted]);
         $pusher->trigger(['releases'], "release-" . $this->release->id, $this->release->toArray());
         $this->fs()->delete($key);
         throw $e;
     }
 }
Esempio n. 2
0
 /**
  * @throws \App\Exceptions\InvalidConfigException
  * @throws ReleaseException
  */
 private function writePlaybooks()
 {
     $this->fs()->put($this->release->path("empty.yml"), "");
     $playbook = new PlaybookConfig();
     $config = $this->release->config();
     foreach ($config->roles() as $play) {
         if (in_array($play->name(), $this->release->roles) || in_array($play->role(), $this->release->roles)) {
             $play->setSudo(true);
             $playbook->add($play);
         }
     }
     $playbook->setVars(["project_name" => $this->release->repo->name, "global" => array_merge($config->defaults(), $config->globals(), (array) $this->release->repo->params), "build_tar" => $this->release->path("build.tar.gz"), "build_path" => $this->release->path(), "build_version" => $this->release->commit, "build_version_short" => $this->release->commit()->getShortHash(), "inventory_name" => $this->release->inventory->name] + $config->getVars());
     $playbookFile = $this->release->path(Release::PLAYBOOK_FILENAME);
     if (!$this->fs()->put($playbookFile, $playbook->render())) {
         throw new ReleaseException($this->release, "Cannot write playbook file: {$playbookFile}!");
     }
     $inventoryFile = $this->release->path(Release::INVENTORY_FILENAME);
     if (!$this->fs()->put($inventoryFile, $this->release->inventory->render())) {
         throw new ReleaseException($this->release, "Cannot write inventory file: {$inventoryFile}!");
     }
 }