From c9ec69e385807533eeb68ae37259087511d71e77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Wed, 11 Jan 2017 17:59:41 -0300 Subject: [PATCH] [Nostromo] Refactor Runtime, added getEnvParam method --- src/Command/BuiltIn/DeployCommand.php | 8 +-- src/Command/BuiltIn/Releases/ListCommand.php | 6 +-- .../BuiltIn/Releases/RollbackCommand.php | 6 +-- src/Runtime/Runtime.php | 51 +++++++++++-------- .../BuiltIn/Deploy/Release/CleanupTask.php | 4 +- .../BuiltIn/Deploy/Release/PrepareTask.php | 2 +- src/Task/BuiltIn/Deploy/ReleaseTask.php | 4 +- src/Task/BuiltIn/Deploy/RsyncTask.php | 8 +-- src/Task/BuiltIn/Deploy/TarGz/CleanupTask.php | 2 +- src/Task/BuiltIn/Deploy/TarGz/CopyTask.php | 6 +-- src/Task/BuiltIn/Deploy/TarGz/PrepareTask.php | 4 +- src/Task/BuiltIn/Git/ChangeBranchTask.php | 7 +-- src/Task/BuiltIn/Git/UpdateTask.php | 7 +-- src/Task/BuiltIn/Symfony/AsseticDumpTask.php | 2 +- .../BuiltIn/Symfony/AssetsInstallTask.php | 2 +- src/Task/BuiltIn/Symfony/CacheClearTask.php | 2 +- src/Task/BuiltIn/Symfony/CacheWarmupTask.php | 2 +- 17 files changed, 62 insertions(+), 61 deletions(-) diff --git a/src/Command/BuiltIn/DeployCommand.php b/src/Command/BuiltIn/DeployCommand.php index 3205e9f..5e3c985 100644 --- a/src/Command/BuiltIn/DeployCommand.php +++ b/src/Command/BuiltIn/DeployCommand.php @@ -70,7 +70,7 @@ class DeployCommand extends AbstractCommand $output->writeln(sprintf(' Environment: %s', $this->runtime->getEnvironment())); $this->log(sprintf('Environment: %s', $this->runtime->getEnvironment())); - if ($this->runtime->getEnvironmentConfig('releases', false)) { + if ($this->runtime->getEnvParam('releases', false)) { $this->runtime->generateReleaseId(); $output->writeln(sprintf(' Release ID: %s', $this->runtime->getReleaseId())); $this->log(sprintf('Release ID: %s', $this->runtime->getReleaseId())); @@ -86,8 +86,8 @@ class DeployCommand extends AbstractCommand $this->runtime->setEnvironmentConfig('branch', $input->getOption('branch')); } - if ($this->runtime->getEnvironmentConfig('branch', false)) { - $output->writeln(sprintf(' Branch: %s', $this->runtime->getEnvironmentConfig('branch'))); + if ($this->runtime->getEnvParam('branch', false)) { + $output->writeln(sprintf(' Branch: %s', $this->runtime->getEnvParam('branch'))); } $output->writeln(''); @@ -140,7 +140,7 @@ class DeployCommand extends AbstractCommand protected function runOnHosts(OutputInterface $output, $tasks) { - $hosts = $this->runtime->getEnvironmentConfig('hosts'); + $hosts = $this->runtime->getEnvParam('hosts'); if (count($hosts) == 0) { $output->writeln(sprintf(' No hosts defined, skipping %s tasks', $this->getStageName())); $output->writeln(''); diff --git a/src/Command/BuiltIn/Releases/ListCommand.php b/src/Command/BuiltIn/Releases/ListCommand.php index 2133249..86b15d0 100644 --- a/src/Command/BuiltIn/Releases/ListCommand.php +++ b/src/Command/BuiltIn/Releases/ListCommand.php @@ -58,7 +58,7 @@ class ListCommand extends AbstractCommand try { $this->runtime->setEnvironment($input->getArgument('environment')); - if (!$this->runtime->getEnvironmentConfig('releases', false)) { + if (!$this->runtime->getEnvParam('releases', false)) { throw new RuntimeException('Releases are not enabled', 70); } @@ -71,12 +71,12 @@ class ListCommand extends AbstractCommand $output->writeln(''); - $hosts = $this->runtime->getEnvironmentConfig('hosts'); + $hosts = $this->runtime->getEnvParam('hosts'); if (count($hosts) == 0) { $output->writeln('No hosts defined'); $output->writeln(''); } else { - $hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/'); + $hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/'); foreach ($hosts as $host) { $this->runtime->setWorkingHost($host); diff --git a/src/Command/BuiltIn/Releases/RollbackCommand.php b/src/Command/BuiltIn/Releases/RollbackCommand.php index 5214199..961a4f7 100644 --- a/src/Command/BuiltIn/Releases/RollbackCommand.php +++ b/src/Command/BuiltIn/Releases/RollbackCommand.php @@ -61,7 +61,7 @@ class RollbackCommand extends DeployCommand $strategy = $this->runtime->guessStrategy(); $this->taskFactory = new TaskFactory($this->runtime); - if (!$this->runtime->getEnvironmentConfig('releases', false)) { + if (!$this->runtime->getEnvParam('releases', false)) { throw new RuntimeException('Releases are not enabled', 70); } @@ -104,8 +104,8 @@ class RollbackCommand extends DeployCommand */ protected function checkReleaseAvailability($releaseToRollback) { - $hosts = $this->runtime->getEnvironmentConfig('hosts'); - $hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/'); + $hosts = $this->runtime->getEnvParam('hosts'); + $hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/'); $availableInHosts = 0; foreach ($hosts as $host) { diff --git a/src/Runtime/Runtime.php b/src/Runtime/Runtime.php index 6e1b773..e8dffe9 100644 --- a/src/Runtime/Runtime.php +++ b/src/Runtime/Runtime.php @@ -207,14 +207,10 @@ class Runtime /** * Returns the configuration for the current Environment - * If $key is provided, it will be returned only that section, if not found the default value will be returned, - * if $key is not provided, the whole Environment's configuration will be returned * - * @param string $key Section name - * @param mixed $default Default value - * @return mixed + * @return array */ - public function getEnvironmentConfig($key = null, $default = null) + public function getEnvironmentConfig() { if (!array_key_exists('environments', $this->configuration) || !is_array($this->configuration['environments'])) { return []; @@ -224,16 +220,31 @@ class Runtime return []; } - $config = $this->configuration['environments'][$this->environment]; - if ($key !== null) { - if (array_key_exists($key, $config)) { - return $config[$key]; - } else { - return $default; - } + return $this->configuration['environments'][$this->environment]; + } + + /** + * Returns the configuration parameter for the current Environment + * + * @param string $key Section/Parameter name + * @param mixed $default Default value + * @return mixed + */ + public function getEnvParam($key, $default = null) + { + if (!array_key_exists('environments', $this->configuration) || !is_array($this->configuration['environments'])) { + return $default; } - return $config; + if (!array_key_exists($this->environment, $this->configuration['environments'])) { + return $default; + } + + if (array_key_exists($key, $this->configuration['environments'][$this->environment])) { + return $this->configuration['environments'][$this->environment][$key]; + } + + return $default; } /** @@ -409,8 +420,8 @@ class Runtime */ public function runRemoteCommand($cmd, $jail = true, $timeout = 120) { - $user = $this->getEnvironmentConfig('user'); - $sudo = $this->getEnvironmentConfig('sudo', false); + $user = $this->getEnvParam('user'); + $sudo = $this->getEnvParam('sudo', false); $host = $this->getWorkingHost(); $sshConfig = $this->getSSHConfig(); @@ -420,7 +431,7 @@ class Runtime } if ($jail) { - $hostPath = rtrim($this->getEnvironmentConfig('host_path'), '/'); + $hostPath = rtrim($this->getEnvParam('host_path'), '/'); if ($this->getReleaseId()) { $cmdDelegate = sprintf('cd %s/releases/%s && %s', $hostPath, $this->getReleaseId(), $cmdDelegate); } else { @@ -441,7 +452,7 @@ class Runtime */ public function getSSHConfig() { - $sshConfig = $this->getEnvironmentConfig('ssh', ['port' => '22', 'flags' => '-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no']); + $sshConfig = $this->getEnvParam('ssh', ['port' => '22', 'flags' => '-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no']); if (!array_key_exists('port', $sshConfig)) { $sshConfig['port'] = '22'; @@ -482,7 +493,7 @@ class Runtime */ public function getBranch() { - return $this->getEnvironmentConfig('branch', false); + return $this->getEnvParam('branch', false); } /** @@ -494,7 +505,7 @@ class Runtime { $strategy = new RsyncStrategy(); - if ($this->getEnvironmentConfig('releases', false)) { + if ($this->getEnvParam('releases', false)) { $strategy = new ReleasesStrategy(); } diff --git a/src/Task/BuiltIn/Deploy/Release/CleanupTask.php b/src/Task/BuiltIn/Deploy/Release/CleanupTask.php index 395b16b..f5f288c 100644 --- a/src/Task/BuiltIn/Deploy/Release/CleanupTask.php +++ b/src/Task/BuiltIn/Deploy/Release/CleanupTask.php @@ -32,9 +32,9 @@ class CleanupTask extends AbstractTask public function execute() { - $hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/'); + $hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/'); $currentReleaseId = $this->runtime->getReleaseId(); - $maxReleases = $this->runtime->getEnvironmentConfig('releases'); + $maxReleases = $this->runtime->getEnvParam('releases'); $cmdListReleases = sprintf('ls -1 %s/releases', $hostPath); diff --git a/src/Task/BuiltIn/Deploy/Release/PrepareTask.php b/src/Task/BuiltIn/Deploy/Release/PrepareTask.php index 2c82833..a4f6651 100644 --- a/src/Task/BuiltIn/Deploy/Release/PrepareTask.php +++ b/src/Task/BuiltIn/Deploy/Release/PrepareTask.php @@ -32,7 +32,7 @@ class PrepareTask extends AbstractTask public function execute() { - $hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/'); + $hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/'); $cmdMakeDir = sprintf('mkdir -p %s/releases/%s', $hostPath, $this->runtime->getReleaseId()); diff --git a/src/Task/BuiltIn/Deploy/ReleaseTask.php b/src/Task/BuiltIn/Deploy/ReleaseTask.php index 8eecf3d..f3dbf82 100644 --- a/src/Task/BuiltIn/Deploy/ReleaseTask.php +++ b/src/Task/BuiltIn/Deploy/ReleaseTask.php @@ -34,11 +34,11 @@ class ReleaseTask extends AbstractTask implements ExecuteOnRollbackInterface public function execute() { - if (!$this->runtime->getEnvironmentConfig('releases', false)) { + if (!$this->runtime->getEnvParam('releases', false)) { throw new ErrorException('This task is only available with releases enabled', 40); } - $hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/'); + $hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/'); $releaseId = $this->runtime->getReleaseId(); $cmdLinkRelease = sprintf('cd %s && ln -snf releases/%s current', $hostPath, $releaseId); diff --git a/src/Task/BuiltIn/Deploy/RsyncTask.php b/src/Task/BuiltIn/Deploy/RsyncTask.php index 539981f..5efc9f8 100644 --- a/src/Task/BuiltIn/Deploy/RsyncTask.php +++ b/src/Task/BuiltIn/Deploy/RsyncTask.php @@ -35,12 +35,12 @@ class RsyncTask extends AbstractTask { $flags = $this->runtime->getConfigOptions('rsync', '-avz'); $sshConfig = $this->runtime->getSSHConfig(); - $user = $this->runtime->getEnvironmentConfig('user', $this->runtime->getCurrentUser()); + $user = $this->runtime->getEnvParam('user', $this->runtime->getCurrentUser()); $host = $this->runtime->getWorkingHost(); - $hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/'); + $hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/'); $targetDir = rtrim($hostPath, '/'); - if ($this->runtime->getEnvironmentConfig('releases', false)) { + if ($this->runtime->getEnvParam('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->getEnvironmentConfig('exclude', []); + $excludes = $this->runtime->getEnvParam('exclude', []); $excludes = array_merge(['.git'], $excludes); foreach ($excludes as &$exclude) { diff --git a/src/Task/BuiltIn/Deploy/TarGz/CleanupTask.php b/src/Task/BuiltIn/Deploy/TarGz/CleanupTask.php index 10bd387..da8618d 100644 --- a/src/Task/BuiltIn/Deploy/TarGz/CleanupTask.php +++ b/src/Task/BuiltIn/Deploy/TarGz/CleanupTask.php @@ -33,7 +33,7 @@ class CleanupTask extends AbstractTask public function execute() { - if (!$this->runtime->getEnvironmentConfig('releases', false)) { + if (!$this->runtime->getEnvParam('releases', false)) { throw new ErrorException('This task is only available with releases enabled', 40); } diff --git a/src/Task/BuiltIn/Deploy/TarGz/CopyTask.php b/src/Task/BuiltIn/Deploy/TarGz/CopyTask.php index a25d348..1e6e09c 100644 --- a/src/Task/BuiltIn/Deploy/TarGz/CopyTask.php +++ b/src/Task/BuiltIn/Deploy/TarGz/CopyTask.php @@ -33,14 +33,14 @@ class CopyTask extends AbstractTask public function execute() { - if (!$this->runtime->getEnvironmentConfig('releases', false)) { + if (!$this->runtime->getEnvParam('releases', false)) { throw new ErrorException('This task is only available with releases enabled', 40); } - $user = $this->runtime->getEnvironmentConfig('user', $this->runtime->getCurrentUser()); + $user = $this->runtime->getEnvParam('user', $this->runtime->getCurrentUser()); $host = $this->runtime->getWorkingHost(); $sshConfig = $sshConfig = $this->runtime->getSSHConfig(); - $hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/'); + $hostPath = rtrim($this->runtime->getEnvParam('host_path'), '/'); $currentReleaseId = $this->runtime->getReleaseId(); $targetDir = sprintf('%s/releases/%s', $hostPath, $currentReleaseId); diff --git a/src/Task/BuiltIn/Deploy/TarGz/PrepareTask.php b/src/Task/BuiltIn/Deploy/TarGz/PrepareTask.php index dad36d3..69e0de3 100644 --- a/src/Task/BuiltIn/Deploy/TarGz/PrepareTask.php +++ b/src/Task/BuiltIn/Deploy/TarGz/PrepareTask.php @@ -33,7 +33,7 @@ class PrepareTask extends AbstractTask public function execute() { - if (!$this->runtime->getEnvironmentConfig('releases', false)) { + if (!$this->runtime->getEnvParam('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->getEnvironmentConfig('exclude', []); + $excludes = $this->runtime->getEnvParam('exclude', []); $excludes = array_merge(['.git'], $excludes); foreach ($excludes as &$exclude) { diff --git a/src/Task/BuiltIn/Git/ChangeBranchTask.php b/src/Task/BuiltIn/Git/ChangeBranchTask.php index 736a213..42d283b 100644 --- a/src/Task/BuiltIn/Git/ChangeBranchTask.php +++ b/src/Task/BuiltIn/Git/ChangeBranchTask.php @@ -70,12 +70,7 @@ class ChangeBranchTask extends AbstractTask protected function getOptions() { - $config = $this->runtime->getEnvironmentConfig(); - $branch = 'master'; - if (array_key_exists('branch', $config)) { - $branch = $config['branch']; - } - + $branch = $this->runtime->getEnvParam('branch', 'master'); $options = array_merge( ['path' => 'git', 'branch' => $branch], $this->options diff --git a/src/Task/BuiltIn/Git/UpdateTask.php b/src/Task/BuiltIn/Git/UpdateTask.php index 80424d4..4eaed2c 100644 --- a/src/Task/BuiltIn/Git/UpdateTask.php +++ b/src/Task/BuiltIn/Git/UpdateTask.php @@ -43,12 +43,7 @@ class UpdateTask extends AbstractTask protected function getOptions() { - $config = $this->runtime->getEnvironmentConfig(); - $branch = 'master'; - if (array_key_exists('branch', $config)) { - $branch = $config['branch']; - } - + $branch = $this->runtime->getEnvParam('branch', 'master'); $options = array_merge( ['path' => 'git', 'branch' => $branch], $this->options diff --git a/src/Task/BuiltIn/Symfony/AsseticDumpTask.php b/src/Task/BuiltIn/Symfony/AsseticDumpTask.php index 933a19e..1fe6e21 100644 --- a/src/Task/BuiltIn/Symfony/AsseticDumpTask.php +++ b/src/Task/BuiltIn/Symfony/AsseticDumpTask.php @@ -44,7 +44,7 @@ class AsseticDumpTask extends AbstractTask protected function getOptions() { $userGlobalOptions = $this->runtime->getConfigOptions('symfony', []); - $userEnvOptions = $this->runtime->getEnvironmentConfig('symfony', []); + $userEnvOptions = $this->runtime->getEnvParam('symfony', []); $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'flags' => ''], (is_array($userGlobalOptions) ? $userGlobalOptions : []), diff --git a/src/Task/BuiltIn/Symfony/AssetsInstallTask.php b/src/Task/BuiltIn/Symfony/AssetsInstallTask.php index 7cd272b..3eb162a 100644 --- a/src/Task/BuiltIn/Symfony/AssetsInstallTask.php +++ b/src/Task/BuiltIn/Symfony/AssetsInstallTask.php @@ -44,7 +44,7 @@ class AssetsInstallTask extends AbstractTask protected function getOptions() { $userGlobalOptions = $this->runtime->getConfigOptions('symfony', []); - $userEnvOptions = $this->runtime->getEnvironmentConfig('symfony', []); + $userEnvOptions = $this->runtime->getEnvParam('symfony', []); $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'target' => 'web', 'flags' => '--symlink --relative'], (is_array($userGlobalOptions) ? $userGlobalOptions : []), diff --git a/src/Task/BuiltIn/Symfony/CacheClearTask.php b/src/Task/BuiltIn/Symfony/CacheClearTask.php index b81c20e..511ed32 100644 --- a/src/Task/BuiltIn/Symfony/CacheClearTask.php +++ b/src/Task/BuiltIn/Symfony/CacheClearTask.php @@ -44,7 +44,7 @@ class CacheClearTask extends AbstractTask protected function getOptions() { $userGlobalOptions = $this->runtime->getConfigOptions('symfony', []); - $userEnvOptions = $this->runtime->getEnvironmentConfig('symfony', []); + $userEnvOptions = $this->runtime->getEnvParam('symfony', []); $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'flags' => ''], (is_array($userGlobalOptions) ? $userGlobalOptions : []), diff --git a/src/Task/BuiltIn/Symfony/CacheWarmupTask.php b/src/Task/BuiltIn/Symfony/CacheWarmupTask.php index 65f34ec..cf6f707 100644 --- a/src/Task/BuiltIn/Symfony/CacheWarmupTask.php +++ b/src/Task/BuiltIn/Symfony/CacheWarmupTask.php @@ -44,7 +44,7 @@ class CacheWarmupTask extends AbstractTask protected function getOptions() { $userGlobalOptions = $this->runtime->getConfigOptions('symfony', []); - $userEnvOptions = $this->runtime->getEnvironmentConfig('symfony', []); + $userEnvOptions = $this->runtime->getEnvParam('symfony', []); $options = array_merge( ['console' => 'bin/console', 'env' => 'dev', 'flags' => ''], (is_array($userGlobalOptions) ? $userGlobalOptions : []),