Install to Laravel WIP

This commit is contained in:
Tony Murray
2020-06-05 11:51:34 -05:00
parent cab235c3e9
commit ad2da9fcdb
7 changed files with 130 additions and 76 deletions

View File

@@ -29,6 +29,7 @@ use Dotenv\Dotenv;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Events\StatementPrepared;
use Illuminate\Events\Dispatcher;
use LibreNMS\Exceptions\DatabaseConnectException;
use LibreNMS\Util\Laravel;
class Eloquent
@@ -86,10 +87,10 @@ class Eloquent
}
}
public static function isConnected()
public static function isConnected($name = null)
{
try {
$conn = self::DB();
$conn = self::DB($name);
if ($conn) {
return !is_null($conn->getPdo());
}
@@ -103,20 +104,21 @@ class Eloquent
/**
* Access the Database Manager for Fluent style queries. Like the Laravel DB facade.
*
* @param string $name
* @return \Illuminate\Database\Connection
*/
public static function DB()
public static function DB($name = null)
{
// check if Laravel is booted
if (Laravel::isBooted()) {
return \DB::connection();
return \DB::connection($name);
}
if (is_null(self::$capsule)) {
return null;
}
return self::$capsule->getDatabaseManager()->connection();
return self::$capsule->getDatabaseManager()->connection($name);
}
public static function getDriver()
@@ -124,4 +126,26 @@ class Eloquent
$connection = config('database.default');
return config("database.connections.{$connection}.driver");
}
public static function setConnection($name, $db_host = null, $db_user = '', $db_pass = '', $db_name = '', $db_port = null, $db_socket = null)
{
if (!is_null($db_host) || !empty($db_name)) {
// legacy connection override
\Config::set("database.connections.$name", [
"driver" => "mysql",
"host" => $db_host,
"port" => $db_port,
"database" => $db_name,
"username" => $db_user,
"password" => $db_pass,
"unix_socket" => $db_socket,
"charset" => "utf8",
"collation" => "utf8_unicode_ci",
"prefix" => "",
"strict" => true,
"engine" => null
]);
\Config::set('database.default', $name);
}
}
}

View File

@@ -29,7 +29,7 @@ class Kernel extends HttpKernel
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\CheckInstalled::class,
'check-installed',
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
@@ -71,6 +71,8 @@ class Kernel extends HttpKernel
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'check-installed' => \App\Http\Middleware\CheckInstalled::class,
'check-not-installed' => \App\Http\Middleware\CheckNotInstalled::class,
'deny-demo' => \App\Http\Middleware\DenyDemoUser::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,

View File

