[Nostromo] Refactor Runtime, added getEnvParam method

This commit is contained in:
Andrés Montañez 2017-01-11 17:59:41 -03:00
parent f407e9d92d
commit c9ec69e385
17 changed files with 62 additions and 61 deletions

View file

@ -70,7 +70,7 @@ 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->getEnvironmentConfig('releases', false)) {
if ($this->runtime->getEnvParam('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()));
@ -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: <fg=green>%s</>', $this->runtime->getEnvironmentConfig('branch')));
if ($this->runtime->getEnvParam('branch', false)) {
$output->writeln(sprintf(' Branch: <fg=green>%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('');

View file

@ -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);

View file

@ -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) {

View file

@ -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();
}

View file

@ -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);

View file

@ -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());

View file

@ -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);

View file

@ -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) {

View file

@ -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);
}

View file

@ -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);

View file

@ -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) {

View file

@ -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

View file

@ -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

View file

@ -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 : []),

View file

@ -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 : []),

View file

@ -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 : []),

View file

@ -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 : []),