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-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-09 23:24:43 -05:00
$config_file = base_path ( 'config.php' );
2020-06-08 02:21:03 -05:00
$config = $this -> getConfigFileContents ();
$messages = [];
$success = true ;
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-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 ) {
$messages [] = $e -> getMessage ();
2020-06-09 23:24:43 -05:00
$config_message = trans ( 'install.finish.config_not_written' );
2020-06-08 02:21:03 -05:00
$success = true ;
}
// write env last only if everything else succeeded
if ( $success ) {
try {
$env = $this -> writeEnvFile ();
} catch ( Exception $e ) {
$messages [] = $e -> getMessage ();
$success = false ;
}
}
2020-06-09 23:24:43 -05:00
if ( $success ) {
2020-06-11 01:58:42 -05:00
session () -> flush ();
2020-06-09 23:24:43 -05:00
}
2020-06-11 01:06:16 -05:00
return view ( 'install.finish' , $this -> formatData ([
2020-06-08 02:21:03 -05:00
'env' => $env ,
'config' => $config ,
'messages' => $messages ,
'success' => $success ,
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-19 00:17:36 -05:00
$this -> configureDatabase ();
$connection = config ( 'database.default' , $this -> connection );
2020-06-20 08:10:52 -05:00
return EnvHelper :: writeEnv ([
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 " ),
'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 " ),
2020-06-11 01:58:42 -05:00
], [ 'INSTALL' ], base_path ( '.env' ));
2020-06-05 14:26:57 -05:00
}
2020-06-06 17:03:32 -05:00
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-08 02:21:03 -05:00
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/) " );
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
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
}