mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
refactor
This commit is contained in:
35
LibreNMS/Interfaces/InstallerStep.php
Normal file
35
LibreNMS/Interfaces/InstallerStep.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* InstallerStep.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\Interfaces;
|
||||
|
||||
interface InstallerStep
|
||||
{
|
||||
public function enabled(): bool;
|
||||
|
||||
public function complete(): bool;
|
||||
|
||||
public function icon(): string;
|
||||
}
|
||||
@@ -25,49 +25,68 @@
|
||||
|
||||
namespace App\Http\Controllers\Install;
|
||||
|
||||
use LibreNMS\Interfaces\InstallerStep;
|
||||
use LibreNMS\Validations\Php;
|
||||
|
||||
class ChecksController extends InstallationController
|
||||
class ChecksController extends InstallationController implements InstallerStep
|
||||
{
|
||||
const MODULES = ['pdo_mysql', 'mysqlnd', 'gd'];
|
||||
|
||||
public function index()
|
||||
{
|
||||
$results = [];
|
||||
$php_ok = version_compare(PHP_VERSION, Php::PHP_MIN_VERSION, '>=');
|
||||
|
||||
// bitwise and so all checks run
|
||||
if ($php_ok
|
||||
& $this->checkPhpModule($results, 'pdo_mysql')
|
||||
& $this->checkPhpModule($results, 'mysqlnd')
|
||||
& $this->checkPhpModule($results, 'gd')
|
||||
) {
|
||||
session(['install.checks' => true]);
|
||||
if ($this->complete()) {
|
||||
$this->markStepComplete('checks');
|
||||
}
|
||||
|
||||
return view('install.checks', $this->formatData([
|
||||
'php_version' => PHP_VERSION,
|
||||
'php_required' => Php::PHP_MIN_VERSION,
|
||||
'php_ok' => $php_ok,
|
||||
'modules' => $results
|
||||
'php_ok' => $this->checkPhpVersion(),
|
||||
'modules' => $this->moduleResults(),
|
||||
]));
|
||||
}
|
||||
|
||||
private function checkPhpModule(&$results, $module)
|
||||
private function moduleResults()
|
||||
{
|
||||
$status = extension_loaded("$module");
|
||||
$results[] = [
|
||||
'name' => str_replace('install.checks.php_module.', '', trans('install.checks.php_module.' . $module)),
|
||||
'status' => $status,
|
||||
];
|
||||
$results = [];
|
||||
|
||||
return $status;
|
||||
foreach (self::MODULES as $module) {
|
||||
$status = extension_loaded($module);
|
||||
$results[] = [
|
||||
'name' => str_replace('install.checks.php_module.', '', trans('install.checks.php_module.' . $module)),
|
||||
'status' => $status,
|
||||
];
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public static function enabled(): bool
|
||||
private function checkPhpVersion()
|
||||
{
|
||||
return version_compare(PHP_VERSION, Php::PHP_MIN_VERSION, '>=');
|
||||
}
|
||||
|
||||
public function complete(): bool
|
||||
{
|
||||
if (!$this->checkPhpVersion()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (self::MODULES as $module) {
|
||||
if (!extension_loaded($module)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function enabled(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function icon(): string
|
||||
public function icon(): string
|
||||
{
|
||||
return 'fa-list-ul fa-flip-horizontal';
|
||||
}
|
||||
|
||||
@@ -25,11 +25,14 @@
|
||||
|
||||
namespace App\Http\Controllers\Install;
|
||||
|
||||
use App\StreamedOutput;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use LibreNMS\DB\Eloquent;
|
||||
use LibreNMS\Interfaces\InstallerStep;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
class DatabaseController extends InstallationController
|
||||
class DatabaseController extends InstallationController implements InstallerStep
|
||||
{
|
||||
const KEYS = ['host', 'username', 'password', 'database', 'port', 'unix_socket'];
|
||||
|
||||
@@ -72,12 +75,42 @@ class DatabaseController extends InstallationController
|
||||
]);
|
||||
}
|
||||
|
||||
public static function enabled(): bool
|
||||
public function migrate(Request $request)
|
||||
{
|
||||
$response = new StreamedResponse(function () use ($request) {
|
||||
try {
|
||||
$this->configureDatabase();
|
||||
$output = new StreamedOutput(fopen('php://stdout', 'w'));
|
||||
echo "Starting Update...\n";
|
||||
$ret = \Artisan::call('migrate', ['--seed' => true, '--force' => true, '--database' => $this->connection], $output);
|
||||
if ($ret !== 0) {
|
||||
throw new \RuntimeException('Migration failed');
|
||||
}
|
||||
echo "\n\nSuccess!";
|
||||
session(['install.migrate' => true]);
|
||||
session()->save();
|
||||
} catch (\Exception $e) {
|
||||
echo $e->getMessage() . "\n\nError!";
|
||||
}
|
||||
});
|
||||
|
||||
$response->headers->set('Content-Type', 'text/plain');
|
||||
$response->headers->set('X-Accel-Buffering', 'no');
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function complete(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function enabled(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function icon(): string
|
||||
public function icon(): string
|
||||
{
|
||||
return 'fa-database';
|
||||
}
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* DatabaseMigrationController.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;
|
||||
|
||||
use App\StreamedOutput;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
class DatabaseMigrationController extends InstallationController
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
if (!self::enabled()) {
|
||||
return redirect()->route('install.database');
|
||||
}
|
||||
|
||||
return view('install.migrate', $this->formatData());
|
||||
}
|
||||
|
||||
public function migrate(Request $request)
|
||||
{
|
||||
$response = new StreamedResponse(function () use ($request) {
|
||||
try {
|
||||
$this->configureDatabase();
|
||||
$output = new StreamedOutput(fopen('php://stdout', 'w'));
|
||||
echo "Starting Update...\n";
|
||||
$ret = \Artisan::call('migrate', ['--seed' => true, '--force' => true, '--database' => $this->connection], $output);
|
||||
if ($ret !== 0) {
|
||||
throw new \RuntimeException('Migration failed');
|
||||
}
|
||||
echo "\n\nSuccess!";
|
||||
session(['install.migrate' => true]);
|
||||
session()->save();
|
||||
} catch (\Exception $e) {
|
||||
echo $e->getMessage() . "\n\nError!";
|
||||
}
|
||||
});
|
||||
|
||||
$response->headers->set('Content-Type', 'text/plain');
|
||||
$response->headers->set('X-Accel-Buffering', 'no');
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public static function enabled(): bool
|
||||
{
|
||||
return (bool)session('install.database');
|
||||
}
|
||||
|
||||
public static function icon(): string
|
||||
{
|
||||
return 'fa-refresh';
|
||||
}
|
||||
}
|
||||
@@ -26,9 +26,10 @@
|
||||
namespace App\Http\Controllers\Install;
|
||||
|
||||
use Exception;
|
||||
use LibreNMS\Interfaces\InstallerStep;
|
||||
use LibreNMS\Util\EnvHelper;
|
||||
|
||||
class FinalizeController extends InstallationController
|
||||
class FinalizeController extends InstallationController implements InstallerStep
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
@@ -106,22 +107,6 @@ class FinalizeController extends InstallationController
|
||||
}
|
||||
}
|
||||
|
||||
public static function enabled($steps): bool
|
||||
{
|
||||
foreach ($steps as $step => $controller) {
|
||||
if ($step !== 'finish' && !session("install.$step")) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function icon(): string
|
||||
{
|
||||
return 'fa-check';
|
||||
}
|
||||
|
||||
private function getConfigFileContents()
|
||||
{
|
||||
$db = session('db');
|
||||
@@ -171,4 +156,25 @@ class FinalizeController extends InstallationController
|
||||
EOD;
|
||||
|
||||
}
|
||||
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,6 @@ class InstallationController extends Controller
|
||||
protected $steps = [
|
||||
'checks' => \App\Http\Controllers\Install\ChecksController::class,
|
||||
'database' => \App\Http\Controllers\Install\DatabaseController::class,
|
||||
'migrate' => \App\Http\Controllers\Install\DatabaseMigrationController::class,
|
||||
'user' => \App\Http\Controllers\Install\MakeUserController::class,
|
||||
'finish' => \App\Http\Controllers\Install\FinalizeController::class,
|
||||
];
|
||||
@@ -65,9 +64,16 @@ class InstallationController extends Controller
|
||||
}, $this->steps));
|
||||
}
|
||||
|
||||
final protected function markStepComplete($step)
|
||||
{
|
||||
session(["install.$step" => true]);
|
||||
}
|
||||
|
||||
final protected function formatData($data = [])
|
||||
{
|
||||
$data['steps'] = $this->steps;
|
||||
$data['steps'] = array_map(function ($class) {
|
||||
return app()->make($class);
|
||||
}, $this->steps);
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,8 +28,9 @@ namespace App\Http\Controllers\Install;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use LibreNMS\Interfaces\InstallerStep;
|
||||
|
||||
class MakeUserController extends InstallationController
|
||||
class MakeUserController extends InstallationController implements InstallerStep
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
@@ -73,12 +74,17 @@ class MakeUserController extends InstallationController
|
||||
return redirect()->back()->with('message', $message);
|
||||
}
|
||||
|
||||
public static function enabled(): bool
|
||||
public function complete(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function enabled(): bool
|
||||
{
|
||||
return (bool)session('install.migrate');
|
||||
}
|
||||
|
||||
public static function icon(): string
|
||||
public function icon(): string
|
||||
{
|
||||
return 'fa-key';
|
||||
}
|
||||
|
||||
@@ -89,13 +89,13 @@
|
||||
<div>
|
||||
<a href="{{ route('install.' . $step) }}"
|
||||
id="install-{{ $step }}-button"
|
||||
class="btn btn-primary btn-circle @if(!$controller::enabled($steps)) disabled @endif"
|
||||
class="btn btn-primary btn-circle @if(!$controller->enabled($steps)) disabled @endif"
|
||||
title="@lang("install.$step.title")"
|
||||
>
|
||||
<i class="fa fa-lg {{ $controller::icon() }}"></i>
|
||||
<i class="fa fa-lg {{ $controller->icon() }}"></i>
|
||||
</a>
|
||||
</div>
|
||||
<div class="install-progress loop"></div>
|
||||
<div id="progress-{{ $step }}-bar" class="install-progress loop @if($controller->complete()) complete @endif"></div>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -153,13 +153,12 @@ Route::group(['prefix' => 'install', 'namespace' => 'Install'], function () {
|
||||
Route::get('/', 'InstallationController@baseIndex')->name('install');
|
||||
Route::get('/checks', 'ChecksController@index')->name('install.checks');
|
||||
Route::get('/database', 'DatabaseController@index')->name('install.database');
|
||||
Route::get('/database/migrate', 'DatabaseMigrationController@index')->name('install.migrate');
|
||||
Route::get('/user', 'MakeUserController@index')->name('install.user');
|
||||
Route::get('/finish', 'FinalizeController@index')->name('install.finish');
|
||||
|
||||
Route::post('/user/create', 'MakeUserController@create')->name('install.action.user');
|
||||
Route::post('/database/test', 'DatabaseController@test')->name('install.acton.test-database');
|
||||
Route::get('/ajax/database/migrate', 'DatabaseMigrationController@migrate')->name('install.action.migrate');
|
||||
Route::get('/ajax/database/migrate', 'DatabaseController@migrate')->name('install.action.migrate');
|
||||
Route::get('/ajax/steps', 'InstallationController@stepsCompleted')->name('install.action.steps');
|
||||
Route::any('{path?}', 'InstallationController@invalid')->where('path', '.*'); // 404
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user