2014-09-10 14:49:38 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
2014-11-10 16:45:20 +01:00
|
|
|
namespace Icinga\Module\Setup\Clicommands;
|
2014-09-10 14:49:38 +02:00
|
|
|
|
2014-11-05 15:32:09 +01:00
|
|
|
use Icinga\Application\Logger;
|
2014-09-10 14:49:38 +02:00
|
|
|
use Icinga\Cli\Command;
|
2014-11-05 15:32:09 +01:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
2014-11-13 12:50:55 +01:00
|
|
|
use Icinga\Module\Setup\Webserver;
|
2014-09-10 14:49:38 +02:00
|
|
|
|
2014-11-10 16:45:20 +01:00
|
|
|
class ConfigCommand extends Command
|
2014-09-10 14:49:38 +02:00
|
|
|
{
|
2014-10-21 16:23:54 +02:00
|
|
|
/**
|
2014-12-30 14:59:23 +01:00
|
|
|
* Create Icinga Web 2's configuration directory
|
2014-10-21 16:23:54 +02:00
|
|
|
*
|
|
|
|
* This command creates the configuration directory for Icinga Web 2. The `group' argument
|
|
|
|
* is mandatory and should be the groupname of the user your web server is running as.
|
|
|
|
*
|
|
|
|
* USAGE:
|
|
|
|
*
|
2014-12-30 14:59:23 +01:00
|
|
|
* icingacli setup config directory <group> [options]
|
2014-10-21 16:23:54 +02:00
|
|
|
*
|
|
|
|
* OPTIONS:
|
|
|
|
*
|
2014-11-12 09:18:19 +01:00
|
|
|
* --mode The access mode to use. Default is: 2770
|
2014-10-21 16:23:54 +02:00
|
|
|
* --path The path to the configuration directory. If omitted the default is used.
|
|
|
|
*
|
|
|
|
* EXAMPLES:
|
|
|
|
*
|
2014-12-30 14:59:23 +01:00
|
|
|
* icingacli setup config directory apache
|
|
|
|
* icingacli setup config directory apache --mode 2775
|
|
|
|
* icingacli setup config directory apache --path /some/path
|
2014-10-21 16:23:54 +02:00
|
|
|
*/
|
2014-12-30 14:59:23 +01:00
|
|
|
public function directoryAction()
|
2014-10-21 16:23:54 +02:00
|
|
|
{
|
|
|
|
$group = $this->params->getStandalone();
|
|
|
|
if ($group === null) {
|
|
|
|
$this->fail($this->translate('The `group\' argument is mandatory.'));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-28 09:34:28 +01:00
|
|
|
$path = $this->params->get('path', $this->app->getConfigDir());
|
|
|
|
if (file_exists($path)) {
|
2014-10-29 08:46:22 +01:00
|
|
|
printf($this->translate("Configuration directory already exists at: %s\n"), $path);
|
|
|
|
return true;
|
2014-10-27 08:34:37 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 09:18:19 +01:00
|
|
|
$mode = octdec($this->params->get('mode', '2770'));
|
2014-10-28 09:34:28 +01:00
|
|
|
if (false === mkdir($path)) {
|
|
|
|
$this->fail(sprintf($this->translate('Unable to create path: %s'), $path));
|
2014-10-27 08:34:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-21 16:23:54 +02:00
|
|
|
$old = umask(0); // Prevent $mode from being mangled by the system's umask ($old)
|
2014-10-28 09:34:28 +01:00
|
|
|
chmod($path, $mode);
|
2014-10-21 16:23:54 +02:00
|
|
|
umask($old);
|
2014-11-10 16:45:20 +01:00
|
|
|
|
|
|
|
if (chgrp($path, $group) === false) {
|
|
|
|
$this->fail(sprintf($this->translate('Unable to change the group of "%s" to "%s".'), $path, $group));
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-29 08:46:22 +01:00
|
|
|
|
|
|
|
printf($this->translate("Successfully created configuration directory at: %s\n"), $path);
|
2014-10-21 16:23:54 +02:00
|
|
|
}
|
|
|
|
|
2014-11-05 15:32:09 +01:00
|
|
|
/**
|
|
|
|
* Create webserver configuration
|
|
|
|
*
|
|
|
|
* USAGE:
|
|
|
|
*
|
2014-11-13 12:50:55 +01:00
|
|
|
* icingacli setup config webserver <apache|nginx> [options]
|
2014-11-05 15:32:09 +01:00
|
|
|
*
|
|
|
|
* OPTIONS:
|
|
|
|
*
|
2014-12-30 14:41:35 +01:00
|
|
|
* --path=<urlpath> The URL path to Icinga Web 2 [/icingaweb2]
|
2014-11-05 15:32:09 +01:00
|
|
|
*
|
2014-12-30 14:41:35 +01:00
|
|
|
* --root/--document-root=<directory> The directory from which the webserver will serve files [/path/to/icingaweb2/public]
|
2014-11-05 15:32:09 +01:00
|
|
|
*
|
2014-12-30 14:41:35 +01:00
|
|
|
* --config=<directory> Path to Icinga Web 2's configuration files [/etc/icingaweb2]
|
2014-11-13 14:05:13 +01:00
|
|
|
*
|
|
|
|
* --file=<filename> Write configuration to file [stdout]
|
2014-11-05 15:32:09 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* EXAMPLES:
|
|
|
|
*
|
2014-11-13 12:50:55 +01:00
|
|
|
* icingacli setup config webserver apache
|
2014-11-05 15:32:09 +01:00
|
|
|
*
|
2014-12-30 14:41:35 +01:00
|
|
|
* icingacli setup config webserver apache --path /icingaweb2 --document-root /usr/share/icingaweb2/public --config=/etc/icingaweb2
|
2014-11-05 15:32:09 +01:00
|
|
|
*
|
2014-12-30 14:41:35 +01:00
|
|
|
* icingacli setup config webserver apache --file /etc/apache2/conf.d/icingaweb2.conf
|
2014-11-07 10:34:53 +01:00
|
|
|
*
|
2014-11-13 12:50:55 +01:00
|
|
|
* icingacli setup config webserver nginx
|
2014-11-05 15:32:09 +01:00
|
|
|
*/
|
|
|
|
public function webserverAction()
|
|
|
|
{
|
|
|
|
if (($type = $this->params->getStandalone()) === null) {
|
|
|
|
$this->fail($this->translate('Argument type is mandatory.'));
|
|
|
|
}
|
|
|
|
try {
|
2014-11-06 10:41:28 +01:00
|
|
|
$webserver = Webserver::createInstance($type);
|
2014-11-05 15:32:09 +01:00
|
|
|
} catch (ProgrammingError $e) {
|
|
|
|
$this->fail($this->translate('Unknown type') . ': ' . $type);
|
|
|
|
}
|
2014-11-13 14:18:14 +01:00
|
|
|
$urlPath = $this->params->get('path', $webserver->getUrlPath());
|
|
|
|
if (! is_string($urlPath) || strlen(trim($urlPath)) === 0) {
|
2014-11-13 13:30:07 +01:00
|
|
|
$this->fail($this->translate('The argument --path expects a URL path'));
|
2014-11-05 15:32:09 +01:00
|
|
|
}
|
2014-11-13 13:40:39 +01:00
|
|
|
$documentRoot = $this->params->get('root', $this->params->get('document-root', $webserver->getDocumentRoot()));
|
2014-11-13 13:30:07 +01:00
|
|
|
if (! is_string($documentRoot) || strlen(trim($documentRoot)) === 0) {
|
|
|
|
$this->fail($this->translate(
|
2014-11-13 13:40:39 +01:00
|
|
|
'The argument --root/--document-root expects a directory from which the webserver will serve files'
|
2014-11-13 13:30:07 +01:00
|
|
|
));
|
2014-11-05 15:32:09 +01:00
|
|
|
}
|
2014-11-13 14:05:13 +01:00
|
|
|
$configDir = $this->params->get('config', $webserver->getConfigDir());
|
2014-12-30 14:37:34 +01:00
|
|
|
if (! is_string($configDir) || strlen(trim($configDir)) === 0) {
|
2014-11-13 14:05:13 +01:00
|
|
|
$this->fail($this->translate(
|
|
|
|
'The argument --config expects a path to Icinga Web 2\'s configuration files'
|
|
|
|
));
|
|
|
|
}
|
2014-11-13 13:47:08 +01:00
|
|
|
$webserver
|
|
|
|
->setDocumentRoot($documentRoot)
|
2014-11-13 14:05:13 +01:00
|
|
|
->setConfigDir($configDir)
|
2014-11-13 14:18:14 +01:00
|
|
|
->setUrlPath($urlPath);
|
2014-11-05 15:32:09 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
echo $config;
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-10 14:49:38 +02:00
|
|
|
}
|