refined finish

This commit is contained in:
Tony Murray
2020-06-21 13:51:47 -05:00
parent 2be176f45a
commit 2c82e919f6
4 changed files with 76 additions and 32 deletions

View File

@@ -26,6 +26,7 @@
namespace App\Http\Controllers\Install; namespace App\Http\Controllers\Install;
use Exception; use Exception;
use LibreNMS\Exceptions\FileWriteFailedException;
use LibreNMS\Interfaces\InstallerStep; use LibreNMS\Interfaces\InstallerStep;
use LibreNMS\Util\EnvHelper; use LibreNMS\Util\EnvHelper;
@@ -40,58 +41,68 @@ class FinalizeController extends InstallationController implements InstallerStep
} }
$env = ''; $env = '';
$config = '';
$config_file = base_path('config.php'); $config_file = base_path('config.php');
$config = $this->getConfigFileContents();
$messages = []; $messages = [];
$success = true; $success = false;
$config_message = file_exists($config_file) ? trans('install.finish.config_exists') : trans('install.finish.config_written'); $config_message = file_exists($config_file) ? trans('install.finish.config_exists') : trans('install.finish.config_written');
$env_message = trans('install.finish.env_written');
try { try {
$this->writeConfigFile(); $this->writeConfigFile();
} catch (Exception $e) { } catch (Exception $e) {
$messages[] = $e->getMessage(); $config = $this->getConfigFileContents();
$config_message = trans('install.finish.config_not_written'); $config_message = trans('install.finish.config_not_written');
}
try {
$this->writeEnvFile();
$success = true; $success = true;
}
// write env last only if everything else succeeded
if ($success) {
try {
$env = $this->writeEnvFile();
} catch (Exception $e) {
$messages[] = $e->getMessage();
$success = false;
}
}
if ($success) {
session()->flush(); session()->flush();
} catch (Exception $e) {
$env = $this->getEnvFileContents();
$messages[] = $e->getMessage();
$env_message = trans('install.finish.env_not_written');
} }
return view('install.finish', $this->formatData([ return view('install.finish', $this->formatData([
'success' => $success,
'env' => $env, 'env' => $env,
'config' => $config, 'config' => $config,
'messages' => $messages, 'messages' => $messages,
'success' => $success, 'env_message' => $env_message,
'config_message' => $config_message, 'config_message' => $config_message,
])); ]));
} }
private function writeEnvFile() private function writeEnvFile()
{
return EnvHelper::writeEnv(
$this->envVars(),
['INSTALL'],
base_path('.env')
);
}
private function envVars()
{ {
$this->configureDatabase(); $this->configureDatabase();
$connection = config('database.default', $this->connection); $connection = config('database.default', $this->connection);
return EnvHelper::writeEnv([ $port = config("database.connections.$connection.port");
return [
'NODE_ID' => uniqid(), 'NODE_ID' => uniqid(),
'DB_HOST' => config("database.connections.$connection.host"), 'DB_HOST' => config("database.connections.$connection.host"),
'DB_PORT' => config("database.connections.$connection.port"), 'DB_PORT' => $port == 3306 ? null : $port, // don't set default port
'DB_USERNAME' => config("database.connections.$connection.username"), 'DB_USERNAME' => config("database.connections.$connection.username"),
'DB_PASSWORD' => config("database.connections.$connection.password"), 'DB_PASSWORD' => config("database.connections.$connection.password"),
'DB_DATABASE' => config("database.connections.$connection.database"), 'DB_DATABASE' => config("database.connections.$connection.database"),
'DB_SOCKET' => config("database.connections.$connection.unix_socket"), 'DB_SOCKET' => config("database.connections.$connection.unix_socket"),
], ['INSTALL'], base_path('.env')); ];
} }
/**
* @throws \LibreNMS\Exceptions\FileWriteFailedException
*/
private function writeConfigFile() private function writeConfigFile()
{ {
$config_file = base_path('config.php'); $config_file = base_path('config.php');
@@ -100,7 +111,7 @@ class FinalizeController extends InstallationController implements InstallerStep
} }
if (!copy(base_path('config.php.default'), $config_file)) { if (!copy(base_path('config.php.default'), $config_file)) {
throw new Exception("We couldn't create the config.php file, please create this manually before continuing by copying the below into a config.php in the root directory of your install (typically /opt/librenms/)"); throw new FileWriteFailedException($config_file);
} }
} }
@@ -109,6 +120,15 @@ class FinalizeController extends InstallationController implements InstallerStep
return file_get_contents(base_path('config.php.default')); return file_get_contents(base_path('config.php.default'));
} }
private function getEnvFileContents()
{
return EnvHelper::setEnv(
file_get_contents(base_path('.env')),
$this->envVars(),
['INSTALL']
);
}
public function enabled(): bool public function enabled(): bool
{ {
foreach ($this->steps as $step => $controller) { foreach ($this->steps as $step => $controller) {

View File

@@ -49,6 +49,7 @@ class InstallationController extends Controller
public function redirectToIncomplete() public function redirectToIncomplete()
{ {
foreach ($this->filterActiveSteps() as $step => $controller) { foreach ($this->filterActiveSteps() as $step => $controller) {
/** @var InstallerStep $controller */
if (!$controller->complete()) { if (!$controller->complete()) {
return redirect()->route("install.$step"); return redirect()->route("install.$step");
} }
@@ -77,12 +78,12 @@ class InstallationController extends Controller
$this->filterActiveSteps(); $this->filterActiveSteps();
$this->configureDatabase(); $this->configureDatabase();
foreach ($this->stepStatus() as $step => $complete) { foreach ($this->stepStatus() as $step => $status) {
if ($step == $this->step) { if ($step == $this->step) {
return true; return true;
} }
if (!$complete) { if (!$status['complete']) {
return false; return false;
} }
} }
@@ -121,7 +122,7 @@ class InstallationController extends Controller
$db['password'] ?? null, $db['password'] ?? null,
$db['database'] ?? 'librenms', $db['database'] ?? 'librenms',
$db['port'] ?? 3306, $db['port'] ?? 3306,
$db['socket'] ?? null, $db['socket'] ?? null
); );
config(['database.default', $this->connection]); config(['database.default', $this->connection]);
} }

View File

@@ -50,8 +50,11 @@ return [
'finish' => [ 'finish' => [
'title' => 'Finish Install', 'title' => 'Finish Install',
'env_written' => '.env file written', 'env_written' => '.env file written',
'env_not_written' => 'Could not write .env file',
'env_manual' => 'Manually update :file with the following content',
'config_exists' => 'config.php file exists', 'config_exists' => 'config.php file exists',
'config_written' => 'config.php file written', 'config_written' => 'config.php file written',
'config_not_required' => 'This file is not required. Here is the default.',
'config_not_written' => 'Could not write config.php', 'config_not_written' => 'Could not write config.php',
'not_finished' => 'You have not quite finished yet!', 'not_finished' => 'You have not quite finished yet!',
'validate' => 'First, you need to :validate and fix any issues.', 'validate' => 'First, you need to :validate and fix any issues.',
@@ -59,5 +62,6 @@ return [
'thanks' => 'Thank you for setting up LibreNMS.', 'thanks' => 'Thank you for setting up LibreNMS.',
'statistics' => 'It would be great if you would consider contributing to our statistics, you can do this on the :about and check the box under Statistics.', 'statistics' => 'It would be great if you would consider contributing to our statistics, you can do this on the :about and check the box under Statistics.',
'statistics_link' => 'About LibreNMS Page', 'statistics_link' => 'About LibreNMS Page',
'retry' => 'Retry'
] ]
]; ];

View File

@@ -2,23 +2,41 @@
@section('content') @section('content')
<div class="card mb-2"> <div class="card mb-2">
<div class="card-header" data-toggle="collapse" data-target="#env-file-text" aria-expanded="false"> <div class="card-header" data-toggle="collapse" data-target="#env-file-text" aria-expanded="{{ $success ? 'false' : 'true' }}">
@lang('install.finish.env_written') @if($success)
<i class="fa fa-chevron-up rotate-if-collapsed pull-right"></i> <i class="fa fa-lg fa-check-circle text-success"></i>
</div> @else
<div id="env-file-text" class="card-body collapse"> <i class="fa fa-lg fa-times-circle text-danger"></i>
<pre class="card bg-light p-3">{{ $env }}</pre> @endif
{{ $env_message }}
@if($env)<i class="fa fa-lg fa-chevron-down rotate-if-collapsed pull-right"></i>@endif($env)
</div> </div>
@if($env)
<div id="env-file-text" class="card-body collapse @if(!$success) show @endif">
<button class="btn btn-primary float-right" onclick="location.reload()">@lang('install.finish.retry')</button>
<strong>
@lang('install.finish.env_manual', ['file' => base_path('.env')])
</strong>
<pre class="card bg-light p-3 mt-3">{{ $env }}</pre>
</div>
@endif
</div> </div>
<div class="card mb-2"> <div class="card mb-2">
<div class="card-header" data-toggle="collapse" data-target="#config-file-text" aria-expanded="false"> <div class="card-header" data-toggle="collapse" data-target="#config-file-text" aria-expanded="false">
<i class="fa fa-lg fa-check-circle text-success"></i>
{{ $config_message }} {{ $config_message }}
<i class="fa fa-chevron-up rotate-if-collapsed pull-right"></i> @if($config)<i class="fa fa-lg fa-chevron-down rotate-if-collapsed pull-right"></i>@endif
</div> </div>
@if($config)
<div id="config-file-text" class="card-body collapse"> <div id="config-file-text" class="card-body collapse">
<pre class="card bg-light p-3">{{ $config }}</pre> <strong>
@lang('install.finish.config_not_required')
</strong>
<pre class="card bg-light p-3 mt-3">{{ $config }}</pre>
</div> </div>
@endif
</div> </div>
@if($success)
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12">
<div class="alert alert-warning"> <div class="alert alert-warning">
@@ -35,4 +53,5 @@
</div> </div>
</div> </div>
</div> </div>
@endif
@endsection @endsection