Files
librenms-librenms/app/Http/Controllers/Install/InstallationController.php

162 lines
4.4 KiB
PHP
Raw Normal View History

2020-06-05 14:26:57 -05:00
<?php
/**
* InstallationController.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 <https://www.gnu.org/licenses/>.
2020-06-05 14:26:57 -05:00
*
* @link https://www.librenms.org
2020-06-05 14:26:57 -05:00
* @copyright 2020 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Http\Controllers\Install;
use App\Http\Controllers\Controller;
2020-06-11 01:06:16 -05:00
use LibreNMS\DB\Eloquent;
2020-06-21 11:27:07 -05:00
use LibreNMS\Interfaces\InstallerStep;
2020-06-05 14:26:57 -05:00
class InstallationController extends Controller
{
2020-06-11 01:06:16 -05:00
protected $connection = 'setup';
2020-06-21 01:07:56 -05:00
protected $step;
2020-06-11 01:06:16 -05:00
protected $steps = [
'checks' => \App\Http\Controllers\Install\ChecksController::class,
'database' => \App\Http\Controllers\Install\DatabaseController::class,
'user' => \App\Http\Controllers\Install\MakeUserController::class,
'finish' => \App\Http\Controllers\Install\FinalizeController::class,
];
2020-06-21 11:27:07 -05:00
public function redirectToFirst()
{
$step = collect($this->filterActiveSteps())->keys()->first(null, 'checks');
2020-06-21 11:27:07 -05:00
return redirect()->route("install.$step");
}
2020-06-21 01:07:56 -05:00
public function redirectToIncomplete()
2020-06-11 01:06:16 -05:00
{
2020-06-21 11:27:07 -05:00
foreach ($this->filterActiveSteps() as $step => $controller) {
2020-06-21 13:51:47 -05:00
/** @var InstallerStep $controller */
if (! $controller->complete()) {
2020-06-21 01:07:56 -05:00
return redirect()->route("install.$step");
}
2020-06-11 01:06:16 -05:00
}
2020-06-21 01:07:56 -05:00
return redirect()->route('install.checks');
2020-06-11 01:06:16 -05:00
}
2020-06-06 17:03:32 -05:00
public function invalid()
2020-06-05 14:26:57 -05:00
{
2020-06-06 17:03:32 -05:00
abort(404);
2020-06-05 14:26:57 -05:00
}
2020-06-11 01:06:16 -05:00
2020-06-11 01:58:42 -05:00
public function stepsCompleted()
{
2020-06-21 01:07:56 -05:00
return response()->json($this->stepStatus());
2020-06-11 01:58:42 -05:00
}
2020-06-21 01:07:56 -05:00
/**
* Init step info and return false if previous steps have not been completed.
*
2020-06-21 11:27:07 -05:00
* @return bool if all previous steps have been completed
2020-06-21 01:07:56 -05:00
*/
final protected function initInstallStep()
2020-06-15 00:45:40 -05:00
{
2020-06-21 11:27:07 -05:00
$this->filterActiveSteps();
2020-06-21 01:07:56 -05:00
$this->configureDatabase();
2020-06-21 13:51:47 -05:00
foreach ($this->stepStatus() as $step => $status) {
2020-06-21 01:07:56 -05:00
if ($step == $this->step) {
return true;
}
if (! $status['complete']) {
2020-06-21 01:07:56 -05:00
return false;
}
}
return false;
}
final protected function markStepComplete()
{
if (! $this->stepCompleted($this->step)) {
2020-06-21 11:27:07 -05:00
session(["install.$this->step" => true]);
session()->save();
}
}
final protected function stepCompleted(string $step)
{
return (bool) session("install.$step");
2020-06-15 00:45:40 -05:00
}
2020-06-11 01:06:16 -05:00
final protected function formatData($data = [])
{
2020-06-21 01:07:56 -05:00
$data['steps'] = $this->hydrateControllers();
2020-06-21 11:27:07 -05:00
$data['step'] = $this->step;
2020-06-11 01:06:16 -05:00
return $data;
}
protected function configureDatabase()
{
$db = session('db');
if (! empty($db)) {
2020-06-11 01:06:16 -05:00
Eloquent::setConnection(
$this->connection,
$db['host'] ?? 'localhost',
$db['username'] ?? 'librenms',
$db['password'] ?? null,
$db['database'] ?? 'librenms',
$db['port'] ?? 3306,
2020-06-21 13:51:47 -05:00
$db['socket'] ?? null
2020-06-11 01:06:16 -05:00
);
config(['database.default', $this->connection]);
}
}
2020-06-21 01:07:56 -05:00
2020-06-21 11:27:07 -05:00
protected function filterActiveSteps()
{
if (is_string(config('librenms.install'))) {
$this->steps = array_intersect_key($this->steps, array_flip(explode(',', config('librenms.install'))));
}
return $this->steps;
}
2020-06-22 00:39:16 -05:00
protected function hydrateControllers()
2020-06-21 01:07:56 -05:00
{
$this->steps = array_map(function ($class) {
return is_object($class) ? $class : app()->make($class);
}, $this->steps);
2020-06-21 11:27:07 -05:00
return $this->steps;
2020-06-21 01:07:56 -05:00
}
private function stepStatus()
{
$this->hydrateControllers();
2020-06-21 11:27:07 -05:00
return array_map(function (InstallerStep $controller) {
return [
'enabled' => $controller->enabled(),
'complete' => $controller->complete(),
];
2020-06-21 21:25:00 -05:00
}, $this->steps);
2020-06-21 01:07:56 -05:00
}
2020-06-05 14:26:57 -05:00
}