This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add task for setting the file permissions (#186)
- Loading branch information
1 parent
9451447
commit 442d51e
Showing
6 changed files
with
378 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# FilePermissionTask | ||
|
||
Updates the permissions of the configured files and directories. | ||
|
||
## Configuration options | ||
|
||
| Name | Type | Default value | Description | | ||
|------|------|---------------|-------------| | ||
| paths | array | | The paths for which you want to update the permissions. | | ||
|
||
### Configure the permissions for directories | ||
Example configuration: | ||
```json | ||
{ | ||
"class": "Accompli\\Task\\FilePermissionTask", | ||
"paths": { | ||
"directory/within/release": { | ||
"recursive": true, | ||
"permissions": "-rwxrwx---" | ||
}, | ||
"another/directory/within/release": { | ||
"recursive": true, | ||
"permissions": "-rwxrwxrwx" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
For every directory permissions can be set by adding a permissions key to the configured path. Several variations for the value are: | ||
`-rwx`, `rwxrwx`, `-rwxrwxr`. The `-` at the beginning is optional, so are the rest of the characters at the end. | ||
|
||
Configuring `-rwx` will result in setting the following permissions `-rwx------`. It is advisable to configure the full 10 characters because this makes it easier for others to understand which permissions are set. | ||
|
||
### Configure recursiveness | ||
It's also possible to set the permissions for all subdirectories of the configured path by setting the key `recursive` to true. This configuration setting is optional and can be omitted when false. | ||
|
||
# Event flow | ||
![Flowchart with highlighted events the FilePermissionTask is listening to](../images/event-flows/FilePermissionTask.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace Accompli\Task; | ||
|
||
use Accompli\AccompliEvents; | ||
use Accompli\Deployment\Connection\ConnectionAdapterInterface; | ||
use Accompli\EventDispatcher\Event\InstallReleaseEvent; | ||
use Accompli\EventDispatcher\Event\LogEvent; | ||
use Accompli\EventDispatcher\EventDispatcherInterface; | ||
use Accompli\Exception\TaskRuntimeException; | ||
use GisoStallenberg\FilePermissionCalculator\FilePermissionCalculator; | ||
use Psr\Log\LogLevel; | ||
|
||
/** | ||
* FilePermissionTask. | ||
* | ||
* @author Deborah van der Vegt <deborah@connectholland.nl> | ||
*/ | ||
class FilePermissionTask extends AbstractConnectedTask | ||
{ | ||
/** | ||
* The array with paths to set the permissions in. | ||
* | ||
* @var array | ||
*/ | ||
private $paths; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
AccompliEvents::INSTALL_RELEASE => array( | ||
array('onInstallReleaseUpdateFilePermissions', 0), | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* Constructs a new FilePermissionTask. | ||
* | ||
* @param array $paths | ||
*/ | ||
public function __construct($paths) | ||
{ | ||
$this->paths = $paths; | ||
} | ||
|
||
/** | ||
* Sets the correct permissions and group for the configured path. | ||
* | ||
* @param InstallReleaseEvent $event | ||
* @param string $eventName | ||
* @param EventDispatcherInterface $eventDispatcher | ||
* | ||
* @throws TaskRuntimeException | ||
*/ | ||
public function onInstallReleaseUpdateFilePermissions(InstallReleaseEvent $event, $eventName, EventDispatcherInterface $eventDispatcher) | ||
{ | ||
$host = $event->getRelease()->getWorkspace()->getHost(); | ||
$connection = $this->ensureConnection($host); | ||
|
||
$eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::INFO, 'Updating permissions for configured paths...', $eventName, $this, array('event.task.action' => TaskInterface::ACTION_IN_PROGRESS))); | ||
|
||
$releasePath = $event->getRelease()->getPath(); | ||
|
||
$result = true; | ||
foreach ($this->paths as $path => $pathSettings) { | ||
$result = $result && $this->updateFilePermissions($connection, $releasePath, $path, $pathSettings); | ||
} | ||
|
||
if ($result === true) { | ||
$eventDispatcher->dispatch(AccompliEvents::LOG, new LogEvent(LogLevel::INFO, 'Updated permissions for configured paths.', $eventName, $this, array('event.task.action' => TaskInterface::ACTION_COMPLETED, 'output.resetLine' => true))); | ||
} else { | ||
throw new TaskRuntimeException('Failed updating the permissions for configured paths.', $this); | ||
} | ||
} | ||
|
||
/** | ||
* Update the file permissions per configured path. | ||
* | ||
* @param ConnectionAdapterInterface $connection | ||
* @param string $releasePath | ||
* @param string $path | ||
* @param string $pathSettings | ||
* | ||
* @return bool | ||
*/ | ||
private function updateFilePermissions(ConnectionAdapterInterface $connection, $releasePath, $path, $pathSettings) | ||
{ | ||
$path = $releasePath.'/'.$path; | ||
|
||
if (isset($pathSettings['permissions']) === false) { | ||
return false; | ||
} | ||
|
||
$permissions = FilePermissionCalculator::fromStringRepresentation(str_pad($pathSettings['permissions'], 10, '-'))->getMode(); | ||
|
||
$recursive = false; | ||
if (isset($pathSettings['recursive'])) { | ||
$recursive = $pathSettings['recursive']; | ||
} | ||
|
||
return $connection->changePermissions($path, $permissions, $recursive); | ||
} | ||
} |
Oops, something went wrong.