Skip to content

Commit

Permalink
Merge pull request #154 from wagnert/master
Browse files Browse the repository at this point in the history
Add provisioning service + move setting access rights to API services
  • Loading branch information
wagnert committed May 9, 2014
2 parents 81209b1 + a2673f1 commit cf2f05e
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 84 deletions.
83 changes: 8 additions & 75 deletions src/TechDivision/ApplicationServer/AbstractExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,95 +401,28 @@ public function getService()
{
return $this->service;
}

/**
* Sets the configured user/group settings on the passed file.
*
*
* @param \SplFileInfo $fileInfo The file to set user/group for
*
*
* @return void
*/
protected function setUserRight(\SplFileInfo $fileInfo)
{

// don't do anything under windows
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
return;
}

// Get our system configuration as it contains the user and group to set
$systemConfiguration = $this->getInitialContext()->getSystemConfiguration();

// Check for the existence of a user
$user = $systemConfiguration->getParam('user');
if (!empty($user)) {
chown($fileInfo, $user);
}

// Check for the existence of a group
$group = $systemConfiguration->getParam('group');
if (!empty($group)) {
chgrp($fileInfo, $group);
}
$this->getService()->setUserRight($fileInfo);
}

/**
* Will set the owner and group of a
* Will set the owner and group on the passed directory.
*
* @param string $targetDir The directory to set the rights for
* @param \SplFileInfo $targetDir The directory to set the rights for
*
* @return void
*/
protected function setUserRights($targetDir)
protected function setUserRights(\SplFileInfo $targetDir)
{
// we don't do anything under Windows
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
return;
}

// we don't have a directory to change the user/group permissions for
if (!is_dir($targetDir)) {
return;
}

// Get our system configuration as it contains the user and group to set
$systemConfiguration = $this->getInitialContext()->getSystemConfiguration();

// As we might have several rootPaths we have to create several RecursiveDirectoryIterators.
$directoryIterator = new \RecursiveDirectoryIterator(
$targetDir,
\RecursiveIteratorIterator::SELF_FIRST
);

// We got them all, now append them onto a new RecursiveIteratorIterator and return it.
$recursiveIterator = new \AppendIterator();
// Append the directory iterator
$recursiveIterator->append(
new \RecursiveIteratorIterator(
$directoryIterator,
\RecursiveIteratorIterator::SELF_FIRST,
\RecursiveIteratorIterator::CATCH_GET_CHILD
)
);

// Check for the existence of a user
$user = $systemConfiguration->getParam('user');
if (!empty($user)) {

// Change the rights of everything within the defined dirs
foreach ($recursiveIterator as $file) {
chown($file, $user);
}
}

// Check for the existence of a group
$group = $systemConfiguration->getParam('group');
if (!empty($group)) {

// Change the rights of everything within the defined dirs
foreach ($recursiveIterator as $file) {
chgrp($file, $group);
}
}
$this->getService()->setUserRights($targetDir);
}
}
91 changes: 91 additions & 0 deletions src/TechDivision/ApplicationServer/Api/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,95 @@ public function persist(NodeInterface $node)
{
// implement this
}

/**
* Sets the configured user/group settings on the passed file.
*
* @param \SplFileInfo $fileInfo The file to set user/group for
*
* @return void
*/
public function setUserRight(\SplFileInfo $fileInfo)
{

// don't do anything under windows
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
return;
}

// Get our system configuration as it contains the user and group to set
$systemConfiguration = $this->getInitialContext()->getSystemConfiguration();

// Check for the existence of a user
$user = $systemConfiguration->getParam('user');
if (!empty($user)) {
chown($fileInfo, $user);
}

// Check for the existence of a group
$group = $systemConfiguration->getParam('group');
if (!empty($group)) {
chgrp($fileInfo, $group);
}
}

/**
* Will set the owner and group on the passed directory.
*
* @param \SplFileInfo $targetDir The directory to set the rights for
*
* @return void
*/
public function setUserRights(\SplFileInfo $targetDir)
{
// we don't do anything under Windows
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
return;
}

// we don't have a directory to change the user/group permissions for
if ($targetDir->isDir() === false) {
return;
}

// Get our system configuration as it contains the user and group to set
$systemConfiguration = $this->getInitialContext()->getSystemConfiguration();

// As we might have several rootPaths we have to create several RecursiveDirectoryIterators.
$directoryIterator = new \RecursiveDirectoryIterator(
$targetDir,
\RecursiveIteratorIterator::SELF_FIRST
);

// We got them all, now append them onto a new RecursiveIteratorIterator and return it.
$recursiveIterator = new \AppendIterator();
// Append the directory iterator
$recursiveIterator->append(
new \RecursiveIteratorIterator(
$directoryIterator,
\RecursiveIteratorIterator::SELF_FIRST,
\RecursiveIteratorIterator::CATCH_GET_CHILD
)
);

// Check for the existence of a user
$user = $systemConfiguration->getParam('user');
if (!empty($user)) {

// Change the rights of everything within the defined dirs
foreach ($recursiveIterator as $file) {
chown($file, $user);
}
}

// Check for the existence of a group
$group = $systemConfiguration->getParam('group');
if (!empty($group)) {

// Change the rights of everything within the defined dirs
foreach ($recursiveIterator as $file) {
chgrp($file, $group);
}
}
}
}
32 changes: 32 additions & 0 deletions src/TechDivision/ApplicationServer/Api/ProvisioningService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* TechDivision\ApplicationServer\Api\ProvisioningService
*
* PHP version 5
*
* @category Appserver
* @package TechDivision_ApplicationServer
* @subpackage Api
* @author Tim Wagner <tw@techdivision.com>
* @copyright 2013 TechDivision GmbH <info@techdivision.com>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link http://www.appserver.io
*/

