public function switchSetupMode($newMode, $configurationFilename, $user)
{
// log a message that we switch setup mode now
$this->getInitialContext()->getSystemLogger()->info(sprintf('Now switch mode to %s!!!', $newMode));
// init setup context
Setup::prepareContext($this->getBaseDirectory());
// init variable for the group
$group = null;
// pattern to replace the user in the etc/appserver/appserver.xml/appserver-single-app.xml file
$configurationUserReplacePattern = '/(<appserver[^>]+>[^<]+<params>.*<param name="user[^>]+>)([^<]+)/s';
// check setup modes
switch ($newMode) {
// prepares everything for developer mode
case ContainerService::SETUP_MODE_DEV:
// get defined group from configuration
$group = Setup::getValue(SetupKeys::GROUP);
// replace user in configuration file
file_put_contents($configurationFilename, preg_replace($configurationUserReplacePattern, '${1}' . $user, file_get_contents($configurationFilename)));
// replace the user in the PHP-FPM configuration file
file_put_contents($this->getEtcDir('php-fpm.conf'), preg_replace('/user = (.*)/', 'user = ' . $user, file_get_contents($this->getEtcDir('php-fpm.conf'))));
// add everyone write access to configuration files for dev mode
FileSystem::recursiveChmod($this->getEtcDir(), 0777, 0777);
break;
// prepares everything for production mode
// prepares everything for production mode
case ContainerService::SETUP_MODE_PROD:
// get defined user and group from configuration
$user = Setup::getValue(SetupKeys::USER);
$group = Setup::getValue(SetupKeys::GROUP);
// replace user to be same as user in configuration file
file_put_contents($configurationFilename, preg_replace($configurationUserReplacePattern, '${1}' . $user, file_get_contents($configurationFilename)));
// replace the user in the PHP-FPM configuration file
file_put_contents($this->getEtcDir('php-fpm.conf'), preg_replace('/user = (.*)/', 'user = ' . $user, file_get_contents($this->getEtcDir('php-fpm.conf'))));
// set correct file permissions for configurations
FileSystem::recursiveChmod($this->getEtcDir());
break;
// prepares everything for first installation which is default mode
// prepares everything for first installation which is default mode
case ContainerService::SETUP_MODE_INSTALL:
// load the flag marked the server as installed
$isInstalledFlag = $this->getIsInstalledFlag();
// first check if it is a fresh installation
if ($isInstalledFlag->isReadable() === false) {
// first iterate over all containers deploy directories and look for application archives
/** @var \AppserverIo\Appserver\Core\Api\Node\ContainerNodeInterface $containerNode */
foreach ($this->getSystemConfiguration()->getContainers() as $containerNode) {
// iterate over all found application archives and create the .dodeploy flag file
foreach (glob($this->getDeployDir($containerNode, '/*.phar')) as $archive) {
touch(sprintf('%s.dodeploy', $archive));
}
}
}
// create is installed flag for prevent further setup install mode calls
touch($isInstalledFlag);
// get defined user and group from configuration
$user = Setup::getValue(SetupKeys::USER);
$group = Setup::getValue(SetupKeys::GROUP);
// set correct file permissions for configurations
FileSystem::recursiveChmod($this->getEtcDir());
break;
default:
throw new \Exception(sprintf('Invalid setup mode %s given', $newMode));
}
// check if user and group is set
if (!is_null($user) && !is_null($group)) {
// get needed files as accessable for all root files remove "." and ".." from the list
$rootFiles = scandir($this->getBaseDirectory());
// iterate all files
foreach ($rootFiles as $rootFile) {
// we want just files on root dir
if (is_file($rootFile) && !in_array($rootFile, array('.', '..'))) {
FileSystem::chmod($rootFile, 0644);
FileSystem::chown($rootFile, $user, $group);
}
}
// ... and change own and mod of following directories
FileSystem::chown($this->getBaseDirectory(), $user, $group);
FileSystem::recursiveChown($this->getBaseDirectory('resources'), $user, $group);
FileSystem::recursiveChmod($this->getBaseDirectory('resources'));
FileSystem::recursiveChown($this->getBaseDirectory('src'), $user, $group);
FileSystem::recursiveChmod($this->getBaseDirectory('src'));
FileSystem::recursiveChown($this->getBaseDirectory('var'), $user, $group);
FileSystem::recursiveChmod($this->getBaseDirectory('var'));
FileSystem::recursiveChown($this->getBaseDirectory('tests'), $user, $group);
FileSystem::recursiveChmod($this->getBaseDirectory('tests'));
FileSystem::recursiveChown($this->getBaseDirectory('vendor'), $user, $group);
FileSystem::recursiveChmod($this->getBaseDirectory('vendor'));
// ... and the change own and mod for the system's temporary directory
FileSystem::recursiveChown($this->getSystemTmpDir(), $user, $group);
FileSystem::recursiveChmod($this->getSystemTmpDir());
// ... and change own and mod for the container specific directories
/** @var \AppserverIo\Appserver\Core\Api\Node\ContainerNodeInterface $containerNode */
foreach ($this->getSystemConfiguration()->getContainers() as $containerNode) {
FileSystem::recursiveChown($this->getWebappsDir($containerNode), $user, $group);
FileSystem::recursiveChmod($this->getWebappsDir($containerNode));
FileSystem::recursiveChown($this->getTmpDir($containerNode), $user, $group);
FileSystem::recursiveChmod($this->getTmpDir($containerNode));
FileSystem::recursiveChown($this->getDeployDir($containerNode), $user, $group);
FileSystem::recursiveChmod($this->getDeployDir($containerNode));
}
// make server.php executable
FileSystem::chmod($this->getBaseDirectory('server.php'), 0755);
// log a message that we successfully switched to the new setup mode
$this->getInitialContext()->getSystemLogger()->info(sprintf("Setup for mode '%s' done successfully!", $newMode));
} else {
throw new \Exception('No user or group given');
}
}