2016-08-22 10:32:05 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* bootstrap.php
|
|
|
|
*
|
|
|
|
* Initialize the Autoloader and includes for phpunit to be able to run tests
|
|
|
|
*
|
|
|
|
* 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 2016 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2018-04-11 10:15:13 -05:00
|
|
|
use LibreNMS\Config;
|
2018-09-11 07:51:35 -05:00
|
|
|
use LibreNMS\DB\Eloquent;
|
2018-04-11 10:15:13 -05:00
|
|
|
use LibreNMS\Exceptions\DatabaseConnectException;
|
2017-12-20 08:36:49 -06:00
|
|
|
use LibreNMS\Util\Snmpsim;
|
|
|
|
|
2017-04-07 09:07:44 -05:00
|
|
|
global $config;
|
|
|
|
|
2016-08-22 10:32:05 -05:00
|
|
|
$install_dir = realpath(__DIR__ . '/..');
|
|
|
|
|
2018-04-11 10:15:13 -05:00
|
|
|
$init_modules = array('web', 'discovery', 'polling', 'nodb');
|
2016-09-19 21:12:26 -05:00
|
|
|
|
2017-02-07 15:08:52 +00:00
|
|
|
if (!getenv('SNMPSIM')) {
|
|
|
|
$init_modules[] = 'mocksnmp';
|
|
|
|
}
|
2016-08-22 10:32:05 -05:00
|
|
|
|
2017-02-07 15:08:52 +00:00
|
|
|
if (getenv('DBTEST')) {
|
|
|
|
if (!is_file($install_dir . '/config.php')) {
|
|
|
|
exec("cp $install_dir/tests/config/config.test.php $install_dir/config.php");
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 17:59:59 -05:00
|
|
|
|
2017-02-07 15:08:52 +00:00
|
|
|
require $install_dir . '/includes/init.php';
|
|
|
|
chdir($install_dir);
|
2016-11-21 14:12:59 -06:00
|
|
|
|
2016-09-23 15:44:17 -05:00
|
|
|
ini_set('display_errors', 1);
|
2017-12-20 08:36:49 -06:00
|
|
|
//error_reporting(E_ALL & ~E_WARNING);
|
2017-04-07 09:07:44 -05:00
|
|
|
|
2017-08-16 19:58:10 +01:00
|
|
|
update_os_cache(true); // Force update of OS Cache
|
2017-04-07 09:07:44 -05:00
|
|
|
|
2018-05-09 06:53:45 -05:00
|
|
|
$snmpsim = new Snmpsim('127.1.6.2', 1162, null);
|
2017-12-20 08:36:49 -06:00
|
|
|
if (getenv('SNMPSIM')) {
|
|
|
|
$snmpsim->fork();
|
|
|
|
|
|
|
|
// make PHP hold on a reference to $snmpsim so it doesn't get destructed
|
|
|
|
register_shutdown_function(function (Snmpsim $ss) {
|
|
|
|
$ss->stop();
|
|
|
|
}, $snmpsim);
|
|
|
|
}
|
|
|
|
|
2017-04-07 09:07:44 -05:00
|
|
|
if (getenv('DBTEST')) {
|
2017-04-26 07:56:00 -05:00
|
|
|
global $schema, $sql_mode;
|
2017-04-07 09:07:44 -05:00
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
// create testing table if needed
|
|
|
|
$db_config = Config::getDatabaseSettings();
|
|
|
|
$db_name = $db_config['db_name'];
|
|
|
|
|
2018-08-19 09:29:34 +01:00
|
|
|
$connection = new PDO("mysql:host={$db_config['db_host']}", $db_config['db_user'], $db_config['db_pass']);
|
2019-01-14 07:44:23 -05:00
|
|
|
$connection->query("CREATE DATABASE IF NOT EXISTS $db_name CHARACTER SET utf8 COLLATE utf8_unicode_ci");
|
2018-08-17 15:29:20 -05:00
|
|
|
unset($connection); // close connection
|
|
|
|
|
|
|
|
\LibreNMS\DB\Eloquent::boot();
|
|
|
|
\LibreNMS\DB\Eloquent::setStrictMode();
|
2018-04-11 10:15:13 -05:00
|
|
|
|
|
|
|
$empty_db = (dbFetchCell("SELECT count(*) FROM `information_schema`.`tables` WHERE `table_type` = 'BASE TABLE' AND `table_schema` = ?", [$db_name]) == 0);
|
2017-04-07 09:07:44 -05:00
|
|
|
|
2017-07-01 15:28:29 -05:00
|
|
|
$cmd = $config['install_dir'] . '/build-base.php';
|
2017-04-07 09:07:44 -05:00
|
|
|
exec($cmd, $schema);
|
2017-04-26 07:56:00 -05:00
|
|
|
|
2018-04-11 10:15:13 -05:00
|
|
|
Config::load(); // reload the config including database config
|
2018-08-11 23:37:45 +02:00
|
|
|
load_all_os();
|
2018-04-11 10:15:13 -05:00
|
|
|
|
2017-04-26 07:56:00 -05:00
|
|
|
register_shutdown_function(function () use ($empty_db, $sql_mode) {
|
|
|
|
global $config;
|
2018-08-17 15:29:20 -05:00
|
|
|
\LibreNMS\DB\Eloquent::boot();
|
2017-04-26 07:56:00 -05:00
|
|
|
|
2017-12-20 08:36:49 -06:00
|
|
|
echo "Cleaning database...\n";
|
|
|
|
|
2018-04-11 10:15:13 -05:00
|
|
|
$db_name = dbFetchCell('SELECT DATABASE()');
|
2017-04-26 07:56:00 -05:00
|
|
|
if ($empty_db) {
|
2018-04-11 10:15:13 -05:00
|
|
|
dbQuery("DROP DATABASE $db_name");
|
|
|
|
} elseif (isset($config['test_db_name']) && $config['test_db_name'] == $db_name) {
|
2017-04-26 07:56:00 -05:00
|
|
|
// truncate tables
|
|
|
|
$tables = dbFetchColumn('SHOW TABLES');
|
|
|
|
|
|
|
|
$excluded = array(
|
|
|
|
'alert_templates',
|
|
|
|
'config', // not sure about this one
|
|
|
|
'dbSchema',
|
2019-02-12 17:45:04 -06:00
|
|
|
'migrations',
|
2017-04-26 07:56:00 -05:00
|
|
|
'widgets',
|
|
|
|
);
|
|
|
|
$truncate = array_diff($tables, $excluded);
|
|
|
|
|
|
|
|
dbQuery("SET FOREIGN_KEY_CHECKS = 0");
|
|
|
|
foreach ($truncate as $table) {
|
|
|
|
dbQuery("TRUNCATE TABLE $table");
|
|
|
|
}
|
|
|
|
dbQuery("SET FOREIGN_KEY_CHECKS = 1");
|
|
|
|
}
|
|
|
|
});
|
2017-04-07 09:07:44 -05:00
|
|
|
}
|