namespace TechDivision\ApplicationServer\Api;

/**
* A service that provides methods needed for application provisioning.
*
* @category Appserver
* @package TechDivision_ApplicationServer
* @subpackage Api
* @author Tim Wagner <tw@techdivision.com>
* @copyright 2013 TechDivision GmbH <info@techdivision.com>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link http://www.appserver.io
*/
class ProvisioningService extends DatasourceService
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function deployArchive(\SplFileInfo $archive)
$this->restoreBackup($archive);

// We have to set the user rights to the user:group configured within the system configuration
$this->setUserRights($webappFolderName);
$this->setUserRights(new \SplFileInfo($webappFolderName));

// flag webapp as deployed
$this->flagArchive($archive, ExtractorInterface::FLAG_DEPLOYED);
Expand Down Expand Up @@ -159,6 +159,6 @@ public function backupArchive(\SplFileInfo $archive)
$this->copyDir($webappFolderName, $tmpFolderName);

// we have to set the user rights to the user:group configured within the system configuration
$this->setUserRights($tmpFolderName);
$this->setUserRights(new \SplFileInfo($tmpFolderName));
}
}
30 changes: 30 additions & 0 deletions src/TechDivision/ApplicationServer/Provisioning/AbstractStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use TechDivision\ApplicationServer\Api\Node\StepNode;
use TechDivision\ApplicationServer\Api\Node\DatasourceNode;
use TechDivision\ApplicationServer\Api\ServiceInterface;

/**
* Abstract base class for a step implementation.
Expand All @@ -33,6 +34,13 @@
abstract class AbstractStep implements Step
{

/**
* The provisioning service.
*
* @var \TechDivision\ApplicationServer\Api\ServiceInterface;
*/
protected $service;

/**
* The step node with the configuration data for this step.
*
Expand Down Expand Up @@ -61,6 +69,18 @@ abstract class AbstractStep implements Step
*/
protected $webappPath;

/**
* Injects the provisioning service.
*
* @param \TechDivision\ApplicationServer\Api\ServiceInterface $service The provisioning service
*
* @return void
*/
public function injectService(ServiceInterface $service)
{
$this->service = $service;
}

/**
* Injects the step node with the configuration data for this step.
*
Expand Down Expand Up @@ -109,6 +129,16 @@ public function injectWebappPath($webappPath)
$this->webappPath = $webappPath;
}

/**
* Returns the provisioning service.
*
* @return \TechDivision\ApplicationServer\Api\ServiceInterface The provisioning service
*/
public function getService()
{
return $this->service;
}

/**
* Returns the step node data.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,35 @@ class CreateDatabaseStep extends AbstractStep
*/
const PARAM_PATH_TO_ENTITIES = 'pathToEntities';

/**
* The DB connection parameter with the path the database file.
*
* @var string
*/
const CONNECTION_PARAM_PATH = 'path';

/**
* The DB connection parameter with the driver to use.
*
* @var string
*/
const CONNECTION_PARAM_DRIVER = 'driver';

/**
* The DB connection parameter with user to connect.
*
* @var string
*/
const CONNECTION_PARAM_USER = 'user';


/**
* The DB connection parameter with the passwort to connect.
*
* @var string
*/
const CONNECTION_PARAM_PASSWORD = 'password';

/**
* Executes the functionality for this step, in this case the execution of
* the PHP script defined in the step configuration.
Expand All @@ -67,9 +96,12 @@ public function execute()
}
}

// load the database connection parameters
$connectionParameters = $this->getConnectionParameters();

// initialize and load the entity manager and the schema tool
$metadataConfiguration = Setup::createAnnotationMetadataConfiguration($absolutePaths, true);
$entityManager = EntityManager::create($this->getConnectionParameters(), $metadataConfiguration);
$entityManager = EntityManager::create($connectionParameters, $metadataConfiguration);
$schemaTool = new SchemaTool($entityManager);

// load the class definitions
Expand All @@ -78,6 +110,11 @@ public function execute()
// drop the schema if it already exists and create it new
$schemaTool->dropSchema($classes);
$schemaTool->createSchema($classes);

// set the user rights for the database we've created
if (isset($connectionParameters[CreateDatabaseStep::CONNECTION_PARAM_PATH])) {
$this->getService()->setUserRight(new \SplFileInfo($connectionParameters[CreateDatabaseStep::CONNECTION_PARAM_PATH]));
}
}

/**
Expand All @@ -97,14 +134,14 @@ public function getConnectionParameters()

// initialize the connection parameters
$connectionParameters = array(
'driver' => $databaseNode->getDriver()->getNodeValue()->__toString(),
'user' => $databaseNode->getUser()->getNodeValue()->__toString(),
'password' => $databaseNode->getPassword()->getNodeValue()->__toString()
CreateDatabaseStep::CONNECTION_PARAM_DRIVER => $databaseNode->getDriver()->getNodeValue()->__toString(),
CreateDatabaseStep::CONNECTION_PARAM_USER => $databaseNode->getUser()->getNodeValue()->__toString(),
CreateDatabaseStep::CONNECTION_PARAM_PASSWORD => $databaseNode->getPassword()->getNodeValue()->__toString()
);

// initialize the path to the database when we use sqlite for example
if ($path = $databaseNode->getPath()->getNodeValue()->__toString()) {
$connectionParameters['path'] = $this->getWebappPath() . DIRECTORY_SEPARATOR . $path;
$connectionParameters[CreateDatabaseStep::CONNECTION_PARAM_PATH] = $this->getWebappPath() . DIRECTORY_SEPARATOR . $path;
}

// set the connection parameters
Expand Down

0 comments on commit cf2f05e

Please sign in to comment.