Skip to content

Commit

Permalink
feature: Improved install experience (librenms#6915)
Browse files Browse the repository at this point in the history
always check that the base sql has been imported.
async db building
After this merge, we could move install.php to the end of the install documentation.  Makes a more natural flow.
  • Loading branch information
murrant authored and laf committed Jul 1, 2017
1 parent 11cce1f commit f02b551
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 141 deletions.
87 changes: 44 additions & 43 deletions build-base.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,54 @@
#!/usr/bin/env php
<?php
/**
* build-base.php
*
* Create database structure.
*
* 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 2017 Tony Murray
* @author Tony Murray <[email protected]>
*/

if (!isset($init_modules)) {
$opts = getopt('dh:u:p:n:t:s:');

$db_vars = array(
'db_host' => 'h',
'db_user' => 'u',
'db_pass' => 'p',
'db_name' => 'n',
'db_port' => 't',
'db_socket' => 's',
);

$config = array();
foreach ($db_vars as $setting => $opt) {
if (isset($opts[$opt])) {
$config[$setting] = $opts[$opt];
}
}

$init_modules = array();
require __DIR__ . '/includes/init.php';

$options = getopt('d');
$debug = isset($options['d']);
}

if (!isset($sql_file)) {
$sql_file = 'build.sql';
}

$sql_fh = fopen($sql_file, 'r');
if ($sql_fh === false) {
echo 'ERROR: Cannot open SQL build script '.$sql_file.PHP_EOL;
exit(1);
}

// only import build.sql to an empty database
$tables = dbFetchRows("SHOW TABLES FROM {$config['db_name']}");
if (empty($tables)) {
$limit = 0;

while (!feof($sql_fh)) {
$line = fgetss($sql_fh);
if (isset($_SESSION['stage'])) {
$limit++;
if (isset($_SESSION['offset']) && $limit < $_REQUEST['offset']) {
continue;
} elseif (time()-$_SESSION['last'] > 45) {
$_SESSION['offset'] = $limit;
$GLOBALS['refresh'] = '<b>Installing, please wait..</b><sub>'.date('r').'</sub><script>window.location.href = "install.php?offset='.$limit.'";</script>';
return;
} else {
echo 'Step #'.$limit.' ...'.PHP_EOL;
}
}

if (!empty($line)) {
$creation = dbQuery($line);
if (!$creation) {
echo 'WARNING: Cannot execute query ('.$line.'): '.mysqli_error($database_link)."\n";
}
}
}
$debug = isset($opts['d']);
}

fclose($sql_fh);

require 'includes/sql-schema/update.php';

exit($return);
19 changes: 13 additions & 6 deletions html/ajax_output.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,23 @@
* the source code distribution for details.
*/

$init_modules = array('web', 'auth', 'alerts');
require realpath(__DIR__ . '/..') . '/includes/init.php';
session_start();
if (isset($_SESSION['stage']) && $_SESSION['stage'] == 2) {
$_SESSION['build-ok'] = true;
$init_modules = array('web', 'nodb');
require realpath(__DIR__ . '/..') . '/includes/init.php';
} else {
$init_modules = array('web', 'auth', 'alerts');
require realpath(__DIR__ . '/..') . '/includes/init.php';

if (!$_SESSION['authenticated']) {
echo "Unauthenticated\n";
exit;
if (!$_SESSION['authenticated']) {
echo "Unauthenticated\n";
exit;
}
}

set_debug($_REQUEST['debug']);
$id = mres($_REQUEST['id']);
$id = str_replace('/', '', $_REQUEST['id']);

if (isset($id)) {
require $config['install_dir'] . "/html/includes/output/$id.inc.php";
Expand Down
67 changes: 67 additions & 0 deletions html/includes/output/db-update.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* db-update.inc.php
*
* Run database update/deploy for installer
*
* 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 2017 Tony Murray
* @author Tony Murray <[email protected]>
*/

if (file_exists($config['install_dir'] . '/config.php')) {
echo("This should only be called during install");
exit;
}

header("Content-type: text/plain");
header('X-Accel-Buffering: no');

$db_vars = array(
'dbhost' => 'h',
'dbuser' => 'u',
'dbpass' => 'p',
'dbname' => 'n',
'dbport' => 't',
'dbsocket' => 's',
);

$cmd = $config['install_dir'] . '/build-base.php';

foreach ($db_vars as $var => $opt) {
if ($_SESSION[$var]) {
$cmd .= " -$opt {$_SESSION[$var]}";
}
}

echo "Starting Update...\n";

if (($fp = popen($cmd . ' 2>&1', "r"))) {
while (!feof($fp)) {
$line = stream_get_line($fp, 1024, "\n");
echo preg_replace('/\033\[[\d;]+m/', '', $line) . PHP_EOL;
ob_flush();
flush(); // you have to flush the buffer
}

if (pclose($fp) === 0) {
echo "Database is up to date!";
$_SESSION['build-ok'] = true;
}
}

session_write_close();
49 changes: 21 additions & 28 deletions html/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
if (empty($_POST) && !empty($_SESSION) && !isset($_REQUEST['stage'])) {
$_POST = $_SESSION;
} else {
$_SESSION = $_POST;
$_SESSION = array_replace($_SESSION, $_POST);
}

$stage = isset($_POST['stage']) ? $_POST['stage'] : 0;
Expand Down Expand Up @@ -64,6 +64,8 @@
$_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)) {
Expand All @@ -89,7 +91,6 @@
$complete = 1;

?>

<!DOCTYPE HTML>
<html>
<head>
Expand All @@ -100,11 +101,9 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="<?php echo($config['stylesheet']); ?>" rel="stylesheet" type="text/css" />
<link href="css/typeahead.js-bootstrap.css" rel="stylesheet" type="text/css" />
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap-hover-dropdown.min.js"></script>
<script src="js/typeahead.min.js"></script>
<script src="js/hogan-2.0.0.js"></script>

</head>
Expand Down Expand Up @@ -309,29 +308,7 @@
</div>
<div class="col-md-6">
<h5 class="text-center">Importing MySQL DB - Do not close this page or interrupt the import</h5>
<?php
// Ok now let's set the db connection up
$config['db_host'] = $dbhost;
$config['db_user'] = $dbuser;
$config['db_pass'] = $dbpass;
$config['db_name'] = $dbname;
$config['db_port'] = $dbport;
$config['db_socket'] = $dbsocket;
$sql_file = '../build.sql';
$_SESSION['last'] = time();
ob_end_flush();
ob_start();
if ($_SESSION['offset'] < 100 && $_REQUEST['offset'] < 94) {
require '../build-base.php';
} else {
require '../includes/sql-schema/update.php';
}
$_SESSION['out'] .= ob_get_clean();
ob_end_clean();
ob_start();
echo $GLOBALS['refresh'];
echo "<pre>".trim($_SESSION['out'])."</pre>";
?>
<textarea readonly id="db-update" class="form-control" rows="20" placeholder="Please Wait..." style="resize:vertical;"></textarea>
</div>
<div class="col-md-3">
</div>
Expand All @@ -349,12 +326,28 @@
<input type="hidden" name="dbname" value="<?php echo $dbname; ?>">
<input type="hidden" name="dbport" value="<?php echo $dbport; ?>">
<input type="hidden" name="dbsocket" value="<?php echo $dbsocket; ?>">
<button type="submit" class="btn btn-success">Goto Add User</button>
<button type="submit" id="add-user-btn" class="btn btn-success" disabled>Goto Add User</button>
</form>
</div>
<div class="col-md-3">
</div>
</div>
<script type="text/javascript">
output = document.getElementById("db-update");
xhr = new XMLHttpRequest();
xhr.open("GET", "ajax_output.php?id=db-update", true);
xhr.onprogress = function (e) {
output.innerHTML = e.currentTarget.responseText;
output.scrollTop = output.scrollHeight - output.clientHeight; // scrolls the output area
};
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log("Complete");
document.getElementById("add-user-btn").removeAttribute('disabled');
}
};
xhr.send();
</script>
<?php
} elseif ($stage == "5") {
?>
Expand Down
2 changes: 1 addition & 1 deletion includes/dbFacile.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function dbConnect($host = null, $user = '', $password = '', $database = '', $po
if (!$database_db) {
$db_create_sql = "CREATE DATABASE " . $config['db_name'] . " CHARACTER SET utf8 COLLATE utf8_unicode_ci";
mysqli_query($database_link, $db_create_sql);
$database_db = mysqli_select_db($database_link, $config['db_name']);
$database_db = mysqli_select_db($database_link, $database);
}

if (!$database_db) {
Expand Down
Loading

0 comments on commit f02b551

Please sign in to comment.