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

133 lines
3.9 KiB
PHP
Raw Normal View History

2020-06-05 14:26:57 -05:00
<?php
/**
* DatabaseController.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;
2020-06-15 00:45:40 -05:00
use App\StreamedOutput;
2020-06-05 18:35:51 -05:00
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use LibreNMS\DB\Eloquent;
2020-06-18 00:41:03 -05:00
use LibreNMS\DB\Schema;
2020-06-15 00:45:40 -05:00
use LibreNMS\Interfaces\InstallerStep;
use Symfony\Component\HttpFoundation\StreamedResponse;
2020-06-05 18:35:51 -05:00
2020-06-15 00:45:40 -05:00
class DatabaseController extends InstallationController implements InstallerStep
2020-06-05 14:26:57 -05:00
{
2020-06-05 18:35:51 -05:00
const KEYS = ['host', 'username', 'password', 'database', 'port', 'unix_socket'];
2020-06-21 01:07:56 -05:00
protected $step = 'database';
2020-06-05 18:35:51 -05:00
2020-06-11 01:06:16 -05:00
public function index(Request $request)
2020-06-05 18:35:51 -05:00
{
if (! $this->initInstallStep()) {
2020-06-21 01:07:56 -05:00
return $this->redirectToIncomplete();
}
2020-06-05 18:35:51 -05:00
$data = Arr::only(session()->get('db') ?: [], self::KEYS);
2020-06-18 00:41:03 -05:00
$data['valid_credentials'] = Eloquent::isConnected();
$data['migrated'] = session('install.database');
2020-06-05 18:35:51 -05:00
2020-06-11 01:06:16 -05:00
return view('install.database', $this->formatData($data));
2020-06-05 18:35:51 -05:00
}
public function test(Request $request)
2020-06-05 14:26:57 -05:00
{
2020-06-05 18:35:51 -05:00
Eloquent::setConnection(
'setup',
$request->get('host', 'localhost'),
$request->get('username', 'librenms'),
$request->get('password', ''),
$request->get('database', 'librenms'),
$request->get('port', 3306),
$request->get('unix_socket')
);
session()->put('db', Arr::only(config('database.connections.setup', []), self::KEYS));
2020-06-18 00:41:03 -05:00
session()->forget('install.database'); // reset db complete status
2020-06-05 18:35:51 -05:00
$ok = false;
$message = '';
try {
2020-06-06 17:03:32 -05:00
$conn = Eloquent::DB('setup');
$ok = $conn && ! is_null($conn->getPdo());
2020-06-05 18:35:51 -05:00
} catch (\Exception $e) {
$message = $e->getMessage();
}
return response()->json([
'result' => $ok ? 'ok' : 'fail',
'message' => $message,
]);
2020-06-05 14:26:57 -05:00
}
2020-06-07 14:53:40 -05:00
2020-06-15 00:45:40 -05:00
public function migrate(Request $request)
{
$response = new StreamedResponse(function () {
2020-06-15 00:45:40 -05:00
try {
$this->configureDatabase();
$output = new StreamedOutput(fopen('php://stdout', 'w'));
echo "Starting Update...\n";
2020-06-18 00:41:03 -05:00
$ret = \Artisan::call('migrate', ['--seed' => true, '--force' => true], $output);
2020-06-15 00:45:40 -05:00
if ($ret !== 0) {
throw new \RuntimeException('Migration failed');
}
echo "\n\nSuccess!";
2020-06-21 01:07:56 -05:00
$this->markStepComplete();
2020-06-15 00:45:40 -05:00
} catch (\Exception $e) {
echo $e->getMessage() . "\n\nError!";
}
});
$response->headers->set('Content-Type', 'text/plain');
$response->headers->set('X-Accel-Buffering', 'no');
return $response;
}
public function complete(): bool
{
2020-06-21 11:27:07 -05:00
if ($this->stepCompleted('database')) {
2020-06-18 00:41:03 -05:00
return true;
}
$this->configureDatabase();
if (Eloquent::isConnected() && Schema::isCurrent()) {
2020-06-21 01:07:56 -05:00
$this->markStepComplete();
2020-06-18 00:41:03 -05:00
return true;
}
2020-06-15 00:45:40 -05:00
return false;
}
public function enabled(): bool
2020-06-07 14:53:40 -05:00
{
return true;
}
2020-06-15 00:45:40 -05:00
public function icon(): string
2020-06-07 14:53:40 -05:00
{
return 'fa-database';
}
2020-06-05 14:26:57 -05:00
}