step status refactor

This commit is contained in:
Tony Murray
2020-06-21 01:07:56 -05:00
parent d1ceb14b9a
commit e267ec1cab
6 changed files with 72 additions and 27 deletions

View File

@@ -31,11 +31,14 @@ use LibreNMS\Validations\Php;
class ChecksController extends InstallationController implements InstallerStep class ChecksController extends InstallationController implements InstallerStep
{ {
const MODULES = ['pdo_mysql', 'mysqlnd', 'gd']; const MODULES = ['pdo_mysql', 'mysqlnd', 'gd'];
protected $step = 'checks';
public function index() public function index()
{ {
$this->initInstallStep();
if ($this->complete()) { if ($this->complete()) {
$this->markStepComplete('checks'); $this->markStepComplete();
} }
return view('install.checks', $this->formatData([ return view('install.checks', $this->formatData([

View File

@@ -36,9 +36,14 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
class DatabaseController extends InstallationController implements InstallerStep class DatabaseController extends InstallationController implements InstallerStep
{ {
const KEYS = ['host', 'username', 'password', 'database', 'port', 'unix_socket']; const KEYS = ['host', 'username', 'password', 'database', 'port', 'unix_socket'];
protected $step = 'database';
public function index(Request $request) public function index(Request $request)
{ {
if (!$this->initInstallStep()) {
return $this->redirectToIncomplete();
}
$data = Arr::only(session()->get('db') ?: [], self::KEYS); $data = Arr::only(session()->get('db') ?: [], self::KEYS);
$data['valid_credentials'] = Eloquent::isConnected(); $data['valid_credentials'] = Eloquent::isConnected();
$data['migrated'] = session('install.database'); $data['migrated'] = session('install.database');
@@ -88,7 +93,7 @@ class DatabaseController extends InstallationController implements InstallerStep
throw new \RuntimeException('Migration failed'); throw new \RuntimeException('Migration failed');
} }
echo "\n\nSuccess!"; echo "\n\nSuccess!";
$this->markStepComplete('database'); $this->markStepComplete();
} catch (\Exception $e) { } catch (\Exception $e) {
echo $e->getMessage() . "\n\nError!"; echo $e->getMessage() . "\n\nError!";
} }
@@ -108,7 +113,7 @@ class DatabaseController extends InstallationController implements InstallerStep
$this->configureDatabase(); $this->configureDatabase();
if (Eloquent::isConnected() && Schema::isCurrent()) { if (Eloquent::isConnected() && Schema::isCurrent()) {
$this->markStepComplete('database'); $this->markStepComplete();
return true; return true;
} }

View File

@@ -31,10 +31,12 @@ use LibreNMS\Util\EnvHelper;
class FinalizeController extends InstallationController implements InstallerStep class FinalizeController extends InstallationController implements InstallerStep
{ {
protected $step = 'finish';
public function index() public function index()
{ {
if (!$this->enabled()) { if (!$this->initInstallStep()) {
return redirect()->route('install'); return $this->redirectToIncomplete();
} }
$env = ''; $env = '';

View File

@@ -31,6 +31,7 @@ use LibreNMS\DB\Eloquent;
class InstallationController extends Controller class InstallationController extends Controller
{ {
protected $connection = 'setup'; protected $connection = 'setup';
protected $step;
protected $steps = [ protected $steps = [
'checks' => \App\Http\Controllers\Install\ChecksController::class, 'checks' => \App\Http\Controllers\Install\ChecksController::class,
'database' => \App\Http\Controllers\Install\DatabaseController::class, 'database' => \App\Http\Controllers\Install\DatabaseController::class,
@@ -38,18 +39,15 @@ class InstallationController extends Controller
'finish' => \App\Http\Controllers\Install\FinalizeController::class, 'finish' => \App\Http\Controllers\Install\FinalizeController::class,
]; ];
public function __construct() public function redirectToIncomplete()
{ {
if (is_string(config('librenms.install'))) { foreach ($this->stepStatus() as $step => $complete) {
$this->steps = array_intersect_key($this->steps, array_flip(explode(',', config('librenms.install')))); if (!$complete) {
return redirect()->route("install.$step");
}
} }
$this->configureDatabase();
}
public function baseIndex() return redirect()->route('install.checks');
{
$initial = key($this->steps) ?: 'checks';
return redirect()->route("install.$initial");
} }
public function invalid() public function invalid()
@@ -59,23 +57,43 @@ class InstallationController extends Controller
public function stepsCompleted() public function stepsCompleted()
{ {
return response()->json(array_map(function ($class) { return response()->json($this->stepStatus());
$controller = app()->make($class);
return $controller->complete();
}, $this->steps));
} }
final protected function markStepComplete($step) /**
* Init step info and return false if previous steps have not been completed.
*
* @return bool
*/
final protected function initInstallStep()
{ {
session(["install.$step" => true]); if (is_string(config('librenms.install'))) {
$this->steps = array_intersect_key($this->steps, array_flip(explode(',', config('librenms.install'))));
}
$this->configureDatabase();
foreach ($this->stepStatus() as $step => $completed) {
if ($step == $this->step) {
return true;
}
if (!$completed) {
return false;
}
}
return false;
}
final protected function markStepComplete()
{
session(["install.$this->step" => true]);
session()->save(); session()->save();
} }
final protected function formatData($data = []) final protected function formatData($data = [])
{ {
$data['steps'] = array_map(function ($class) { $data['steps'] = $this->hydrateControllers();
return app()->make($class);
}, $this->steps);
return $data; return $data;
} }
@@ -95,4 +113,19 @@ class InstallationController extends Controller
config(['database.default', $this->connection]); config(['database.default', $this->connection]);
} }
} }
private function hydrateControllers()
{
$this->steps = array_map(function ($class) {
return is_object($class) ? $class : app()->make($class);
}, $this->steps);
}
private function stepStatus()
{
$this->hydrateControllers();
return array_map(function ($controller) {
return $controller->complete();
}, $this->steps);
}
} }

View File

@@ -33,10 +33,12 @@ use LibreNMS\Interfaces\InstallerStep;
class MakeUserController extends InstallationController implements InstallerStep class MakeUserController extends InstallationController implements InstallerStep
{ {
protected $step = 'user';
public function index(Request $request) public function index(Request $request)
{ {
if (!self::enabled()) { if (!$this->initInstallStep()) {
return redirect()->route('install'); return $this->redirectToIncomplete();
} }
if (session('install.database')) { if (session('install.database')) {
@@ -44,7 +46,7 @@ class MakeUserController extends InstallationController implements InstallerStep
} }
if (isset($user)) { if (isset($user)) {
$this->markStepComplete('user'); $this->markStepComplete();
return view('install.user-created', $this->formatData([ return view('install.user-created', $this->formatData([
'user' => $user, 'user' => $user,
])); ]));

View File

@@ -150,7 +150,7 @@ Route::group(['middleware' => ['auth'], 'guard' => 'auth'], function () {
// installation routes // installation routes
Route::group(['prefix' => 'install', 'namespace' => 'Install'], function () { Route::group(['prefix' => 'install', 'namespace' => 'Install'], function () {
Route::get('/', 'InstallationController@baseIndex')->name('install'); Route::get('/', 'InstallationController@redirectToIncomplete')->name('install');
Route::get('/checks', 'ChecksController@index')->name('install.checks'); Route::get('/checks', 'ChecksController@index')->name('install.checks');
Route::get('/database', 'DatabaseController@index')->name('install.database'); Route::get('/database', 'DatabaseController@index')->name('install.database');
Route::get('/user', 'MakeUserController@index')->name('install.user'); Route::get('/user', 'MakeUserController@index')->name('install.user');