Files
librenms-librenms/app/Http/Controllers/Install/FinalizeController.php

153 lines
4.4 KiB
PHP
Raw Normal View History

2020-06-05 14:26:57 -05:00
<?php
/**
* FinalizeController.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2020 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Http\Controllers\Install;
2020-06-08 02:21:03 -05:00
use Exception;
2020-06-21 13:51:47 -05:00
use LibreNMS\Exceptions\FileWriteFailedException;
2020-06-15 00:45:40 -05:00
use LibreNMS\Interfaces\InstallerStep;
2020-06-08 02:21:03 -05:00
use LibreNMS\Util\EnvHelper;
2020-06-15 00:45:40 -05:00
class FinalizeController extends InstallationController implements InstallerStep
2020-06-05 14:26:57 -05:00
{
2020-06-21 01:07:56 -05:00
protected $step = 'finish';
2020-06-11 01:06:16 -05:00
public function index()
2020-06-05 14:26:57 -05:00
{
2020-06-21 01:07:56 -05:00
if (!$this->initInstallStep()) {
return $this->redirectToIncomplete();
2020-06-11 01:06:16 -05:00
}
2020-06-08 02:21:03 -05:00
$env = '';
2020-06-21 13:51:47 -05:00
$config = '';
2020-06-09 23:24:43 -05:00
$config_file = base_path('config.php');
2020-06-08 02:21:03 -05:00
$messages = [];
2020-06-21 13:51:47 -05:00
$success = false;
2020-06-09 23:24:43 -05:00
$config_message = file_exists($config_file) ? trans('install.finish.config_exists') : trans('install.finish.config_written');
2020-06-21 13:51:47 -05:00
$env_message = trans('install.finish.env_written');
2020-06-08 02:21:03 -05:00
try {
2020-06-19 00:17:36 -05:00
$this->writeConfigFile();
2020-06-08 02:21:03 -05:00
} catch (Exception $e) {
2020-06-21 13:51:47 -05:00
$config = $this->getConfigFileContents();
2020-06-09 23:24:43 -05:00
$config_message = trans('install.finish.config_not_written');
2020-06-08 02:21:03 -05:00
}
2020-06-21 13:51:47 -05:00
try {
$this->writeEnvFile();
$success = true;
2020-06-11 01:58:42 -05:00
session()->flush();
2020-06-21 13:51:47 -05:00
} catch (Exception $e) {
$env = $this->getEnvFileContents();
$messages[] = $e->getMessage();
$env_message = trans('install.finish.env_not_written');
2020-06-09 23:24:43 -05:00
}
2020-06-11 01:06:16 -05:00
return view('install.finish', $this->formatData([
2020-06-21 13:51:47 -05:00
'success' => $success,
2020-06-08 02:21:03 -05:00
'env' => $env,
'config' => $config,
'messages' => $messages,
2020-06-21 13:51:47 -05:00
'env_message' => $env_message,
2020-06-09 23:24:43 -05:00
'config_message' => $config_message,
2020-06-11 01:06:16 -05:00
]));
2020-06-08 02:21:03 -05:00
}
private function writeEnvFile()
2020-06-21 13:51:47 -05:00
{
return EnvHelper::writeEnv(
$this->envVars(),
['INSTALL'],
base_path('.env')
);
}
private function envVars()
2020-06-08 02:21:03 -05:00
{
2020-06-19 00:17:36 -05:00
$this->configureDatabase();
$connection = config('database.default', $this->connection);
2020-06-21 13:51:47 -05:00
$port = config("database.connections.$connection.port");
return [
2020-06-08 02:21:03 -05:00
'NODE_ID' => uniqid(),
2020-06-19 00:17:36 -05:00
'DB_HOST' => config("database.connections.$connection.host"),
2020-06-21 13:51:47 -05:00
'DB_PORT' => $port == 3306 ? null : $port, // don't set default port
2020-06-19 00:17:36 -05:00
'DB_USERNAME' => config("database.connections.$connection.username"),
'DB_PASSWORD' => config("database.connections.$connection.password"),
'DB_DATABASE' => config("database.connections.$connection.database"),
'DB_SOCKET' => config("database.connections.$connection.unix_socket"),
2020-06-21 13:51:47 -05:00
];
2020-06-05 14:26:57 -05:00
}
2020-06-06 17:03:32 -05:00
2020-06-21 13:51:47 -05:00
/**
* @throws \LibreNMS\Exceptions\FileWriteFailedException
*/
2020-06-19 00:17:36 -05:00
private function writeConfigFile()
2020-06-06 17:03:32 -05:00
{
2020-06-19 00:17:36 -05:00
$config_file = base_path('config.php');
if (file_exists($config_file)) {
return;
}
2020-06-08 02:21:03 -05:00
2020-06-19 00:17:36 -05:00
if (!copy(base_path('config.php.default'), $config_file)) {
2020-06-21 13:51:47 -05:00
throw new FileWriteFailedException($config_file);
2020-06-06 17:03:32 -05:00
}
}
private function getConfigFileContents()
{
2020-06-19 00:17:36 -05:00
return file_get_contents(base_path('config.php.default'));
2020-06-06 17:03:32 -05:00
}
2020-06-15 00:45:40 -05:00
2020-06-21 13:51:47 -05:00
private function getEnvFileContents()
{
return EnvHelper::setEnv(
file_get_contents(base_path('.env')),
$this->envVars(),
['INSTALL']
);
}
2020-06-15 00:45:40 -05:00
public function enabled(): bool
{
foreach ($this->steps as $step => $controller) {
if ($step !== 'finish' && !session("install.$step")) {
return false;
}
}
return true;
}
public function complete(): bool
{
return false;
}
public function icon(): string
{
return 'fa-check';
}
2020-06-05 14:26:57 -05:00
}