diff --git a/.gitignore b/.gitignore index 5dc363a2..a147ed33 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,9 @@ -.settings -.settings/* -.project -.buildpath -.idea vendor +mage.phar # OS generated files # // GitHub Recommendation ###################### .DS_Store* ehthumbs.db Icon? -Thumbs.db \ No newline at end of file +Thumbs.db diff --git a/LICENSE b/LICENSE index ccdd780d..78cd3842 100644 --- a/LICENSE +++ b/LICENSE @@ -24,4 +24,4 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------- The Yaml Library Parser is (c) by Fabien Potencier, and belongs to the Symfony Proyect --------- \ No newline at end of file +-------- diff --git a/LICENSE_YAML b/LICENSE_YAML index 4acdf9df..0b3292cf 100644 --- a/LICENSE_YAML +++ b/LICENSE_YAML @@ -16,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. \ No newline at end of file +THE SOFTWARE. diff --git a/Mage/Command/AbstractCommand.php b/Mage/Command/AbstractCommand.php index b513517c..3ed5a9db 100644 --- a/Mage/Command/AbstractCommand.php +++ b/Mage/Command/AbstractCommand.php @@ -52,4 +52,4 @@ public function getConfig() { return $this->config; } -} \ No newline at end of file +} diff --git a/Mage/Command/BuiltIn/AddCommand.php b/Mage/Command/BuiltIn/AddCommand.php index fab6f713..d7b1d12a 100644 --- a/Mage/Command/BuiltIn/AddCommand.php +++ b/Mage/Command/BuiltIn/AddCommand.php @@ -68,7 +68,7 @@ protected function addEnvironment() throw new Exception('The environment already exists.'); } - Console::output('Adding new environment: ' . $environmentName . ''); + Console::output('Adding new environment: ' . $environmentName . ''); $releasesConfig = 'releases:' . PHP_EOL . ' enabled: true' . PHP_EOL @@ -93,10 +93,10 @@ protected function addEnvironment() $result = file_put_contents($environmentConfigFile, $baseConfig); if ($result) { - Console::output('Success!! Environment config file for ' . $environmentName . ' created successfully at ' . $environmentConfigFile . ''); - Console::output('So please! Review and adjust its configuration.', 2, 2); + Console::output('Success!! Environment config file for ' . $environmentName . ' created successfully at ' . $environmentConfigFile . ''); + Console::output('So please! Review and adjust its configuration.', 2, 2); } else { - Console::output('Error!! Unable to create config file for environment called ' . $environmentName . '', 1, 2); + Console::output('Error!! Unable to create config file for environment called ' . $environmentName . '', 1, 2); } } } diff --git a/Mage/Command/BuiltIn/CompileCommand.php b/Mage/Command/BuiltIn/CompileCommand.php index f67e8755..9cd94b66 100644 --- a/Mage/Command/BuiltIn/CompileCommand.php +++ b/Mage/Command/BuiltIn/CompileCommand.php @@ -28,7 +28,7 @@ public function run() { if (ini_get('phar.readonly')) { Console::output('The php.ini variable phar.readonly must be Off.', 1, 2); - return 300; + return 200; } $compiler = new Compiler; diff --git a/Mage/Command/BuiltIn/DeployCommand.php b/Mage/Command/BuiltIn/DeployCommand.php index d1888b38..943a6309 100644 --- a/Mage/Command/BuiltIn/DeployCommand.php +++ b/Mage/Command/BuiltIn/DeployCommand.php @@ -3,6 +3,7 @@ * This file is part of the Magallanes package. * * (c) Andrés Montañez +* (c) Alex V Kotelnikov * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -16,6 +17,7 @@ use Mage\Task\AbstractTask; use Mage\Task\Releases\SkipOnOverride; use Mage\Task\ErrorWithMessageException; +use Mage\Task\RollbackException; use Mage\Task\SkipException; use Mage\Console; use Mage\Config; @@ -109,20 +111,20 @@ public static function getStatus() */ public function run() { - $exitCode = 1000; + $exitCode = 240; // Check if Environment is not Locked $lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock'; if (file_exists($lockFile)) { Console::output('This environment is locked!', 1, 2); echo file_get_contents($lockFile); - return 1010; + return 231; } // Check for running instance and Lock if (file_exists(getcwd() . '/.mage/~working.lock')) { Console::output('There is already an instance of Magallanes running!', 1, 2); - return 1020; + return 230; } else { touch(getcwd() . '/.mage/~working.lock'); } @@ -131,21 +133,21 @@ public function run() $this->getConfig()->setReleaseId(date('YmdHis')); // Deploy Summary - Console::output('Deploy summary', 1, 1); + Console::output('Deploy summary', 1, 1); // Deploy Summary - Environment - Console::output('Environment: ' . $this->getConfig()->getEnvironment() . '', 2, 1); + Console::output('Environment: ' . $this->getConfig()->getEnvironment() . '', 2, 1); // Deploy Summary - Releases if ($this->getConfig()->release('enabled', false)) { - Console::output('Release ID: ' . $this->getConfig()->getReleaseId() . '', 2, 1); + Console::output('Release ID: ' . $this->getConfig()->getReleaseId() . '', 2, 1); } // Deploy Summary - SCM if ($this->getConfig()->deployment('scm', false)) { $scmConfig = $this->getConfig()->deployment('scm'); if (isset($scmConfig['branch'])) { - Console::output('SCM Branch: ' . $scmConfig['branch'] . '', 2, 1); + Console::output('SCM Branch: ' . $scmConfig['branch'] . '', 2, 1); } } @@ -160,7 +162,7 @@ public function run() // Check Status if (self::$failedTasks > 0) { self::$deployStatus = self::FAILED; - Console::output('A total of ' . self::$failedTasks . ' deployment tasks failed: ABORTING', 1, 2); + Console::output('A total of ' . self::$failedTasks . ' deployment tasks failed: ABORTING', 1, 2); } else { // Run Deployment Tasks @@ -169,7 +171,7 @@ public function run() // Check Status if (self::$failedTasks > 0) { self::$deployStatus = self::FAILED; - Console::output('A total of ' . self::$failedTasks . ' deployment tasks failed: ABORTING', 1, 2); + Console::output('A total of ' . self::$failedTasks . ' deployment tasks failed: ABORTING', 1, 2); } // Run Post-Deployment Tasks @@ -179,15 +181,15 @@ public function run() // Time Information Hosts if ($this->hostsCount > 0) { $timeTextHost = $this->transcurredTime($this->endTimeHosts - $this->startTimeHosts); - Console::output('Time for deployment: ' . $timeTextHost . '.'); + Console::output('Time for deployment: ' . $timeTextHost . '.'); $timeTextPerHost = $this->transcurredTime(round(($this->endTimeHosts - $this->startTimeHosts) / $this->hostsCount)); - Console::output('Average time per host: ' . $timeTextPerHost . '.'); + Console::output('Average time per host: ' . $timeTextPerHost . '.'); } // Time Information General $timeText = $this->transcurredTime(time() - $this->startTime); - Console::output('Total time: ' . $timeText . '.', 1, 2); + Console::output('Total time: ' . $timeText . '.', 1, 2); // Send Notifications $this->sendNotification(self::$failedTasks > 0 ? false : true); @@ -249,10 +251,10 @@ protected function runNonDeploymentTasks($stage, Config $config, $title) } if (count($tasksToRun) == 0) { - Console::output('No ' . $title . ' tasks defined.', 1, 3); + Console::output('No ' . $title . ' tasks defined.', 1, 3); } else { - Console::output('Starting ' . $title . ' tasks:'); + Console::output('Starting ' . $title . ' tasks:'); $tasks = 0; $completedTasks = 0; @@ -274,7 +276,7 @@ protected function runNonDeploymentTasks($stage, Config $config, $title) $tasksColor = 'red'; } - Console::output('Finished ' . $title . ' tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.', 1, 3); + Console::output('Finished ' . $title . ' tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.', 1, 3); } } @@ -290,7 +292,7 @@ protected function runDeploymentTasks() self::$failedTasks = 0; if ($this->hostsCount == 0) { - Console::output('Warning! No hosts defined, skipping deployment tasks.', 1, 3); + Console::output('Warning! No hosts defined, skipping deployment tasks.', 1, 3); } else { $this->startTimeHosts = time(); @@ -311,7 +313,7 @@ protected function runDeploymentTasks() $tasks = 0; $completedTasks = 0; - Console::output('Deploying to ' . $this->getConfig()->getHost() . ''); + Console::output('Deploying to ' . $this->getConfig()->getHost() . ''); $tasksToRun = $this->getConfig()->getTasks(); @@ -320,8 +322,8 @@ protected function runDeploymentTasks() array_unshift($tasksToRun, $deployStrategy); if (count($tasksToRun) == 0) { - Console::output('Warning! No Deployment tasks defined.', 2); - Console::output('Deployment to ' . $host . ' skipped!', 1, 3); + Console::output('Warning! No Deployment tasks defined.', 2); + Console::output('Deployment to ' . $host . ' skipped!', 1, 3); } else { foreach ($tasksToRun as $taskData) { @@ -341,7 +343,7 @@ protected function runDeploymentTasks() $tasksColor = 'red'; } - Console::output('Deployment to ' . $this->getConfig()->getHost() . ' completed: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.', 1, 3); + Console::output('Deployment to ' . $this->getConfig()->getHost() . ' completed: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.', 1, 3); } // Reset Host Config @@ -356,9 +358,9 @@ protected function runDeploymentTasks() } // Releasing - if (self::$deployStatus == self::SUCCEDED && $this->getConfig()->release('enabled', false) == true) { + if (self::$deployStatus == self::SUCCEDED && $this->getConfig()->release('enabled', false) === true) { // Execute the Releases - Console::output('Starting the Releasing'); + Console::output('Starting the Releasing'); $completedTasks = 0; foreach ($hosts as $hostKey => $host) { @@ -382,7 +384,7 @@ protected function runDeploymentTasks() // Reset Host Config $this->getConfig()->setHostConfig(null); } - Console::output('Finished the Releasing', 1, 3); + Console::output('Finished the Releasing', 1, 3); // Execute the Post-Release Tasks foreach ($hosts as $hostKey => $host) { @@ -403,7 +405,7 @@ protected function runDeploymentTasks() $completedTasks = 0; if (count($tasksToRun) > 0) { - Console::output('Starting Post-Release tasks for ' . $host . ':'); + Console::output('Starting Post-Release tasks for ' . $host . ':'); foreach ($tasksToRun as $task) { $task = Factory::get($task, $this->getConfig(), false, AbstractTask::STAGE_POST_RELEASE); @@ -418,7 +420,7 @@ protected function runDeploymentTasks() } else { $tasksColor = 'red'; } - Console::output('Finished Post-Release tasks for ' . $host . ': <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.', 1, 3); + Console::output('Finished Post-Release tasks for ' . $host . ': <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.', 1, 3); } // Reset Host Config @@ -428,6 +430,28 @@ protected function runDeploymentTasks() } } + protected function runRollbackTask(){ + $this->getConfig()->reload(); + $hosts = $this->getConfig()->getHosts(); + + if (count($hosts) == 0) { + Console::output('Warning! No hosts defined, unable to get releases.', 1, 3); + + } else { + $result = true; + foreach ($hosts as $host) { + $this->getConfig()->setHost($host); + + $this->getConfig()->setReleaseId(-1); + $task = Factory::get('releases/rollback', $this->getConfig()); + $task->init(); + $result = $task->run() && $result; + } + return $result; + } + return false; + } + /** * Runs a Task * @@ -439,7 +463,7 @@ protected function runTask(AbstractTask $task, $title = null) { $task->init(); - if ($title == null) { + if ($title === null) { $title = 'Running ' . $task->getName() . ' ... '; } Console::output($title, 2, 0); @@ -449,11 +473,11 @@ protected function runTask(AbstractTask $task, $title = null) $runTask = false; } - if ($runTask == true) { + if ($runTask === true) { try { $result = $task->run(); - if ($result == true) { + if ($result === true) { Console::output('OK', 0); $result = true; @@ -461,6 +485,11 @@ protected function runTask(AbstractTask $task, $title = null) Console::output('FAIL', 0); $result = false; } + } catch (RollbackException $e) { + Console::output('FAIL, Rollback started [Message: ' . $e->getMessage() . ']', 0); + $this->runRollbackTask(); + $result = false; + } catch (ErrorWithMessageException $e) { Console::output('FAIL [Message: ' . $e->getMessage() . ']', 0); $result = false; @@ -564,7 +593,7 @@ protected function chooseDeployStrategy() case self::DEPLOY_STRATEGY_GUESS: default: - if ($this->getConfig()->release('enabled', false) == true) { + if ($this->getConfig()->release('enabled', false) === true) { $deployStrategy = 'deployment/strategy/tar-gz'; } else { $deployStrategy = 'deployment/strategy/rsync'; diff --git a/Mage/Command/BuiltIn/InitCommand.php b/Mage/Command/BuiltIn/InitCommand.php index f9963350..90d665f0 100644 --- a/Mage/Command/BuiltIn/InitCommand.php +++ b/Mage/Command/BuiltIn/InitCommand.php @@ -30,11 +30,11 @@ public function run() $exitCode = 50; $configDir = getcwd() . '/.mage'; - Console::output('Initiating managing process for application with Magallanes'); + Console::output('Initiating managing process for application with Magallanes'); // Check if there is already a config dir if (file_exists($configDir)) { - Console::output('Error!! Already exists .mage directory.', 1, 2); + Console::output('Error!! Already exists .mage directory.', 1, 2); } else { $results = array(); $results[] = mkdir($configDir); @@ -48,8 +48,8 @@ public function run() $results[] = file_put_contents($configDir . '/config/general.yml', $this->getGeneralConfig()); if (!in_array(false, $results)) { - Console::output('Success!! The configuration for Magallanes has been generated at .mage directory.'); - Console::output('Please!! Review and adjust the configuration.', 2, 2); + Console::output('Success!! The configuration for Magallanes has been generated at .mage directory.'); + Console::output('Please!! Review and adjust the configuration.', 2, 2); $exitCode = 0; } else { diff --git a/Mage/Command/BuiltIn/InstallCommand.php b/Mage/Command/BuiltIn/InstallCommand.php index 47a8411a..4271d43b 100644 --- a/Mage/Command/BuiltIn/InstallCommand.php +++ b/Mage/Command/BuiltIn/InstallCommand.php @@ -27,7 +27,7 @@ class InstallCommand extends AbstractCommand public function run() { $exitCode = 88; - Console::output('Installing Magallanes... ', 1, 0); + Console::output('Installing Magallanes... ', 1, 0); // Vars $installDir = $this->getConfig()->getParameter('installDir', '/opt/magallanes'); @@ -122,4 +122,4 @@ protected function recursiveCopy($from, $to) return false; } } -} \ No newline at end of file +} diff --git a/Mage/Command/BuiltIn/ListCommand.php b/Mage/Command/BuiltIn/ListCommand.php index 48e89bf7..0d278628 100644 --- a/Mage/Command/BuiltIn/ListCommand.php +++ b/Mage/Command/BuiltIn/ListCommand.php @@ -31,7 +31,7 @@ class ListCommand extends AbstractCommand */ public function run() { - $exitCode = 600; + $exitCode = 221; $subCommand = $this->getConfig()->getArgument(1); try { @@ -56,7 +56,7 @@ public function run() */ protected function listEnvironments() { - $exitCode = 600; + $exitCode = 220; $environments = array(); $content = scandir(getcwd() . '/.mage/config/environment/'); foreach ($content as $file) { @@ -67,7 +67,7 @@ protected function listEnvironments() sort($environments); if (count($environments) > 0) { - Console::output('These are your configured environments:', 1, 1); + Console::output('These are your configured environments:', 1, 1); foreach ($environments as $environment) { Console::output('* ' . $environment . '', 2, 1); } @@ -75,7 +75,7 @@ protected function listEnvironments() $exitCode = 0; } else { - Console::output('You don\'t have any environment configured.', 1, 2); + Console::output('You don\'t have any environment configured.', 1, 2); } return $exitCode; diff --git a/Mage/Command/BuiltIn/ReleasesCommand.php b/Mage/Command/BuiltIn/ReleasesCommand.php index 28379036..3a40439e 100644 --- a/Mage/Command/BuiltIn/ReleasesCommand.php +++ b/Mage/Command/BuiltIn/ReleasesCommand.php @@ -28,7 +28,7 @@ class ReleasesCommand extends AbstractCommand implements RequiresEnvironment */ public function run() { - $exitCode = 400; + $exitCode = 100; $subCommand = $this->getConfig()->getArgument(1); // Run Tasks for Deployment @@ -36,16 +36,25 @@ public function run() if (count($hosts) == 0) { Console::output( - 'Warning! No hosts defined, unable to get releases.', + 'Warning! No hosts defined, unable to get releases.', 1, 3 ); - return 401; + return 101; } $result = true; - foreach ($hosts as $host) { + foreach ($hosts as $hostKey => $host) { + // Check if Host has specific configuration + $hostConfig = null; + if (is_array($host)) { + $hostConfig = $host; + $host = $hostKey; + } + + // Set Host and Host Specific Config $this->getConfig()->setHost($host); + $this->getConfig()->setHostConfig($hostConfig); switch ($subCommand) { case 'list': @@ -58,7 +67,7 @@ public function run() if (!is_numeric($this->getConfig()->getParameter('release', ''))) { Console::output('Missing required releaseid.', 1, 2); - return 410; + return 102; } $lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock'; @@ -66,7 +75,7 @@ public function run() Console::output('This environment is locked!', 1, 2); echo file_get_contents($lockFile); - return 420; + return 103; } $releaseId = $this->getConfig()->getParameter('release', ''); diff --git a/Mage/Command/BuiltIn/RollbackCommand.php b/Mage/Command/BuiltIn/RollbackCommand.php index 7a5b85b4..8d85ac80 100644 --- a/Mage/Command/BuiltIn/RollbackCommand.php +++ b/Mage/Command/BuiltIn/RollbackCommand.php @@ -28,31 +28,40 @@ class RollbackCommand extends AbstractCommand implements RequiresEnvironment */ public function run() { - $exitCode = 450; + $exitCode = 105; $releaseId = $this->getConfig()->getArgument(1); if (!is_numeric($releaseId)) { Console::output('This release is mandatory.', 1, 2); - return 451; + return 104; } $lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock'; if (file_exists($lockFile)) { Console::output('This environment is locked!', 1, 2); echo file_get_contents($lockFile); - return 20; + return 106; } // 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); } else { $result = true; - foreach ($hosts as $host) { + foreach ($hosts as $hostKey => $host) { + // Check if Host has specific configuration + $hostConfig = null; + if (is_array($host)) { + $hostConfig = $host; + $host = $hostKey; + } + + // Set Host and Host Specific Config $this->getConfig()->setHost($host); + $this->getConfig()->setHostConfig($hostConfig); $this->getConfig()->setReleaseId($releaseId); $task = Factory::get('releases/rollback', $this->getConfig()); diff --git a/Mage/Command/BuiltIn/UpdateCommand.php b/Mage/Command/BuiltIn/UpdateCommand.php index 9cba1cbb..14dc75a9 100644 --- a/Mage/Command/BuiltIn/UpdateCommand.php +++ b/Mage/Command/BuiltIn/UpdateCommand.php @@ -35,7 +35,7 @@ public function run() Console::output('Updating application via ' . $task->getName() . ' ... ', 1, 0); $result = $task->run(); - if ($result == true) { + if ($result === true) { Console::output('OK' . PHP_EOL, 0); $exitCode = 0; @@ -45,5 +45,4 @@ public function run() return $exitCode; } - -} \ No newline at end of file +} diff --git a/Mage/Command/BuiltIn/UpgradeCommand.php b/Mage/Command/BuiltIn/UpgradeCommand.php index 5de6a2d3..94fe6e05 100644 --- a/Mage/Command/BuiltIn/UpgradeCommand.php +++ b/Mage/Command/BuiltIn/UpgradeCommand.php @@ -38,8 +38,8 @@ class UpgradeCommand extends AbstractCommand */ public function run() { - $exitCode = 100; - Console::output('Upgrading Magallanes ... ', 1, 0); + $exitCode = 99; + Console::output('Upgrading Magallanes ... ', 1, 0); $user = ''; // Check if user is root @@ -49,7 +49,7 @@ public function run() if ($user != 'root' && $user != $owner) { Console::output('FAIL', 0, 1); - Console::output('You need to be the ' . $owner . ' user to perform the upgrade, or root.', 2); + Console::output('You need to be the ' . $owner . ' user to perform the upgrade, or root.', 2); } else { // Check version diff --git a/Mage/Command/BuiltIn/VersionCommand.php b/Mage/Command/BuiltIn/VersionCommand.php index ca591d7d..0c7938c0 100644 --- a/Mage/Command/BuiltIn/VersionCommand.php +++ b/Mage/Command/BuiltIn/VersionCommand.php @@ -26,9 +26,8 @@ class VersionCommand extends AbstractCommand */ public function run() { - Console::output('Running Magallanes version ' . MAGALLANES_VERSION . '', 0, 2); + Console::output('Running Magallanes version ' . MAGALLANES_VERSION . '', 0, 2); return 0; } - -} \ No newline at end of file +} diff --git a/Mage/Command/Factory.php b/Mage/Command/Factory.php index 0584046b..40882397 100644 --- a/Mage/Command/Factory.php +++ b/Mage/Command/Factory.php @@ -12,7 +12,6 @@ use Mage\Command\AbstractCommand; use Mage\Config; -use Mage\Autoload; use Exception; @@ -59,4 +58,4 @@ public static function get($commandName, Config $config) return $instance; } -} \ No newline at end of file +} diff --git a/Mage/Command/RequiresEnvironment.php b/Mage/Command/RequiresEnvironment.php index 5ee07710..9a0a56cb 100644 --- a/Mage/Command/RequiresEnvironment.php +++ b/Mage/Command/RequiresEnvironment.php @@ -17,4 +17,4 @@ */ interface RequiresEnvironment { -} \ No newline at end of file +} diff --git a/Mage/Config.php b/Mage/Config.php index 2991acd4..e839c22c 100644 --- a/Mage/Config.php +++ b/Mage/Config.php @@ -13,7 +13,6 @@ use Mage\Config\ConfigNotFoundException; use Mage\Config\RequiredConfigNotFoundException; use Mage\Console; -use Mage\Yaml\Exception\RuntimeException; use Mage\Yaml\Yaml; use Exception; @@ -121,29 +120,6 @@ 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 @@ -156,9 +132,6 @@ 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; } @@ -490,6 +463,11 @@ public function deployment($option, $default = false) } } + public function setSourceTemporal($directory) + { + $this->environmentConfig['deployment']['source']['temporal'] = $directory; + } + /** * Returns Releasing Options * diff --git a/Mage/Console.php b/Mage/Console.php index 11733546..b9dd97cd 100644 --- a/Mage/Console.php +++ b/Mage/Console.php @@ -26,7 +26,6 @@ class Console { /** - * TODO refactor into own static class * @var array */ public static $paramsNotRequiringEnvironment = array('install' => 'install', 'upgrade' => 'upgrade', 'version' => 'version'); @@ -116,11 +115,10 @@ public function run($arguments) } else { self::output('Starting Magallanes', 0, 1); self::log("Logging enabled"); - self::output('Logging enabled: ' . self::getLogFile() . '', 1, 2); + self::output('Logging enabled: ' . self::getLogFile() . '', 1, 2); } } - // Run Command - Check if there is a Configuration Error if ($configError !== false) { self::output('' . $configError . '', 1, 2); @@ -131,12 +129,21 @@ public function run($arguments) $command = Factory::get($commandName, $config); if ($command instanceOf RequiresEnvironment) { - if ($config->getEnvironment() == false) { + if ($config->getEnvironment() === false) { throw new Exception('You must specify an environment for this command.'); } } + + // Run the Command $exitCode = $command->run(); + // Check for errors + if (is_int($exitCode) && $exitCode !== 0) { + throw new Exception('Command execution failed with following exit code: ' . $exitCode, $exitCode); + } elseif (is_bool($exitCode) && !$exitCode) { + $exitCode = 1; + throw new Exception('Command execution failed.', $exitCode); + } } catch (Exception $exception) { self::output('' . $exception->getMessage() . '', 1, 2); } @@ -213,7 +220,7 @@ public static function executeCommand($command, &$output = null) public static function log($message) { if (self::$logEnabled) { - if (self::$log == null) { + if (self::$log === null) { self::$logFile = realpath(getcwd() . '/.mage/logs') . '/log-' . date('Ymd-His') . '.log'; self::$log = fopen(self::$logFile, 'w'); } diff --git a/Mage/Console/Colors.php b/Mage/Console/Colors.php index 84bfb622..5ab48bce 100644 --- a/Mage/Console/Colors.php +++ b/Mage/Console/Colors.php @@ -25,7 +25,7 @@ class Colors */ private static $foregroundColors = array( 'black' => '0;30', - 'dark_gray' => '1;30', + 'bold' => '1', 'blue' => '0;34', 'light_blue' => '1;34', 'green' => '0;32', diff --git a/Mage/Mailer.php b/Mage/Mailer.php index e0b3443e..f2fb33d5 100644 --- a/Mage/Mailer.php +++ b/Mage/Mailer.php @@ -84,6 +84,6 @@ public function send($result) . $attachment . self::EOL . '--Mage-mixed-' . $boundary . '--' . self::EOL; - @mail($this->address, $subject, $message, $headers); + mail($this->address, $subject, $message, $headers); } -} \ No newline at end of file +} diff --git a/Mage/Task/AbstractTask.php b/Mage/Task/AbstractTask.php index c61f87d8..663b387c 100644 --- a/Mage/Task/AbstractTask.php +++ b/Mage/Task/AbstractTask.php @@ -181,7 +181,7 @@ protected final function runCommandLocal($command, &$output = null) */ protected final function runCommandRemote($command, &$output = null, $cdToDirectoryFirst = true) { - if ($this->getConfig()->release('enabled', false) == true) { + if ($this->getConfig()->release('enabled', false) === true) { if ($this instanceOf IsReleaseAware) { $releasesDirectory = ''; @@ -223,7 +223,7 @@ protected final function runCommandRemote($command, &$output = null, $cdToDirect */ protected final function runCommand($command, &$output = null) { - if ($this->getStage() == self::STAGE_DEPLOY) { + if ($this->getStage() == self::STAGE_DEPLOY || $this->getStage() == self::STAGE_POST_RELEASE) { return $this->runCommandRemote($command, $output); } else { return $this->runCommandLocal($command, $output); @@ -238,7 +238,7 @@ protected final function runCommand($command, &$output = null) */ protected function getReleasesAwareCommand($command) { - if ($this->getConfig()->release('enabled', false) == true) { + if ($this->getConfig()->release('enabled', false) === true) { $releasesDirectory = $this->getConfig()->release('directory', 'releases'); $deployToDirectory = $releasesDirectory . '/' . $this->getConfig()->getReleaseId(); diff --git a/Mage/Task/BuiltIn/Deployment/ReleaseTask.php b/Mage/Task/BuiltIn/Deployment/ReleaseTask.php index bf42643c..84187ad5 100644 --- a/Mage/Task/BuiltIn/Deployment/ReleaseTask.php +++ b/Mage/Task/BuiltIn/Deployment/ReleaseTask.php @@ -37,7 +37,7 @@ public function getName() public function run() { $resultFetch = false; - if ($this->getConfig()->release('enabled', false) == true) { + if ($this->getConfig()->release('enabled', false) === true) { $releasesDirectory = $this->getConfig()->release('directory', 'releases'); $symlink = $this->getConfig()->release('symlink', 'current'); @@ -107,7 +107,7 @@ public function run() protected function cleanUpReleases() { // Count Releases - if ($this->getConfig()->release('enabled', false) == true) { + if ($this->getConfig()->release('enabled', false) === true) { $releasesDirectory = $this->getConfig()->release('directory', 'releases'); $symlink = $this->getConfig()->release('symlink', 'current'); diff --git a/Mage/Task/BuiltIn/Deployment/Strategy/BaseStrategyTaskAbstract.php b/Mage/Task/BuiltIn/Deployment/Strategy/BaseStrategyTaskAbstract.php index 5affe973..9e787c73 100644 --- a/Mage/Task/BuiltIn/Deployment/Strategy/BaseStrategyTaskAbstract.php +++ b/Mage/Task/BuiltIn/Deployment/Strategy/BaseStrategyTaskAbstract.php @@ -30,7 +30,7 @@ protected function checkOverrideRelease() $overrideRelease = $this->getParameter('overrideRelease', false); $symlink = $this->getConfig()->release('symlink', 'current'); - if ($overrideRelease == true) { + if ($overrideRelease === true) { $releaseToOverride = false; $resultFetch = $this->runCommandRemote('ls -ld ' . $symlink . ' | cut -d"/" -f2', $releaseToOverride); if ($resultFetch && is_numeric($releaseToOverride)) { diff --git a/Mage/Task/BuiltIn/Deployment/Strategy/DisabledTask.php b/Mage/Task/BuiltIn/Deployment/Strategy/DisabledTask.php index 958faef4..e3255b58 100644 --- a/Mage/Task/BuiltIn/Deployment/Strategy/DisabledTask.php +++ b/Mage/Task/BuiltIn/Deployment/Strategy/DisabledTask.php @@ -38,5 +38,4 @@ public function run() { throw new SkipException; } - -} \ No newline at end of file +} diff --git a/Mage/Task/BuiltIn/Deployment/Strategy/GitRebaseTask.php b/Mage/Task/BuiltIn/Deployment/Strategy/GitRebaseTask.php index a7e47740..b19e06e7 100644 --- a/Mage/Task/BuiltIn/Deployment/Strategy/GitRebaseTask.php +++ b/Mage/Task/BuiltIn/Deployment/Strategy/GitRebaseTask.php @@ -10,7 +10,6 @@ namespace Mage\Task\BuiltIn\Deployment\Strategy; -use Mage\Task\AbstractTask; use Mage\Task\Releases\IsReleaseAware; /** @@ -35,20 +34,6 @@ public function getName() */ public function run() { - $this->checkOverrideRelease(); - $excludes = $this->getExcludes(); - - // If we are working with releases - $deployToDirectory = $this->getConfig()->deployment('to'); - if ($this->getConfig()->release('enabled', false) == true) { - $releasesDirectory = $this->getConfig()->release('directory', 'releases'); - - $deployToDirectory = rtrim($this->getConfig()->deployment('to'), '/') - . '/' . $releasesDirectory - . '/' . $this->getConfig()->getReleaseId(); - $this->runCommandRemote('mkdir -p ' . $releasesDirectory . '/' . $this->getConfig()->getReleaseId()); - } - $branch = $this->getParameter('branch', 'master'); $remote = $this->getParameter('remote', 'origin'); diff --git a/Mage/Task/BuiltIn/Deployment/Strategy/GitRemoteCacheTask.php b/Mage/Task/BuiltIn/Deployment/Strategy/GitRemoteCacheTask.php index 43296c3a..3934e480 100644 --- a/Mage/Task/BuiltIn/Deployment/Strategy/GitRemoteCacheTask.php +++ b/Mage/Task/BuiltIn/Deployment/Strategy/GitRemoteCacheTask.php @@ -42,7 +42,7 @@ public function run() { $overrideRelease = $this->getParameter('overrideRelease', false); - if ($overrideRelease == true) { + if ($overrideRelease === true) { $releaseToOverride = false; $resultFetch = $this->runCommandRemote('ls -ld current | cut -d"/" -f2', $releaseToOverride); if ($resultFetch && is_numeric($releaseToOverride)) { @@ -63,7 +63,7 @@ public function run() $userExcludes = $this->getConfig()->deployment('excludes', array()); $deployToDirectory = $this->getConfig()->deployment('to'); - if ($this->getConfig()->release('enabled', false) == true) { + if ($this->getConfig()->release('enabled', false) === true) { $releasesDirectory = $this->getConfig()->release('directory', 'releases'); $deployToDirectory = rtrim($this->getConfig()->deployment('to'), '/') @@ -97,39 +97,8 @@ public function run() $command = 'cd ' . $remoteCacheFolder . ' && /usr/bin/env git archive ' . $branch . ' | tar -x -C ' . $deployToDirectory . ' ' . $excludeCmd; $result = $this->runCommandRemote($command) && $result; - // Count Releases - if ($this->getConfig()->release('enabled', false) == true) { - $releasesDirectory = $this->getConfig()->release('directory', 'releases'); - $symlink = $this->getConfig()->release('symlink', 'current'); - - if (substr($symlink, 0, 1) == '/') { - $releasesDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory; - } - - $maxReleases = $this->getConfig()->release('max', false); - if (($maxReleases !== false) && ($maxReleases > 0)) { - $releasesList = ''; - $countReleasesFetch = $this->runCommandRemote('ls -1 ' . $releasesDirectory, $releasesList); - $releasesList = trim($releasesList); - - if ($countReleasesFetch && $releasesList != '') { - $releasesList = explode(PHP_EOL, $releasesList); - if (count($releasesList) > $maxReleases) { - $releasesToDelete = array_diff($releasesList, array($this->getConfig()->getReleaseId())); - sort($releasesToDelete); - $releasesToDeleteCount = count($releasesToDelete) - $maxReleases; - $releasesToDelete = array_slice($releasesToDelete, 0, $releasesToDeleteCount + 1); - - foreach ($releasesToDelete as $releaseIdToDelete) { - $directoryToDelete = $releasesDirectory . '/' . $releaseIdToDelete; - if ($directoryToDelete != '/') { - $command = 'rm -rf ' . $directoryToDelete; - $result = $result && $this->runCommandRemote($command); - } - } - } - } - } + if ($result) { + $this->cleanUpReleases(); } return $result; diff --git a/Mage/Task/BuiltIn/Deployment/Strategy/RsyncTask.php b/Mage/Task/BuiltIn/Deployment/Strategy/RsyncTask.php index c4c92adf..3555ce03 100644 --- a/Mage/Task/BuiltIn/Deployment/Strategy/RsyncTask.php +++ b/Mage/Task/BuiltIn/Deployment/Strategy/RsyncTask.php @@ -27,8 +27,8 @@ class RsyncTask extends BaseStrategyTaskAbstract implements IsReleaseAware */ public function getName() { - if ($this->getConfig()->release('enabled', false) == true) { - if ($this->getConfig()->getParameter('overrideRelease', false) == true) { + if ($this->getConfig()->release('enabled', false) === true) { + if ($this->getConfig()->getParameter('overrideRelease', false) === true) { return 'Deploy via Rsync (with Releases override) [built-in]'; } else { $rsync_copy = $this->getConfig()->deployment("rsync"); @@ -52,10 +52,11 @@ public function run() $this->checkOverrideRelease(); $excludes = $this->getExcludes(); + $excludesListFilePath = $this->getConfig()->deployment('excludes_file', ''); // If we are working with releases $deployToDirectory = $this->getConfig()->deployment('to'); - if ($this->getConfig()->release('enabled', false) == true) { + if ($this->getConfig()->release('enabled', false) === true) { $releasesDirectory = $this->getConfig()->release('directory', 'releases'); $symlink = $this->getConfig()->release('symlink', 'current'); @@ -95,6 +96,7 @@ public function run() . $strategyFlags . ' ' . '--rsh="ssh ' . $this->getConfig()->getHostIdentityFileOption() . '-p' . $this->getConfig()->getHostPort() . '" ' . $this->excludes($excludes) . ' ' + . $this->excludesListFile($excludesListFilePath) . ' ' . $this->getConfig()->deployment('from') . ' ' . $this->getConfig()->deployment('user') . '@' . $this->getConfig()->getHostName() . ':' . $deployToDirectory; $result = $this->runCommandLocal($command); @@ -117,4 +119,18 @@ protected function excludes(Array $excludes) $excludesRsync = trim($excludesRsync); return $excludesRsync; } + + /** + * Generates the Exclude from file for rsync + * @param string $excludesFile + * @return string + */ + protected function excludesListFile($excludesFile) + { + $excludesListFileRsync = ''; + if(!empty($excludesFile) && file_exists($excludesFile) && is_file($excludesFile) && is_readable($excludesFile)) { + $excludesListFileRsync = ' --exclude-from=' . $excludesFile; + } + return $excludesListFileRsync; + } } diff --git a/Mage/Task/BuiltIn/Deployment/Strategy/TarGzTask.php b/Mage/Task/BuiltIn/Deployment/Strategy/TarGzTask.php index 2077eb7a..d45ea17a 100644 --- a/Mage/Task/BuiltIn/Deployment/Strategy/TarGzTask.php +++ b/Mage/Task/BuiltIn/Deployment/Strategy/TarGzTask.php @@ -10,7 +10,6 @@ namespace Mage\Task\BuiltIn\Deployment\Strategy; -use Mage\Console; use Mage\Task\BuiltIn\Deployment\Strategy\BaseStrategyTaskAbstract; use Mage\Task\Releases\IsReleaseAware; @@ -27,8 +26,8 @@ class TarGzTask extends BaseStrategyTaskAbstract implements IsReleaseAware */ public function getName() { - if ($this->getConfig()->release('enabled', false) == true) { - if ($this->getConfig()->getParameter('overrideRelease', false) == true) { + if ($this->getConfig()->release('enabled', false) === true) { + if ($this->getConfig()->getParameter('overrideRelease', false) === true) { return 'Deploy via TarGz (with Releases override) [built-in]'; } else { return 'Deploy via TarGz (with Releases) [built-in]'; @@ -47,10 +46,11 @@ public function run() $this->checkOverrideRelease(); $excludes = $this->getExcludes(); + $excludesListFilePath = $this->getConfig()->deployment('excludes_file', '');; // If we are working with releases $deployToDirectory = $this->getConfig()->deployment('to'); - if ($this->getConfig()->release('enabled', false) == true) { + if ($this->getConfig()->release('enabled', false) === true) { $releasesDirectory = $this->getConfig()->release('directory', 'releases'); $deployToDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory @@ -67,6 +67,8 @@ public function run() $excludeCmd .= ' --exclude=' . $excludeFile; } + $excludeFromFileCmd = $this->excludesListFile($excludesListFilePath); + // Strategy Flags $strategyFlags = $this->getConfig()->deployment('strategy_flags', $this->getConfig()->general('strategy_flags', array())); if (isset($strategyFlags['targz']) && isset($strategyFlags['targz']['create'])) { @@ -75,7 +77,7 @@ public function run() $strategyFlags = ''; } - $command = 'tar cfzh' . $strategyFlags . ' ' . $localTarGz . '.tar.gz ' . $excludeCmd . ' -C ' . $this->getConfig()->deployment('from') . ' .'; + $command = 'tar cfzh' . $strategyFlags . ' ' . $localTarGz . '.tar.gz ' . $excludeCmd . $excludeFromFileCmd . ' -C ' . $this->getConfig()->deployment('from') . ' .'; $result = $this->runCommandLocal($command); // Strategy Flags @@ -113,4 +115,18 @@ public function run() return $result; } + + /** + * Generates the Exclude from file for rsync + * @param string $excludesFile + * @return string + */ + protected function excludesListFile($excludesFile) + { + $excludesListFileRsync = ''; + if(!empty($excludesFile) && file_exists($excludesFile) && is_file($excludesFile) && is_readable($excludesFile)) { + $excludesListFileRsync = ' --exclude-from=' . $excludesFile; + } + return $excludesListFileRsync; + } } diff --git a/Mage/Task/BuiltIn/Filesystem/LinkSharedFilesTask.php b/Mage/Task/BuiltIn/Filesystem/LinkSharedFilesTask.php index 86dc6459..4ed89a18 100644 --- a/Mage/Task/BuiltIn/Filesystem/LinkSharedFilesTask.php +++ b/Mage/Task/BuiltIn/Filesystem/LinkSharedFilesTask.php @@ -8,6 +8,16 @@ class LinkSharedFilesTask extends AbstractTask implements IsReleaseAware { + const LINKED_FOLDERS = 'linked_folders'; + const LINKED_STRATEGY = 'linking_strategy'; + + const ABSOLUTE_LINKING = 'absolute'; + const RELATIVE_LINKING = 'relative'; + + public $linkingStrategies = array( + self::ABSOLUTE_LINKING, + self::RELATIVE_LINKING + ); /** * Returns the Title of the Task * @return string @@ -25,28 +35,41 @@ public function getName() */ public function run() { - $linkedFiles = $this->getParameter('linked_files', []); - $linkedFolders = $this->getParameter('linked_folders', []); + $linkedFiles = $this->getParameter('linked_files', []); + $linkedFolders = $this->getParameter(self::LINKED_FOLDERS, []); + $linkingStrategy = $this->getParameter(self::LINKED_STRATEGY, self::ABSOLUTE_LINKING); + + $linkedEntities = array_merge($linkedFiles,$linkedFolders); + if (sizeof($linkedFiles) == 0 && sizeof($linkedFolders) == 0) { throw new SkipException('No files and folders configured for sym-linking.'); } $sharedFolderName = $this->getParameter('shared', 'shared'); - $sharedFolderName = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $sharedFolderName; + $sharedFolderPath = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $sharedFolderName; $releasesDirectory = $this->getConfig()->release('directory', 'releases'); - $releasesDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory; + $releasesDirectoryPath = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory; - $currentCopy = $releasesDirectory . '/' . $this->getConfig()->getReleaseId(); - foreach ($linkedFolders as $folder) { - $command = "ln -nfs $sharedFolderName/$folder $currentCopy/$folder"; - $this->runCommandRemote($command); - } + $currentCopy = $releasesDirectoryPath . '/' . $this->getConfig()->getReleaseId(); + $relativeDiffPath = str_replace($this->getConfig()->deployment('to'),'',$currentCopy) . '/'; - foreach ($linkedFiles as $folder) { - $command = "ln -nfs $sharedFolderName/$folder $currentCopy/$folder"; + foreach ($linkedEntities as $ePath) { + if(is_array($ePath) && in_array($strategy = reset($ePath), $this->linkingStrategies ) ) { + $entityPath = key($ePath); + } else { + $strategy = $linkingStrategy; + $entityPath = $ePath; + } + $sharedEntityLinkedPath = "$sharedFolderPath/$entityPath"; + if($strategy==self::RELATIVE_LINKING) { + $parentFolderPath = dirname($entityPath); + $relativePath = $parentFolderPath=='.'?$relativeDiffPath:$relativeDiffPath.$parentFolderPath.'/'; + $sharedEntityLinkedPath = ltrim(preg_replace('/(\w+\/)/', '../', $relativePath),'/').$sharedFolderName .'/'. $entityPath; + } + $command = "ln -nfs $sharedEntityLinkedPath $currentCopy/$entityPath"; $this->runCommandRemote($command); } return true; } -} +} \ No newline at end of file diff --git a/Mage/Task/BuiltIn/Magento/ClearCacheTask.php b/Mage/Task/BuiltIn/Magento/ClearCacheTask.php index 8a8cf551..7db77809 100644 --- a/Mage/Task/BuiltIn/Magento/ClearCacheTask.php +++ b/Mage/Task/BuiltIn/Magento/ClearCacheTask.php @@ -39,4 +39,4 @@ public function run() return $result; } -} \ No newline at end of file +} diff --git a/Mage/Task/BuiltIn/Magento/ClearFullPageCacheTask.php b/Mage/Task/BuiltIn/Magento/ClearFullPageCacheTask.php index 8dd0b498..17c3d66f 100644 --- a/Mage/Task/BuiltIn/Magento/ClearFullPageCacheTask.php +++ b/Mage/Task/BuiltIn/Magento/ClearFullPageCacheTask.php @@ -39,4 +39,4 @@ public function run() return $result; } -} \ No newline at end of file +} diff --git a/Mage/Task/BuiltIn/Releases/ListTask.php b/Mage/Task/BuiltIn/Releases/ListTask.php index ef88004b..ffbd2c48 100644 --- a/Mage/Task/BuiltIn/Releases/ListTask.php +++ b/Mage/Task/BuiltIn/Releases/ListTask.php @@ -34,11 +34,11 @@ public function getName() */ public function run() { - if ($this->getConfig()->release('enabled', false) == true) { + if ($this->getConfig()->release('enabled', false) === true) { $releasesDirectory = $this->getConfig()->release('directory', 'releases'); $symlink = $this->getConfig()->release('symlink', 'current'); - Console::output('Releases available on ' . $this->getConfig()->getHost() . ''); + Console::output('Releases available on ' . $this->getConfig()->getHost() . ''); // Get Releases $output = ''; @@ -51,7 +51,7 @@ public function run() $currentRelease = trim(array_pop($currentRelease)); if (count($releases) == 0) { - Console::output('No releases available ... ', 2); + Console::output('No releases available ... ', 2); } else { rsort($releases); $releases = array_slice($releases, 0, 10); @@ -80,8 +80,8 @@ public function run() Console::output( 'Release: ' . $release . ' ' - . '- Date: ' . $releaseDate . ' ' - . '- Index: ' . $releaseIndex . '' . $dateDiff . $isCurrent, 2); + . '- Date: ' . $releaseDate . ' ' + . '- Index: ' . $releaseIndex . '' . $dateDiff . $isCurrent, 2); } } @@ -146,5 +146,4 @@ protected function dateDiff($releaseDate) return $textDiff; } - -} \ No newline at end of file +} diff --git a/Mage/Task/BuiltIn/Releases/RollbackTask.php b/Mage/Task/BuiltIn/Releases/RollbackTask.php index 759c0428..7a437fda 100644 --- a/Mage/Task/BuiltIn/Releases/RollbackTask.php +++ b/Mage/Task/BuiltIn/Releases/RollbackTask.php @@ -47,7 +47,7 @@ public function getReleaseId() */ public function run() { - if ($this->getConfig()->release('enabled', false) == true) { + if ($this->getConfig()->release('enabled', false) === true) { $releasesDirectory = $this->getConfig()->release('directory', 'releases'); $symlink = $this->getConfig()->release('symlink', 'current'); @@ -56,7 +56,7 @@ public function run() $releases = ($output == '') ? array() : explode(PHP_EOL, $output); if (count($releases) == 0) { - Console::output('Release are not available for ' . $this->getConfig()->getHost() . ' ... FAIL'); + Console::output('Release are not available for ' . $this->getConfig()->getHost() . ' ... FAIL'); } else { rsort($releases); @@ -81,10 +81,10 @@ public function run() } if (!$releaseIsAvailable) { - Console::output('Release ' . $this->getReleaseId() . ' is invalid or unavailable for ' . $this->getConfig()->getHost() . ' ... FAIL'); + Console::output('Release ' . $this->getReleaseId() . ' is invalid or unavailable for ' . $this->getConfig()->getHost() . ' ... FAIL'); } else { - Console::output('Rollback release on ' . $this->getConfig()->getHost() . ''); + Console::output('Rollback release on ' . $this->getConfig()->getHost() . ''); $rollbackTo = $releasesDirectory . '/' . $releaseId; // Get Current Release @@ -111,7 +111,7 @@ public function run() $tasks++; $result = $task->run(); - if ($result == true) { + if ($result === true) { Console::output('OK', 0); $completedTasks++; } else { @@ -161,7 +161,7 @@ public function run() $tasks++; $result = $task->run(); - if ($result == true) { + if ($result === true) { Console::output('OK', 0); $completedTasks++; } else { @@ -178,7 +178,7 @@ public function run() $tasksColor = 'red'; } - Console::output('Release rollback on ' . $this->getConfig()->getHost() . ' compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.', 1, 3); + Console::output('Release rollback on ' . $this->getConfig()->getHost() . ' compted: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . ' tasks done.', 1, 3); } } @@ -188,5 +188,4 @@ public function run() return false; } } - -} \ No newline at end of file +} diff --git a/Mage/Task/BuiltIn/Scm/ChangeBranchTask.php b/Mage/Task/BuiltIn/Scm/ChangeBranchTask.php index dfb61271..8c5e33fd 100644 --- a/Mage/Task/BuiltIn/Scm/ChangeBranchTask.php +++ b/Mage/Task/BuiltIn/Scm/ChangeBranchTask.php @@ -105,4 +105,4 @@ public function run() return $result; } -} \ No newline at end of file +} diff --git a/Mage/Task/BuiltIn/Scm/CloneTask.php b/Mage/Task/BuiltIn/Scm/CloneTask.php index c5c1170e..54021179 100644 --- a/Mage/Task/BuiltIn/Scm/CloneTask.php +++ b/Mage/Task/BuiltIn/Scm/CloneTask.php @@ -53,6 +53,15 @@ public function init() $this->name = 'SCM Clone (GIT) [built-in]'; break; } + + // Create temporal directory for clone + if (is_array($this->source)) { + if (trim($this->source['temporal']) == '') { + $this->source['temporal'] = sys_get_temp_dir(); + } + $this->source['temporal'] = rtrim($this->source['temporal'], '/') . '/' . md5(microtime()) . '/'; + $this->getConfig()->setSourceTemporal($this->source['temporal']); + } } /** diff --git a/Mage/Task/BuiltIn/Scm/ForceUpdateTask.php b/Mage/Task/BuiltIn/Scm/ForceUpdateTask.php new file mode 100644 index 00000000..9d68e096 --- /dev/null +++ b/Mage/Task/BuiltIn/Scm/ForceUpdateTask.php @@ -0,0 +1,87 @@ + +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code. +*/ + +namespace Mage\Task\BuiltIn\Scm; + +use Mage\Task\AbstractTask; +use Mage\Task\SkipException; + +/** + * Task for Force Updating a Working Copy + * + * 'git fetch' downloads the latest from remote without trying to merge or rebase anything. + * 'git reset' resets the master branch to what you just fetched. + * The '--hard' option changes all the files in your working tree to match the files in origin/master, + * so if you have any local changes, they will be lost. + * + * @author Samuel Chiriluta + */ +class ForceUpdateTask extends AbstractTask +{ + /** + * Name of the Task + * @var string + */ + private $name = 'SCM Force Update [built-in]'; + + /** + * (non-PHPdoc) + * @see \Mage\Task\AbstractTask::getName() + */ + public function getName() + { + return $this->name; + } + + /** + * (non-PHPdoc) + * @see \Mage\Task\AbstractTask::init() + */ + public function init() + { + switch ($this->getConfig()->general('scm')) { + case 'git': + $this->name = 'SCM Force Update (GIT) [built-in]'; + break; + } + } + + /** + * Force Updates the Working Copy + * @see \Mage\Task\AbstractTask::run() + */ + public function run() + { + switch ($this->getConfig()->general('scm')) { + case 'git': + $branch = $this->getParameter('branch', 'master'); + $remote = $this->getParameter('remote', 'origin'); + + $command = 'git fetch ' . $remote . ' ' . $branch; + $result = $this->runCommandRemote($command); + + $command = 'git reset --hard ' . $remote . '/' . $branch; + $result = $result && $this->runCommandRemote($command); + + $command = 'git pull ' . $remote . ' ' . $branch; + $result = $result && $this->runCommandRemote($command); + break; + + default: + throw new SkipException; + break; + } + + $result = $this->runCommandLocal($command); + $this->getConfig()->reload(); + + return $result; + } +} diff --git a/Mage/Task/BuiltIn/Scm/RemoveCloneTask.php b/Mage/Task/BuiltIn/Scm/RemoveCloneTask.php index 5084622c..e0c102d7 100644 --- a/Mage/Task/BuiltIn/Scm/RemoveCloneTask.php +++ b/Mage/Task/BuiltIn/Scm/RemoveCloneTask.php @@ -58,4 +58,4 @@ public function run() { return $this->runCommandLocal('rm -rf ' . $this->source['temporal']); } -} \ No newline at end of file +} diff --git a/Mage/Task/BuiltIn/Scm/UpdateTask.php b/Mage/Task/BuiltIn/Scm/UpdateTask.php index e883a2c1..22c496e4 100644 --- a/Mage/Task/BuiltIn/Scm/UpdateTask.php +++ b/Mage/Task/BuiltIn/Scm/UpdateTask.php @@ -69,4 +69,4 @@ public function run() return $result; } -} \ No newline at end of file +} diff --git a/Mage/Task/BuiltIn/Symfony2/AsseticDumpTask.php b/Mage/Task/BuiltIn/Symfony2/AsseticDumpTask.php index 7b086606..7b1c69c8 100644 --- a/Mage/Task/BuiltIn/Symfony2/AsseticDumpTask.php +++ b/Mage/Task/BuiltIn/Symfony2/AsseticDumpTask.php @@ -42,4 +42,4 @@ public function run() return $result; } -} \ No newline at end of file +} diff --git a/Mage/Task/BuiltIn/Symfony2/AssetsInstallTask.php b/Mage/Task/BuiltIn/Symfony2/AssetsInstallTask.php index c53292c5..f7ca9811 100644 --- a/Mage/Task/BuiltIn/Symfony2/AssetsInstallTask.php +++ b/Mage/Task/BuiltIn/Symfony2/AssetsInstallTask.php @@ -49,4 +49,4 @@ public function run() return $result; } -} \ No newline at end of file +} diff --git a/Mage/Task/BuiltIn/Symfony2/CacheClearTask.php b/Mage/Task/BuiltIn/Symfony2/CacheClearTask.php index fff5c24d..44acc465 100644 --- a/Mage/Task/BuiltIn/Symfony2/CacheClearTask.php +++ b/Mage/Task/BuiltIn/Symfony2/CacheClearTask.php @@ -42,4 +42,4 @@ public function run() return $result; } -} \ No newline at end of file +} diff --git a/Mage/Task/BuiltIn/Symfony2/DoctrineMigrate.php b/Mage/Task/BuiltIn/Symfony2/DoctrineMigrate.php new file mode 100644 index 00000000..b76633d6 --- /dev/null +++ b/Mage/Task/BuiltIn/Symfony2/DoctrineMigrate.php @@ -0,0 +1,40 @@ + +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code. +*/ + +namespace Mage\Task\BuiltIn\Symfony2; + +use Mage\Task\BuiltIn\Symfony2\SymfonyAbstractTask; + +/** + * Task for Doctrine migrations + */ +class DoctrineMigrate extends SymfonyAbstractTask +{ + /** + * (non-PHPdoc) + * @see \Mage\Task\AbstractTask::getName() + */ + public function getName() + { + return 'Symfony v2 - Migrate doctrine entities [built-in]'; + } + + /** + * Migrates Doctrine entities + * + * @see \Mage\Task\AbstractTask::run() + */ + public function run() + { + $env = $this->getParameter('env', 'dev'); + $command = $this->getAppPath() . ' doctrine:migrations:migrate -n --env=' . $env; + return $this->runCommand($command); + } +} diff --git a/Mage/Task/BuiltIn/Symfony2/SymfonyAbstractTask.php b/Mage/Task/BuiltIn/Symfony2/SymfonyAbstractTask.php index 4fae2e21..7ac246cd 100644 --- a/Mage/Task/BuiltIn/Symfony2/SymfonyAbstractTask.php +++ b/Mage/Task/BuiltIn/Symfony2/SymfonyAbstractTask.php @@ -29,4 +29,4 @@ protected function getAppPath() return $this->getConfig()->general('symfony_app_path', $defaultAppPath); } -} \ No newline at end of file +} diff --git a/Mage/Task/Factory.php b/Mage/Task/Factory.php index fce60505..912026f9 100644 --- a/Mage/Task/Factory.php +++ b/Mage/Task/Factory.php @@ -11,7 +11,6 @@ namespace Mage\Task; use Mage\Config; -use Mage\Autoload; use Exception; diff --git a/Mage/Task/RollbackException.php b/Mage/Task/RollbackException.php new file mode 100644 index 00000000..d2e56df5 --- /dev/null +++ b/Mage/Task/RollbackException.php @@ -0,0 +1,23 @@ + +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code. +*/ + +namespace Mage\Task; + +use Exception; + +/** + * Exception that indicates that the Task was Failed and rollback needed + * + * @author Alex V Kotelnikov + */ +class RollbackException extends Exception +{ + +} \ No newline at end of file diff --git a/Mage/Task/SkipException.php b/Mage/Task/SkipException.php index e26684a9..66bf4a81 100644 --- a/Mage/Task/SkipException.php +++ b/Mage/Task/SkipException.php @@ -19,4 +19,4 @@ */ class SkipException extends Exception { -} \ No newline at end of file +} diff --git a/Mage/Yaml/Dumper.php b/Mage/Yaml/Dumper.php index 50905135..1a594df8 100644 --- a/Mage/Yaml/Dumper.php +++ b/Mage/Yaml/Dumper.php @@ -72,4 +72,4 @@ public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = return $output; } -} \ No newline at end of file +} diff --git a/Mage/Yaml/Escaper.php b/Mage/Yaml/Escaper.php index 021b7eab..ad75520f 100644 --- a/Mage/Yaml/Escaper.php +++ b/Mage/Yaml/Escaper.php @@ -86,4 +86,4 @@ public static function escapeWithSingleQuotes($value) { return sprintf("'%s'", str_replace('\'', '\'\'', $value)); } -} \ No newline at end of file +} diff --git a/Mage/Yaml/Exception/DumpException.php b/Mage/Yaml/Exception/DumpException.php index bc3581b3..20ac47c3 100644 --- a/Mage/Yaml/Exception/DumpException.php +++ b/Mage/Yaml/Exception/DumpException.php @@ -22,4 +22,4 @@ */ class DumpException extends RuntimeException { -} \ No newline at end of file +} diff --git a/Mage/Yaml/Exception/ExceptionInterface.php b/Mage/Yaml/Exception/ExceptionInterface.php index fa120007..cc550423 100644 --- a/Mage/Yaml/Exception/ExceptionInterface.php +++ b/Mage/Yaml/Exception/ExceptionInterface.php @@ -20,4 +20,4 @@ */ interface ExceptionInterface { -} \ No newline at end of file +} diff --git a/Mage/Yaml/Exception/ParseException.php b/Mage/Yaml/Exception/ParseException.php index 3c4465b0..6064ce45 100644 --- a/Mage/Yaml/Exception/ParseException.php +++ b/Mage/Yaml/Exception/ParseException.php @@ -147,4 +147,4 @@ private function updateRepr() $this->message .= '.'; } } -} \ No newline at end of file +} diff --git a/Mage/Yaml/Exception/RuntimeException.php b/Mage/Yaml/Exception/RuntimeException.php index 51245bd2..5bbe8a32 100644 --- a/Mage/Yaml/Exception/RuntimeException.php +++ b/Mage/Yaml/Exception/RuntimeException.php @@ -22,4 +22,4 @@ */ class RuntimeException extends \RuntimeException implements ExceptionInterface { -} \ No newline at end of file +} diff --git a/Mage/Yaml/Inline.php b/Mage/Yaml/Inline.php index 2586ddeb..0341f787 100644 --- a/Mage/Yaml/Inline.php +++ b/Mage/Yaml/Inline.php @@ -502,4 +502,4 @@ private static function getTimestampRegex() $~x EOF; } -} \ No newline at end of file +} diff --git a/Mage/Yaml/Parser.php b/Mage/Yaml/Parser.php index fa7007d1..4237dc59 100644 --- a/Mage/Yaml/Parser.php +++ b/Mage/Yaml/Parser.php @@ -655,5 +655,4 @@ private function isStringUnIndentedCollectionItem() { return (0 === strpos($this->currentLine, '- ')); } - -} \ No newline at end of file +} diff --git a/Mage/Yaml/Unescaper.php b/Mage/Yaml/Unescaper.php index fc3bbcf3..e7a25b3e 100644 --- a/Mage/Yaml/Unescaper.php +++ b/Mage/Yaml/Unescaper.php @@ -139,4 +139,4 @@ private static function utf8chr($c) return chr(0xF0 | $c >> 18) . chr(0x80 | $c >> 12 & 0x3F) . chr(0x80 | $c >> 6 & 0x3F) . chr(0x80 | $c & 0x3F); } -} \ No newline at end of file +} diff --git a/Mage/Yaml/Yaml.php b/Mage/Yaml/Yaml.php index a476c602..3845c807 100644 --- a/Mage/Yaml/Yaml.php +++ b/Mage/Yaml/Yaml.php @@ -99,4 +99,4 @@ public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvali return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport); } -} \ No newline at end of file +} diff --git a/bin/mage b/bin/mage index b668d45a..728e3aaf 100755 --- a/bin/mage +++ b/bin/mage @@ -13,7 +13,7 @@ date_default_timezone_set('UTC'); $baseDir = dirname(dirname(__FILE__)); -define('MAGALLANES_VERSION', '1.0.2'); +define('MAGALLANES_VERSION', '1.0.3'); define('MAGALLANES_DIRECTORY', $baseDir); if (file_exists(__DIR__ . '/../vendor/autoload.php')) { @@ -32,4 +32,4 @@ array_shift($argv); $console = new Mage\Console; $exitCode = $console->run($argv); -exit($exitCode); +exit((integer) $exitCode); diff --git a/box.json b/box.json new file mode 100644 index 00000000..cc32edb8 --- /dev/null +++ b/box.json @@ -0,0 +1,16 @@ +{ + "files": ["LICENSE"], + "finder": [ + { + "name": "*.php", + "exclude": [ + "docs" + ], + "in": "Mage" + } + ], + "git-version": "git_tag", + "main": "bin/mage", + "output": "mage.phar", + "stub": true +} diff --git a/docs/example-config/.mage/config/environment/ioncube.yml b/docs/example-config/.mage/config/environment/ioncube.yml.txt similarity index 92% rename from docs/example-config/.mage/config/environment/ioncube.yml rename to docs/example-config/.mage/config/environment/ioncube.yml.txt index da001d87..9a6037be 100644 --- a/docs/example-config/.mage/config/environment/ioncube.yml +++ b/docs/example-config/.mage/config/environment/ioncube.yml.txt @@ -10,11 +10,11 @@ deployment: ioncube: test releases: - enabled: true - symlink: current - directory: releases + enabled: true + symlink: current + directory: releases hosts: - - localhost + - localhost tasks: pre-deploy: - ioncube/encrypt @@ -27,9 +27,8 @@ ioncube: encoder: ioncube_encoder54 checkencoding: true checkignorepaths: - -/public/js/* - -/public/css/* - + - /public/js/* + - /public/css/* projfile: project.prj project: replace-target: @@ -57,7 +56,6 @@ ioncube: - 'Comment 2' - "(c) ACTweb 2013" - "Draft Version" - loader-event: - corrupt-file=Corupted files - expired-file=System needs updated diff --git a/docs/example-config/.mage/config/environment/production.yml b/docs/example-config/.mage/config/environment/production.yml index e785fe0d..5122d7d6 100644 --- a/docs/example-config/.mage/config/environment/production.yml +++ b/docs/example-config/.mage/config/environment/production.yml @@ -2,11 +2,6 @@ deployment: user: root from: ./ -# source: -# type: git -# repository: git://github.com/andres-montanez/Magallanes.git -# from: master -# temporal: /tmp/myAppClone to: /var/www/vhosts/example.com/www excludes: - application/data/cache/twig/* @@ -16,25 +11,13 @@ releases: symlink: current directory: releases hosts: - s01.example.com:22: - deployment: - user: nobody - s02.example.com: - deployment: - user: toor - to: /home/web/public - releases: - max: 10 - tasks: - on-deploy: - - privileges - - s03.example.com + - s01.example.com + - s02.example.com tasks: pre-deploy: - scm/update on-deploy: -# - symfony2/cache-warmup: {env: prod} + - symfony2/cache-warmup: { env: prod } - privileges - sampleTask - sampleTaskRollbackAware - #post-deploy: \ No newline at end of file diff --git a/docs/example-config/.mage/config/environment/production.yml.advanced.txt b/docs/example-config/.mage/config/environment/production.yml.advanced.txt new file mode 100644 index 00000000..538e5172 --- /dev/null +++ b/docs/example-config/.mage/config/environment/production.yml.advanced.txt @@ -0,0 +1,38 @@ +#production +deployment: + user: root + from: ./ +# source: +# type: git +# repository: git://github.com/andres-montanez/Magallanes.git +# from: master +# temporal: /tmp/myAppClone + to: /var/www/vhosts/example.com/www + excludes: + - application/data/cache/twig/* +releases: + enabled: true + max: 5 + symlink: current + directory: releases +hosts: + - s01.example.com + - s02.example.com +# s02.example.com: +# deployment: +# user: toor +# to: /home/web/public +# releases: +# max: 10 +# tasks: +# on-deploy: +# - privileges +tasks: + pre-deploy: + - scm/update + on-deploy: + - symfony2/cache-warmup: { env: prod } + - privileges + - sampleTask + - sampleTaskRollbackAware + #post-deploy: diff --git a/docs/example-config/.mage/config/general.yml b/docs/example-config/.mage/config/general.yml index f601999b..3834ff31 100644 --- a/docs/example-config/.mage/config/general.yml +++ b/docs/example-config/.mage/config/general.yml @@ -5,4 +5,4 @@ notifications: true logging: true scm: type: git - url: git://github.com/andres-montanez/Zend-Framework-Twig-example-app.git \ No newline at end of file + url: git://github.com/andres-montanez/Zend-Framework-Twig-example-app.git diff --git a/docs/example-config/.mage/logs/.gitignore b/docs/example-config/.mage/logs/.gitignore index 6976a484..5592679c 100644 --- a/docs/example-config/.mage/logs/.gitignore +++ b/docs/example-config/.mage/logs/.gitignore @@ -1 +1 @@ -log-* \ No newline at end of file +log-* diff --git a/docs/example-config/.mage/tasks/FailTask.php b/docs/example-config/.mage/tasks/FailTask.php index 91e9c529..641e8878 100644 --- a/docs/example-config/.mage/tasks/FailTask.php +++ b/docs/example-config/.mage/tasks/FailTask.php @@ -14,4 +14,4 @@ public function run() { return false; } -} \ No newline at end of file +} diff --git a/docs/example-config/.mage/tasks/Privileges.php b/docs/example-config/.mage/tasks/Privileges.php index f5d88bde..1092533c 100644 --- a/docs/example-config/.mage/tasks/Privileges.php +++ b/docs/example-config/.mage/tasks/Privileges.php @@ -17,4 +17,4 @@ public function run() return $result; } -} \ No newline at end of file +} diff --git a/docs/example-config/.mage/tasks/SampleTask.php b/docs/example-config/.mage/tasks/SampleTask.php index fc9f9a1b..380a4ecf 100644 --- a/docs/example-config/.mage/tasks/SampleTask.php +++ b/docs/example-config/.mage/tasks/SampleTask.php @@ -14,4 +14,4 @@ public function run() { return true; } -} \ No newline at end of file +} diff --git a/docs/example-config/.mage/tasks/SampleTaskRollbackAware.php b/docs/example-config/.mage/tasks/SampleTaskRollbackAware.php index ef02d131..d9213f4d 100644 --- a/docs/example-config/.mage/tasks/SampleTaskRollbackAware.php +++ b/docs/example-config/.mage/tasks/SampleTaskRollbackAware.php @@ -19,4 +19,5 @@ public function run() { return true; } -} \ No newline at end of file +} + diff --git a/docs/example-config/.mage/tasks/TaskWithParameters.php b/docs/example-config/.mage/tasks/TaskWithParameters.php index bf99deb7..003ffef1 100644 --- a/docs/example-config/.mage/tasks/TaskWithParameters.php +++ b/docs/example-config/.mage/tasks/TaskWithParameters.php @@ -18,8 +18,10 @@ public function getName() public function run() { - //throw new Mage_Task_SkipException; - //return false; - return true; + if ($this->getParameter('booleanOption', false)) { + return true; + } else { + return false; + } } -} \ No newline at end of file +}