setup: Add --config switch to TokenCommand::createAction()

refs #7906
This commit is contained in:
Eric Lippmann 2014-12-30 14:55:44 +01:00
parent 59dc5c5bc3
commit 4b6ea55cbb
1 changed files with 18 additions and 7 deletions

View File

@ -54,24 +54,35 @@ class TokenCommand extends Command
* *
* USAGE: * USAGE:
* *
* icingacli setup token create * icingacli setup token create [options]
*
* OPTIONS:
*
* --config=<directory> Path to Icinga Web 2's configuration files [/etc/icingaweb2]
*/ */
public function createAction() public function createAction()
{ {
$configDir = $this->params->get('config', $this->app->getConfigDir());
if (! is_string($configDir) || strlen(trim($configDir)) === 0) {
$this->fail($this->translate(
'The argument --config expects a path to Icinga Web 2\'s configuration files'
));
}
$file = $configDir . '/setup.token';
if (function_exists('openssl_random_pseudo_bytes')) { if (function_exists('openssl_random_pseudo_bytes')) {
$token = bin2hex(openssl_random_pseudo_bytes(8)); $token = bin2hex(openssl_random_pseudo_bytes(8));
} else { } else {
$token = substr(md5(mt_rand()), 16); $token = substr(md5(mt_rand()), 16);
} }
$filepath = $this->app->getConfigDir() . '/setup.token'; if (false === file_put_contents($file, $token)) {
$this->fail(sprintf($this->translate('Cannot write setup token "%s" to disk.'), $file));
if (false === file_put_contents($filepath, $token)) {
$this->fail(sprintf($this->translate('Cannot write setup token "%s" to disk.'), $filepath));
} }
if (false === chmod($filepath, 0660)) { if (! chmod($file, 0660)) {
$this->fail(sprintf($this->translate('Cannot change access mode of "%s" to %o.'), $filepath, 0660)); $this->fail(sprintf($this->translate('Cannot change access mode of "%s" to %o.'), $file, 0660));
} }
printf($this->translate("The newly generated setup token is: %s\n"), $token); printf($this->translate("The newly generated setup token is: %s\n"), $token);