Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Add task for setting the file permissions (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
deborahvandervegt authored and niels-nijens committed Jul 12, 2016
1 parent 9451447 commit 442d51e
Show file tree
Hide file tree
Showing 6 changed files with 378 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"php": "^5.5 || ^7.0",
"accompli/chrono": "^0.3.0",
"composer/semver": "^1.2",
"gisostallenberg/file-permission-calculator": "^1.0",
"justinrainbow/json-schema": "^1.3 || ^2.0",
"niels-nijens/protocol-stream": "^1.0",
"niels-nijens/utilities": "^3.2",
Expand Down
47 changes: 45 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added docs/images/event-flows/FilePermissionTask.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions docs/tasks/FilePermissionTask.md
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)
107 changes: 107 additions & 0 deletions src/Task/FilePermissionTask.php
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);
}
}
Loading

0 comments on commit 442d51e

Please sign in to comment.