From d63ca9cc04f6c67228ba555aaf2cc149da0a9996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81s=20Montan=CC=83ez?= Date: Wed, 19 Sep 2012 18:53:14 -0300 Subject: [PATCH] Verify if environment is defined. Verify that rollback has release point. --- Mage/Config.php | 74 ++++++++++++---------- Mage/Console.php | 138 +++++++++++++++++++++++------------------ Mage/Task/Deploy.php | 77 ++++++++++++----------- Mage/Task/Releases.php | 23 ++++--- 4 files changed, 171 insertions(+), 141 deletions(-) diff --git a/Mage/Config.php b/Mage/Config.php index b05ff55..f189a4f 100644 --- a/Mage/Config.php +++ b/Mage/Config.php @@ -7,13 +7,13 @@ class Mage_Config private $_general = null; private $_host = null; private $_releaseId = null; - + public function loadEnvironment($environment) { if (($environment != '') && file_exists('.mage/config/environment/' . $environment . '.yml')) { $this->_environment = spyc_load_file('.mage/config/environment/' . $environment . '.yml'); $this->_environmentName = $environment; - + // Create temporal directory for clone if (isset($this->_environment['deployment']['source']) && is_array($this->_environment['deployment']['source'])) { if (trim($this->_environment['deployment']['source']['temporal']) == '') { @@ -22,34 +22,40 @@ class Mage_Config $newTemporal = rtrim($this->_environment['deployment']['source']['temporal'], '/') . '/' . md5(microtime()) . '/'; $this->_environment['deployment']['source']['temporal'] = $newTemporal; - } + } + + return true; + } else if (($environment != '') && !file_exists('.mage/config/environment/' . $environment . '.yml')) { + return false; } + + return true; } - + public function loadSCM() { if (file_exists('.mage/config/scm.yml')) { - $this->_scm = spyc_load_file('.mage/config/scm.yml'); + $this->_scm = spyc_load_file('.mage/config/scm.yml'); } } - + public function loadGeneral() { if (file_exists('.mage/config/general.yml')) { $this->_general = spyc_load_file('.mage/config/general.yml'); } } - + public function getEnvironment() { return $this->_environment; } - + public function getEnvironmentName() { return $this->_environmentName; } - + public function getSCM() { return $this->_scm; @@ -59,64 +65,64 @@ class Mage_Config { return $this->_global; } - + public function getHosts() { $config = $this->getEnvironment(); $hosts = array(); - + if (isset($config['hosts'])) { if (is_array($config['hosts'])) { - $hosts = (array) $config['hosts']; + $hosts = (array) $config['hosts']; } else if (is_string($config['hosts'])) { $fileContent = fopen($config['hosts'], 'r'); while (($host = fgets($fileContent)) == true) { $host = trim($host); if ($host != '') { - $hosts[] = $host; + $hosts[] = $host; } } } } - + return $hosts; } - + public function setHost($host) { $this->_host = $host; return $this; } - + public function getHostName() { $info = explode(':', $this->_host); return $info[0]; } - + public function getHostPort() { $info = explode(':', $this->_host); $info[] = $this->deployment('port', '22'); return $info[1]; } - + public function getHost() { return $this->_host; } - + public function setReleaseId($id) { $this->_releaseId = $id; return $this; } - + public function getReleaseId() { return $this->_releaseId; } - + public function getTasks($stage = 'on-deploy') { switch ($stage) { @@ -124,24 +130,24 @@ class Mage_Config $type = 'tasks'; $stage = 'pre-deploy'; break; - + case 'post-deploy': $type = 'tasks'; $stage = 'post-deploy'; break; - + case 'post-release': $type = 'releases'; $stage = 'post-release'; break; - + case 'on-deploy': default: $type = 'tasks'; $stage = 'on-deploy'; break; } - + $tasks = array(); $config = $this->getEnvironment(); @@ -151,17 +157,17 @@ class Mage_Config return $tasks; } - + public function getConfig($host = false) { $taskConfig = array(); $taskConfig['deploy'] = $this->getEnvironment(); $taskConfig['deploy']['host'] = $host; $taskConfig['scm'] = $this->getSCM(); - + unset($taskConfig['deploy']['tasks']); unset($taskConfig['deploy']['hosts']); - + return $taskConfig; } @@ -170,7 +176,7 @@ class Mage_Config $this->_environment['deployment']['from'] = $from; return $this; } - + public function deployment($option, $default = false) { $options = $this->getEnvironment(); @@ -178,13 +184,13 @@ class Mage_Config if (is_array($default) && ($options['deployment'][$option] == '')) { return $default; } else { - return $options['deployment'][$option]; + return $options['deployment'][$option]; } } else { return $default; } } - + public function release($option, $default = false) { $options = $this->getEnvironment(); @@ -198,7 +204,7 @@ class Mage_Config return $default; } } - + public function scm($option, $default = false) { $options = $this->_scm; @@ -213,7 +219,7 @@ class Mage_Config return $default; } } - + public function general($option, $default = false) { $options = $this->_general; @@ -227,7 +233,7 @@ class Mage_Config return $default; } } - + public function mail($option, $default = false) { $options = $this->_general; diff --git a/Mage/Console.php b/Mage/Console.php index 512044c..cf61e0d 100644 --- a/Mage/Console.php +++ b/Mage/Console.php @@ -9,13 +9,13 @@ class Mage_Console private static $_logEnabled = true; private static $_screenBuffer = ''; private static $_commandsOutput = ''; - + public function setArgs($args) { $this->_args = $args; array_shift($this->_args); } - + public function parse() { if (count($this->_args) == 0) { @@ -27,7 +27,7 @@ class Mage_Console } else if ($this->_args[0] == 'releases') { $this->_action = 'releases'; - + } else if ($this->_args[0] == 'update') { $this->_action = 'update'; @@ -36,20 +36,20 @@ class Mage_Console } else if ($this->_args[0] == 'add') { $this->_action = 'add'; - + } else if ($this->_args[0] == 'install') { $this->_action = 'install'; - + } else if ($this->_args[0] == 'upgrade') { $this->_action = 'upgrade'; - + } else if ($this->_args[0] == 'version') { $this->_action = 'version'; } else if ($this->_args[0] == 'init') { $this->_action = 'init'; - } - + } + foreach ($this->_args as $argument) { if (preg_match('/to:[\w]+/i', $argument)) { $this->_environment = str_replace('to:', '', $argument); @@ -64,17 +64,17 @@ class Mage_Console } } } - + public function getAction() { return $this->_action; } - + public function getEnvironment() { return $this->_environment; } - + public static function getActionOption($name, $default = false) { if (isset(self::$_actionOptions[$name])) { @@ -83,22 +83,22 @@ class Mage_Console return $default; } } - + public static function output($message, $tabs = 1, $newLine = 1) { self::log(strip_tags($message)); - + self::$_screenBuffer .= str_repeat("\t", $tabs) . strip_tags($message) . str_repeat(PHP_EOL, $newLine); - + $output = str_repeat("\t", $tabs) . Mage_Console_Colors::color($message) . str_repeat(PHP_EOL, $newLine); echo $output; } - + public static function executeCommand($command, &$output = null) { self::log('---------------------------------'); @@ -108,24 +108,24 @@ class Mage_Console $log = array(); exec($command . ' 2>&1', $log, $return); $log = implode(PHP_EOL, $log); - + if (!$return) { - $output = trim($log); + $output = trim($log); } - self::$_commandsOutput .= PHP_EOL . trim($log) . PHP_EOL; - + self::$_commandsOutput .= PHP_EOL . trim($log) . PHP_EOL; + self::log($log); self::log('---------------------------------'); return !$return; } - + public function run() - { + { // Load Config $config = new Mage_Config; $config->loadGeneral(); - $config->loadEnvironment($this->getEnvironment()); + $environmentOk = $config->loadEnvironment($this->getEnvironment()); $config->loadSCM(); // Logging @@ -136,59 +136,72 @@ class Mage_Console } else { self::$_logEnabled = $config->general('logging', false); } - + // Grettings if ($showGrettings) { Mage_Console::output('Starting Magallanes', 0, 2); } - switch ($this->getAction()) { - case 'deploy': - $task = new Mage_Task_Deploy; - $task->run($config); - break; - - case 'releases': - $task = new Mage_Task_Releases; - switch ($this->_args[1]) { - case 'list': - $task->setAction($this->_args[1]); - break; + if (!$environmentOk) { + Mage_Console::output('You have selected an invalid environment', 0, 2); - case 'rollback': - $task->setAction($this->_args[1]); - $task->setRelease($this->_args[2]); - break; - } - $task->run($config); - break; + } else { + switch ($this->getAction()) { + case 'deploy': + $task = new Mage_Task_Deploy; + $task->run($config); + break; - case 'update'; + case 'releases': + $task = new Mage_Task_Releases; + if (!isset($this->_args[1])) { + Mage_Console::output('You must indicate a task', 0, 2); + break; + } + switch ($this->_args[1]) { + case 'list': + $task->setAction($this->_args[1]); + break; + + case 'rollback': + if (!isset($this->_args[2])) { + Mage_Console::output('You must indicate a release point', 0, 2); + break 2; + } + + $task->setAction($this->_args[1]); + $task->setRelease($this->_args[2]); + break; + } + $task->run($config); + break; + + case 'update'; $task = new Mage_Task_Update; $task->run($config); break; - case 'compile'; + case 'compile'; $task = new Mage_Task_Compile; $task->run($config); break; - case 'install'; + case 'install'; $task = new Mage_Task_Install; $task->run(); break; - - case 'upgrade'; + + case 'upgrade'; $task = new Mage_Task_Upgrade; $task->run(); break; - - case 'init'; + + case 'init'; $task = new Mage_Task_Init; $task->run(); break; - - case 'add'; + + case 'add'; switch ($this->_args[1]) { case 'environment': if (isset($this->_args[3]) && ($this->_args[3] == '--with-releases')) { @@ -202,35 +215,36 @@ class Mage_Console break; } break; - - case 'version'; + + case 'version'; $this->showVersion(); break; - - default: - Mage_Console::output('Invalid action', 0, 2); - break; + + default: + Mage_Console::output('Invalid action', 0, 2); + break; + } } - + if ($showGrettings) { Mage_Console::output('Finished Magallanes', 0, 2); } } - + public function showVersion() { Mage_Console::output('Running Magallanes version ' . MAGALLANES_VERSION .'', 0, 2); } - + public static function log($message, $continuation = false) { if (self::$_logEnabled) { if (self::$_log == null) { self::$_log = fopen('.mage/logs/log-' . date('Ymd-His') . '.log', 'w'); } - + $message = date('Y-m-d H:i:s -- ') . $message; - fwrite(self::$_log, $message . PHP_EOL); + fwrite(self::$_log, $message . PHP_EOL); } } } \ No newline at end of file diff --git a/Mage/Task/Deploy.php b/Mage/Task/Deploy.php index 272aece..1091696 100644 --- a/Mage/Task/Deploy.php +++ b/Mage/Task/Deploy.php @@ -7,26 +7,31 @@ class Mage_Task_Deploy private $_startTimeHosts = null; private $_endTimeHosts = null; private $_hostsCount = 0; - + public function __construct() { $this->_releaseId = date('YmdHis'); } - + public function run(Mage_Config $config) { $this->_startTime = time(); $this->_config = $config; + if ($config->getEnvironment() == '') { + Mage_Console::output('You must specify an environment', 0, 2); + return; + } + // Run Pre-Deployment Tasks $this->_runNonDeploymentTasks('pre-deploy', $config, 'Pre-Deployment'); - + // Run Tasks for Deployment $hosts = $config->getHosts(); - + if (count($hosts) == 0) { Mage_Console::output('Warning! No hosts defined, skipping deployment tasks.', 1, 3); - + } else { $this->_startTimeHosts = time(); foreach ($hosts as $host) { @@ -34,9 +39,9 @@ class Mage_Task_Deploy $config->setHost($host); $tasks = 0; $completedTasks = 0; - + Mage_Console::output('Deploying to ' . $config->getHost() . ''); - + $tasksToRun = $config->getTasks(); array_unshift($tasksToRun, 'deployment/rsync'); @@ -54,10 +59,10 @@ class Mage_Task_Deploy $tasks++; $task = Mage_Task_Factory::get($taskName, $config, false, 'deploy'); $task->init(); - + Mage_Console::output('Running ' . $task->getName() . ' ... ', 2, false); $result = $task->run(); - + if ($result == true) { Mage_Console::output('OK', 0); $completedTasks++; @@ -65,13 +70,13 @@ class Mage_Task_Deploy Mage_Console::output('FAIL', 0); } } - + if ($completedTasks == $tasks) { - $tasksColor = 'green'; + $tasksColor = 'green'; } else { - $tasksColor = 'red'; + $tasksColor = 'red'; } - + Mage_Console::output('Deployment to ' . $config->getHost() . ' compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.', 1, 3); } } @@ -85,24 +90,24 @@ class Mage_Task_Deploy if ($this->_hostsCount > 0) { $timeTextHost = $this->_transcurredTime($this->_endTimeHosts - $this->_startTimeHosts); Mage_Console::output('Time for deployment: ' . $timeTextHost . '.'); - + $timeTextPerHost = $this->_transcurredTime(round(($this->_endTimeHosts - $this->_startTimeHosts) / $this->_hostsCount)); - Mage_Console::output('Average time per host: ' . $timeTextPerHost . '.'); + Mage_Console::output('Average time per host: ' . $timeTextPerHost . '.'); } - + // Time Information General $timeText = $this->_transcurredTime(time() - $this->_startTime); Mage_Console::output('Total time: ' . $timeText . '.', 1, 2); } - + private function _runNonDeploymentTasks($stage, Mage_Config $config, $title) { $tasksToRun = $config->getTasks($stage); - + // Look for Remote Source if (is_array($this->_config->deployment('source', null))) { if ($stage == 'pre-deploy') { - array_unshift($tasksToRun, 'scm/clone'); + array_unshift($tasksToRun, 'scm/clone'); } elseif ($stage == 'post-deploy') { array_unshift($tasksToRun, 'scm/remove-clone'); } @@ -110,60 +115,60 @@ class Mage_Task_Deploy if (count($tasksToRun) == 0) { Mage_Console::output('No ' . $title . ' tasks defined.', 1, 3); - + } else { Mage_Console::output('Starting ' . $title . ' tasks:'); - + $tasks = 0; $completedTasks = 0; - + foreach ($tasksToRun as $taskName) { $tasks++; $task = Mage_Task_Factory::get($taskName, $config, false, $stage); $task->init(); - + Mage_Console::output('Running ' . $task->getName() . ' ... ', 2, 0); $result = $task->run(); - + if ($result == true) { Mage_Console::output('OK', 0); $completedTasks++; } else { Mage_Console::output('FAIL', 0); - } + } } - + if ($completedTasks == $tasks) { - $tasksColor = 'green'; + $tasksColor = 'green'; } else { - $tasksColor = 'red'; + $tasksColor = 'red'; } - - Mage_Console::output('Finished ' . $title . ' tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.', 1, 3); + + Mage_Console::output('Finished ' . $title . ' tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.', 1, 3); } - + } - + private function _transcurredTime($time) { $hours = floor($time / 3600); $minutes = floor(($time - ($hours * 3600)) / 60); $seconds = $time - ($minutes * 60) - ($hours * 3600); $timeText = array(); - + if ($hours > 0) { $timeText[] = $hours . ' hours'; } - + if ($minutes > 0) { $timeText[] = $minutes . ' minutes'; } - + if ($seconds > 0) { $timeText[] = $seconds . ' seconds'; } - + return implode(' ', $timeText); } } \ No newline at end of file diff --git a/Mage/Task/Releases.php b/Mage/Task/Releases.php index fb93518..38e502f 100644 --- a/Mage/Task/Releases.php +++ b/Mage/Task/Releases.php @@ -4,39 +4,44 @@ class Mage_Task_Releases private $_config = null; private $_action = null; private $_release = null; - + public function setAction($action) { $this->_action = $action; return $this; } - + public function getAction() { return $this->_action; } - + public function setRelease($releaseId) { $this->_release = $releaseId; return $this; } - + public function getRelease() { return $this->_release; } - + public function run(Mage_Config $config) { $this->_config = $config; - + + if ($config->getEnvironment() == '') { + Mage_Console::output('You must specify an environment', 0, 2); + return; + } + // Run Tasks for Deployment $hosts = $config->getHosts(); - + if (count($hosts) == 0) { Mage_Console::output('Warning! No hosts defined, unable to get releases.', 1, 3); - + } else { foreach ($hosts as $host) { $config->setHost($host); @@ -46,7 +51,7 @@ class Mage_Task_Releases $task->init(); $result = $task->run(); break; - + case 'rollback': $task = Mage_Task_Factory::get('releases/rollback', $config); $task->init();