Allow parameters per stage

This commit is contained in:
jelte 2014-08-06 11:26:41 +02:00
parent 94e0463e43
commit 0298522b2b
4 changed files with 108 additions and 25 deletions

View File

@ -8,8 +8,9 @@
namespace Deployer\Console;
use Deployer\Deployer;
use Deployer\Server\DryRun;
use Deployer\Environment;
use Deployer\Server\ServerInterface;
use Deployer\Stage\Stage;
use Deployer\Task\AbstractTask;
use Deployer\Task\Runner;
use Deployer\Task\TaskInterface;
@ -68,12 +69,29 @@ class RunTaskCommand extends BaseCommand
// Nothing to do now.
}
$servers = Deployer::$servers;
if (Deployer::$multistage) {
if (null === $input->getArgument('stage')) {
throw new \InvalidArgumentException('You have turned on multistage support, but not defined a stage (or default stage).');
}
if (!isset(Deployer::$stages[$input->getArgument('stage')])) {
throw new \InvalidArgumentException('This stage is not defined.');
}
/** @var Stage $stage */
$stage = Deployer::$stages[$input->getArgument('stage')];
$servers = $stage->getServers();
foreach ( $stage->getOptions() as $key => $value ) {
set($key, $value);
}
}
try {
foreach ($this->task->get() as $runner) {
$isPrinted = $this->writeDesc($output, $runner->getDesc());
$this->runSeries($runner, $input, $output);
$this->runSeries($runner, $servers, $input, $output);
if ($isPrinted) {
$this->writeOk($output);
@ -81,28 +99,20 @@ class RunTaskCommand extends BaseCommand
}
} catch (\Exception $e) {
$this->rollbackOnDeploy($input, $output);
$this->rollbackOnDeploy($servers, $input, $output);
throw $e;
}
}
private function runSeries(Runner $runner, InputInterface $input, OutputInterface $output)
private function runSeries(Runner $runner, array $servers, InputInterface $input, OutputInterface $output)
{
$taskName = $runner->getName();
$taskName = empty($taskName) ? 'UnNamed' : $taskName;
$servers = Deployer::$servers;
if ( Deployer::$multistage ) {
if (null === $input->getArgument('stage')) {
throw new \InvalidArgumentException('You have turned on multistage support, but not defined a stage (or default stage).');
}
if (!isset(Deployer::$stages[$input->getArgument('stage')])) {
throw new \InvalidArgumentException('This stage is not defined.');
}
$servers = Deployer::$stages[$input->getArgument('stage')];
}
/**
* @var string $name
* @var ServerInterface $server
*/
foreach ($servers as $name => $server) {
// Skip to specified server.
$onServer = $input->getOption('server');
@ -170,7 +180,7 @@ class RunTaskCommand extends BaseCommand
* @param InputInterface $input
* @param OutputInterface $output
*/
private function rollbackOnDeploy(InputInterface $input, OutputInterface $output)
private function rollbackOnDeploy(array $servers, InputInterface $input, OutputInterface $output)
{
if (!isset(Deployer::$tasks['deploy:rollback'])) {
return;
@ -179,7 +189,7 @@ class RunTaskCommand extends BaseCommand
$task = Deployer::$tasks['deploy:rollback'];
foreach ($task->get() as $runner) {
$this->runSeries($runner, $input, $output);
$this->runSeries($runner, $servers, $input, $output);
}
}
}

66
src/Stage/Stage.php Normal file
View File

@ -0,0 +1,66 @@
<?php
namespace Deployer\Stage;
class Stage
{
/**
* @var string
*/
protected $name;
/**
* @var array
*/
protected $servers = [];
/**
* @var array
*/
protected $options = [];
/**
* @param string $name Name of the stage
* @param array $servers List of servers
* @param array $options List of additional options
*/
public function __construct($name, array $servers, array $options = array())
{
$this->name = $name;
$this->servers = $servers;
$this->options = $options;
}
public function getName()
{
return $this->name;
}
public function getServers()
{
return $this->servers;
}
public function options(array $options)
{
$this->options = $options;
}
public function set($key, $value)
{
return $this->options[$key] = $value;
}
public function get($key, $default)
{
return array_key_exists($key, $this->options) ? $this->options[$key] : $default;
}
public function getOptions()
{
return $this->options;
}
}

View File

@ -8,15 +8,14 @@ use Deployer\Deployer;
class StageFactory
{
/**
* @param $name
* @param array $servers
* @param bool $default
* @return array
* @return Stage
* @throws \RuntimeException
*/
public static function create($name, array $servers, $default = false)
public static function create($name, array $servers, array $options = array(), $default = false)
{
if ( count(Deployer::$servers) == 0 ) {
throw new \RuntimeException('Server should be defined before you define any stages.');
@ -35,13 +34,13 @@ class StageFactory
});
// Register the stage serverlist
Deployer::$stages[$name] = $servers;
Deployer::$stages[$name] = new Stage($name, $servers, $options);
// When defined as default, set the stage as default on Deployer
if ( $default ) {
Deployer::$defaultStage = $name;
}
return $servers;
return Deployer::$stages[$name];
}
}

View File

@ -31,9 +31,17 @@ function multistage($defaultStage = 'develop')
Deployer::$defaultStage = $defaultStage;
}
function stage($name, array $servers, $default = false)
/**
* Define a new stage
* @param string $name Name of current stage
* @param array $servers List of servers
* @param array $options List of addition options
* @param bool $default Set as default stage
* @return Stage\Stage
*/
function stage($name, array $servers, array $options = array(), $default = false)
{
return Stage\StageFactory::create($name, $servers, $default);
return Stage\StageFactory::create($name, $servers, $options, $default);
}
/**