diff --git a/src/Console/RunTaskCommand.php b/src/Console/RunTaskCommand.php index bbde0c60..26c52155 100644 --- a/src/Console/RunTaskCommand.php +++ b/src/Console/RunTaskCommand.php @@ -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); } } } \ No newline at end of file diff --git a/src/Stage/Stage.php b/src/Stage/Stage.php new file mode 100644 index 00000000..d2ba442b --- /dev/null +++ b/src/Stage/Stage.php @@ -0,0 +1,66 @@ +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; + } +} \ No newline at end of file diff --git a/src/Stage/StageFactory.php b/src/Stage/StageFactory.php index 786d4840..b6e93f65 100644 --- a/src/Stage/StageFactory.php +++ b/src/Stage/StageFactory.php @@ -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]; } } \ No newline at end of file diff --git a/src/functions.php b/src/functions.php index 7d4ba81c..c97d6a4f 100644 --- a/src/functions.php +++ b/src/functions.php @@ -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); } /**