From eaccc0dab886ac32185ac8aad542b08851f919fe Mon Sep 17 00:00:00 2001 From: Vladimir Grigor Date: Sat, 26 Jul 2014 15:33:07 +0300 Subject: [PATCH 1/6] refactoring --- Mage/Config.php | 188 ++++++++++++++---- Mage/Config/ConfigNotFoundException.php | 13 ++ .../OptionalConfigNotFoundException.php | 13 ++ .../RequiredConfigNotFoundException.php | 13 ++ Mage/Console.php | 19 +- Mage/Task/AbstractTask.php | 10 +- 6 files changed, 208 insertions(+), 48 deletions(-) create mode 100644 Mage/Config/ConfigNotFoundException.php create mode 100644 Mage/Config/OptionalConfigNotFoundException.php create mode 100644 Mage/Config/RequiredConfigNotFoundException.php diff --git a/Mage/Config.php b/Mage/Config.php index 1d8bbc0..68ce703 100644 --- a/Mage/Config.php +++ b/Mage/Config.php @@ -10,6 +10,10 @@ namespace Mage; +use Mage\Config\ConfigNotFoundException; +use Mage\Config\RequiredConfigNotFoundException; +use Mage\Console; +use Mage\Yaml\Exception\RuntimeException; use Mage\Yaml\Yaml; use Exception; @@ -51,23 +55,20 @@ class Config private $hostConfig = array(); /** - * The Relase ID + * The Release ID * @var integer */ private $releaseId = null; /** * Magallanes Global and Environment configuration - * @var array */ - private $config = array( - 'general' => array(), - 'environment' => array(), - ); + private $generalConfig = array(); + private $environmentConfig = array(); /** * Parse the Command Line options - * @return boolean + * @param $arguments */ protected function parse($arguments) { @@ -95,45 +96,115 @@ class Config } /** - * Loads the General Configuration + * Initializes the General Configuration */ - protected function loadGeneral() + protected function initGeneral() { - if (file_exists(getcwd() . '/.mage/config/general.yml')) { - $this->config['general'] = Yaml::parse(file_get_contents(getcwd() . '/.mage/config/general.yml')); - } + try { + $this->generalConfig = $this->loadGeneral(getcwd() . '/.mage/config/general.yml'); + } catch (ConfigNotFoundException $e) { + // normal situation + } + } + + /** + * Load general config from the given file + * + * @param $filePath + * + * @return array + * @throws Config\ConfigNotFoundException + */ + protected function loadGeneral($filePath){ + return $this->parseConfigFile($filePath); } + + /** + * Obviously this method is a HACK. It was refactored from ::loadEnvironment() + * TODO Please put it to SCM functionality. + * + * @param array $settings + * + * @return array + */ + protected function updateSCMTempDir(array $settings) + { + // Create temporal directory for clone + if (isset($settings['deployment']['source']) && is_array($settings['deployment']['source'])) { + if (trim($settings['deployment']['source']['temporal']) == '') { + $settings['deployment']['source']['temporal'] = sys_get_temp_dir(); + } + $settings['deployment']['source']['temporal'] + = rtrim($settings['deployment']['source']['temporal'], '/') . '/' . md5(microtime()) . '/'; + } + + return $settings; + } /** * Loads the Environment configuration + * @param $filePath string + * + * @throws Exception + * @return boolean + */ + protected function loadEnvironment($filePath) + { + + $settings = $this->parseConfigFile($filePath); + + //this is a HACK in the old code - no time to remove it now, so I factored it out in own method + $settings = $this->updateSCMTempDir($settings); + + return $settings; + + } + + /** + * Initializes the Environment configuration * * @throws Exception * @return boolean */ - protected function loadEnvironment() + protected function initEnvironment() { $environment = $this->getEnvironment(); - if (($environment != false) && file_exists(getcwd() . '/.mage/config/environment/' . $environment . '.yml')) { - $this->config['environment'] = Yaml::parse(file_get_contents(getcwd() . '/.mage/config/environment/' . $environment . '.yml')); - // Create temporal directory for clone - if (isset($this->config['environment']['deployment']['source']) && is_array($this->config['environment']['deployment']['source'])) { - if (trim($this->config['environment']['deployment']['source']['temporal']) == '') { - $this->config['environment']['deployment']['source']['temporal'] = '/tmp'; - } - $newTemporal = rtrim($this->config['environment']['deployment']['source']['temporal'], '/') - . '/' . md5(microtime()) . '/'; - $this->config['environment']['deployment']['source']['temporal'] = $newTemporal; - } - return true; + $configFilePath = getcwd() . '/.mage/config/environment/' . $environment . '.yml'; - } else if (($environment != '') && !file_exists(getcwd() . '/.mage/config/environment/' . $environment . '.yml')) { - throw new Exception('Environment does not exists.'); - } + $parameters = $this->getParameters(); + + if (empty($environment) && (!empty($parameters) || !$this->isRunInSpecialMode($parameters))) { + throw new RuntimeException('Environment is not set'); + } + + try { + $this->environmentConfig = $this->loadEnvironment($configFilePath); + } catch (ConfigNotFoundException $e) { + throw new RequiredConfigNotFoundException("Not found required config $configFilePath for environment $environment", 0 , $e); + } - return false; } + /** + * + * @param array $parameters + * @return boolean + */ + protected function isRunInSpecialMode(array $parameters) + { + if(empty($parameters)) + return true; + foreach($parameters as $parameter) + { + if(isset(Console::$paramsNotRequiringEnvironment[$parameter])) + { + return true; + } + } + + return false; + } /** * Load the Configuration and parses the Arguments * @@ -142,8 +213,8 @@ class Config public function load($arguments) { $this->parse($arguments); - $this->loadGeneral(); - $this->loadEnvironment(); + $this->initGeneral(); + $this->initEnvironment(); } /** @@ -151,8 +222,8 @@ class Config */ public function reload() { - $this->loadGeneral(); - $this->loadEnvironment(); + $this->initGeneral(); + $this->initEnvironment(); } /** @@ -280,11 +351,12 @@ class Config { $hosts = array(); - if (isset($this->config['environment']['hosts'])) { - if (is_array($this->config['environment']['hosts'])) { - $hosts = (array) $this->config['environment']['hosts']; - } else if (is_string($this->config['environment']['hosts']) && file_exists($this->config['environment']['hosts']) && is_readable($this->config['environment']['hosts'])) { - $fileContent = fopen($this->config['environment']['hosts'], 'r'); + $envConfig = $this->getEnvironmentConfig(); + if (isset($envConfig['hosts'])) { + if (is_array($envConfig['hosts'])) { + $hosts = (array) $envConfig['hosts']; + } else if (is_string($envConfig['hosts']) && file_exists($envConfig['hosts']) && is_readable($envConfig['hosts'])) { + $fileContent = fopen($envConfig['hosts'], 'r'); while (($host = fgets($fileContent)) == true) { $host = trim($host); if ($host != '') { @@ -373,7 +445,7 @@ class Config */ public function general($option, $default = false) { - $config = $this->config['general']; + $config = $this->getGeneralConfig(); if (isset($config[$option])) { if (is_array($default) && ($config[$option] == '')) { return $default; @@ -462,7 +534,8 @@ class Config */ public function setFrom($from) { - $this->config['environment']['deployment']['from'] = $from; + $envConfig = $this->getEnvironmentConfig(); + $envConfig['deployment']['from'] = $from; return $this; } @@ -497,7 +570,7 @@ class Config */ protected function getEnvironmentOption($option, $default = array()) { - $config = $this->config['environment']; + $config = $this->getEnvironmentConfig(); if (isset($config[$option])) { return $config[$option]; } else { @@ -505,4 +578,37 @@ class Config } } + /** + * Utility methods. TODO To be extracted into own Class + */ + public function parseConfigFile($filePath) + { + if(!file_exists($filePath)) + { + throw new ConfigNotFoundException("Cannot find the file at path $filePath"); + } + + return $this->parseConfigText(file_get_contents($filePath)); + } + public function parseConfigText($input) + { + return Yaml::parse($input); + } + + /** + * @return array + */ + protected function getGeneralConfig() + { + return $this->generalConfig; + } + + /** + * @return array + */ + protected function getEnvironmentConfig() + { + return $this->environmentConfig; + } + } diff --git a/Mage/Config/ConfigNotFoundException.php b/Mage/Config/ConfigNotFoundException.php new file mode 100644 index 0000000..a912ca2 --- /dev/null +++ b/Mage/Config/ConfigNotFoundException.php @@ -0,0 +1,13 @@ + + */ +class ConfigNotFoundException extends RuntimeException +{ +} diff --git a/Mage/Config/OptionalConfigNotFoundException.php b/Mage/Config/OptionalConfigNotFoundException.php new file mode 100644 index 0000000..b443a81 --- /dev/null +++ b/Mage/Config/OptionalConfigNotFoundException.php @@ -0,0 +1,13 @@ + + */ +class OptionalConfigNotFoundException extends RuntimeException +{ +} diff --git a/Mage/Config/RequiredConfigNotFoundException.php b/Mage/Config/RequiredConfigNotFoundException.php new file mode 100644 index 0000000..21dcd1c --- /dev/null +++ b/Mage/Config/RequiredConfigNotFoundException.php @@ -0,0 +1,13 @@ + + */ +class RequiredConfigNotFoundException extends RuntimeException +{ +} diff --git a/Mage/Console.php b/Mage/Console.php index 98517ac..3aed87b 100644 --- a/Mage/Console.php +++ b/Mage/Console.php @@ -24,6 +24,12 @@ use RecursiveDirectoryIterator; */ class Console { + /** + * TODO refactor into own static class + * @var array + */ + public static $paramsNotRequiringEnvironment = array('install'=>'install', 'upgrade'=>'upgrade', 'version'=>'version'); + /** * Handler to the current Log File. * @var mixed @@ -93,16 +99,17 @@ class Console $commandName = $config->getArgument(0); // Logging - $showGrettings = true; - if (in_array($commandName, array('install', 'upgrade', 'version'))) { + $showGreetings = true; + + if (in_array($commandName, self::$paramsNotRequiringEnvironment)) { self::$logEnabled = false; - $showGrettings = false; + $showGreetings = false; } else { self::$logEnabled = $config->general('logging', false); } - // Grettings - if ($showGrettings) { + // Greetings + if ($showGreetings) { self::output('Starting Magallanes', 0, 2); } @@ -128,7 +135,7 @@ class Console } } - if ($showGrettings) { + if ($showGreetings) { self::output('Finished Magallanes', 0, 2); if (file_exists(getcwd() . '/.mage/~working.lock')) { unlink(getcwd() . '/.mage/~working.lock'); diff --git a/Mage/Task/AbstractTask.php b/Mage/Task/AbstractTask.php index 69ba25f..e530829 100644 --- a/Mage/Task/AbstractTask.php +++ b/Mage/Task/AbstractTask.php @@ -150,7 +150,15 @@ abstract class AbstractTask */ public function getParameter($name, $default = null) { - return $this->getConfig()->getParameter($name, $default, $this->parameters); + return $this->getConfig()->getParameter($name, $default, $this->getParameters()); + } + + /** + * @return array + */ + protected function getParameters() + { + return $this->parameters; } /** From 6ecaf413368498ca3d454826052117fd20648a6e Mon Sep 17 00:00:00 2001 From: Vladimir Grigor Date: Sun, 27 Jul 2014 22:53:49 +0300 Subject: [PATCH 2/6] removed unnecessary checks in Config --- Mage/Config.php | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/Mage/Config.php b/Mage/Config.php index 68ce703..e7474d1 100644 --- a/Mage/Config.php +++ b/Mage/Config.php @@ -170,20 +170,17 @@ class Config { $environment = $this->getEnvironment(); - $configFilePath = getcwd() . '/.mage/config/environment/' . $environment . '.yml'; + if(!empty($environment)) + { + $configFilePath = getcwd() . '/.mage/config/environment/' . $environment . '.yml'; - $parameters = $this->getParameters(); + try { + $this->environmentConfig = $this->loadEnvironment($configFilePath); + } catch (ConfigNotFoundException $e) { + throw new RequiredConfigNotFoundException("Not found required config $configFilePath for environment $environment", 0 , $e); + } - if (empty($environment) && (!empty($parameters) || !$this->isRunInSpecialMode($parameters))) { - throw new RuntimeException('Environment is not set'); } - - try { - $this->environmentConfig = $this->loadEnvironment($configFilePath); - } catch (ConfigNotFoundException $e) { - throw new RequiredConfigNotFoundException("Not found required config $configFilePath for environment $environment", 0 , $e); - } - } /** From 5ce3394b3cf6109068a192d36b739971c23d2592 Mon Sep 17 00:00:00 2001 From: Vladimir Grigor Date: Mon, 28 Jul 2014 11:53:22 +0300 Subject: [PATCH 3/6] refactored Autoloader; added support for json encoded format for environment "hosts" option --- Mage/Autoload.php | 44 +++++++++++++++++++++++----------------- Mage/Command/Factory.php | 14 +++++-------- Mage/Config.php | 42 ++++++++++++++++++++++++++++++-------- Mage/Task/Factory.php | 15 ++++---------- bin/mage | 5 ++++- 5 files changed, 71 insertions(+), 49 deletions(-) diff --git a/Mage/Autoload.php b/Mage/Autoload.php index a9bcc2f..132c6c3 100644 --- a/Mage/Autoload.php +++ b/Mage/Autoload.php @@ -20,34 +20,40 @@ class Autoload /** * Autoload a Class by it's Class Name * @param string $className + * @return boolean */ - public static function autoload($className) + public function autoLoad($className) { - $baseDir = dirname(dirname(__FILE__)); - $classFile = $baseDir . '/' . str_replace(array('_', '\\'), '/', $className . '.php'); - require_once $classFile; - } + $className = ltrim($className, '/'); + $postfix = '/' . str_replace(array('_', '\\'), '/', $className . '.php'); - /** - * Checks if a Class can be loaded. - * @param string $className - * @return boolean - */ - public static function isLoadable($className) - { + //Try to load a normal Mage class (or Task). Think that Mage component is compiled to .phar $baseDir = dirname(dirname(__FILE__)); - $classFile = $baseDir . '/' . str_replace(array('_', '\\'), '/', $className . '.php'); - return (file_exists($classFile) && is_readable($classFile)); + $classFileWithinPhar = $baseDir . $postfix; + if($this->isReadable($classFileWithinPhar)) + { + require_once $classFileWithinPhar; + return true; + } + + //Try to load a custom Task or Class. Notice that the path is absolute to CWD + $classFileOutsidePhar = getcwd() . '/.mage/tasks' . $postfix; + if($this->isReadable($classFileOutsidePhar)){ + require_once $classFileOutsidePhar; + return true; + } + + return false; } /** - * Loads a User's Tasks - * @param string $taskName + * Checks if a file can be read. + * @param string $filePath + * @return boolean */ - public static function loadUserTask($taskName) + public function isReadable($filePath) { - $classFile = getcwd() . '/.mage/tasks/' . ucfirst($taskName) . '.php'; - require_once $classFile; + return is_readable($filePath); } } diff --git a/Mage/Command/Factory.php b/Mage/Command/Factory.php index da87189..19c9452 100644 --- a/Mage/Command/Factory.php +++ b/Mage/Command/Factory.php @@ -38,18 +38,14 @@ class Factory $commandName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $commandName))); $className = 'Mage\\Command\\BuiltIn\\' . $commandName . 'Command'; - if (Autoload::isLoadable($className)) { - $instance = new $className; - assert($instance instanceOf AbstractCommand); - $instance->setConfig($config); - } else { - throw new Exception('Command not found.'); - } - - if(!($instance instanceOf AbstractCommand)) { + /** @var AbstractCommand $instance */ + $instance = new $className; + if(!is_a($instance, "Mage\Command\AbstractCommand")) { throw new Exception('The command ' . $commandName . ' must be an instance of Mage\Command\AbstractCommand.'); } + $instance->setConfig($config); + return $instance; } } \ No newline at end of file diff --git a/Mage/Config.php b/Mage/Config.php index e7474d1..8b4b532 100644 --- a/Mage/Config.php +++ b/Mage/Config.php @@ -24,7 +24,8 @@ use Exception; */ class Config { - /** + const HOST_NAME_LENGTH = 1000; + /** * Arguments loaded * @var array */ @@ -353,13 +354,7 @@ class Config if (is_array($envConfig['hosts'])) { $hosts = (array) $envConfig['hosts']; } else if (is_string($envConfig['hosts']) && file_exists($envConfig['hosts']) && is_readable($envConfig['hosts'])) { - $fileContent = fopen($envConfig['hosts'], 'r'); - while (($host = fgets($fileContent)) == true) { - $host = trim($host); - if ($host != '') { - $hosts[] = $host; - } - } + $hosts = $this->getHostsFromFile($envConfig['hosts']); } } @@ -565,7 +560,7 @@ class Config * @param mixed $default * @return mixed */ - protected function getEnvironmentOption($option, $default = array()) + public function getEnvironmentOption($option, $default = array()) { $config = $this->getEnvironmentConfig(); if (isset($config[$option])) { @@ -608,4 +603,33 @@ class Config return $this->environmentConfig; } + /** + * @param string $filePath + * + * @return array + */ + protected function getHostsFromFile($filePath) + { + $handle = fopen($filePath, 'r'); + + $hosts = array(); + + try { + $fileContent = stream_get_contents($handle); + $hosts = json_decode($fileContent); + } catch (Exception $e) { + + rewind($handle); + //do it old-style: one host per line + while (($host = stream_get_line($handle, self::HOST_NAME_LENGTH)) !== false) { + $host = trim($host); + if (!empty($host)) { + $hosts[] = $host; + } + } + } + + return $hosts; + } + } diff --git a/Mage/Task/Factory.php b/Mage/Task/Factory.php index a8a7066..ecbc2b2 100644 --- a/Mage/Task/Factory.php +++ b/Mage/Task/Factory.php @@ -48,22 +48,15 @@ class Factory $taskName = str_replace(' ', '', $taskName); if (strpos($taskName, '/') === false) { - Autoload::loadUserTask($taskName); - $className = 'Task\\' . ucfirst($taskName); + $className = $taskName; } else { - $taskName = str_replace(' ', '\\', ucwords(str_replace('/', ' ', $taskName))); - $className = 'Mage\\Task\\BuiltIn\\' . $taskName . 'Task'; + $className = 'Mage\\Task\\BuiltIn\\' . str_replace(' ', '\\', ucwords(str_replace('/', ' ', $taskName))) . 'Task'; } + $instance = new $className($taskConfig, $inRollback, $stage, $taskParameters); - if (class_exists($className) || Autoload::isLoadable($className)) { - $instance = new $className($taskConfig, $inRollback, $stage, $taskParameters); - } else { - throw new ErrorWithMessageException('The Task "' . $taskName . '" doesn\'t exists.'); - } - - if (!($instance instanceOf AbstractTask)) { + if (!is_a($instance,'Mage\Task\AbstractTask')) { throw new Exception('The Task ' . $taskName . ' must be an instance of Mage\Task\AbstractTask.'); } diff --git a/bin/mage b/bin/mage index a9f0e22..9f6e436 100755 --- a/bin/mage +++ b/bin/mage @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +use Mage\Autoload; + date_default_timezone_set('UTC'); $baseDir = dirname(dirname(__FILE__)); @@ -18,7 +20,8 @@ define('MAGALLANES_DIRECTORY', $baseDir); // Preload require_once $baseDir . '/Mage/Autoload.php'; -spl_autoload_register(array('Mage\\Autoload', 'autoload')); +$loader = new Autoload(); +spl_autoload_register(array($loader, 'autoLoad')); // Clean arguments array_shift($argv); From d495d10aca055dda0cbc6d953afa50182cd0b8b9 Mon Sep 17 00:00:00 2001 From: Vladimir Grigor Date: Mon, 28 Jul 2014 13:49:38 +0300 Subject: [PATCH 4/6] fixed a bug: if deploy strategy was set to "disable", release was still trying to do remote stuff. Now release step respects strategy. --- Mage/Command/BuiltIn/DeployCommand.php | 82 +++++++++++++++++--------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/Mage/Command/BuiltIn/DeployCommand.php b/Mage/Command/BuiltIn/DeployCommand.php index ab7b427..3c48992 100644 --- a/Mage/Command/BuiltIn/DeployCommand.php +++ b/Mage/Command/BuiltIn/DeployCommand.php @@ -298,35 +298,9 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment $tasksToRun = $this->getConfig()->getTasks(); - // Guess a Deploy Strategy - switch ($this->getConfig()->deployment('strategy', 'guess')) { - case 'disabled': - $deployStrategy = 'deployment/strategy/disabled'; - break; - - case 'rsync': - $deployStrategy = 'deployment/strategy/rsync'; - break; - - case 'targz': - $deployStrategy = 'deployment/strategy/tar-gz'; - break; - - case 'git-rebase': - $deployStrategy = 'deployment/strategy/git-rebase'; - break; - - case 'guess': - default: - if ($this->getConfig()->release('enabled', false) == true) { - $deployStrategy = 'deployment/strategy/tar-gz'; - } else { - $deployStrategy = 'deployment/strategy/rsync'; - } - break; - } + $deployStrategy = $this->chooseDeployStrategy(); - array_unshift($tasksToRun, $deployStrategy); + array_unshift($tasksToRun, $deployStrategy); if (count($tasksToRun) == 0) { Console::output('Warning! No Deployment tasks defined.', 2); @@ -381,7 +355,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment $this->getConfig()->setHost($host); $this->getConfig()->setHostConfig($hostConfig); - $task = Factory::get('deployment/release', $this->getConfig(), false, AbstractTask::STAGE_DEPLOY); + $task = Factory::get($this->chooseReleaseStrategy(), $this->getConfig(), false, AbstractTask::STAGE_DEPLOY); if ($this->runTask($task, 'Releasing on host ' . $host . ' ... ')) { $completedTasks++; @@ -543,4 +517,54 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment return true; } + /** + * @return string + */ + protected function chooseDeployStrategy() + { + // Guess a Deploy Strategy + switch ($this->getConfig()->deployment('strategy', 'guess')) { + case 'disabled': + $deployStrategy = 'deployment/strategy/disabled'; + break; + + case 'rsync': + $deployStrategy = 'deployment/strategy/rsync'; + break; + + case 'targz': + $deployStrategy = 'deployment/strategy/tar-gz'; + break; + + case 'git-rebase': + $deployStrategy = 'deployment/strategy/git-rebase'; + break; + + case 'guess': + default: + if ($this->getConfig()->release('enabled', false) == true) { + $deployStrategy = 'deployment/strategy/tar-gz'; + } else { + $deployStrategy = 'deployment/strategy/rsync'; + } + break; + } + return $deployStrategy; + } + + /** + * @return string + */ + protected function chooseReleaseStrategy() + { + + if ($this->getConfig()->release('enabled', false) === true) { + $strategy = 'deployment/strategy/disabled'; + } else { + $strategy = 'deployment/release'; + } + + return $strategy; + } + } From e7cad31681686080f8390567387281c953c7d026 Mon Sep 17 00:00:00 2001 From: Vladimir Grigor Date: Mon, 28 Jul 2014 13:58:14 +0300 Subject: [PATCH 5/6] fixed a bug: symlink config value was not respected --- .../BuiltIn/Deployment/Strategy/BaseStrategyTaskAbstract.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Mage/Task/BuiltIn/Deployment/Strategy/BaseStrategyTaskAbstract.php b/Mage/Task/BuiltIn/Deployment/Strategy/BaseStrategyTaskAbstract.php index f17afbd..b99e750 100644 --- a/Mage/Task/BuiltIn/Deployment/Strategy/BaseStrategyTaskAbstract.php +++ b/Mage/Task/BuiltIn/Deployment/Strategy/BaseStrategyTaskAbstract.php @@ -28,10 +28,11 @@ abstract class BaseStrategyTaskAbstract extends AbstractTask implements IsReleas protected function checkOverrideRelease() { $overrideRelease = $this->getParameter('overrideRelease', false); + $symlink = $this->getConfig()->release('symlink', 'current'); if ($overrideRelease == true) { $releaseToOverride = false; - $resultFetch = $this->runCommandRemote('ls -ld current | cut -d"/" -f2', $releaseToOverride); + $resultFetch = $this->runCommandRemote('ls -ld '.$symlink.' | cut -d"/" -f2', $releaseToOverride); if ($resultFetch && is_numeric($releaseToOverride)) { $this->getConfig()->setReleaseId($releaseToOverride); } From 11bdd68fffe1f9e52e8db499b92ce98738f77706 Mon Sep 17 00:00:00 2001 From: Vladimir Grigor Date: Mon, 28 Jul 2014 15:43:52 +0300 Subject: [PATCH 6/6] - factored constants out; - fixed bug: "releases list" command would die checking for unnecessary --release parameter; - console prints log file name if if logging is enabled --- Mage/Command/BuiltIn/DeployCommand.php | 30 +++++++---- Mage/Command/BuiltIn/ReleasesCommand.php | 65 +++++++++++++----------- Mage/Console.php | 5 ++ 3 files changed, 61 insertions(+), 39 deletions(-) diff --git a/Mage/Command/BuiltIn/DeployCommand.php b/Mage/Command/BuiltIn/DeployCommand.php index 3c48992..a4fa92b 100644 --- a/Mage/Command/BuiltIn/DeployCommand.php +++ b/Mage/Command/BuiltIn/DeployCommand.php @@ -30,7 +30,15 @@ use Exception; */ class DeployCommand extends AbstractCommand implements RequiresEnvironment { - /** + const DEFAULT_RELEASE_IS_ENABLED = false; + const DEPLOY_STRATEGY_DISABLED = 'disabled'; + const DEPLOY_STRATEGY_RSYNC = 'rsync'; + const DEPLOY_STRATEGY_TARGZ = 'targz'; + const DEPLOY_STRATEGY_GIT_REBASE = 'git-rebase'; + const DEPLOY_STRATEGY_GUESS = 'guess'; + const DEFAULT_DEPLOY_STRATEGY = self::DEPLOY_STRATEGY_GUESS; + + /** * Deploy has Failed * @var string */ @@ -523,24 +531,24 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment protected function chooseDeployStrategy() { // Guess a Deploy Strategy - switch ($this->getConfig()->deployment('strategy', 'guess')) { - case 'disabled': + switch ($this->getConfig()->deployment('strategy', self::DEFAULT_DEPLOY_STRATEGY)) { + case self::DEPLOY_STRATEGY_DISABLED: $deployStrategy = 'deployment/strategy/disabled'; break; - case 'rsync': + case self::DEPLOY_STRATEGY_RSYNC: $deployStrategy = 'deployment/strategy/rsync'; break; - case 'targz': + case self::DEPLOY_STRATEGY_TARGZ: $deployStrategy = 'deployment/strategy/tar-gz'; break; - case 'git-rebase': + case self::DEPLOY_STRATEGY_GIT_REBASE: $deployStrategy = 'deployment/strategy/git-rebase'; break; - case 'guess': + case self::DEPLOY_STRATEGY_GUESS: default: if ($this->getConfig()->release('enabled', false) == true) { $deployStrategy = 'deployment/strategy/tar-gz'; @@ -558,10 +566,12 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment protected function chooseReleaseStrategy() { - if ($this->getConfig()->release('enabled', false) === true) { - $strategy = 'deployment/strategy/disabled'; - } else { + if ($this->getConfig()->release('enabled', self::DEFAULT_RELEASE_IS_ENABLED) + && $this->getConfig()->deployment('strategy', self::DEFAULT_DEPLOY_STRATEGY) !== self::DEPLOY_STRATEGY_DISABLED + ) { $strategy = 'deployment/release'; + } else { + $strategy = 'deployment/strategy/disabled'; } return $strategy; diff --git a/Mage/Command/BuiltIn/ReleasesCommand.php b/Mage/Command/BuiltIn/ReleasesCommand.php index 293faaf..fadedfa 100644 --- a/Mage/Command/BuiltIn/ReleasesCommand.php +++ b/Mage/Command/BuiltIn/ReleasesCommand.php @@ -28,44 +28,51 @@ class ReleasesCommand extends AbstractCommand implements RequiresEnvironment */ public function run() { - if (!is_numeric($this->getConfig()->getParameter('release', ''))) { - Console::output('This release is mandatory.', 1, 2); - return false; - } - - $subcommand = $this->getConfig()->getArgument(1); - $lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock'; - if (file_exists($lockFile) && ($subcommand == 'rollback')) { - Console::output('This environment is locked!', 1, 2); - echo file_get_contents($lockFile); - return null; - } + $subCommand = $this->getConfig()->getArgument(1); // Run Tasks for Deployment $hosts = $this->getConfig()->getHosts(); if (count($hosts) == 0) { - Console::output('Warning! No hosts defined, unable to get releases.', 1, 3); + Console::output( + 'Warning! No hosts defined, unable to get releases.', + 1, 3 + ); + + return false; + } + + foreach ($hosts as $host) { + $this->getConfig()->setHost($host); + + switch ($subCommand) { + case 'list': + $task = Factory::get('releases/list', $this->getConfig()); + $task->init(); + $result = $task->run(); + break; - } else { - foreach ($hosts as $host) { - $this->getConfig()->setHost($host); + case 'rollback': + if (!is_numeric($this->getConfig()->getParameter('release', ''))) { + Console::output('Missing required releaseid.', 1, 2); - switch ($subcommand) { - case 'list': - $task = Factory::get('releases/list', $this->getConfig()); - $task->init(); - $result = $task->run(); - break; + return false; + } + + $lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock'; + if (file_exists($lockFile)) { + Console::output('This environment is locked!', 1, 2); + echo file_get_contents($lockFile); - case 'rollback': - $releaseId = $this->getConfig()->getParameter('release', ''); - $task = Factory::get('releases/rollback', $this->getConfig()); - $task->init(); - $task->setRelease($releaseId); - $result = $task->run(); - break; + return false; } + + $releaseId = $this->getConfig()->getParameter('release', ''); + $task = Factory::get('releases/rollback', $this->getConfig()); + $task->init(); + $task->setRelease($releaseId); + $result = $task->run(); + break; } } diff --git a/Mage/Console.php b/Mage/Console.php index 3aed87b..bd4c90b 100644 --- a/Mage/Console.php +++ b/Mage/Console.php @@ -106,6 +106,11 @@ class Console $showGreetings = false; } else { self::$logEnabled = $config->general('logging', false); + if(self::$logEnabled) + { + self::log("Logging enabled"); + self::output(' Logging enabled: ' . self::getLogFile() . '', 1, 1); + } } // Greetings