diff --git a/Mage/Task/BuiltIn/Symfony2/ApplyFaclsTask.php b/Mage/Task/BuiltIn/Symfony2/ApplyFaclsTask.php new file mode 100644 index 0000000..572af3e --- /dev/null +++ b/Mage/Task/BuiltIn/Symfony2/ApplyFaclsTask.php @@ -0,0 +1,64 @@ + + */ +class ApplyFaclsTask extends AbstractTask implements IsReleaseAware +{ + /** + * Returns the Title of the Task + * @return string + */ + public function getName() + { + return 'Set Symfony file ACLs on remote system [built-in]'; + } + + /** + * Runs the task + * + * @return boolean + * @throws SkipException + */ + public function run() + { + $releasesDirectory = $this->getConfig()->release('directory', 'releases'); + $currentCopy = $releasesDirectory . '/' . $this->getConfig()->getReleaseId(); + + + $httpUser = $this->getParameter('httpuser', ''); + if (empty($httpUser)) { + throw new SkipException('Parameter httpuser not set.'); + } + $localuser = $this->getParameter('localuser', ''); + if (empty($localuser)) { + throw new SkipException('Parameter localuser not set.'); + } + $folders = $this->getParameter('folders', array()); + + foreach ($folders as $folder) { + $folderPath = $currentCopy."/".$folder; + $this->runCommandRemote($this->createFaclCommand('-R', $httpUser,$localuser, $folderPath), $output); + $this->runCommandRemote($this->createFaclCommand('-dR', $httpUser,$localuser, $folderPath), $output); + } + + return true; + } + + public function createFaclCommand($setFaclOptions, $httpUser,$localuser, $folder) + { + return sprintf('setfacl %s -m u:%s:rwX -m u:%s:rwX %s', + $setFaclOptions, $httpUser, $localuser, $folder + ); + } + +} diff --git a/docs/example-config/.mage/config/environment/production.yml b/docs/example-config/.mage/config/environment/production.yml index 1af997f..0fa9374 100644 --- a/docs/example-config/.mage/config/environment/production.yml +++ b/docs/example-config/.mage/config/environment/production.yml @@ -21,6 +21,12 @@ tasks: - privileges - sampleTask - sampleTaskRollbackAware + - symfony2/apply-facls: + httpuser: www-data + localuser: deployer + folders: + - var + - web verbose_logging: true env: variables: diff --git a/tests/MageTest/Task/BuiltIn/Symfony2/ApplyFaclsTaskTest.php b/tests/MageTest/Task/BuiltIn/Symfony2/ApplyFaclsTaskTest.php new file mode 100644 index 0000000..e4274f4 --- /dev/null +++ b/tests/MageTest/Task/BuiltIn/Symfony2/ApplyFaclsTaskTest.php @@ -0,0 +1,109 @@ +config = $this->getMock('Mage\\Config'); + } + + /** + * @covers Mage\Task\Factory::get + */ + public function testGet() + { + $task = Factory::get('symfony2/apply-facls', $this->config); + $this->assertInstanceOf('\\Mage\\Task\\BuiltIn\\Symfony2\\ApplyFaclsTask', $task); + } + + /** + * @covers Mage\Task\Factory::get + */ + public function testGetTaskDataIsArray() + { + $taskData = array( + 'name' => 'symfony2/apply-facls', + 'parameters' => array( + 'httpuser' => 'www-data', + 'localuser' => 'deployer', + 'folders' => array('var', 'web') + ), + ); + + $task = Factory::get($taskData, $this->config); + $this->assertInstanceOf('\\Mage\\Task\\BuiltIn\\Symfony2\\ApplyFaclsTask', $task); + } + + /** + * @expectedException \Mage\Task\SkipException + * @expectedExceptionMessage Parameter httpuser not set. + * @covers Mage\Task\BuiltIn\Symfony2\ApplyFaclsTask::run + */ + public function test_Run_WithNoHttpUserParameter_Expect_throw_SkipException() + { + // Arrange + $config = new Config(); + $config->addParameter('httpuser', ''); + $task = new ApplyFaclsTask($config); + // Act + $task->run(); + } + + /** + * @group unit + * @expectedException \Mage\Task\SkipException + * @expectedExceptionMessage Parameter localuser not set. + * @covers Mage\Task\BuiltIn\Symfony2\ApplyFaclsTask::run + */ + public function test_run_withNoLocalUserParameter_trow_SkipException() + { + // Arrange + $config = new Config(); + $config->addParameter('httpuser', 'www-data'); + $config->addParameter('localuser', ''); + $task = new ApplyFaclsTask($config); + // Act + $task->run(); + } + + public function provider_createFaclsCommand() + { + return array( + 'no facls options' => array( + '', 'www-data', 'deployer', 'var', 'setfacl -m u:www-data:rwX -m u:deployer:rwX var' + ), + 'with facls options' => array( + '-dR', 'www-data', 'deployer', 'var', 'setfacl -dR -m u:www-data:rwX -m u:deployer:rwX var' + ) + ); + } + + /** + * @group unit + * @dataProvider provider_createFaclsCommand + * @covers Mage\Task\BuiltIn\Symfony2\ApplyFaclsTask::createFaclCommand + * + * @param string $faclOptions + * @param string $httpUser + * @param string $localUser + * @param string $folder + * @param string $expectedCommand + */ + public function test_createFaclsCommand($faclOptions, $httpUser, $localUser, $folder, $expectedCommand) + { + // Arrange + $task = new ApplyFaclsTask($this->config); + // Act + $command = $task->createFaclCommand($faclOptions, $httpUser, $localUser, $folder); + // Assert + $this->assertEquals($expectedCommand, $command); + } +}