[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
@@ -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];
+7 -7
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('');
+5 -5
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);
@@ -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) {
@@ -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
@@ -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
+25 -36
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();
}
@@ -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 : []),
+1 -1
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 : []),
@@ -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);
@@ -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());
+2 -2
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);
+5 -5
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) {
@@ -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);
}
+3 -3
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);
@@ -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) {
+1 -1
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
+1 -1
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
+2 -2
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 : []),
@@ -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 : []),
+2 -2
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 : []),
+2 -2
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 : []),