1 | <?php |
||||
2 | /** |
||||
3 | * DatabaseController.php |
||||
4 | * |
||||
5 | * -Description- |
||||
6 | * |
||||
7 | * This program is free software: you can redistribute it and/or modify |
||||
8 | * it under the terms of the GNU General Public License as published by |
||||
9 | * the Free Software Foundation, either version 3 of the License, or |
||||
10 | * (at your option) any later version. |
||||
11 | * |
||||
12 | * This program is distributed in the hope that it will be useful, |
||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||||
15 | * GNU General Public License for more details. |
||||
16 | * |
||||
17 | * You should have received a copy of the GNU General Public License |
||||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||||
19 | * |
||||
20 | * @link https://www.librenms.org |
||||
21 | * |
||||
22 | * @copyright 2020 Tony Murray |
||||
23 | * @author Tony Murray <[email protected]> |
||||
24 | */ |
||||
25 | |||||
26 | namespace App\Http\Controllers\Install; |
||||
27 | |||||
28 | use App\StreamedOutput; |
||||
29 | use Illuminate\Http\Request; |
||||
30 | use Illuminate\Support\Arr; |
||||
31 | use LibreNMS\DB\Eloquent; |
||||
32 | use LibreNMS\DB\Schema; |
||||
33 | use LibreNMS\Interfaces\InstallerStep; |
||||
34 | use LibreNMS\ValidationResult; |
||||
35 | use LibreNMS\Validations\Database; |
||||
36 | use LibreNMS\Validator; |
||||
37 | use Symfony\Component\HttpFoundation\StreamedResponse; |
||||
38 | |||||
39 | class DatabaseController extends InstallationController implements InstallerStep |
||||
40 | { |
||||
41 | const KEYS = ['host', 'username', 'password', 'database', 'port', 'unix_socket']; |
||||
42 | protected $step = 'database'; |
||||
43 | |||||
44 | public function index(Request $request) |
||||
0 ignored issues
–
show
|
|||||
45 | { |
||||
46 | if (! $this->initInstallStep()) { |
||||
47 | return $this->redirectToIncomplete(); |
||||
48 | } |
||||
49 | |||||
50 | $data = Arr::only(session()->get('db') ?: [], self::KEYS); |
||||
51 | $data['valid_credentials'] = Eloquent::isConnected(); |
||||
52 | $data['migrated'] = session('install.database'); |
||||
53 | |||||
54 | return view('install.database', $this->formatData($data)); |
||||
55 | } |
||||
56 | |||||
57 | public function test(Request $request) |
||||
58 | { |
||||
59 | Eloquent::setConnection( |
||||
60 | 'setup', |
||||
61 | $request->get('host', 'localhost'), |
||||
62 | $request->get('username', 'librenms'), |
||||
63 | $request->get('password', ''), |
||||
64 | $request->get('database', 'librenms'), |
||||
65 | $request->get('port', 3306), |
||||
66 | $request->get('unix_socket') |
||||
67 | ); |
||||
68 | |||||
69 | session()->put('db', Arr::only(config('database.connections.setup', []), self::KEYS)); |
||||
70 | session()->forget('install.database'); // reset db complete status |
||||
71 | |||||
72 | $ok = false; |
||||
73 | $messages = []; |
||||
74 | try { |
||||
75 | $conn = Eloquent::DB('setup'); |
||||
76 | $ok = $conn && ! is_null($conn->getPdo()); |
||||
77 | |||||
78 | // validate Database |
||||
79 | $validator = new Validator(); |
||||
80 | (new Database())->validateSystem($validator); |
||||
81 | $results = $validator->getResults('database'); |
||||
82 | |||||
83 | foreach ($results as $result) { |
||||
84 | if ($result->getStatus() == ValidationResult::FAILURE) { |
||||
85 | $ok = false; |
||||
86 | $messages[] = $result->getMessage() . ' ' . $result->getFix(); |
||||
87 | } |
||||
88 | } |
||||
89 | } catch (\Exception $e) { |
||||
90 | $messages[] = $e->getMessage(); |
||||
91 | } |
||||
92 | |||||
93 | return response()->json([ |
||||
94 | 'result' => $ok ? 'ok' : 'fail', |
||||
95 | 'message' => implode('<br />', $messages), |
||||
96 | ]); |
||||
97 | } |
||||
98 | |||||
99 | public function migrate(Request $request) |
||||
0 ignored issues
–
show
The parameter
$request is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
|
|||||
100 | { |
||||
101 | $response = new StreamedResponse(function () { |
||||
102 | try { |
||||
103 | $this->configureDatabase(); |
||||
104 | $output = new StreamedOutput(fopen('php://stdout', 'w')); |
||||
105 | echo "Starting Update...\n"; |
||||
106 | $ret = \Artisan::call('migrate', ['--seed' => true, '--force' => true], $output); |
||||
107 | if ($ret !== 0) { |
||||
108 | throw new \RuntimeException('Migration failed'); |
||||
109 | } |
||||
110 | echo "\n\nSuccess!"; |
||||
111 | $this->markStepComplete(); |
||||
112 | } catch (\Exception $e) { |
||||
113 | echo $e->getMessage() . "\n\nError!"; |
||||
114 | } |
||||
115 | }); |
||||
116 | |||||
117 | $response->headers->set('Content-Type', 'text/plain'); |
||||
118 | $response->headers->set('X-Accel-Buffering', 'no'); |
||||
119 | |||||
120 | return $response; |
||||
121 | } |
||||
122 | |||||
123 | public function complete(): bool |
||||
124 | { |
||||
125 | if ($this->stepCompleted('database')) { |
||||
126 | return true; |
||||
127 | } |
||||
128 | |||||
129 | $this->configureDatabase(); |
||||
130 | if (Eloquent::isConnected() && Schema::isCurrent()) { |
||||
131 | $this->markStepComplete(); |
||||
132 | |||||
133 | return true; |
||||
134 | } |
||||
135 | |||||
136 | return false; |
||||
137 | } |
||||
138 | |||||
139 | public function enabled(): bool |
||||
140 | { |
||||
141 | return true; |
||||
142 | } |
||||
143 | |||||
144 | public function icon(): string |
||||
145 | { |
||||
146 | return 'fa-database'; |
||||
147 | } |
||||
148 | } |
||||
149 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.