2014-09-10 08:49:38 -04:00
|
|
|
<?php
|
2016-02-08 09:41:00 -05:00
|
|
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
2014-09-10 08:49:38 -04:00
|
|
|
|
2014-11-10 10:45:20 -05:00
|
|
|
namespace Icinga\Module\Setup\Clicommands;
|
2014-09-10 08:49:38 -04:00
|
|
|
|
2014-11-05 09:32:09 -05:00
|
|
|
use Icinga\Application\Logger;
|
2014-09-10 08:49:38 -04:00
|
|
|
use Icinga\Cli\Command;
|
2014-11-05 09:32:09 -05:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
2014-11-13 06:50:55 -05:00
|
|
|
use Icinga\Module\Setup\Webserver;
|
2014-09-10 08:49:38 -04:00
|
|
|
|
2014-11-10 10:45:20 -05:00
|
|
|
class ConfigCommand extends Command
|
2014-09-10 08:49:38 -04:00
|
|
|
{
|
2014-10-21 10:23:54 -04:00
|
|
|
/**
|
2014-12-30 08:59:23 -05:00
|
|
|
* Create Icinga Web 2's configuration directory
|
2014-10-21 10:23:54 -04:00
|
|
|
*
|
|
|
|
|
* USAGE:
|
|
|
|
|
*
|
2014-12-30 09:22:53 -05:00
|
|
|
* icingacli setup config directory [options]
|
2014-10-21 10:23:54 -04:00
|
|
|
*
|
|
|
|
|
* OPTIONS:
|
|
|
|
|
*
|
2014-12-30 09:24:31 -05:00
|
|
|
* --config=<directory> Path to Icinga Web 2's configuration files [/etc/icingaweb2]
|
2014-12-30 09:22:53 -05:00
|
|
|
*
|
2014-12-30 09:46:22 -05:00
|
|
|
* --mode=<mode> The access mode to use [2770]
|
|
|
|
|
*
|
2014-12-30 09:22:53 -05:00
|
|
|
* --group=<group> Owner group for the configuration directory [icingaweb2]
|
2014-10-21 10:23:54 -04:00
|
|
|
*
|
|
|
|
|
* EXAMPLES:
|
|
|
|
|
*
|
2014-12-30 09:22:53 -05:00
|
|
|
* icingacli setup config directory
|
|
|
|
|
*
|
2015-06-10 11:20:27 -04:00
|
|
|
* icingacli setup config directory --mode=2775 --config=/opt/icingaweb2/etc
|
2014-10-21 10:23:54 -04:00
|
|
|
*/
|
2014-12-30 08:59:23 -05:00
|
|
|
public function directoryAction()
|
2014-10-21 10:23:54 -04:00
|
|
|
{
|
2014-12-30 09:46:22 -05:00
|
|
|
$configDir = trim($this->params->get('config', $this->app->getConfigDir()));
|
|
|
|
|
if (strlen($configDir) === 0) {
|
|
|
|
|
$this->fail($this->translate(
|
|
|
|
|
'The argument --config expects a path to Icinga Web 2\'s configuration files'
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-30 09:22:53 -05:00
|
|
|
$group = trim($this->params->get('group', 'icingaweb2'));
|
|
|
|
|
if (strlen($group) === 0) {
|
|
|
|
|
$this->fail($this->translate(
|
|
|
|
|
'The argument --group expects a owner group for the configuration directory'
|
|
|
|
|
));
|
2014-10-21 10:23:54 -04:00
|
|
|
}
|
|
|
|
|
|
2014-12-30 09:46:22 -05:00
|
|
|
$mode = trim($this->params->get('mode', '2770'));
|
|
|
|
|
if (strlen($mode) === 0) {
|
|
|
|
|
$this->fail($this->translate(
|
|
|
|
|
'The argument --mode expects an access mode for the configuration directory'
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-21 05:12:06 -05:00
|
|
|
if (! file_exists($configDir) && ! @mkdir($configDir, 0755, true)) {
|
2014-12-30 10:03:05 -05:00
|
|
|
$e = error_get_last();
|
|
|
|
|
$this->fail(sprintf(
|
|
|
|
|
$this->translate('Can\'t create configuration directory %s: %s'),
|
|
|
|
|
$configDir,
|
|
|
|
|
$e['message']
|
|
|
|
|
));
|
2014-10-27 03:34:37 -04:00
|
|
|
}
|
|
|
|
|
|
2014-12-30 10:03:05 -05:00
|
|
|
if (! @chmod($configDir, octdec($mode))) {
|
|
|
|
|
$e = error_get_last();
|
2014-12-30 09:54:07 -05:00
|
|
|
$this->fail(sprintf(
|
2014-12-30 10:03:05 -05:00
|
|
|
$this->translate('Can\'t change the mode of the configuration directory to %s: %s'),
|
|
|
|
|
$mode,
|
|
|
|
|
$e['message']
|
2014-12-30 09:51:00 -05:00
|
|
|
));
|
|
|
|
|
}
|
2014-11-10 10:45:20 -05:00
|
|
|
|
2014-12-30 10:03:05 -05:00
|
|
|
if (! @chgrp($configDir, $group)) {
|
|
|
|
|
$e = error_get_last();
|
2014-12-30 09:54:07 -05:00
|
|
|
$this->fail(sprintf(
|
2014-12-30 10:03:05 -05:00
|
|
|
$this->translate('Can\'t change the group of %s to %s: %s'),
|
2014-12-30 09:54:07 -05:00
|
|
|
$configDir,
|
2014-12-30 10:03:05 -05:00
|
|
|
$group,
|
|
|
|
|
$e['message']
|
2014-12-30 09:54:07 -05:00
|
|
|
));
|
2014-11-10 10:45:20 -05:00
|
|
|
}
|
2014-10-29 03:46:22 -04:00
|
|
|
|
2014-12-30 09:55:14 -05:00
|
|
|
printf($this->translate('Successfully created configuration directory %s') . PHP_EOL, $configDir);
|
2014-10-21 10:23:54 -04:00
|
|
|
}
|
|
|
|
|
|
2014-11-05 09:32:09 -05:00
|
|
|
/**
|
|
|
|
|
* Create webserver configuration
|
|
|
|
|
*
|
|
|
|
|
* USAGE:
|
|
|
|
|
*
|
2014-11-13 06:50:55 -05:00
|
|
|
* icingacli setup config webserver <apache|nginx> [options]
|
2014-11-05 09:32:09 -05:00
|
|
|
*
|
|
|
|
|
* OPTIONS:
|
|
|
|
|
*
|
2014-12-30 08:41:35 -05:00
|
|
|
* --path=<urlpath> The URL path to Icinga Web 2 [/icingaweb2]
|
2014-11-05 09:32:09 -05:00
|
|
|
*
|
2017-09-19 08:41:10 -04:00
|
|
|
* --root|--document-root=<directory> The directory from which the webserver will serve files
|
|
|
|
|
* [/path/to/icingaweb2/public]
|
2014-11-05 09:32:09 -05:00
|
|
|
*
|
2018-12-21 09:29:06 -05:00
|
|
|
* --enable-fpm Enable FPM handler for Apache (Nginx is always enabled)
|
|
|
|
|
*
|
2018-01-18 02:54:35 -05:00
|
|
|
* --fpm-uri=<uri> Address or path where to pass requests to FPM [127.0.0.1:9000]
|
|
|
|
|
*
|
2014-12-30 08:41:35 -05:00
|
|
|
* --config=<directory> Path to Icinga Web 2's configuration files [/etc/icingaweb2]
|
2014-11-13 08:05:13 -05:00
|
|
|
*
|
|
|
|
|
* --file=<filename> Write configuration to file [stdout]
|
2014-11-05 09:32:09 -05:00
|
|
|
*
|
|
|
|
|
* EXAMPLES:
|
|
|
|
|
*
|
2014-12-30 09:22:53 -05:00
|
|
|
* icingacli setup config webserver apache
|
2014-11-05 09:32:09 -05:00
|
|
|
*
|
2017-09-19 08:41:10 -04:00
|
|
|
* icingacli setup config webserver apache \
|
|
|
|
|
* --path=/icingaweb2 \
|
|
|
|
|
* --document-root=/usr/share/icingaweb2/public \
|
|
|
|
|
* --config=/etc/icingaweb2
|
2014-11-05 09:32:09 -05:00
|
|
|
*
|
2017-09-19 08:41:10 -04:00
|
|
|
* icingacli setup config webserver apache \
|
|
|
|
|
* --file=/etc/apache2/conf.d/icingaweb2.conf
|
2014-11-07 04:34:53 -05:00
|
|
|
*
|
2018-01-18 02:54:35 -05:00
|
|
|
* icingacli setup config webserver nginx \
|
|
|
|
|
* --root=/usr/share/icingaweb2/public \
|
|
|
|
|
* --fpm-uri=unix:/var/run/php5-fpm.sock
|
2014-11-05 09:32:09 -05:00
|
|
|
*/
|
|
|
|
|
public function webserverAction()
|
|
|
|
|
{
|
|
|
|
|
if (($type = $this->params->getStandalone()) === null) {
|
|
|
|
|
$this->fail($this->translate('Argument type is mandatory.'));
|
|
|
|
|
}
|
|
|
|
|
try {
|
2014-11-06 04:41:28 -05:00
|
|
|
$webserver = Webserver::createInstance($type);
|
2014-11-05 09:32:09 -05:00
|
|
|
} catch (ProgrammingError $e) {
|
|
|
|
|
$this->fail($this->translate('Unknown type') . ': ' . $type);
|
|
|
|
|
}
|
2014-12-30 12:49:47 -05:00
|
|
|
$urlPath = trim($this->params->get('path', $webserver->getUrlPath()));
|
|
|
|
|
if (strlen($urlPath) === 0) {
|
2014-11-13 07:30:07 -05:00
|
|
|
$this->fail($this->translate('The argument --path expects a URL path'));
|
2014-11-05 09:32:09 -05:00
|
|
|
}
|
2014-12-30 12:49:47 -05:00
|
|
|
$documentRoot = trim(
|
|
|
|
|
$this->params->get('root', $this->params->get('document-root', $webserver->getDocumentRoot()))
|
|
|
|
|
);
|
|
|
|
|
if (strlen($documentRoot) === 0) {
|
2014-11-13 07:30:07 -05:00
|
|
|
$this->fail($this->translate(
|
2014-11-13 07:40:39 -05:00
|
|
|
'The argument --root/--document-root expects a directory from which the webserver will serve files'
|
2014-11-13 07:30:07 -05:00
|
|
|
));
|
2014-11-05 09:32:09 -05:00
|
|
|
}
|
2014-12-30 12:49:47 -05:00
|
|
|
$configDir = trim($this->params->get('config', $webserver->getConfigDir()));
|
|
|
|
|
if (strlen($configDir) === 0) {
|
2014-11-13 08:05:13 -05:00
|
|
|
$this->fail($this->translate(
|
|
|
|
|
'The argument --config expects a path to Icinga Web 2\'s configuration files'
|
|
|
|
|
));
|
|
|
|
|
}
|
2018-12-21 09:29:06 -05:00
|
|
|
|
|
|
|
|
$enableFpm = $this->params->shift('enable-fpm', $webserver->getEnableFpm());
|
|
|
|
|
|
2018-01-18 02:54:35 -05:00
|
|
|
$fpmUri = trim($this->params->get('fpm-uri', $webserver->getFpmUri()));
|
|
|
|
|
if (empty($fpmUri)) {
|
|
|
|
|
$this->fail($this->translate(
|
|
|
|
|
'The argument --fpm-uri expects an address or path where to pass requests to FPM'
|
|
|
|
|
));
|
|
|
|
|
}
|
2014-11-13 07:47:08 -05:00
|
|
|
$webserver
|
|
|
|
|
->setDocumentRoot($documentRoot)
|
2014-11-13 08:05:13 -05:00
|
|
|
->setConfigDir($configDir)
|
2018-01-18 02:54:35 -05:00
|
|
|
->setUrlPath($urlPath)
|
2018-12-21 09:29:06 -05:00
|
|
|
->setEnableFpm($enableFpm)
|
2018-01-18 02:54:35 -05:00
|
|
|
->setFpmUri($fpmUri);
|
2014-11-05 09:32:09 -05: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 08:49:38 -04:00
|
|
|
}
|