From 6d5e7ef22f9d351fc73a12ed637d33658e2e4acc Mon Sep 17 00:00:00 2001 From: Andrs Montaez Date: Wed, 23 Nov 2011 23:15:37 -0200 Subject: [PATCH] Added functionality for adding new environments. Changed feedback and controls of who tasks are run. --- Mage/Config.php | 8 ++- Mage/Console.php | 25 +++++++--- Mage/Task/Add.php | 37 ++++++++++++++ Mage/Task/Deploy.php | 116 +++++++++++++++++++++++++------------------ bin/mage.php | 6 +-- 5 files changed, 134 insertions(+), 58 deletions(-) create mode 100644 Mage/Task/Add.php diff --git a/Mage/Config.php b/Mage/Config.php index 9e4b732..85ed1b9 100644 --- a/Mage/Config.php +++ b/Mage/Config.php @@ -29,7 +29,13 @@ class Mage_Config public function getHosts() { $config = $this->getEnvironment(); - return $config['hosts']; + $hosts = array(); + + if (isset($config['hosts'])) { + $hosts = (array) $config['hosts']; + } + + return $hosts; } public function getTasks($type = 'tasks') diff --git a/Mage/Console.php b/Mage/Console.php index 5a8e63b..0dca4b8 100644 --- a/Mage/Console.php +++ b/Mage/Console.php @@ -14,14 +14,18 @@ class Mage_Console public function parse() { - foreach ($this->_args as $argument) { - if ($argument == 'deploy') { + if ($this->_args[0] == 'deploy') { $this->_action = 'deploy'; - } else if ($argument == 'update') { + } else if ($this->_args[0] == 'update') { $this->_action = 'update'; - } else if (preg_match('/to:[\w]+/i', $argument)) { + } else if ($this->_args[0] == 'add') { + $this->_action = 'add'; + } + + foreach ($this->_args as $argument) { + if (preg_match('/to:[\w]+/i', $argument)) { $this->_environment = str_replace('to:', '', $argument); } } @@ -37,11 +41,11 @@ class Mage_Console return $this->_environment; } - public static function output($message, $tabs = 1, $newLine = true) + public static function output($message, $tabs = 1, $newLine = 1) { $output = str_repeat("\t", $tabs) . Mage_Console_Colors::color($message) - . ($newLine ? PHP_EOL : ''); + . str_repeat(PHP_EOL, $newLine); echo $output; } @@ -71,6 +75,15 @@ class Mage_Console $task = new Mage_Task_Update; $task->run($config); break; + + case 'add'; + switch ($this->_args[1]) { + case 'environment': + $task = new Mage_Task_Add; + $task->environment($this->_args[2]); + break; + } + break; } } } \ No newline at end of file diff --git a/Mage/Task/Add.php b/Mage/Task/Add.php new file mode 100644 index 0000000..4a6a7fd --- /dev/null +++ b/Mage/Task/Add.php @@ -0,0 +1,37 @@ +' . $environmentName . ''); + + // Check if there is already an environment with the same name + if (file_exists($environmentConfigFile)) { + Mage_Console::output('Error!! Already exists an environment called ' . $environmentName . ''); + } else { + $baseConfig = '#' . $environmentName . PHP_EOL + . 'user: dummy' . PHP_EOL + . 'deploy-from: ./' . PHP_EOL + . 'deploy-to: /var/www/vhosts/example.com/www' . PHP_EOL + . 'hosts:' . PHP_EOL + . 'pre-tasks:' . PHP_EOL + . 'tasks:' . PHP_EOL + . ' - deployment/rsync' . PHP_EOL + . 'post-tasks:' . PHP_EOL; + $result = file_put_contents($environmentConfigFile, $baseConfig); + + if ($result) { + Mage_Console::output('Success!! Environment config file for ' . $environmentName . ' created successfully at ' . $environmentConfigFile . ''); + Mage_Console::output('So please! Review and adjust its configuration.', 2); + } else { + Mage_Console::output('Error!! Unable to create config file for environment called ' . $environmentName . ''); + } + } + + Mage_Console::output(''); + } + +} \ No newline at end of file diff --git a/Mage/Task/Deploy.php b/Mage/Task/Deploy.php index d9d4aab..39eb13c 100644 --- a/Mage/Task/Deploy.php +++ b/Mage/Task/Deploy.php @@ -11,73 +11,95 @@ class Mage_Task_Deploy $this->_runNonDeploymentTasks('pre', $config); // Run Tasks for Deployment - foreach ($config->getHosts() as $host) { - $taskConfig = $config->getConfig($host); - $tasks = 0; - $completedTasks = 0; + $hosts = $config->getHosts(); + + if (count($hosts) == 0) { + Mage_Console::output('Warning! No hosts defined, skipping deployment tasks.', 1, 3); + + } else { + foreach ($hosts as $host) { + $taskConfig = $config->getConfig($host); + $tasks = 0; + $completedTasks = 0; + + Mage_Console::output('Deploying to ' . $host . ''); + + $tasksToRun = $config->getTasks(); + if (count($tasksToRun) == 0) { + Mage_Console::output('Warning! No Deployment tasks defined.', 2); + Mage_Console::output('Deployment to ' . $host . ' skipped!', 1, 3); - Mage_Console::output('Deploying to ' . $host . ''); + } else { + foreach ($tasksToRun as $taskName) { + $tasks++; + $task = Mage_Task_Factory::get($taskName, $taskConfig); + $task->init(); + + Mage_Console::output('Running ' . $task->getName() . ' ... ', 2, false); + $result = $task->run(); + + if ($result == true) { + Mage_Console::output('OK'); + $completedTasks++; + } else { + Mage_Console::output('FAIL'); + } + } + + if ($completedTasks == $tasks) { + $tasksColor = 'green'; + } else { + $tasksColor = 'red'; + } + + Mage_Console::output('Deployment to ' . $host . ' compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.', 1, 3); + } + } + } + + // Run Post-Deployment Tasks + $this->_runNonDeploymentTasks('post', $config); + } + + private function _runNonDeploymentTasks($type, Mage_Config $config) + { + $tasksToRun = $config->getTasks($type); + + if (count($tasksToRun) == 0) { + Mage_Console::output('No ' . ucfirst($type) . '-Deployment tasks defined.', 1, 3); - foreach ($config->getTasks() as $taskName) { + } else { + Mage_Console::output('Starting ' . ucfirst($type) . '-Deployment tasks:'); + + $taskConfig = $config->getConfig(); + $tasks = 0; + $completedTasks = 0; + + foreach ($tasksToRun as $taskName) { $tasks++; $task = Mage_Task_Factory::get($taskName, $taskConfig); $task->init(); - + Mage_Console::output('Running ' . $task->getName() . ' ... ', 2, false); $result = $task->run(); - + if ($result == true) { Mage_Console::output('OK'); $completedTasks++; } else { Mage_Console::output('FAIL'); - } + } } - + if ($completedTasks == $tasks) { $tasksColor = 'green'; } else { $tasksColor = 'red'; } - - Mage_Console::output('Deployment to ' . $host . ' compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.' . PHP_EOL . PHP_EOL); - } - - // Run Post-Deployment Tasks - $this->_runNonDeploymentTasks('post', $config); - } - - private function _runNonDeploymentTasks($type, Mage_Config $config) - { - Mage_Console::output('Starting ' . ucfirst($type) . '-Deployment tasks:'); - - $taskConfig = $config->getConfig(); - $tasks = 0; - $completedTasks = 0; - - foreach ($config->getTasks($type) as $taskName) { - $tasks++; - $task = Mage_Task_Factory::get($taskName, $taskConfig); - $task->init(); - - Mage_Console::output('Running ' . $task->getName() . ' ... ', 2, false); - $result = $task->run(); - - if ($result == true) { - Mage_Console::output('OK'); - $completedTasks++; - } else { - Mage_Console::output('FAIL'); - } - } - - if ($completedTasks == $tasks) { - $tasksColor = 'green'; - } else { - $tasksColor = 'red'; + + Mage_Console::output('Finished ' . ucfirst($type) . '-Deployment tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.', 1, 3); } - Mage_Console::output('Finished ' . ucfirst($type) . '-Deployment tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.' . PHP_EOL . PHP_EOL); } diff --git a/bin/mage.php b/bin/mage.php index 4a7e422..10aab3d 100644 --- a/bin/mage.php +++ b/bin/mage.php @@ -16,9 +16,7 @@ $baseDir = dirname(dirname(__FILE__)); require_once $baseDir . '/Mage/Autoload.php'; spl_autoload_register(array('Mage_Autoload', 'autoload')); -Mage_Console::output('Starting Magallanes', 0); -Mage_Console::output(''); - +Mage_Console::output('Starting Magallanes', 0, 2); $console = new Mage_Console; $console->setArgs($argv); @@ -27,4 +25,4 @@ $console->parse(); $console->run(); -Mage_Console::output('Finished Magallanes', 0); +Mage_Console::output('Finished Magallanes', 0, 2);