diff --git a/.gitignore b/.gitignore index 66f4d84..b5538e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ vendor mage.phar +bin +!bin/mage # OS generated files # // GitHub Recommendation ###################### @@ -7,4 +9,7 @@ mage.phar ehthumbs.db Icon? Thumbs.db -nbproject \ No newline at end of file + +# IDE generated files +.idea +nbproject diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..7af6191 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,123 @@ +Contributor guidelines for Magallanes +===================================== +Welcome to Magallanes! We are much appreciated you've decided to contribute to this project! +Please read the following guidelines to make your and our work easier and cleaner. + +**TL;DR** + +1. Write clean code with no mess left +2. Contribute the docs when adding configurable new feature +3. Create your pull request from `develop` branch +4. Ensure your code is fully covered by tests + + +---------- + +# Reporting issues +If you have a problem or you've noticed some bug, please feel free to open new issue. However please follow the rules below: +* First, make sure that similar/the same issue doesn't already exist +* If you've already found the solution of the problem you are about to report, please feel free to open a new pull request. Then follow the rules below in **Developing Magallanes** section. +* If you are able to, include some test cases or steps to reproduce the bug for us to examine the problem to reach the problem origin. + +## Opening pull requests +Pull Request are actually some kind of issues with code, so please follow the rules above in **Reporting issues** section before making the pull requests. +Our code isn't so beautiful, tested and testable as we would like it to be but if you're pushing your code please be sure it meets the requirements from **Organization and code quality** chapter. We want to improve the existing code to facilitate extending it and making fixes quicker. So if you are editing some class and you find it ugly, please do not ignore it. Following [The Boy Scout Rule](http://www.informit.com/articles/article.aspx?p=1235624&seqNum=6) - *Leave the campground cleaner than you found it* - we all can improve the existing code. +Keep your git commits as atomic as possible. It brings better history description only by commit messages and allow us to eventually revert the single commits with no affects. Your commit messages should be also descriptive. The first line of commit should be short, try to limit it up to 50 characters. The messages should be written imperatively, like following: +``` +Add MyCustomTask +``` +If you need to write more about your tasks, please enter the description in the next lines. There you can write whatever you want, like why you made this commit and what it consists of. +``` +Add MyCustomTask + +This task has very important role for the project. I found this very useful for all developers. I think the deploy with it should be a lot easier. +``` + +Optionally you can tag your messages in square brackets. It can be issue number or simple flag. Examples: +``` +[#183] Add new CONTRIBUTING document +[FIX] Set correct permissions on deploy stage +[FEATURE] Create new PermissionsTask +``` +Remember of square brackets when adding issue number. If you'd forget adding them, your whole message will be a comment! + +## Contributing the documentation +Magallanes is made to deploy your application quick and with no need to write redudant code. Usage is as simple as writing the configuration for target project in YAML files. In the nearest future we would like to make some Wiki with all available options, tasks and commands. For now, the only "documentation" are example files in `docs` directory. If the code you are going to include in your pull requests adds or changes config options, please make sure that you create a new sample in those files. You should also do the same with commands. + +# Developing Magallanes +## Branches +The flow is pretty simple. +In most common cases we work on `develop` branch. It's a branch with the newest changes which sometimes need more testing. All pull requests are opened to be merged into that branch. That keeps us safe to not deploy unsafe code into production - `master` branch. When we decide that every changeset in `develop` is tested manually and works as it's intented, we merge it to master. +If the change you commited is pretty hot and needs to be released ASAP, you are allowed to make a pull request to `master` branch. But it's the only case, please try to avoid it. All pull request that are not made on `develop` will be rejected. +If you want to use develop branch in your code, simple pass `dev-develop` to dependency version in your `composer.json` file: +```json +{ + "require": { + "andres-montanez/magallanes": "dev-develop" + } +} +``` +## Organization and code quality +We use [PSR2](http://www.php-fig.org/psr/psr-2/) as PHP coding standard. +Some of the rules we follow that are not included in document above: + +* Variables' and properties' names are camelCased (e.g.: `$thisIsMyVariable`) +* Avoid too long or too short variables' and methods' names, like `$thisIsMyAwesomeVariableAndImProudOfIt` +* Names of your properties/methods should be intuitive and self-describing - that means your code should look like a book. Developers who read the code should immediately know what a variable includes or what a method does. +* Let your methods will be verbs. For boolean methods, prefix it with `is`, `has`, and so on. E.g.: `isConfigurable`, `hasChildren`. +* Be [SOLID](http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29) and follow [KISS](http://en.wikipedia.org/wiki/KISS_principle) - let the class be responsible only for its tasks. +* Write testable code and if there's a need - easy extendible. +* Avoid duplications + +The rules above have been set a long time after the project has started. If you notice some violations, please open a new issue or even pull request with fixes. It'll be much appreciated. + +### Tools you can use to ensure your code quality + +1. **PHP-CodeSniffer** +2. **PHP Mess Detector** +3. PHP Copy/Paste Detector +4. PHP Dead Code Detector +5. [PHP Coding Standards Fixer](http://cs.sensiolabs.org) + +## Testing and quality +We use PHPUnit to test our code. The whole project is not covered with tests right now but we've been working on it for some time. If you want your code to be merged into Magallanes, we want you to push it with proper tests. We would love to reach and keep at least 90% of line code coverage. In short time we want to configure quality tools to make sure your code is tested properly with minimum coverage. Anyway, try to keep 100% of Code Coverage in your pull requests. To run your tests with code coverage report, you can either run it with: +``` +bin/phpunit --coverage-text +``` +or with more friendly and detailed user graphical representation, into HTML: +``` +bin/phpunit --coverate-html report +``` +where `report` is the directory where html report files shall be stored. +Tests structure follow the same structure as production code with `Test` suffix in class and file name. All tests should go to `tests` directory in project root. So if you've created a class `Mage\Tasks\BuilIn\NewTask` the testing class should be called `MageTest\Tasks\BuiltIn\NewTaskTest`. +To provide more strict tests, point what the method actually test and omit testing some classes indirectly, remember to add annotations to your tests: + +* **`@coversDefaultClass` class annotations** +This prevent to to write full class name each time you write `@covers` for test method (see next point) +```php + +/** + * @coversDefaultClass Mage\Console\Colors + */ +class ColorsTest extends PHPUnit_Framework_TestCase +{ +``` +* **`@covers` methods annotations** +```php +/** + * @covers ::add + */ +public function testAddOnePlusOne() +{ + // ... +} +``` +**Note:** If you omit `coversDefaultClass` for test class, you need to write full class name in `@covers` annotation. + +**Test class musn't test more than one class and any other classes than class being actually tested** + +## Configuration +Magallanes configuration is kept in YAML files. Please follow those rules while adding or changing the configuration: +* Keep 2 spaces indentation in each level +* Multi-word config keys should be joined with dash (`-`), like `my-custom-task` +* If your contribution includes new config key, please be sure that you've documented it in configuration documentation. diff --git a/Mage/Command/BuiltIn/DeployCommand.php b/Mage/Command/BuiltIn/DeployCommand.php index e4e2a78..8649ef8 100644 --- a/Mage/Command/BuiltIn/DeployCommand.php +++ b/Mage/Command/BuiltIn/DeployCommand.php @@ -211,7 +211,11 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment if (self::$failedTasks === 0) { $exitCode = 0; } - + + if (self::$deployStatus === self::FAILED) { + $exitCode = 1; + } + return $exitCode; } diff --git a/Mage/Command/Factory.php b/Mage/Command/Factory.php index 4088239..d5668ee 100644 --- a/Mage/Command/Factory.php +++ b/Mage/Command/Factory.php @@ -43,14 +43,17 @@ class Factory // try a custom command $className = 'Command\\' . $commandName; + // TODO use a custom exception if (!class_exists($className)) { throw new Exception('Command "' . $commandName . '" not found.'); } } /** @var AbstractCommand $instance */ + // TODO dependencies like $config should be injected into constructor $instance = new $className; if (! $instance instanceOf AbstractCommand) { + // TODO use a custom exception throw new Exception('The command ' . $commandName . ' must be an instance of Mage\Command\AbstractCommand.'); } diff --git a/Mage/Config.php b/Mage/Config.php index e839c22..dd78593 100644 --- a/Mage/Config.php +++ b/Mage/Config.php @@ -391,6 +391,16 @@ class Config return $this->deployment('identity-file') ? ('-i ' . $this->deployment('identity-file') . ' ') : ''; } + /** + * Get the ConnectTimeout option + * + * @return string + */ + public function getConnectTimeoutOption() + { + return $this->environmentConfig('connect-timeout') ? ('-o ConnectTimeout=' . $this->environmentConfig('connect-timeout') . ' ') : ''; + } + /** * Get the current Host * diff --git a/Mage/Task/AbstractTask.php b/Mage/Task/AbstractTask.php index 663b387..491adfb 100644 --- a/Mage/Task/AbstractTask.php +++ b/Mage/Task/AbstractTask.php @@ -201,6 +201,7 @@ abstract class AbstractTask $localCommand = 'ssh ' . $this->getConfig()->getHostIdentityFileOption() . $needs_tty . ' -p ' . $this->getConfig()->getHostPort() . ' ' . '-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' + . $this->getConfig()->getConnectTimeoutOption() . $this->getConfig()->deployment('user') . '@' . $this->getConfig()->getHostName(); $remoteCommand = str_replace('"', '\"', $command); diff --git a/Mage/Task/BuiltIn/Deployment/ReleaseTask.php b/Mage/Task/BuiltIn/Deployment/ReleaseTask.php index 84187ad..4ecf586 100644 --- a/Mage/Task/BuiltIn/Deployment/ReleaseTask.php +++ b/Mage/Task/BuiltIn/Deployment/ReleaseTask.php @@ -74,20 +74,21 @@ class ReleaseTask extends AbstractTask implements IsReleaseAware, SkipOnOverride } } - // Remove symlink if exists; create new symlink and change owners - $command = 'rm -f ' . $symlink - . ' ; ' - . 'ln -sf ' . $currentCopy . ' ' . $symlink; - - if ($resultFetch && $userGroup != '') { - $command .= ' && ' - . 'chown -h ' . $userGroup . ' ' . $symlink + if ($resultFetch && $userGroup != '') { + $command = 'chown -h ' . $userGroup . ' ' . $symlink . ' && ' . 'chown -R ' . $userGroup . ' ' . $currentCopy . ' && ' . 'chown ' . $userGroup . ' ' . $releasesDirectory; + $result = $this->runCommandRemote($command); + if (!$result) { + return $result; + } } + // Remove symlink if exists; create new symlink and change owner + $tmplink = $currentCopy . '.tmp'; + $command = "ln -sfn {$currentCopy} {$tmplink} && mv -fT {$tmplink} {$symlink}"; $result = $this->runCommandRemote($command); if ($result) { diff --git a/Mage/Task/BuiltIn/Deployment/Strategy/TarGzTask.php b/Mage/Task/BuiltIn/Deployment/Strategy/TarGzTask.php index d45ea17..2345860 100644 --- a/Mage/Task/BuiltIn/Deployment/Strategy/TarGzTask.php +++ b/Mage/Task/BuiltIn/Deployment/Strategy/TarGzTask.php @@ -77,7 +77,10 @@ class TarGzTask extends BaseStrategyTaskAbstract implements IsReleaseAware $strategyFlags = ''; } - $command = 'tar cfzh' . $strategyFlags . ' ' . $localTarGz . '.tar.gz ' . $excludeCmd . $excludeFromFileCmd . ' -C ' . $this->getConfig()->deployment('from') . ' .'; + // remove h option only if dump-symlinks is allowed in the release config part + $dumpSymlinks = $this->getConfig()->release('dump-symlinks') ? '' : 'h'; + + $command = 'tar cfz'. $dumpSymlinks . $strategyFlags . ' ' . $localTarGz . '.tar.gz ' . $excludeCmd . $excludeFromFileCmd . ' -C ' . $this->getConfig()->deployment('from') . ' .'; $result = $this->runCommandLocal($command); // Strategy Flags @@ -89,7 +92,7 @@ class TarGzTask extends BaseStrategyTaskAbstract implements IsReleaseAware } // Copy Tar Gz to Remote Host - $command = 'scp ' . $strategyFlags . ' ' . $this->getConfig()->getHostIdentityFileOption() . '-P ' . $this->getConfig()->getHostPort() . ' ' . $localTarGz . '.tar.gz ' + $command = 'scp ' . $strategyFlags . ' ' . $this->getConfig()->getHostIdentityFileOption() . $this->getConfig()->getConnectTimeoutOption() . '-P ' . $this->getConfig()->getHostPort() . ' ' . $localTarGz . '.tar.gz ' . $this->getConfig()->deployment('user') . '@' . $this->getConfig()->getHostName() . ':' . $deployToDirectory; $result = $this->runCommandLocal($command) && $result; @@ -106,11 +109,11 @@ class TarGzTask extends BaseStrategyTaskAbstract implements IsReleaseAware $result = $this->runCommandRemote($command) && $result; // Delete Tar Gz from Remote Host - $command = $this->getReleasesAwareCommand('rm ' . $remoteTarGz . '.tar.gz'); + $command = $this->getReleasesAwareCommand('rm -f ' . $remoteTarGz . '.tar.gz'); $result = $this->runCommandRemote($command) && $result; // Delete Tar Gz from Local - $command = 'rm ' . $localTarGz . ' ' . $localTarGz . '.tar.gz'; + $command = 'rm -f ' . $localTarGz . ' ' . $localTarGz . '.tar.gz'; $result = $this->runCommandLocal($command) && $result; return $result; diff --git a/Mage/Task/BuiltIn/Filesystem/ApplyFaclsTask.php b/Mage/Task/BuiltIn/Filesystem/ApplyFaclsTask.php index 03786ce..3f40ee7 100644 --- a/Mage/Task/BuiltIn/Filesystem/ApplyFaclsTask.php +++ b/Mage/Task/BuiltIn/Filesystem/ApplyFaclsTask.php @@ -25,7 +25,6 @@ class ApplyFaclsTask extends AbstractTask implements IsReleaseAware public function run() { $releasesDirectory = $this->getConfig()->release('directory', 'releases'); - $releasesDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory; $currentCopy = $releasesDirectory . '/' . $this->getConfig()->getReleaseId(); diff --git a/Mage/Task/BuiltIn/Filesystem/LinkSharedFilesTask.php b/Mage/Task/BuiltIn/Filesystem/LinkSharedFilesTask.php index 4ed89a1..2682b86 100644 --- a/Mage/Task/BuiltIn/Filesystem/LinkSharedFilesTask.php +++ b/Mage/Task/BuiltIn/Filesystem/LinkSharedFilesTask.php @@ -5,21 +5,47 @@ use Mage\Task\AbstractTask; use Mage\Task\Releases\IsReleaseAware; use Mage\Task\SkipException; +/** + * Class LinkSharedFilesTask + * + * @package Mage\Task\BuiltIn\Filesystem + * @author Andrey Kolchenko + */ class LinkSharedFilesTask extends AbstractTask implements IsReleaseAware { + /** + * Linked folders parameter name + */ + const LINKED_FILES = 'linked_files'; + /** + * Linked folders parameter name + */ + const LINKED_FOLDERS = 'linked_folders'; + /** + * Linking strategy parameter name + */ + const LINKED_STRATEGY = 'linking_strategy'; - const LINKED_FOLDERS = 'linked_folders'; - const LINKED_STRATEGY = 'linking_strategy'; - + /** + * Absolute linked strategy + */ const ABSOLUTE_LINKING = 'absolute'; + /** + * Relative linked strategy + */ const RELATIVE_LINKING = 'relative'; - public $linkingStrategies = array( + /** + * @var array + */ + private static $linkingStrategies = array( self::ABSOLUTE_LINKING, self::RELATIVE_LINKING ); + /** * Returns the Title of the Task + * * @return string */ public function getName() @@ -35,41 +61,92 @@ class LinkSharedFilesTask extends AbstractTask implements IsReleaseAware */ public function run() { - $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); + $linkedEntities = array_merge( + $this->getParameter(self::LINKED_FILES, array()), + $this->getParameter(self::LINKED_FOLDERS, array()) + ); - if (sizeof($linkedFiles) == 0 && sizeof($linkedFolders) == 0) { + if (empty($linkedEntities)) { throw new SkipException('No files and folders configured for sym-linking.'); } - $sharedFolderName = $this->getParameter('shared', 'shared'); - $sharedFolderPath = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $sharedFolderName; - $releasesDirectory = $this->getConfig()->release('directory', 'releases'); - $releasesDirectoryPath = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory; - + $remoteDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/'; + $sharedFolderPath = $remoteDirectory . $this->getParameter('shared', 'shared'); + $releasesDirectoryPath = $remoteDirectory . $this->getConfig()->release('directory', 'releases'); $currentCopy = $releasesDirectoryPath . '/' . $this->getConfig()->getReleaseId(); - $relativeDiffPath = str_replace($this->getConfig()->deployment('to'),'',$currentCopy) . '/'; foreach ($linkedEntities as $ePath) { - if(is_array($ePath) && in_array($strategy = reset($ePath), $this->linkingStrategies ) ) { - $entityPath = key($ePath); + list($entityPath, $strategy) = $this->getPath($ePath); + if ($strategy === self::RELATIVE_LINKING) { + $dirName = dirname($currentCopy . '/' . $entityPath); + $target = $this->makePathRelative($sharedFolderPath, $dirName) . $entityPath; } 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; + $target = $sharedFolderPath . '/' . $entityPath; } - $command = "ln -nfs $sharedEntityLinkedPath $currentCopy/$entityPath"; + $command = 'mkdir -p ' . escapeshellarg(dirname($target)); + $this->runCommandRemote($command); + $command = 'ln -nfs ' . escapeshellarg($target) . ' ' . escapeshellarg($currentCopy . '/' . $entityPath); $this->runCommandRemote($command); } return true; } -} \ No newline at end of file + + /** + * Given an existing path, convert it to a path relative to a given starting path + * + * @param string $endPath Absolute path of target + * @param string $startPath Absolute path where traversal begins + * + * @return string Path of target relative to starting path + * + * @author Fabien Potencier + * @see https://github.com/symfony/Filesystem/blob/v2.6.1/Filesystem.php#L332 + */ + private function makePathRelative($endPath, $startPath) + { + // Normalize separators on Windows + if (defined('PHP_WINDOWS_VERSION_MAJOR')) { + $endPath = strtr($endPath, '\\', '/'); + $startPath = strtr($startPath, '\\', '/'); + } + // Split the paths into arrays + $startPathArr = explode('/', trim($startPath, '/')); + $endPathArr = explode('/', trim($endPath, '/')); + // Find for which directory the common path stops + $index = 0; + while (isset($startPathArr[$index]) && isset($endPathArr[$index]) && $startPathArr[$index] === $endPathArr[$index]) { + $index++; + } + // Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels) + $depth = count($startPathArr) - $index; + // Repeated "../" for each level need to reach the common path + $traverser = str_repeat('../', $depth); + $endPathRemainder = implode('/', array_slice($endPathArr, $index)); + // Construct $endPath from traversing to the common path, then to the remaining $endPath + $relativePath = $traverser . (strlen($endPathRemainder) > 0 ? $endPathRemainder . '/' : ''); + + return (strlen($relativePath) === 0) ? './' : $relativePath; + } + + /** + * @param array|string $linkedEntity + * + * @return array [$path, $strategy] + */ + private function getPath($linkedEntity) + { + $linkingStrategy = $this->getParameter(self::LINKED_STRATEGY, self::ABSOLUTE_LINKING); + if (is_array($linkedEntity)) { + list($path, $strategy) = each($linkedEntity); + if (!in_array($strategy, self::$linkingStrategies)) { + $strategy = $linkingStrategy; + } + } else { + $strategy = $linkingStrategy; + $path = $linkedEntity; + } + + return [$path, $strategy]; + } +} diff --git a/Mage/Task/BuiltIn/Filesystem/PermissionsReadableOnlyByWebServerTask.php b/Mage/Task/BuiltIn/Filesystem/PermissionsReadableOnlyByWebServerTask.php new file mode 100644 index 0000000..375489d --- /dev/null +++ b/Mage/Task/BuiltIn/Filesystem/PermissionsReadableOnlyByWebServerTask.php @@ -0,0 +1,60 @@ + + */ +class PermissionsReadableOnlyByWebServerTask extends PermissionsTask +{ + /** + * Set group with web server user and give group write permissions. + */ + public function init() + { + parent::init(); + + $this->setGroup($this->getParameter('group') ? $this->getParameter('group') : $this->getWebServerUser()) + ->setRights('040'); + } + + /** + * @return string + */ + public function getName() + { + return "Giving read permissions only to web server user for given paths [built-in]"; + } + + /** + * Tries to guess the web server user by going thru the running processes. + * + * @return string + * @throws SkipException + */ + protected function getWebServerUser() + { + $this->runCommand("ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1", $webServerUser); + + if (empty($webServerUser)) { + throw new SkipException("Can't guess web server user. Please check if it is running or force it by setting the group parameter"); + } + + return $webServerUser; + } +} diff --git a/Mage/Task/BuiltIn/Filesystem/PermissionsTask.php b/Mage/Task/BuiltIn/Filesystem/PermissionsTask.php new file mode 100644 index 0000000..f5f9505 --- /dev/null +++ b/Mage/Task/BuiltIn/Filesystem/PermissionsTask.php @@ -0,0 +1,324 @@ + + */ +class PermissionsTask extends AbstractTask +{ + /** + * Paths to change of permissions in an array or a string separated by + * PATH_SEPARATOR. + * + * If the stage is on local host you should give full paths. If on remote + * you may give full or relative to the current release directory paths. + * + * @var string + */ + private $paths; + + /** + * If set to true, will check existance of given paths on the host and + * throw SkipException if at least one does not exist. + * + * @var boolean + */ + private $checkPathsExist = true; + + /** + * Owner to set for the given paths (ex : "www-data" or "www-data:www-data" + * to set both owner and group at the same time) + * + * @var string + */ + private $owner; + + /** + * Group to set for the given paths (ex : "www-data") + * + * @var string + */ + private $group; + + /** + * Rights to set for the given paths (ex: "755" or "g+w") + * + * @var string + */ + private $rights; + + /** + * If set to true, will recursively change permissions on given paths. + * + * @var string + */ + private $recursive = false; + + /** + * Initialize parameters. + * + * @throws SkipException + */ + public function init() + { + parent::init(); + + if (! is_null($this->getParameter('checkPathsExist'))) { + $this->setCheckPathsExist($this->getParameter('checkPathsExist')); + } + + if (! $this->getParameter('paths')) { + throw new SkipException('Param paths is mandatory'); + } + $this->setPaths(is_array($this->getParameter('paths')) ? $this->getParameter('paths') : explode(PATH_SEPARATOR, $this->getParameter('paths', ''))); + + if (! is_null($owner = $this->getParameter('owner'))) { + if (strpos($owner, ':') !== false) { + $this->setOwner(array_shift(explode(':', $owner))); + $this->setGroup(array_pop(explode(':', $owner))); + } else { + $this->setOwner($owner); + } + } + + if (! is_null($group = $this->getParameter('group'))) { + $this->setGroup($group); + } + + if (! is_null($rights = $this->getParameter('rights'))) { + $this->setRights($rights); + } + + if (! is_null($recursive = $this->getParameter('recursive'))) { + $this->setRecursive($recursive); + } + } + + /** + * @return string + */ + public function getName() + { + return "Changing rights / owner / group for given paths [built-in]"; + } + + /** + * @return boolean + */ + public function run() + { + $commands = array(); + + if ($this->paths && $this->owner) { + $commands []= 'chown '. $this->getOptionsForCmd() .' ' . $this->owner . ' ' . $this->getPathsForCmd(); + } + + if ($this->paths && $this->group) { + $commands []= 'chgrp '. $this->getOptionsForCmd() .' ' . $this->group . ' ' . $this->getPathsForCmd(); + } + + if ($this->paths && $this->rights) { + $commands []= 'chmod '. $this->getOptionsForCmd() .' ' . $this->rights . ' ' . $this->getPathsForCmd(); + } + + $result = $this->runCommand(implode(' && ', $commands)); + + return $result; + } + + /** + * Returns the options for the commands to run. Only supports -R for now. + * + * @return string + */ + protected function getOptionsForCmd() + { + $optionsForCmd = ''; + $options = array( + 'R' => $this->recursive + ); + + foreach($options as $option => $apply) { + if ($apply === true) { + $optionsForCmd .= $option; + } + } + + return $optionsForCmd ? '-' . $optionsForCmd : ''; + } + + /** + * Transforms paths array to a string separated by 1 space in order to use + * it in a command line. + * + * @return string + */ + protected function getPathsForCmd($paths = null) + { + if (is_null($paths)) { + $paths = $this->paths; + } + + return implode(' ', $paths); + } + + /** + * Set paths. Will check if they exist on the host depending on + * checkPathsExist flag. + * + * @param array $paths + * @return PermissionsTask + * @throws SkipException + */ + protected function setPaths(array $paths) + { + if ($this->checkPathsExist === true) { + $commands = array(); + foreach ($paths as $path) { + $commands[] = '(([ -f ' . $path . ' ]) || ([ -d ' . $path . ' ]))'; + } + + $command = implode(' && ', $commands); + if (! $this->runCommand($command)) { + throw new SkipException('Make sure all paths given exist on the host : ' . $this->getPathsForCmd($paths)); + } + } + + $this->paths = $paths; + + return $this; + } + + /** + * @return string + */ + protected function getPaths() + { + return $this->paths; + } + + /** + * Set checkPathsExist. + * + * @param boolean $checkPathsExist + * @return PermissionsTask + */ + protected function setCheckPathsExist($checkPathsExist) + { + $this->checkPathsExist = (bool) $checkPathsExist; + + return $this; + } + + /** + * @return boolean + */ + protected function getCheckPathsExist() + { + return $this->checkPathsExist; + } + + /** + * Set owner. + * + * @param string $owner + * @return PermissionsTask + */ + protected function setOwner($owner) + { + $this->owner = $owner; + + return $this; + } + + /** + * @return string + */ + protected function getOwner() + { + return $this->owner; + } + + /** + * Set group. + * + * @param string $group + * @return PermissionsTask + */ + protected function setGroup($group) + { + $this->group = $group; + + return $this; + } + + /** + * @return string + */ + protected function getGroup() + { + return $this->group; + } + + /** + * Set rights. + * + * @param string $rights + * @return PermissionsTask + */ + protected function setRights($rights) + { + $this->rights = $rights; + + return $this; + } + + /** + * @return string + */ + protected function getRights() + { + return $this->rights; + } + + /** + * Set recursive. + * + * @param boolean $recursive + * @return PermissionsTask + */ + protected function setRecursive($recursive) + { + $this->recursive = (bool) $recursive; + + return $this; + } + + /** + * @return boolean + */ + protected function getRecursive() + { + return $this->recursive; + } +} diff --git a/Mage/Task/BuiltIn/Filesystem/PermissionsWritableByWebServerTask.php b/Mage/Task/BuiltIn/Filesystem/PermissionsWritableByWebServerTask.php new file mode 100644 index 0000000..6abaf07 --- /dev/null +++ b/Mage/Task/BuiltIn/Filesystem/PermissionsWritableByWebServerTask.php @@ -0,0 +1,60 @@ + + */ +class PermissionsWritableByWebServerTask extends PermissionsTask +{ + /** + * Set group with web server user and give group write permissions. + */ + public function init() + { + parent::init(); + + $this->setGroup($this->getParameter('group') ? $this->getParameter('group') : $this->getWebServerUser()) + ->setRights('g+w'); + } + + /** + * @return string + */ + public function getName() + { + return "Giving write permissions to web server user for given paths [built-in]"; + } + + /** + * Tries to guess the web server user by going thru the running processes. + * + * @return string + * @throws SkipException + */ + protected function getWebServerUser() + { + $this->runCommand("ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1", $webServerUser); + + if (empty($webServerUser)) { + throw new SkipException("Can't guess web server user. Please check if it is running or force it by setting the group parameter"); + } + + return $webServerUser; + } +} diff --git a/Mage/Task/BuiltIn/Ioncube/EncryptTask.php b/Mage/Task/BuiltIn/Ioncube/EncryptTask.php index f147be8..8565036 100644 --- a/Mage/Task/BuiltIn/Ioncube/EncryptTask.php +++ b/Mage/Task/BuiltIn/Ioncube/EncryptTask.php @@ -33,7 +33,6 @@ use Mage\Task\ErrorWithMessageException; * * Example enviroment.yaml file at end * - * @todo add support for creating license files. * * (c) ActWeb 2013 * (c) Matt Lowe (marl.scot.1@googlemail.com) @@ -202,7 +201,7 @@ class EncryptTask extends AbstractTask /* * Get all our IonCube config options */ - $this->_getAllIonCubeConfigs(); + $this->getAllIonCubeConfigs(); /* * get the source code location */ @@ -279,7 +278,7 @@ class EncryptTask extends AbstractTask * * @return void */ - private function _getAllIonCubeConfigs() + private function getAllIonCubeConfigs() { /* @@ -553,7 +552,6 @@ class EncryptTask extends AbstractTask * D - Default options as stored in script * * more options could be added to make this a bit more flexable - * @todo I'm sure this could be combined into a loop to make it easier and shorter * */ $s = array(); diff --git a/Mage/Task/BuiltIn/Releases/RollbackTask.php b/Mage/Task/BuiltIn/Releases/RollbackTask.php index 1de9831..a881ec6 100644 --- a/Mage/Task/BuiltIn/Releases/RollbackTask.php +++ b/Mage/Task/BuiltIn/Releases/RollbackTask.php @@ -51,12 +51,14 @@ class RollbackTask extends AbstractTask implements IsReleaseAware $releasesDirectory = $this->getConfig()->release('directory', 'releases'); $symlink = $this->getConfig()->release('symlink', 'current'); + if (substr($symlink, 0, 1) == '/') { + $releasesDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory; + } + $output = ''; $result = $this->runCommandRemote('ls -1 ' . $releasesDirectory, $output); $releases = ($output == '') ? array() : explode(PHP_EOL, $output); - $inDeploy = $this->getParameter('inDeploy',false); - if (count($releases) == 0) { Console::output('Release are not available for ' . $this->getConfig()->getHost() . ' ... FAIL'); @@ -133,9 +135,11 @@ class RollbackTask extends AbstractTask implements IsReleaseAware $userGroup = ''; $resultFetch = $this->runCommandRemote('ls -ld ' . $rollbackTo . ' | awk \'{print \$3":"\$4}\'', $userGroup); - $command = 'rm -f ' . $symlink + + $tmplink = $rollbackTo . '.tmp'; + $command = 'ln -sfn ' . $currentCopy . ' ' . $tmplink . ' && ' - . 'ln -sf ' . $rollbackTo . ' ' . $symlink; + . 'mv -T ' . $tmplink . ' ' . $symlink; if ($resultFetch) { $command .= ' && chown -h ' . $userGroup . ' ' . $symlink; diff --git a/Mage/Task/BuiltIn/Scm/ChangeBranchTask.php b/Mage/Task/BuiltIn/Scm/ChangeBranchTask.php index 8c5e33f..20859c5 100644 --- a/Mage/Task/BuiltIn/Scm/ChangeBranchTask.php +++ b/Mage/Task/BuiltIn/Scm/ChangeBranchTask.php @@ -63,14 +63,15 @@ class ChangeBranchTask extends AbstractTask */ public function run() { + $preCommand = 'cd ' . $this->getConfig()->deployment('from', './') . '; '; switch ($this->getConfig()->general('scm')) { case 'git': if ($this->getParameter('_changeBranchRevert', false)) { - $command = 'git checkout ' . self::$startingBranch; + $command = $preCommand . 'git checkout ' . self::$startingBranch; $result = $this->runCommandLocal($command); } else { - $command = 'git branch | grep \'*\' | cut -d\' \' -f 2'; + $command = $preCommand . 'git branch | grep \'*\' | cut -d\' \' -f 2'; $currentBranch = 'master'; $result = $this->runCommandLocal($command, $currentBranch); diff --git a/Mage/Task/BuiltIn/Scm/UpdateTask.php b/Mage/Task/BuiltIn/Scm/UpdateTask.php index 22c496e..3347a97 100644 --- a/Mage/Task/BuiltIn/Scm/UpdateTask.php +++ b/Mage/Task/BuiltIn/Scm/UpdateTask.php @@ -54,9 +54,10 @@ class UpdateTask extends AbstractTask */ public function run() { + $command = 'cd ' . $this->getConfig()->deployment('from', './') . '; '; switch ($this->getConfig()->general('scm')) { case 'git': - $command = 'git pull'; + $command .= 'git pull'; break; default: diff --git a/Mage/Task/Factory.php b/Mage/Task/Factory.php index 37e1cee..db53818 100644 --- a/Mage/Task/Factory.php +++ b/Mage/Task/Factory.php @@ -59,7 +59,7 @@ class Factory $instance = new $className($taskConfig, $inRollback, $stage, $taskParameters); - if (!$instance instanceof AbstractTask) { + if (!($instance instanceof AbstractTask)) { throw new Exception('The Task ' . $taskName . ' must be an instance of Mage\Task\AbstractTask.'); } diff --git a/README.md b/README.md index 4ec18cb..064c932 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Magallanes # +[![SensioLabsInsight](https://insight.sensiolabs.com/projects/ed0de53a-a12e-459b-9464-34def5907b56/mini.png)](https://insight.sensiolabs.com/projects/ed0de53a-a12e-459b-9464-34def5907b56) + ### What's Magallanes? ### Magallanes is a deployment tool for PHP applications; it's quite simple to use and manage. It will get your application to a safe harbor. @@ -16,7 +18,7 @@ Simply add the following dependency to your project’s composer.json file: ```js "require-dev": { // ... - "andres-montanez/magallanes": "~1.0.1" + "andres-montanez/magallanes": "~1.0.*" // ... } ``` @@ -35,7 +37,7 @@ $ bin/mage version ### System-wide installation with composer ### ```bash -$ composer global require "andres-montanez/magallanes=~1.0.1" +$ composer global require "andres-montanez/magallanes=~1.0.*" ``` Make sure you have ~/.composer/vendor/bin/ in your path. @@ -52,7 +54,6 @@ Like this: $ mage deploy to:production ``` - ### What's this sorcery?! ### Easy boy. It's not sorcery, just some *technomagick*! @@ -69,3 +70,4 @@ Enjoy your magic trip with **Magallanes** to the land of the easily deployable a ### "develop" branch ### Please, all pull request now must be on the develop branch. Thanks! + diff --git a/bin/mage b/bin/mage index 728e3aa..80d9b88 100755 --- a/bin/mage +++ b/bin/mage @@ -13,18 +13,19 @@ date_default_timezone_set('UTC'); $baseDir = dirname(dirname(__FILE__)); -define('MAGALLANES_VERSION', '1.0.3'); +define('MAGALLANES_VERSION', '1.0.4'); define('MAGALLANES_DIRECTORY', $baseDir); if (file_exists(__DIR__ . '/../vendor/autoload.php')) { require_once __DIR__ . '/../vendor/autoload.php'; +} else if (file_exists(__DIR__ . '/../../../autoload.php')) { + require_once __DIR__ . '/../../../autoload.php'; } else { require_once $baseDir . '/Mage/Autoload.php'; $loader = new \Mage\Autoload(); spl_autoload_register(array($loader, 'autoLoad')); } - // Clean arguments array_shift($argv); diff --git a/composer.json b/composer.json index 2183c74..5295d56 100644 --- a/composer.json +++ b/composer.json @@ -14,10 +14,13 @@ "autoload": { "psr-4": { "Mage\\": "./Mage", - "Task\\": ".mage/tasks", - "Command\\": ".mage/commands" + "Task\\": [".mage/tasks", "../../../.mage/tasks"], + "Command\\": [".mage/tasks", "../../../.mage/commands"] } }, + "config": { + "bin-dir": "bin" + }, "bin": [ "bin/mage" ] diff --git a/composer.lock b/composer.lock index c1ca3ca..a927b43 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "a19528b890d301384e45c1ed7d221e26", + "hash": "7a55b88add493fbc9910519e7e9c3a3b", "packages": [], "packages-dev": [ { @@ -63,16 +63,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "2.0.11", + "version": "2.0.13", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "53603b3c995f5aab6b59c8e08c3a663d2cc810b7" + "reference": "0e7d2eec5554f869fa7a4ec2d21e4b37af943ea5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/53603b3c995f5aab6b59c8e08c3a663d2cc810b7", - "reference": "53603b3c995f5aab6b59c8e08c3a663d2cc810b7", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/0e7d2eec5554f869fa7a4ec2d21e4b37af943ea5", + "reference": "0e7d2eec5554f869fa7a4ec2d21e4b37af943ea5", "shasum": "" }, "require": { @@ -124,7 +124,7 @@ "testing", "xunit" ], - "time": "2014-08-31 06:33:04" + "time": "2014-12-03 06:41:44" }, { "name": "phpunit/php-file-iterator", @@ -439,16 +439,16 @@ }, { "name": "sebastian/comparator", - "version": "1.0.1", + "version": "1.1.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "e54a01c0da1b87db3c5a3c4c5277ddf331da4aef" + "reference": "c484a80f97573ab934e37826dba0135a3301b26a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/e54a01c0da1b87db3c5a3c4c5277ddf331da4aef", - "reference": "e54a01c0da1b87db3c5a3c4c5277ddf331da4aef", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/c484a80f97573ab934e37826dba0135a3301b26a", + "reference": "c484a80f97573ab934e37826dba0135a3301b26a", "shasum": "" }, "require": { @@ -462,7 +462,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.1.x-dev" } }, "autoload": { @@ -499,7 +499,7 @@ "compare", "equality" ], - "time": "2014-05-11 23:00:21" + "time": "2014-11-16 21:32:38" }, { "name": "sebastian/diff", @@ -555,16 +555,16 @@ }, { "name": "sebastian/environment", - "version": "1.2.0", + "version": "1.2.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "0d9bf79554d2a999da194a60416c15cf461eb67d" + "reference": "6e6c71d918088c251b181ba8b3088af4ac336dd7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/0d9bf79554d2a999da194a60416c15cf461eb67d", - "reference": "0d9bf79554d2a999da194a60416c15cf461eb67d", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/6e6c71d918088c251b181ba8b3088af4ac336dd7", + "reference": "6e6c71d918088c251b181ba8b3088af4ac336dd7", "shasum": "" }, "require": { @@ -601,7 +601,7 @@ "environment", "hhvm" ], - "time": "2014-10-22 06:38:05" + "time": "2014-10-25 08:00:45" }, { "name": "sebastian/exporter", @@ -705,17 +705,17 @@ }, { "name": "symfony/yaml", - "version": "v2.5.7", + "version": "v2.6.1", "target-dir": "Symfony/Component/Yaml", "source": { "type": "git", "url": "https://github.com/symfony/Yaml.git", - "reference": "900d38bc8f74a50343ce65dd1c1e9819658ee56b" + "reference": "3346fc090a3eb6b53d408db2903b241af51dcb20" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Yaml/zipball/900d38bc8f74a50343ce65dd1c1e9819658ee56b", - "reference": "900d38bc8f74a50343ce65dd1c1e9819658ee56b", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/3346fc090a3eb6b53d408db2903b241af51dcb20", + "reference": "3346fc090a3eb6b53d408db2903b241af51dcb20", "shasum": "" }, "require": { @@ -724,7 +724,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.5-dev" + "dev-master": "2.6-dev" } }, "autoload": { @@ -748,13 +748,14 @@ ], "description": "Symfony Yaml Component", "homepage": "http://symfony.com", - "time": "2014-11-20 13:22:25" + "time": "2014-12-02 20:19:20" } ], "aliases": [], "minimum-stability": "stable", "stability-flags": [], "prefer-stable": false, + "prefer-lowest": false, "platform": { "php": ">=5.3" }, diff --git a/docs/example-config/.mage/config/environment/production.yml.dump-symlinks.txt b/docs/example-config/.mage/config/environment/production.yml.dump-symlinks.txt new file mode 100644 index 0000000..1ff2547 --- /dev/null +++ b/docs/example-config/.mage/config/environment/production.yml.dump-symlinks.txt @@ -0,0 +1,43 @@ +#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 +# This option allows to dump the symlink with the TarGz strategy and use the symlink on the deployment host. +# This is useful, if the files the symlink point to only exist on the deployment host and not on the host who runs the mage command. +# The default value is false to keep bc. +# See : http://linux.die.net/man/1/tar -h, --dereference + dump-symlinks: true +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/tasks/SampleTaskRollbackAware.php b/docs/example-config/.mage/tasks/SampleTaskRollbackAware.php index d9213f4..c8dadc9 100644 --- a/docs/example-config/.mage/tasks/SampleTaskRollbackAware.php +++ b/docs/example-config/.mage/tasks/SampleTaskRollbackAware.php @@ -1,23 +1,22 @@ -inRollback()) { - return 'A Sample Task aware of rollbacks [in rollback]'; - } else { - return 'A Sample Task aware of rollbacks [not in rollback]'; - } - } - - public function run() - { - return true; - } -} - +inRollback()) { + return 'A Sample Task aware of rollbacks [in rollback]'; + } else { + return 'A Sample Task aware of rollbacks [not in rollback]'; + } + } + + public function run() + { + return true; + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a5e1c3b..6fbbd61 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -5,8 +5,8 @@ backupGlobals="false" verbose="true" strict="true" - colors="false"> - + colors="true"> + tests diff --git a/tests/MageTest/Command/FactoryTest.php b/tests/MageTest/Command/FactoryTest.php new file mode 100644 index 0000000..b91f9b6 --- /dev/null +++ b/tests/MageTest/Command/FactoryTest.php @@ -0,0 +1,63 @@ +config = $this->getMock('Mage\Config'); + } + + public function testGet() + { + $command = Factory::get('add', $this->config); + $this->assertInstanceOf('Mage\\Command\\BuiltIn\\AddCommand', $command); + } + + /** + * @expectedException \Exception + */ + public function testGetClassNotFoundException() + { + $command = Factory::get('commanddoesntexist', $this->config); + } + + public function testGetCustomCommand() + { + $this->getMockBuilder('Mage\\Command\\AbstractCommand') + ->setMockClassName('MyCommand') + ->getMock(); + + /** + * current workaround + * @link https://github.com/sebastianbergmann/phpunit-mock-objects/issues/134 + */ + class_alias('MyCommand', 'Command\\MyCommand'); + + $command = Factory::get('my-command', $this->config); + $this->assertInstanceOf('Command\\MyCommand', $command); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage The command MyInconsistentCommand must be an instance of Mage\Command\AbstractCommand. + */ + public function testGetInconsistencyException() + { + $this->getMock('Command\\MyInconsistentCommand'); + + $command = Factory::get('my-inconsistent-command', $this->config); + } +} diff --git a/tests/MageTest/Console/ColorsTest.php b/tests/MageTest/Console/ColorsTest.php index 2655409..5038303 100644 --- a/tests/MageTest/Console/ColorsTest.php +++ b/tests/MageTest/Console/ColorsTest.php @@ -7,17 +7,21 @@ use PHPUnit_Framework_TestCase; /** * @group Mage_Console_Colors + * @coversDefaultClass Mage\Console\Colors */ class ColorsTest extends PHPUnit_Framework_TestCase { + private $noColorParameter = "no-color"; /** * @group 159 + * @covers ::color */ public function testColor() { $config = $this->getMock('Mage\Config'); $config->expects($this->once()) ->method('getParameter') + ->with($this->noColorParameter) ->will($this->returnValue(false)); $string = 'FooBar'; @@ -31,12 +35,14 @@ class ColorsTest extends PHPUnit_Framework_TestCase /** * @group 159 + * @covers ::color */ public function testColorNoColor() { $config = $this->getMock('Mage\Config'); $config->expects($this->once()) ->method('getParameter') + ->with($this->noColorParameter) ->will($this->returnValue(true)); $string = 'FooBar'; @@ -50,12 +56,14 @@ class ColorsTest extends PHPUnit_Framework_TestCase /** * @group 159 + * @covers ::color */ public function testColorUnknownColorName() { $config = $this->getMock('Mage\Config'); $config->expects($this->once()) ->method('getParameter') + ->with($this->noColorParameter) ->will($this->returnValue(false)); $string = 'FooBar';