[Nostromo] Refactor Runtime methods.

This commit is contained in:
Andrés Montañez 2017-01-11 22:22:21 -03:00
parent 7493e3cfae
commit b095cd0bb8
24 changed files with 75 additions and 108 deletions

View file

@ -48,7 +48,7 @@ class EnvironmentsCommand extends AbstractCommand
$table = new Table($output);
$table->setHeaders(['Environment', 'User', 'Branch', 'Hosts']);
$configuration = $this->runtime->getConfigOptions('environments');
$configuration = $this->runtime->getConfigOption('environments');
foreach ($configuration as $environment => $config) {
$row = [$environment];

View file

@ -70,24 +70,24 @@ class DeployCommand extends AbstractCommand
$output->writeln(sprintf(' Environment: <fg=green>%s</>', $this->runtime->getEnvironment()));
$this->log(sprintf('Environment: %s', $this->runtime->getEnvironment()));
if ($this->runtime->getEnvParam('releases', false)) {
if ($this->runtime->getEnvOption('releases', false)) {
$this->runtime->generateReleaseId();
$output->writeln(sprintf(' Release ID: <fg=green>%s</>', $this->runtime->getReleaseId()));
$this->log(sprintf('Release ID: %s', $this->runtime->getReleaseId()));
}
if ($this->runtime->getConfigOptions('log_file', false)) {
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOptions('log_file')));
if ($this->runtime->getConfigOption('log_file', false)) {
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOption('log_file')));
}
$output->writeln(sprintf(' Strategy: <fg=green>%s</>', $strategy->getName()));
if ($input->getOption('branch') !== false) {
$this->runtime->setEnvironmentConfig('branch', $input->getOption('branch'));
$this->runtime->setEnvOption('branch', $input->getOption('branch'));
}
if ($this->runtime->getEnvParam('branch', false)) {
$output->writeln(sprintf(' Branch: <fg=green>%s</>', $this->runtime->getEnvParam('branch')));
if ($this->runtime->getEnvOption('branch', false)) {
$output->writeln(sprintf(' Branch: <fg=green>%s</>', $this->runtime->getEnvOption('branch')));
}
$output->writeln('');
@ -140,7 +140,7 @@ class DeployCommand extends AbstractCommand
protected function runOnHosts(OutputInterface $output, $tasks)
{
$hosts = $this->runtime->getEnvParam('hosts');
$hosts = $this->runtime->getEnvOption('hosts');
if (count($hosts) == 0) {
$output->writeln(sprintf(' No hosts defined, skipping %s tasks', $this->getStageName()));
$output->writeln('');

View file

@ -58,25 +58,25 @@ class ListCommand extends AbstractCommand
try {
$this->runtime->setEnvironment($input->getArgument('environment'));
if (!$this->runtime->getEnvParam('releases', false)) {
if (!$this->runtime->getEnvOption('releases', false)) {
throw new RuntimeException('Releases are not enabled', 70);
}
$output->writeln(sprintf(' Environment: <fg=green>%s</>', $this->runtime->getEnvironment()));
$this->log(sprintf('Environment: %s', $this->runtime->getEnvironment()));
if ($this->runtime->getConfigOptions('log_file', false)) {
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOptions('log_file')));
if ($this->runtime->getConfigOption('log_file', false)) {
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOption('log_file')));
}
$output->writeln('');
$hosts = $this->runtime->getEnvParam('hosts');
$hosts = $this->runtime->getEnvOption('hosts');
if (count($hosts) == 0) {
$output->writeln('No hosts defined');
$output->writeln('');
} else {
$hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/');
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
foreach ($hosts as $host) {
$this->runtime->setWorkingHost($host);

View file

@ -61,7 +61,7 @@ class RollbackCommand extends DeployCommand
$strategy = $this->runtime->guessStrategy();
$this->taskFactory = new TaskFactory($this->runtime);
if (!$this->runtime->getEnvParam('releases', false)) {
if (!$this->runtime->getEnvOption('releases', false)) {
throw new RuntimeException('Releases are not enabled', 70);
}
@ -78,8 +78,8 @@ class RollbackCommand extends DeployCommand
$output->writeln(sprintf(' Rollback to Release Id: <fg=green>%s</>', $this->runtime->getReleaseId()));
$this->log(sprintf('Release ID: %s', $this->runtime->getReleaseId()));
if ($this->runtime->getConfigOptions('log_file', false)) {
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOptions('log_file')));
if ($this->runtime->getConfigOption('log_file', false)) {
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOption('log_file')));
}
$output->writeln(sprintf(' Strategy: <fg=green>%s</>', $strategy->getName()));
@ -104,8 +104,8 @@ class RollbackCommand extends DeployCommand
*/
protected function checkReleaseAvailability($releaseToRollback)
{
$hosts = $this->runtime->getEnvParam('hosts');
$hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/');
$hosts = $this->runtime->getEnvOption('hosts');
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
$availableInHosts = 0;
foreach ($hosts as $host) {

View file

@ -8,11 +8,10 @@
* file that was distributed with this source code.
*/
namespace Mage\Deploy\Strategy\Releases;
namespace Mage\Deploy\Strategy;
use Mage\Runtime\Exception\RuntimeException;
use Mage\Runtime\Runtime;
use Mage\Deploy\Strategy\StrategyInterface;
/**
* Strategy for Deployment with Releases, using TarGz and SCP

View file

@ -8,11 +8,10 @@
* file that was distributed with this source code.
*/
namespace Mage\Deploy\Strategy\Rsync;
namespace Mage\Deploy\Strategy;
use Mage\Runtime\Exception\RuntimeException;
use Mage\Runtime\Runtime;
use Mage\Deploy\Strategy\StrategyInterface;
/**
* Strategy for Deployment with Rsync

View file

@ -10,8 +10,8 @@
namespace Mage\Runtime;
use Mage\Deploy\Strategy\Releases\ReleasesStrategy;
use Mage\Deploy\Strategy\Rsync\RsyncStrategy;
use Mage\Deploy\Strategy\ReleasesStrategy;
use Mage\Deploy\Strategy\RsyncStrategy;
use Mage\Deploy\Strategy\StrategyInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
@ -190,13 +190,13 @@ class Runtime
}
/**
* Retrieves the Configuration options for a specific section in the configuration
* Retrieves the Configuration Option for a specific section in the configuration
*
* @param string $key Section name
* @param mixed $default Default value
* @return mixed
*/
public function getConfigOptions($key, $default = null)
public function getConfigOption($key, $default = null)
{
if (array_key_exists($key, $this->configuration)) {
return $this->configuration[$key];
@ -206,31 +206,13 @@ class Runtime
}
/**
* Returns the configuration for the current Environment
*
* @return array
*/
public function getEnvironmentConfig()
{
if (!array_key_exists('environments', $this->configuration) || !is_array($this->configuration['environments'])) {
return [];
}
if (!array_key_exists($this->environment, $this->configuration['environments'])) {
return [];
}
return $this->configuration['environments'][$this->environment];
}
/**
* Returns the configuration parameter for the current Environment
* Returns the Configuration Option for a specific section the current Environment
*
* @param string $key Section/Parameter name
* @param mixed $default Default value
* @return mixed
*/
public function getEnvParam($key, $default = null)
public function getEnvOption($key, $default = null)
{
if (!array_key_exists('environments', $this->configuration) || !is_array($this->configuration['environments'])) {
return $default;
@ -248,13 +230,13 @@ class Runtime
}
/**
* Overwrites an Environment configuration option
* Overwrites an Environment Configuration Option
*
* @param string $key
* @param mixed $value
* @return Runtime
*/
public function setEnvironmentConfig($key, $value)
public function setEnvOption($key, $value)
{
if (array_key_exists('environments', $this->configuration) && is_array($this->configuration['environments'])) {
if (array_key_exists($this->environment, $this->configuration['environments'])) {
@ -321,10 +303,17 @@ class Runtime
*/
public function getTasks()
{
$config = $this->getEnvironmentConfig();
if (array_key_exists($this->stage, $config)) {
if (is_array($config[$this->stage])) {
return $config[$this->stage];
if (!array_key_exists('environments', $this->configuration) || !is_array($this->configuration['environments'])) {
return [];
}
if (!array_key_exists($this->environment, $this->configuration['environments'])) {
return [];
}
if (array_key_exists($this->stage, $this->configuration['environments'][$this->environment])) {
if (is_array($this->configuration['environments'][$this->environment][$this->stage])) {
return $this->configuration['environments'][$this->environment][$this->stage];
}
}
@ -420,8 +409,8 @@ class Runtime
*/
public function runRemoteCommand($cmd, $jail, $timeout = 120)
{
$user = $this->getEnvParam('user');
$sudo = $this->getEnvParam('sudo', false);
$user = $this->getEnvOption('user');
$sudo = $this->getEnvOption('sudo', false);
$host = $this->getWorkingHost();
$sshConfig = $this->getSSHConfig();
@ -430,7 +419,7 @@ class Runtime
$cmdDelegate = sprintf('sudo %s', $cmd);
}
$hostPath = rtrim($this->getEnvParam('host_path'), '/');
$hostPath = rtrim($this->getEnvOption('host_path'), '/');
if ($jail && $this->getReleaseId()) {
$cmdDelegate = sprintf('cd %s/releases/%s && %s', $hostPath, $this->getReleaseId(), $cmdDelegate);
} elseif ($jail) {
@ -450,7 +439,7 @@ class Runtime
*/
public function getSSHConfig()
{
$sshConfig = $this->getEnvParam('ssh', ['port' => '22', 'flags' => '-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no']);
$sshConfig = $this->getEnvOption('ssh', ['port' => '22', 'flags' => '-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no']);
if (!array_key_exists('port', $sshConfig)) {
$sshConfig['port'] = '22';
@ -491,7 +480,7 @@ class Runtime
*/
public function getBranch()
{
return $this->getEnvParam('branch', false);
return $this->getEnvOption('branch', false);
}
/**
@ -503,7 +492,7 @@ class Runtime
{
$strategy = new RsyncStrategy();
if ($this->getEnvParam('releases', false)) {
if ($this->getEnvOption('releases', false)) {
$strategy = new ReleasesStrategy();
}

View file

@ -43,7 +43,7 @@ class DumpAutoloadTask extends AbstractTask
protected function getOptions()
{
$userOptions = $this->runtime->getConfigOptions('composer', []);
$userOptions = $this->runtime->getConfigOption('composer', []);
$options = array_merge(
['path' => 'composer', 'flags' => '--optimize'],
(is_array($userOptions) ? $userOptions : []),

View file

@ -43,7 +43,7 @@ class InstallTask extends AbstractTask
protected function getOptions()
{
$userOptions = $this->runtime->getConfigOptions('composer', []);
$userOptions = $this->runtime->getConfigOption('composer', []);
$options = array_merge(
['path' => 'composer', 'flags' => '--optimize-autoloader'],
(is_array($userOptions) ? $userOptions : []),

View file

@ -32,9 +32,9 @@ class CleanupTask extends AbstractTask
public function execute()
{
$hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/');
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
$currentReleaseId = $this->runtime->getReleaseId();
$maxReleases = $this->runtime->getEnvParam('releases');
$maxReleases = $this->runtime->getEnvOption('releases');
$cmdListReleases = sprintf('ls -1 %s/releases', $hostPath);

View file

@ -32,7 +32,7 @@ class PrepareTask extends AbstractTask
public function execute()
{
$hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/');
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
$cmdMakeDir = sprintf('mkdir -p %s/releases/%s', $hostPath, $this->runtime->getReleaseId());

View file

@ -34,11 +34,11 @@ class ReleaseTask extends AbstractTask implements ExecuteOnRollbackInterface
public function execute()
{
if (!$this->runtime->getEnvParam('releases', false)) {
if (!$this->runtime->getEnvOption('releases', false)) {
throw new ErrorException('This task is only available with releases enabled', 40);
}
$hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/');
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
$releaseId = $this->runtime->getReleaseId();
$cmdLinkRelease = sprintf('cd %s && ln -snf releases/%s current', $hostPath, $releaseId);

View file

@ -33,14 +33,14 @@ class RsyncTask extends AbstractTask
public function execute()
{
$flags = $this->runtime->getConfigOptions('rsync', '-avz');
$flags = $this->runtime->getConfigOption('rsync', '-avz');
$sshConfig = $this->runtime->getSSHConfig();
$user = $this->runtime->getEnvParam('user', $this->runtime->getCurrentUser());
$user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser());
$host = $this->runtime->getWorkingHost();
$hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/');
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
$targetDir = rtrim($hostPath, '/');
if ($this->runtime->getEnvParam('releases', false)) {
if ($this->runtime->getEnvOption('releases', false)) {
throw new ErrorException('Can\'t be used with Releases, use "deploy/targz/copy"');
}
@ -54,7 +54,7 @@ class RsyncTask extends AbstractTask
protected function getExcludes()
{
$excludes = $this->runtime->getEnvParam('exclude', []);
$excludes = $this->runtime->getEnvOption('exclude', []);
$excludes = array_merge(['.git'], $excludes);
foreach ($excludes as &$exclude) {

View file

@ -33,7 +33,7 @@ class CleanupTask extends AbstractTask
public function execute()
{
if (!$this->runtime->getEnvParam('releases', false)) {
if (!$this->runtime->getEnvOption('releases', false)) {
throw new ErrorException('This task is only available with releases enabled', 40);
}

View file

@ -33,14 +33,14 @@ class CopyTask extends AbstractTask
public function execute()
{
if (!$this->runtime->getEnvParam('releases', false)) {
if (!$this->runtime->getEnvOption('releases', false)) {
throw new ErrorException('This task is only available with releases enabled', 40);
}
$user = $this->runtime->getEnvParam('user', $this->runtime->getCurrentUser());
$user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser());
$host = $this->runtime->getWorkingHost();
$sshConfig = $sshConfig = $this->runtime->getSSHConfig();
$hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/');
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
$currentReleaseId = $this->runtime->getReleaseId();
$targetDir = sprintf('%s/releases/%s', $hostPath, $currentReleaseId);

View file

@ -33,7 +33,7 @@ class PrepareTask extends AbstractTask
public function execute()
{
if (!$this->runtime->getEnvParam('releases', false)) {
if (!$this->runtime->getEnvOption('releases', false)) {
throw new ErrorException('This task is only available with releases enabled', 40);
}
@ -50,7 +50,7 @@ class PrepareTask extends AbstractTask
protected function getExcludes()
{
$excludes = $this->runtime->getEnvParam('exclude', []);
$excludes = $this->runtime->getEnvOption('exclude', []);
$excludes = array_merge(['.git'], $excludes);
foreach ($excludes as &$exclude) {

View file

@ -70,7 +70,7 @@ class ChangeBranchTask extends AbstractTask
protected function getOptions()
{
$branch = $this->runtime->getEnvParam('branch', 'master');
$branch = $this->runtime->getEnvOption('branch', 'master');
$options = array_merge(
['path' => 'git', 'branch' => $branch],
$this->options

View file

@ -43,7 +43,7 @@ class UpdateTask extends AbstractTask
protected function getOptions()
{
$branch = $this->runtime->getEnvParam('branch', 'master');
$branch = $this->runtime->getEnvOption('branch', 'master');
$options = array_merge(
['path' => 'git', 'branch' => $branch],
$this->options

View file

@ -43,8 +43,8 @@ class AsseticDumpTask extends AbstractTask
protected function getOptions()
{
$userGlobalOptions = $this->runtime->getConfigOptions('symfony', []);
$userEnvOptions = $this->runtime->getEnvParam('symfony', []);
$userGlobalOptions = $this->runtime->getConfigOption('symfony', []);
$userEnvOptions = $this->runtime->getEnvOption('symfony', []);
$options = array_merge(
['console' => 'bin/console', 'env' => 'dev', 'flags' => ''],
(is_array($userGlobalOptions) ? $userGlobalOptions : []),

View file

@ -43,8 +43,8 @@ class AssetsInstallTask extends AbstractTask
protected function getOptions()
{
$userGlobalOptions = $this->runtime->getConfigOptions('symfony', []);
$userEnvOptions = $this->runtime->getEnvParam('symfony', []);
$userGlobalOptions = $this->runtime->getConfigOption('symfony', []);
$userEnvOptions = $this->runtime->getEnvOption('symfony', []);
$options = array_merge(
['console' => 'bin/console', 'env' => 'dev', 'target' => 'web', 'flags' => '--symlink --relative'],
(is_array($userGlobalOptions) ? $userGlobalOptions : []),

View file

@ -43,8 +43,8 @@ class CacheClearTask extends AbstractTask
protected function getOptions()
{
$userGlobalOptions = $this->runtime->getConfigOptions('symfony', []);
$userEnvOptions = $this->runtime->getEnvParam('symfony', []);
$userGlobalOptions = $this->runtime->getConfigOption('symfony', []);
$userEnvOptions = $this->runtime->getEnvOption('symfony', []);
$options = array_merge(
['console' => 'bin/console', 'env' => 'dev', 'flags' => ''],
(is_array($userGlobalOptions) ? $userGlobalOptions : []),

View file

@ -43,8 +43,8 @@ class CacheWarmupTask extends AbstractTask
protected function getOptions()
{
$userGlobalOptions = $this->runtime->getConfigOptions('symfony', []);
$userEnvOptions = $this->runtime->getEnvParam('symfony', []);
$userGlobalOptions = $this->runtime->getConfigOption('symfony', []);
$userEnvOptions = $this->runtime->getEnvOption('symfony', []);
$options = array_merge(
['console' => 'bin/console', 'env' => 'dev', 'flags' => ''],
(is_array($userGlobalOptions) ? $userGlobalOptions : []),

View file

@ -10,8 +10,8 @@
namespace Mage\Tests\Deploy;
use Mage\Deploy\Strategy\Releases\ReleasesStrategy;
use Mage\Deploy\Strategy\Rsync\RsyncStrategy;
use Mage\Deploy\Strategy\ReleasesStrategy;
use Mage\Deploy\Strategy\RsyncStrategy;
use Mage\Runtime\Exception\RuntimeException;
use Mage\Runtime\Runtime;
use Exception;

View file

@ -40,26 +40,6 @@ class RuntimeTest extends TestCase
$this->assertLessThanOrEqual(2, $dateDiff->s);
}
public function testEmptyEnvironmentConfig()
{
$runtime = new Runtime();
$config = $runtime->getEnvironmentConfig();
$this->assertTrue(is_array($config));
$this->assertEquals(0, count($config));
}
public function testInvalidEnvironmentConfig()
{
$runtime = new RuntimeMockup();
$runtime->setConfiguration(['environments' => ['valid' => []]]);
$runtime->setInvalidEnvironment('invalid');
$config = $runtime->getEnvironmentConfig();
$this->assertTrue(is_array($config));
$this->assertEquals(0, count($config));
}
public function testInvalidEnvironments()
{
try {