mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
finish conversion
This commit is contained in:
@@ -99,7 +99,7 @@ class ComposerHelper
|
||||
|
||||
@include 'config.php';
|
||||
|
||||
EnvHelper::setEnv([
|
||||
EnvHelper::writeEnv([
|
||||
'NODE_ID' => uniqid(),
|
||||
'DB_HOST' => $config['db_host'],
|
||||
'DB_PORT' => $config['db_port'],
|
||||
|
||||
36
LibreNMS/Exceptions/FileWriteFailedException.php
Normal file
36
LibreNMS/Exceptions/FileWriteFailedException.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/**
|
||||
* FileWriteFailedException.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 LibreNMS\Exceptions;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class FileWriteFailedException extends \Exception
|
||||
{
|
||||
public function __construct($file, $code = 0, Throwable $previous = null)
|
||||
{
|
||||
parent::__construct("Failed to write file: $file", $code, $previous);
|
||||
}
|
||||
}
|
||||
@@ -25,24 +25,31 @@
|
||||
|
||||
namespace LibreNMS\Util;
|
||||
|
||||
use LibreNMS\Exceptions\FileWriteFailedException;
|
||||
|
||||
class EnvHelper
|
||||
{
|
||||
/**
|
||||
* Fix .env with # in them without a space before it
|
||||
* Set a setting in .env file.
|
||||
* Will only set non-empty unset variables
|
||||
*
|
||||
* @param array $settings KEY => value list of settings
|
||||
* @param array $unset Remove the given KEYS from the config
|
||||
* @param string $file
|
||||
* @return string
|
||||
*/
|
||||
public static function fixComments($dotenv)
|
||||
public static function writeEnv($settings, $unset = [], $file = '.env')
|
||||
{
|
||||
return implode(PHP_EOL, array_map(function ($line) {
|
||||
$parts = explode('=', $line, 2);
|
||||
if (isset($parts[1])
|
||||
&& preg_match('/(?<!\s)#/', $parts[1]) // number symbol without a space before it
|
||||
&& !preg_match('/^(".*"|\'.*\')$/', $parts[1]) // not already quoted
|
||||
) {
|
||||
return trim($parts[0]) . '="' . trim($parts[1]) . '"';
|
||||
}
|
||||
$original_content = file_get_contents($file);
|
||||
|
||||
return $line;
|
||||
}, explode(PHP_EOL, $dotenv)));
|
||||
$new_content = self::setEnv($original_content, $settings, $unset);
|
||||
|
||||
// only write if the content has changed
|
||||
if ($new_content !== $original_content) {
|
||||
file_put_contents($file, $new_content);
|
||||
}
|
||||
|
||||
return $new_content;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,12 +60,35 @@ class EnvHelper
|
||||
* @param array $unset Remove the given KEYS from the config
|
||||
* @param string $file
|
||||
* @return string
|
||||
* @throws \LibreNMS\Exceptions\FileWriteFailedException
|
||||
*/
|
||||
public static function setEnv($settings, $unset = [], $file = '.env')
|
||||
public static function tryWriteEnv($settings, $unset = [], $file = '.env')
|
||||
{
|
||||
$original_content = $content = file_get_contents($file);
|
||||
// TODO implement force
|
||||
$original_content = file_get_contents($file);
|
||||
|
||||
$new_content = self::setEnv($original_content, $settings, $unset);
|
||||
|
||||
// only write if the content has changed
|
||||
if ($new_content !== $original_content) {
|
||||
if(!file_put_contents($file, $new_content)) {
|
||||
throw new FileWriteFailedException($file);
|
||||
}
|
||||
}
|
||||
|
||||
return $new_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a setting in .env file.
|
||||
* Will only set non-empty unset variables
|
||||
*
|
||||
* @param string $content
|
||||
* @param array $settings KEY => value list of settings
|
||||
* @param array $unset Remove the given KEYS from the config
|
||||
* @return string
|
||||
*/
|
||||
public static function setEnv($content, $settings, $unset = [])
|
||||
{
|
||||
// ensure trailing line return
|
||||
if (substr($content, -1) !== PHP_EOL) {
|
||||
$content .= PHP_EOL;
|
||||
@@ -76,10 +106,7 @@ class EnvHelper
|
||||
continue;
|
||||
}
|
||||
|
||||
// quote strings with spaces
|
||||
if (strpos($value, ' ') !== false) {
|
||||
$value = "\"$value\"";
|
||||
}
|
||||
$value = self::escapeValue($value);
|
||||
|
||||
if (strpos($content, "$key=") !== false) {
|
||||
// only replace ones that aren't already set for safety and uncomment
|
||||
@@ -90,13 +117,42 @@ class EnvHelper
|
||||
}
|
||||
}
|
||||
|
||||
$content = self::fixComments($content);
|
||||
return self::fixComments($content);
|
||||
}
|
||||
|
||||
// only write if the content has changed
|
||||
if ($content !== $original_content) {
|
||||
file_put_contents($file, $content);
|
||||
/**
|
||||
* Fix .env with # in them without a space before it
|
||||
*
|
||||
* @param string $dotenv
|
||||
* @return string
|
||||
*/
|
||||
private static function fixComments($dotenv)
|
||||
{
|
||||
return implode(PHP_EOL, array_map(function ($line) {
|
||||
$parts = explode('=', $line, 2);
|
||||
if (isset($parts[1])
|
||||
&& preg_match('/(?<!\s)#/', $parts[1]) // number symbol without a space before it
|
||||
&& !preg_match('/^(".*"|\'.*\')$/', $parts[1]) // not already quoted
|
||||
) {
|
||||
return trim($parts[0]) . '="' . trim($parts[1]) . '"';
|
||||
}
|
||||
|
||||
return $line;
|
||||
}, explode(PHP_EOL, $dotenv)));
|
||||
}
|
||||
|
||||
/**
|
||||
* quote strings with spaces
|
||||
*
|
||||
* @param $value
|
||||
* @return string
|
||||
*/
|
||||
private static function escapeValue($value)
|
||||
{
|
||||
if (strpos($value, ' ') !== false) {
|
||||
return "\"$value\"";
|
||||
}
|
||||
|
||||
return $content;
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ class FinalizeController extends InstallationController implements InstallerStep
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
if (!self::enabled($this->steps)) {
|
||||
if (!$this->enabled()) {
|
||||
return redirect()->route('install');
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ class FinalizeController extends InstallationController implements InstallerStep
|
||||
$config_message = file_exists($config_file) ? trans('install.finish.config_exists') : trans('install.finish.config_written');
|
||||
|
||||
try {
|
||||
$this->writeConfigFile($config, $config_file);
|
||||
$this->writeConfigFile();
|
||||
} catch (Exception $e) {
|
||||
$messages[] = $e->getMessage();
|
||||
$config_message = trans('install.finish.config_not_written');
|
||||
@@ -77,84 +77,34 @@ class FinalizeController extends InstallationController implements InstallerStep
|
||||
|
||||
private function writeEnvFile()
|
||||
{
|
||||
return EnvHelper::setEnv([
|
||||
$this->configureDatabase();
|
||||
$connection = config('database.default', $this->connection);
|
||||
return EnvHelper::tryWriteEnv([
|
||||
'NODE_ID' => uniqid(),
|
||||
'DB_HOST' => session('db.host'),
|
||||
'DB_PORT' => session('db.port'),
|
||||
'DB_USERNAME' => session('db.username'),
|
||||
'DB_PASSWORD' => session('db.password'),
|
||||
'DB_DATABASE' => session('db.database'),
|
||||
'DB_SOCKET' => session('db.socket'),
|
||||
'DB_HOST' => config("database.connections.$connection.host"),
|
||||
'DB_PORT' => config("database.connections.$connection.port"),
|
||||
'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"),
|
||||
], ['INSTALL'], base_path('.env'));
|
||||
}
|
||||
|
||||
private function writeConfigFile($config_contents, $config_file)
|
||||
private function writeConfigFile()
|
||||
{
|
||||
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_contents = stripslashes($config_contents);
|
||||
fwrite($conf, $config_contents);
|
||||
fclose($conf);
|
||||
return;
|
||||
}
|
||||
$config_file = base_path('config.php');
|
||||
if (file_exists($config_file)) {
|
||||
return;
|
||||
}
|
||||
|
||||
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/)");
|
||||
}
|
||||
}
|
||||
|
||||
private function getConfigFileContents()
|
||||
{
|
||||
$db = session('db');
|
||||
$install_dir = base_path();
|
||||
|
||||
return <<<"EOD"
|
||||
## 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']}';
|
||||
|
||||
// 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";
|
||||
|
||||
### 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";
|
||||
|
||||
### 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";
|
||||
|
||||
### Default community
|
||||
\$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
|
||||
|
||||
### 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";
|
||||
|
||||
# Update configuration
|
||||
#\$config['update_channel'] = 'release'; # uncomment to follow the monthly release channel
|
||||
#\$config['update'] = 0; # uncomment to completely disable updates
|
||||
EOD;
|
||||
|
||||
return file_get_contents(base_path('config.php.default'));
|
||||
}
|
||||
|
||||
public function enabled(): bool
|
||||
|
||||
@@ -61,7 +61,7 @@ class InstallationController extends Controller
|
||||
{
|
||||
return response()->json(array_map(function ($class) {
|
||||
$controller = app()->make($class);
|
||||
return $controller->enabled($this->steps);
|
||||
return $controller->complete();
|
||||
}, $this->steps));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user