Merge branch 'develop' into command-factory

This commit is contained in:
Claudio Zizza 2015-02-09 20:32:27 +01:00
commit 27423f1f3e
21 changed files with 775 additions and 69 deletions

7
.gitignore vendored
View file

@ -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
# IDE generated files
.idea
nbproject

97
CONTRIBUTING.md Normal file
View file

@ -0,0 +1,97 @@
Contributor guidelines for Magallanes
=====================================
Welcome to Magallanes! We are much appreciated you've decided to contribute 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. Push your code into `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, plaase feel free to open 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 keep 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 allcan improve the existing code.
Keep your git commits as atomic as it's 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 impreatively, like following:
```
Add MyCustomTask
```
If you need to write more about your tasks, please enter the description in 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 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" is examples file in `doc` directory. If the code you are going to include in your pull requests adds or changes config options, please make sure you that you create a new sample in that file. You should also to 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 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/method should be intuitive and self-describing - that means your code should look like a book. Developer who reads the code should immediately know what the variable includes or what the 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, plase open new issue or even pull request with fixes, we'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
## Testing and quality
We use PHPUnit to test our classes. Now not the whole project is covered with tests 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`.
## 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.

View file

@ -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;
}

View file

@ -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
*

View file

@ -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);

View file

@ -75,9 +75,10 @@ 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;
$tmplink = $currentCopy . '.tmp';
$command = 'ln -sfn ' . $currentCopy . ' ' . $tmplink
. ' && '
. 'mv -T ' . $tmplink . ' ' . $symlink;
if ($resultFetch && $userGroup != '') {
$command .= ' && '

View file

@ -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;

View file

@ -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();

View file

@ -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 <andrey@kolchenko.me>
*/
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(
$this->getParameter(self::LINKED_FILES, array()),
$this->getParameter(self::LINKED_FOLDERS, array())
);
$linkedEntities = array_merge($linkedFiles,$linkedFolders);
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;
$target = $sharedFolderPath . '/' . $entityPath;
}
$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";
$command = 'mkdir -p ' . escapeshellarg(dirname($target));
$this->runCommandRemote($command);
$command = 'ln -nfs ' . escapeshellarg($target) . ' ' . escapeshellarg($currentCopy . '/' . $entityPath);
$this->runCommandRemote($command);
}
return true;
}
}
/**
* 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 <fabien@symfony.com>
* @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];
}
}

View file

@ -0,0 +1,60 @@
<?php
namespace Mage\Task\BuiltIn\Filesystem;
use Mage\Task\SkipException;
/**
* Task for giving only to web server read permissions on given paths.
*
* Usage :
* pre-deploy:
* - filesystem/permissions-readable-only-by-web-server: {paths: /var/www/myapp/app/config/config.yml:/var/www/myapp/app/config/parameters.yml, recursive: false, checkPathsExist: true}
* - filesystem/permissions-readable-only-by-web-server:
* paths:
* - /var/www/myapp/app/config/config.yml
* - /var/www/myapp/app/config/parameters.yml
* recursive: false
* checkPathsExist: true
* on-deploy:
* - filesystem/permissions-readable-only-by-web-server: {paths: app/config/config.yml:app/config/parameters.yml, recursive: false, checkPathsExist: true}
*
* @author Jérémy Huet <jeremy.huet@gmail.com>
*/
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;
}
}

View file

@ -0,0 +1,330 @@
<?php
namespace Mage\Task\BuiltIn\Filesystem;
use Mage\Task\AbstractTask;
use Mage\Task\SkipException;
/**
* Task for setting permissions on given paths. Change will be done on local or
* remote host depending on the stage of the deployment.
*
* Usage :
* pre-deploy:
* - filesystem/permissions: {paths: /var/www/myapp/app/cache:/var/www/myapp/app/cache, recursive: false, checkPathsExist: true, owner: www-data, group: www-data, rights: 775}
* - filesystem/permissions:
* paths:
* - /var/www/myapp/app/cache
* - /var/www/myapp/app/logs
* recursive: false
* checkPathsExist: true
* owner: www-data:www-data
* rights: 775
* on-deploy:
* - filesystem/permissions: {paths: app/cache:app/logs, recursive: false, checkPathsExist: true, owner: www-data, group: www-data, rights: 775}
*
* @author Jérémy Huet <jeremy.huet@gmail.com>
*/
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.
*
* @todo check existance of $owner on host, might be different ways depending on OS.
*
* @param string $owner
* @return PermissionsTask
*/
protected function setOwner($owner)
{
$this->owner = $owner;
return $this;
}
/**
* @return string
*/
protected function getOwner()
{
return $this->owner;
}
/**
* Set group.
*
* @todo check existance of $group on host, might be different ways depending on OS.
*
* @param string $group
* @return PermissionsTask
*/
protected function setGroup($group)
{
$this->group = $group;
return $this;
}
/**
* @return string
*/
protected function getGroup()
{
return $this->group;
}
/**
* Set rights.
*
* @todo check if $rights is in a correct format.
*
* @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;
}
}

View file

@ -0,0 +1,60 @@
<?php
namespace Mage\Task\BuiltIn\Filesystem;
use Mage\Task\SkipException;
/**
* Task for giving web server write permissions on given paths.
*
* Usage :
* pre-deploy:
* - filesystem/permissions-writable-by-web-server: {paths: /var/www/myapp/app/cache:/var/www/myapp/app/logs, recursive: false, checkPathsExist: true}
* - filesystem/permissions-writable-by-web-server:
* paths:
* - /var/www/myapp/app/cache
* - /var/www/myapp/app/logs
* recursive: false
* checkPathsExist: true
* on-deploy:
* - filesystem/permissions-writable-by-web-server: {paths: app/cache:app/logs, recursive: false, checkPathsExist: true}
*
* @author Jérémy Huet <jeremy.huet@gmail.com>
*/
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;
}
}

View file

@ -133,9 +133,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;

View file

@ -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);

View file

@ -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:

View file

@ -59,7 +59,7 @@ class Factory
$instance = new $className($taskConfig, $inRollback, $stage, $taskParameters);
if (!is_a($instance, 'Mage\Task\AbstractTask')) {
if (!($instance instanceof AbstractTask)) {
throw new Exception('The Task ' . $taskName . ' must be an instance of Mage\Task\AbstractTask.');
}

View file

@ -18,6 +18,9 @@
"Command\\": ".mage/commands"
}
},
"config": {
"bin-dir": "bin"
},
"bin": [
"bin/mage"
]

47
composer.lock generated
View file

@ -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": "c66ae8cd7e44d614445b273f310d9c34",
"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"
},

View file

@ -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:

View file

@ -5,8 +5,8 @@
backupGlobals="false"
verbose="true"
strict="true"
colors="false">
<testsuite name="small">
colors="true">
<testsuite name="unit-tests">
<directory suffix="Test.php">tests</directory>
</testsuite>

View file

@ -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 = '<green>FooBar</green>';
@ -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 = '<black>FooBar</black>';
@ -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 = '<foo>FooBar</foo>';