@@ -38,9 +38,9 @@ class CheckInstalled
*/
public function handle($request, Closure $next)
{
if (!file_exists(base_path('config.php')) && !$request->is('install.php')) {
if (!file_exists(base_path('config.php')) && !$request->is(['install', 'ajax/db-update'])) {
// no config.php does so let's redirect to the install
return redirect(url('/install.php'));
return redirect(url('/install'));
}
return $next($request);

44
app/StreamedOutput.php Normal file
View File

@@ -0,0 +1,44 @@
<?php
/**
* StreamedOutput.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;
use RuntimeException;
use Symfony\Component\Console\Output\StreamOutput;
class StreamedOutput extends StreamOutput
{
protected function doWrite($message, $newline)
{
if (false === @fwrite($this->getStream(), $message) || ($newline && (false === @fwrite($this->getStream(), PHP_EOL)))) {
throw new RuntimeException('Unable to write output.');
}
echo $message;
ob_flush();
flush();
}
}

View File

@@ -2,60 +2,55 @@
use LibreNMS\Authentication\LegacyAuth;
use LibreNMS\Config;
session_start();
$librenms_dir = realpath(__DIR__ . '/..');
$init_modules = array('web', 'nodb');
require realpath(__DIR__ . '/..') . '/includes/init.php';
if (empty($_POST) && !empty($_SESSION) && !isset($_REQUEST['stage'])) {
$_POST = $_SESSION;
} elseif (!file_exists("{$librenms_dir}/config.php")) {
$allowed_vars = array('stage','build-ok','dbhost','dbuser','dbpass','dbname','dbport','dbsocket','add_user','add_pass','add_email');
foreach ($allowed_vars as $allowed) {
if (isset($_POST[$allowed])) {
$_SESSION[$allowed] = $_POST[$allowed];
}
$allowed_vars = array('stage','build-ok','dbhost','dbuser','dbpass','dbname','dbport','dbsocket','add_user','add_pass','add_email');
foreach ($allowed_vars as $allowed) {
if (isset($_POST[$allowed])) {
session([$allowed => $_POST[$allowed]]);
// $_SESSION[$allowed] = $_POST[$allowed];
}
}
$stage = isset($_POST['stage']) ? $_POST['stage'] : 0;
$stage = session('stage', 0);
// Before we do anything, if we see config.php, redirect back to the homepage.
if (file_exists("{$librenms_dir}/config.php") && $stage != 6) {
unset($_SESSION['stage']);
header("Location: /");
exit;
}
//if (file_exists("{$librenms_dir}/config.php") && $stage != 6) {
// unset($_SESSION['stage']);
// header("Location: /");
// exit;
//}
// do not use the DB in init, we'll bring it up ourselves
$init_modules = array('web', 'nodb');
require realpath(__DIR__ . '/..') . '/includes/init.php';
// List of php modules we expect to see
$modules = array('gd','mysqlnd', 'pdo_mysql');
$dbhost = @$_POST['dbhost'] ?: 'localhost';
$dbuser = @$_POST['dbuser'] ?: 'librenms';
$dbpass = @$_POST['dbpass'] ?: '';
$dbname = @$_POST['dbname'] ?: 'librenms';
$dbport = @$_POST['dbport'] ?: 3306;
if (empty($_POST['dbsocket'])) {
$dbsocket = null;
} else {
$dbhost = session('dbhost', 'localhost');
$dbuser = session('dbuser', 'librenms');
$dbpass = session('dbpass', '');
$dbname = session('dbname', 'librenms');
$dbport = session('dbport', 3306);
if ($dbsocket = session('dbsocket')) {
$dbhost = 'localhost';
$dbsocket = $_POST['dbsocket'];
$dbport = null;
}
$add_user = @$_POST['add_user'] ?: '';
$add_pass = @$_POST['add_pass'] ?: '';
$add_email = @$_POST['add_email'] ?: '';
$add_user = session('add_user', '');
$add_pass = session('add_pass', '');
$add_email = session('add_email', '');
// Check we can connect to MySQL DB, if not, back to stage 1 :)
if ($stage > 1) {
try {
if ($stage != 6) {
dbConnect($dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbsocket);
if (dbIsConnected() === false) {
\LibreNMS\DB\Eloquent::setConnection('setup', $dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbsocket);
if (\LibreNMS\DB\Eloquent::isConnected('setup') === false) {
$msg = "We could not connect to your database, please check the details and try again";
$stage = 1;
}
@@ -68,11 +63,9 @@ if ($stage > 1) {
$stage = 1;
$msg = "Couldn't connect to the database, please check your details<br /> " . $e->getMessage();
}
$_SESSION['stage'] = $stage;
session(['stage' => $stage]);
}
session_write_close();
if ($stage == 4) {
// Now check we have a username, password and email before adding new user
if (empty($add_user) || empty($add_pass) || empty($add_email)) {
@@ -87,17 +80,15 @@ if ($stage == 4) {
$stage = 5;
} else {
// all done, remove all traces of the install session
session_unset();
session_destroy();
setcookie(session_name(), '', 0, '/');
session_regenerate_id(true);
session()->flush();
}
}
session()->save();
if (empty($stage)) {
$stage = 0;
}
var_dump(cookie());
$total_stages = 6;
$stage_perc = $stage / $total_stages * 100;
$complete = 1;
@@ -210,7 +201,7 @@ if ($stage == 0) {
}
}
echo "</td></tr>";
if (is_writable(Config::get('temp_dir'))) {
$status = 'yes';
$row_class = 'success';
@@ -343,7 +334,9 @@ if ($stage == 0) {
<script type="text/javascript">
var output = document.getElementById("db-update");
xhr = new XMLHttpRequest();
xhr.open("GET", "ajax_output.php?id=db-update", true);
xhr.open("GET", "ajax/db-update", true);
xhr.setRequestHeader('X-Requested-With','XMLHttpRequest');
xhr.withCredentials = true;
xhr.onprogress = function (e) {
output.innerHTML = e.currentTarget.responseText;
output.scrollTop = output.scrollHeight - output.clientHeight; // scrolls the output area

View File

@@ -23,40 +23,28 @@
* @author Tony Murray <murraytony@gmail.com>
*/
$init_modules = ['web', 'nodb'];
require \LibreNMS\Config::get('install_dir') . '/includes/init.php';
var_dump(session()->all()); exit;
if (file_exists(\LibreNMS\Config::get('install_dir') . '/config.php')) {
echo("This should only be called during install");
exit;
}
$init_modules = ['nodb'];
require \LibreNMS\Config::get('install_dir') . '/includes/init.php';
header("Content-type: text/plain");
header('X-Accel-Buffering: no');
$db_vars = array(
'dbhost' => 'host',
'dbuser' => 'username',
'dbpass' => 'password',
'dbname' => 'database',
'dbport' => 'port',
'dbsocket' => 'unix_socket',
\LibreNMS\DB\Eloquent::setConnection(
'setup',
session('dbhost'),
session('dbuser'),
session('dbpass'),
session('dbname'),
session('dbport')
);
\Config::set('database.connections.setup', [
"driver" => "mysql",
"host" => $_SESSION['dbhost'] ?: 'localhost',
"port" => $_SESSION['dbhost'] ?: 3306,
"database" => $_SESSION['dbname'] ?: 'librenms',
"username" => $_SESSION['dbuser'] ?: 'librenms',
"password" => $_SESSION['dbpass'] ?: '',
"charset" => "utf8",
"collation" => "utf8_unicode_ci",
"prefix" => "",
"strict" => true,
"engine" => null
]);
echo "Starting Update...\n";
try {
$ret = \Artisan::call('migrate', ['--seed' => true, '--force' => true, '--database' => 'setup']);
@@ -74,6 +62,3 @@ try {
http_response_code(500);
}
ob_end_flush();
flush();
session_write_close();

View File

@@ -148,6 +148,12 @@ Route::group(['middleware' => ['auth'], 'guard' => 'auth'], function () {
Route::permanentRedirect('demo', '/');
});
// installation routes
Route::group(['middleware' => ['check-not-installed']], function () {
Route::get('/install', 'Install\InstallationController');
Route::any('/ajax/db-update', 'Ajax\DatabaseUpdateController');
});
// Legacy routes
Route::any('/dummy_legacy_auth/{path?}', 'LegacyController@dummy')->middleware('auth');
Route::any('/dummy_legacy_unauth/{path?}', 'LegacyController@dummy');