mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
config file writing
This commit is contained in:
@@ -50,11 +50,11 @@ class EnvHelper
|
||||
* Will only set non-empty unset variables
|
||||
*
|
||||
* @param array $settings KEY => value list of settings
|
||||
* @param array $force
|
||||
* @param array $unset Remove the given KEYS from the config
|
||||
* @param string $file
|
||||
* @return string
|
||||
*/
|
||||
public static function setEnv($settings, $force = [], $file = '.env')
|
||||
public static function setEnv($settings, $unset = [], $file = '.env')
|
||||
{
|
||||
$original_content = $content = file_get_contents($file);
|
||||
// TODO implement force
|
||||
@@ -64,6 +64,12 @@ class EnvHelper
|
||||
$content .= PHP_EOL;
|
||||
}
|
||||
|
||||
// unset the given keys
|
||||
if (!empty($unset)) {
|
||||
$regex = '/^(' . implode('|', $unset) . ')=.*$\n/m';
|
||||
$content = preg_replace($regex, '', $content);
|
||||
}
|
||||
|
||||
foreach ($settings as $key => $value) {
|
||||
// only add non-empty settings
|
||||
if (empty($value)) {
|
||||
|
||||
@@ -33,14 +33,17 @@ class FinalizeController extends \App\Http\Controllers\Controller
|
||||
public function __invoke()
|
||||
{
|
||||
$env = '';
|
||||
$config_file = base_path('config.php');
|
||||
$config = $this->getConfigFileContents();
|
||||
$messages = [];
|
||||
$success = true;
|
||||
$config_message = file_exists($config_file) ? trans('install.finish.config_exists') : trans('install.finish.config_written');
|
||||
|
||||
try {
|
||||
$this->writeConfigFile($config);
|
||||
$this->writeConfigFile($config, $config_file);
|
||||
} catch (Exception $e) {
|
||||
$messages[] = $e->getMessage();
|
||||
$config_message = trans('install.finish.config_not_written');
|
||||
$success = true;
|
||||
}
|
||||
|
||||
@@ -54,11 +57,18 @@ class FinalizeController extends \App\Http\Controllers\Controller
|
||||
}
|
||||
}
|
||||
|
||||
if ($success) {
|
||||
// TODO clear session
|
||||
// session()->forget('install');
|
||||
// session()->forget('db');
|
||||
}
|
||||
|
||||
return view('install.finish', [
|
||||
'env' => $env,
|
||||
'config' => $config,
|
||||
'messages' => $messages,
|
||||
'success' => $success,
|
||||
'config_message' => $config_message,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -72,21 +82,20 @@ class FinalizeController extends \App\Http\Controllers\Controller
|
||||
'DB_PASSWORD' => session('db.password'),
|
||||
'DB_DATABASE' => session('db.database'),
|
||||
'DB_SOCKET' => session('db.socket'),
|
||||
], base_path('.env'));
|
||||
], [], base_path('.env')); // TODO unset INSTALL
|
||||
}
|
||||
|
||||
private function writeConfigFile($config_file)
|
||||
private function writeConfigFile($config_contents, $config_file)
|
||||
{
|
||||
$file = base_path('config.php');
|
||||
if (!file_exists($file)) {
|
||||
$conf = fopen($file, 'w');
|
||||
if (!file_exists($config_file)) {
|
||||
$conf = fopen($config_file, 'w');
|
||||
if ($conf !== false) {
|
||||
if (fwrite($conf, "<?php\n") === false) {
|
||||
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/)");
|
||||
}
|
||||
|
||||
$config_file = stripslashes($config_file);
|
||||
fwrite($conf, $config_file);
|
||||
$config_contents = stripslashes($config_contents);
|
||||
fwrite($conf, $config_contents);
|
||||
fclose($conf);
|
||||
return;
|
||||
}
|
||||
@@ -120,43 +129,43 @@ class FinalizeController extends \App\Http\Controllers\Controller
|
||||
## Have a look in defaults.inc.php for examples of settings you can set here. DO NOT EDIT defaults.inc.php!
|
||||
|
||||
### Database config
|
||||
\$config\['db_host'\] = '{$db['host']}';
|
||||
\$config\['db_port'\] = '{$db['port']}';
|
||||
\$config\['db_user'\] = '{$db['username']}';
|
||||
\$config\['db_pass'\] = '{$db['password']}';
|
||||
\$config\['db_name'\] = '{$db['database']}';
|
||||
\$config\['db_socket'\] = '{$db['unix_socket']}';
|
||||
\$config['db_host'] = '{$db['host']}';
|
||||
\$config['db_port'] = '{$db['port']}';
|
||||
\$config['db_user'] = '{$db['username']}';
|
||||
\$config['db_pass'] = '{$db['password']}';
|
||||
\$config['db_name'] = '{$db['database']}';
|
||||
\$config['db_socket'] = '{$db['unix_socket']}';
|
||||
|
||||
// This is the user LibreNMS will run as
|
||||
//Please ensure this user is created and has the correct permissions to your install
|
||||
\$config['user'] = 'librenms';
|
||||
|
||||
### Locations - it is recommended to keep the default
|
||||
#\$config\['install_dir'\] = "$install_dir";
|
||||
#\$config['install_dir'] = "$install_dir";
|
||||
|
||||
### This should *only* be set if you want to *force* a particular hostname/port
|
||||
### It will prevent the web interface being usable form any other hostname
|
||||
#\$config\['base_url'\] = "http://librenms.company.com";
|
||||
#\$config['base_url'] = "http://librenms.company.com";
|
||||
|
||||
### Enable this to use rrdcached. Be sure rrd_dir is within the rrdcached dir
|
||||
### and that your web server has permission to talk to rrdcached.
|
||||
#\$config\['rrdcached'\] = "unix:/var/run/rrdcached.sock";
|
||||
#\$config['rrdcached'] = "unix:/var/run/rrdcached.sock";
|
||||
|
||||
### Default community
|
||||
\$config\['snmp'\]\['community'\] = array("public");
|
||||
\$config['snmp']['community'] = ['public'];
|
||||
|
||||
### Authentication Model
|
||||
\$config\['auth_mechanism'\] = "mysql"; # default, other options: ldap, http-auth
|
||||
#\$config\['http_auth_guest'\] = "guest"; # remember to configure this user if you use http-auth
|
||||
\$config['auth_mechanism'] = "mysql"; # default, other options: ldap, http-auth
|
||||
#\$config['http_auth_guest'] = "guest"; # remember to configure this user if you use http-auth
|
||||
|
||||
### List of RFC1918 networks to allow scanning-based discovery
|
||||
#\$config\['nets'\]\[\] = "10.0.0.0/8";
|
||||
#\$config\['nets'\]\[\] = "172.16.0.0/12";
|
||||
#\$config\['nets'\]\[\] = "192.168.0.0/16";
|
||||
#\$config['nets'][] = "10.0.0.0/8";
|
||||
#\$config['nets'][] = "172.16.0.0/12";
|
||||
#\$config['nets'][] = "192.168.0.0/16";
|
||||
|
||||
# Update configuration
|
||||
#\$config\['update_channel'\] = 'release'; # uncomment to follow the monthly release channel
|
||||
#\$config\['update'\] = 0; # uncomment to completely disable updates
|
||||
#\$config['update_channel'] = 'release'; # uncomment to follow the monthly release channel
|
||||
#\$config['update'] = 0; # uncomment to completely disable updates
|
||||
EOD;
|
||||
|
||||
}
|
||||
|
||||
@@ -49,6 +49,10 @@ return [
|
||||
'failure' => 'Failed to create user'
|
||||
],
|
||||
'finish' => [
|
||||
'title' => 'Finish Install'
|
||||
'title' => 'Finish Install',
|
||||
'env_written' => '.env file written',
|
||||
'config_exists' => 'config.php file exists',
|
||||
'config_written' => 'config.php file written',
|
||||
'config_not_written' => 'Could not write config.php',
|
||||
]
|
||||
];
|
||||
|
||||
@@ -1,14 +1,38 @@
|
||||
@extends('layouts.install')
|
||||
|
||||
@section('title', trans('install.finish.title'))
|
||||
|
||||
@section('content')
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
.env file
|
||||
<div class="card mb-1">
|
||||
<div class="card-header" data-toggle="collapse" data-target="#env-file-text" aria-expanded="false">
|
||||
@lang('install.finish.env_written')
|
||||
<i class="fa fa-chevron-up rotate-if-collapsed pull-right"></i>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<h4 class="card-title">Written</h4>
|
||||
<p class="card-text"><textarea disabled>{{ $env }}</textarea></p>
|
||||
<a href="#" class="btn btn-primary">button</a>
|
||||
<div id="env-file-text" class="card-body collapse">
|
||||
<pre class="card bg-light p-3">{{ $env }}</pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card mb-1">
|
||||
<div class="card-header" data-toggle="collapse" data-target="#config-file-text" aria-expanded="false">
|
||||
{{ $config_message }}
|
||||
<i class="fa fa-chevron-up rotate-if-collapsed pull-right"></i>
|
||||
</div>
|
||||
<div id="config-file-text" class="card-body collapse">
|
||||
<pre class="card bg-light p-3">{{ $config }}</pre>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@section('style')
|
||||
<style type="text/css">
|
||||
.rotate-if-collapsed {
|
||||
transition: .4s transform;
|
||||
}
|
||||
[data-toggle="collapse"] {
|
||||
cursor: pointer;
|
||||
}
|
||||
[data-toggle="collapse"][aria-expanded="true"] > .rotate-if-collapsed {
|
||||
transform: rotate(180deg);
|
||||
}
|
||||
</style>
|
||||
@endsection
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
<style type="text/css">
|
||||
.primary-panel {
|
||||
box-shadow: 0 0 25px #333;
|
||||
min-height: 540px;
|
||||
}
|
||||
|
||||
body {
|
||||
|
||||
Reference in New Issue
Block a user