Generate apache2 and apache 2.4 configuration

refs #6120
This commit is contained in:
Marius Hein 2014-11-05 15:32:09 +01:00
parent 7569c55796
commit bb7f465c0e
4 changed files with 308 additions and 0 deletions

View File

@ -4,7 +4,10 @@
namespace Icinga\Clicommands;
use Icinga\Application\Logger;
use Icinga\Cli\Command;
use Icinga\Config\Webserver\WebServer;
use Icinga\Exception\ProgrammingError;
/**
* Setup Icinga Web 2
@ -120,6 +123,71 @@ class SetupCommand extends Command
printf($this->translate("Successfully created configuration directory at: %s\n"), $path);
}
/**
* Create webserver configuration
*
* USAGE:
*
* icingacli setup webserver <apache2|apache24|nginx> [options]
*
* OPTIONS:
*
* --path=<webpath> Path for the web server, default /icingaweb
*
* --publicPath=<wwwroot> Path to htdocs system path
*
* --file=<filename> Write configuration to file
*
*
* EXAMPLES:
*
* icingacli setup webserver apache24
*
* icingacli setup webserver apache2 --path /icingaweb --publicPath /usr/share/icingaweb/public
*
* icingacli setup webserver apache2 --file /etc/apache2/conf.d/icingaweb.conf
*/
public function webserverAction()
{
if (($type = $this->params->getStandalone()) === null) {
$this->fail($this->translate('Argument type is mandatory.'));
}
try {
$webserver = WebServer::createInstance($type);
} catch (ProgrammingError $e) {
$this->fail($this->translate('Unknown type') . ': ' . $type);
}
$webserver->setApp($this->app);
if (($sapi = $this->params->get('sapi', 'server')) === null) {
$this->fail($this->translate('argument --sapi is mandatory.'));
}
if (($path = $this->params->get('path', '/icingaweb')) === null) {
$this->fail($this->translate('argument --path is mandatory.'));
}
if (($publicPath = $this->params->get('publicPath', $webserver->getPublicPath())) === null) {
$this->fail($this->translate('argument --publicPath is mandatory.'));
}
$webserver->setWebPath($path);
$webserver->setPublicPath($publicPath);
$webserver->setSapi($sapi);
$config = $webserver->generate() . "\n";
if (($file = $this->params->get('file')) !== null) {
if (file_exists($file) === true) {
$this->fail(sprintf($this->translate('File %s already exists. Please delete it first.'), $file));
}
Logger::info($this->translate('Write %s configuration to file: %s'), $type, $file);
$re = file_put_contents($file, $config);
if ($re === false) {
$this->fail($this->translate('Could not write to file') . ': ' . $file);
}
Logger::info($this->translate('Successfully written %d bytes to file'), $re);
return true;
}
printf("# Your %s configuration:\n", $type);
echo $config;
return true;
}
/**
* Return whether the current user is a super user
*

View File

@ -0,0 +1,28 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Config\Webserver;
/**
* Generate apache 2.x (< 2.4) configuration
*/
class Apache2 extends WebServer
{
/**
* @return array
*/
protected function getTemplate()
{
return array(
'Alias {webPath} {publicPath}',
'<directory {publicPath}>',
' Options -Indexes',
' AllowOverride All',
' Order allow,deny',
' Allow from all',
' EnableSendfile Off',
'</directory>'
);
}
}

View File

@ -0,0 +1,26 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Config\Webserver;
/**
* Generate apache2.4 configuration
*/
class Apache24 extends Apache2
{
/**
* Use default template and change granted syntax for 2.4
*
* @return array
*/
protected function getTemplate()
{
$template = parent::getTemplate();
$replace = array(
' Require all granted'
);
array_splice($template, count($template)-4, 2, $replace);
return $template;
}
}

View File

@ -0,0 +1,186 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Config\Webserver;
use Icinga\Application\ApplicationBootstrap;
use Icinga\Exception\ProgrammingError;
/**
* Generate webserver configuration
*/
abstract class WebServer
{
/**
* Web path
*
* @var string
*/
protected $webPath;
/**
* SAPI name (e.g for cgi config generation)
*
* @var string
*/
protected $sapi;
/**
* System path to public documents
*
* @var string
*/
protected $publicPath;
/**
* Application
*
* @var ApplicationBootstrap
*/
protected $app;
/**
* Create instance by type name
*
* @param string $type
*
* @return WebServer
*
* @throws ProgrammingError
*/
public static function createInstance($type)
{
$class = __NAMESPACE__ . '\\' . ucfirst($type);
if (class_exists($class)) {
return new $class();
}
throw new ProgrammingError('Class "%s" does not exist', $class);
}
/**
* Generate configuration
*
* @return string
*/
public function generate()
{
$template = $this->getTemplate();
if (is_array($template)) {
$template = implode(PHP_EOL, $template);
}
$searchTokens = array(
'{webPath}',
'{publicPath}'
);
$replaceTokens = array(
$this->getWebPath(),
$this->getPublicPath()
);
$template = str_replace($searchTokens, $replaceTokens, $template);
return $template;
}
/**
* Specific template
*
* @return array|string
*/
abstract protected function getTemplate();
/**
* Setter for SAPI name
*
* @param string $sapi
*/
public function setSapi($sapi)
{
$this->sapi = $sapi;
}
/**
* Getter for SAPI name
*
* @return string
*/
public function getSapi()
{
return $this->sapi;
}
/**
* Setter for web path
*
* @param string $webPath
*/
public function setWebPath($webPath)
{
$this->webPath = $webPath;
}
/**
* Getter for web path
*
* @return string
*/
public function getWebPath()
{
return $this->webPath;
}
/**
* @param string $publicPath
*/
public function setPublicPath($publicPath)
{
$this->publicPath = $publicPath;
}
/**
* Detect public root
*
* @return string
*/
public function detectPublicPath()
{
$applicationPath = $this->getApp()->getApplicationDir();
$applicationPath = dirname($applicationPath) . DIRECTORY_SEPARATOR . 'public';
if (is_dir($applicationPath) === true) {
return $applicationPath;
}
return null;
}
/**
* Getter for public root
*
* @return string
*/
public function getPublicPath()
{
if ($this->publicPath === null) {
$this->publicPath = $this->detectPublicPath();
}
return $this->publicPath;
}
/**
* Setter for application bootstrap
*
* @param ApplicationBootstrap $app
*/
public function setApp(ApplicationBootstrap $app)
{
$this->app = $app;
}
/**
* Getter for application bootstrap
*
* @return ApplicationBootstrap
*/
public function getApp()
{
return $this->app;
}
}