protected function allServers(Environment $environment, InputInterface $input, OutputInterface $output, \Closure $action) { //Loop on the servers /** * @var \Onigoetz\Deployer\Configuration\Server */ foreach ($environment->getServers() as $server) { $output->writeln("Deploying on <info>{$server->getHost()}</info>"); $output->writeln('-------------------------------------------------'); //Ask server password if needed ? if (!($password = $server->getPassword())) { $text = "Password for <info>{$server->getUsername()}@{$server->getHost()}</info>:"; $question = new Question($text, false); $question->setHidden(true)->setHiddenFallback(false); $password = $this->getHelper('question')->ask($input, $output, $question); } //Login to server $ssh = new Net_SFTP($server->getHost()); if (!$ssh->login($server->getUsername(), $password)) { throw new \Exception("Login failed on host '{$server->getHost()}'"); } $action($ssh); $ssh->disconnect(); } }
protected function rollback(OutputInterface $output, Net_SFTP $ssh, Environment $environment) { $runner = new RemoteActionRunner($output, $ssh); $previous = trim($ssh->exec('cat ' . $environment->getDirectories()->getRoot() . '/previous')); if ($previous == '') { $output->writeln('<error>Cannot find previous file !!!</error>'); return; } $actions = ['Removing the symlink of the release to rollback' => ['action' => 'rmfile', 'file' => $environment->getDirectories()->getDeploy()], 'Link it again to the snapshot ' . $previous => ['action' => 'symlink', 'target' => $previous, 'link_name' => $environment->getDirectories()->getDeploy()]]; $output->writeln('Previous snapshot : ' . $previous); $output->writeln("Reverting...\n", 'blue'); $this->runActions($runner, $actions, $output, $environment->getSubstitutions($previous)); $output->writeln("Done\n"); }
protected function deploy(InputInterface $input, OutputInterface $output, Net_SFTP $ssh, Environment $environment, $destination) { $destinationDir = dirname($destination); $runner = new RemoteActionRunner($output, $ssh); $this->runAction('Create folders on server', $output, function () use($runner, $destinationDir) { $runner->setupServer($destinationDir); }); if ($environment->getSource() instanceof Cloned) { $class = 'Onigoetz\\Deployer\\SCM\\' . ucfirst($environment->getSource()->getType()); if (!class_exists($class)) { throw new \Exception("Cannot find SCM '{$class}'"); } /** * @var \Onigoetz\Deployer\SCM\SCM */ $scm = new $class($environment); $final = $environment->getSource()->getFinalUrl($this->getHelper('question'), $input, $output); $this->runAction('Clone the latest version', $output, function () use($scm, $ssh, $runner, $final, $destination) { $git = $scm->getCommand($ssh); return $runner->exec($scm->cloneCommand($git, $final, $destination)); }); } $substitutions = $environment->getSubstitutions($destination); $output->writeln('<fg=blue;options=bold>Before deployment actions</fg=blue;options=bold>'); $this->runActions($runner, $environment->getTasks('before'), $output, $substitutions); $output->writeln('<fg=blue;options=bold>Deployment</fg=blue;options=bold>'); $this->runAction('Store the current deployment for eventual rollback', $output, function () use($runner, $environment, $ssh) { $previous = $runner->getSymlinkDestination($environment->getDirectories()->getDeploy()); $ssh->put($environment->getDirectories()->getRoot() . '/previous', $previous); return "Previous snapshot : {$previous}"; }); $this->runAction('Symlink the new deployment', $output, function () use($environment, $runner, $destination) { $deploy = $environment->getDirectories()->getDeploy(); $runner->rmfile($deploy); $runner->symlink($destination, $deploy); }); $output->writeln(''); $output->writeln('<fg=blue;options=bold>After deployment actions</fg=blue;options=bold>'); $this->runActions($runner, $environment->getTasks('after'), $output, $substitutions); $output->writeln('Done'); }