Skip to content

Commit 0a84098

Browse files
authored
Port Validation Page to Laravel (librenms#13921)
* Revamp validate web page to load page then validate, instead of validate then load page * style fixes * lint cleanups * fixes * translations and a couple fixes * style fixes * move result serialization into the class.
1 parent e495769 commit 0a84098

File tree

20 files changed

+344
-445
lines changed

20 files changed

+344
-445
lines changed

LibreNMS/Util/Html.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@ public static function graphRow($graph_array, $print = false)
136136
return $graph_data;
137137
}
138138

139+
/**
140+
* Find all links in some text and turn them into html links.
141+
*
142+
* @param string|null $text
143+
* @return string
144+
*/
145+
public static function linkify(?string $text): string
146+
{
147+
$regex = "#(http|https|ftp|ftps)://[a-z0-9\-.]*[a-z0-9\-]+(/\S*)?#i";
148+
149+
return preg_replace($regex, '<a href="$0">$0</a>', $text);
150+
}
151+
139152
public static function percentageBar($width, $height, $percent, $left_text = '', $right_text = '', $warn = null, $shadow = null, $colors = null)
140153
{
141154
$percent = min($percent, 100);

LibreNMS/Util/Version.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525

2626
namespace LibreNMS\Util;
2727

28+
use Illuminate\Http\Client\ConnectionException;
2829
use LibreNMS\Config;
2930
use LibreNMS\DB\Eloquent;
3031
use Symfony\Component\Process\Process;
3132

3233
class Version
3334
{
3435
// Update this on release
35-
const VERSION = '22.3.0';
36+
public const VERSION = '22.3.0';
3637

3738
/**
3839
* @var bool
@@ -58,6 +59,50 @@ public function local(): string
5859
return self::VERSION;
5960
}
6061

62+
/**
63+
* Compiles local commit data
64+
*
65+
* @return array with keys sha, date, and branch
66+
*/
67+
public function localCommit(): array
68+
{
69+
if ($this->is_git_install) {
70+
[$local_sha, $local_date] = explode('|', rtrim(`git show --pretty='%H|%ct' -s HEAD`));
71+
72+
return [
73+
'sha' => $local_sha,
74+
'date' => $local_date,
75+
'branch' => rtrim(`git rev-parse --abbrev-ref HEAD`),
76+
];
77+
}
78+
79+
return ['sha' => null, 'date' => null, 'branch' => null];
80+
}
81+
82+
/**
83+
* Fetches the remote commit from the github api if on the daily release channel
84+
*
85+
* @return array
86+
*/
87+
public function remoteCommit(): array
88+
{
89+
if ($this->is_git_install && Config::get('update_channel') == 'master') {
90+
try {
91+
$github = \Http::withOptions(['proxy' => Proxy::forGuzzle()])->get(Config::get('github_api') . 'commits/master');
92+
93+
return $github->json();
94+
} catch (ConnectionException $e) {
95+
}
96+
}
97+
98+
return [];
99+
}
100+
101+
public function databaseServer(): string
102+
{
103+
return \LibreNMS\DB\Eloquent::isConnected() ? \LibreNMS\DB\Eloquent::version() : 'Not Connected';
104+
}
105+
61106
public function database(): array
62107
{
63108
if (Eloquent::isConnected()) {

LibreNMS/ValidationResult.php

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,34 @@
2525

2626
namespace LibreNMS;
2727

28+
use LibreNMS\Util\Html;
29+
2830
class ValidationResult
2931
{
30-
const FAILURE = 0;
31-
const WARNING = 1;
32-
const SUCCESS = 2;
33-
const INFO = 3;
32+
public const FAILURE = 0;
33+
public const WARNING = 1;
34+
public const SUCCESS = 2;
35+
public const INFO = 3;
3436

37+
/** @var string */
3538
private $message;
39+
/** @var int */
3640
private $status;
41+
/** @var string */
3742
private $list_description = '';
43+
/** @var array */
3844
private $list;
45+
/** @var string|null */
3946
private $fix;
4047

4148
/**
4249
* ValidationResult constructor.
4350
*
4451
* @param string $message The message to describe this result
4552
* @param int $status The status of this result FAILURE, WARNING, or SUCCESS
46-
* @param string $fix a suggested fix to highlight for the user
53+
* @param string|null $fix a suggested fix to highlight for the user
4754
*/
48-
public function __construct($message, $status, $fix = null)
55+
public function __construct(string $message, int $status, string $fix = null)
4956
{
5057
$this->message = $message;
5158
$this->status = $status;
@@ -56,10 +63,10 @@ public function __construct($message, $status, $fix = null)
5663
* Create a new ok Validation result
5764
*
5865
* @param string $message The message to describe this result
59-
* @param string $fix a suggested fix to highlight for the user
66+
* @param string|null $fix a suggested fix to highlight for the user
6067
* @return ValidationResult
6168
*/
62-
public static function ok($message, $fix = null)
69+
public static function ok(string $message, string $fix = null): ValidationResult
6370
{
6471
return new self($message, self::SUCCESS, $fix);
6572
}
@@ -68,10 +75,10 @@ public static function ok($message, $fix = null)
6875
* Create a new warning Validation result
6976
*
7077
* @param string $message The message to describe this result
71-
* @param string $fix a suggested fix to highlight for the user
78+
* @param string|null $fix a suggested fix to highlight for the user
7279
* @return ValidationResult
7380
*/
74-
public static function warn($message, $fix = null)
81+
public static function warn(string $message, string $fix = null): ValidationResult
7582
{
7683
return new self($message, self::WARNING, $fix);
7784
}
@@ -82,7 +89,7 @@ public static function warn($message, $fix = null)
8289
* @param string $message The message to describe this result
8390
* @return ValidationResult
8491
*/
85-
public static function info($message)
92+
public static function info(string $message): ValidationResult
8693
{
8794
return new self($message, self::INFO);
8895
}
@@ -91,10 +98,10 @@ public static function info($message)
9198
* Create a new failure Validation result
9299
*
93100
* @param string $message The message to describe this result
94-
* @param string $fix a suggested fix to highlight for the user
101+
* @param string|null $fix a suggested fix to highlight for the user
95102
* @return ValidationResult
96103
*/
97-
public static function fail($message, $fix = null)
104+
public static function fail(string $message, string $fix = null): ValidationResult
98105
{
99106
return new self($message, self::FAILURE, $fix);
100107
}
@@ -105,27 +112,27 @@ public static function fail($message, $fix = null)
105112
*
106113
* @return int
107114
*/
108-
public function getStatus()
115+
public function getStatus(): int
109116
{
110117
return $this->status;
111118
}
112119

113-
public function getMessage()
120+
public function getMessage(): string
114121
{
115122
return $this->message;
116123
}
117124

118-
public function hasList()
125+
public function hasList(): bool
119126
{
120127
return ! empty($this->list);
121128
}
122129

123-
public function getList()
130+
public function getList(): ?array
124131
{
125132
return $this->list;
126133
}
127134

128-
public function setList($description, array $list)
135+
public function setList(string $description, array $list): ValidationResult
129136
{
130137
if (is_array(current($list))) {
131138
$list = array_map(function ($item) {
@@ -139,11 +146,14 @@ public function setList($description, array $list)
139146
return $this;
140147
}
141148

142-
public function hasFix()
149+
public function hasFix(): bool
143150
{
144151
return ! empty($this->fix);
145152
}
146153

154+
/**
155+
* @return string|array|null
156+
*/
147157
public function getFix()
148158
{
149159
return $this->fix;
@@ -156,7 +166,7 @@ public function getFix()
156166
* @param string|array $fix
157167
* @return ValidationResult $this
158168
*/
159-
public function setFix($fix)
169+
public function setFix($fix): ValidationResult
160170
{
161171
$this->fix = $fix;
162172

@@ -166,7 +176,7 @@ public function setFix($fix)
166176
/**
167177
* Print out this result to the console. Formatted nicely and with color.
168178
*/
169-
public function consolePrint()
179+
public function consolePrint(): void
170180
{
171181
c_echo(str_pad('[' . $this->getStatusText($this->status) . ']', 12) . $this->message . PHP_EOL);
172182

@@ -188,7 +198,7 @@ public function consolePrint()
188198
*
189199
* @return string
190200
*/
191-
public static function getStatusText($status)
201+
public static function getStatusText(int $status): string
192202
{
193203
$table = [
194204
self::SUCCESS => '%gOK%n',
@@ -200,19 +210,35 @@ public static function getStatusText($status)
200210
return $table[$status] ?? 'Unknown';
201211
}
202212

203-
public function getListDescription()
213+
public function getListDescription(): string
204214
{
205215
return $this->list_description;
206216
}
207217

218+
public function toArray(): array
219+
{
220+
$resultStatus = $this->getStatus();
221+
$resultFix = $this->getFix();
222+
$resultList = $this->getList();
223+
224+
return [
225+
'status' => $resultStatus,
226+
'statusText' => $this->getStatusText($resultStatus),
227+
'message' => $this->getMessage(),
228+
'fix' => is_array($resultFix) ? $resultFix : ($resultList ? [Html::linkify($resultFix)] : []),
229+
'listDescription' => $this->getListDescription(),
230+
'list' => is_array($resultList) ? array_values($resultList) : [],
231+
];
232+
}
233+
208234
/**
209235
* Print a list of items up to a max amount
210236
* If over that number, a line will print the total items
211237
*
212238
* @param string $format format as consumed by printf()
213239
* @param int $max the max amount of items to print, default 15
214240
*/
215-
private function printList($format = "\t %s\n", $max = 15)
241+
private function printList(string $format = "\t %s\n", int $max = 15): void
216242
{
217243
foreach (array_slice($this->list, 0, $max) as $item) {
218244
printf($format, $item);

LibreNMS/Validations/Rrd.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace LibreNMS\Validations;
2727

2828
use LibreNMS\Config;
29+
use LibreNMS\Util\Version;
2930
use LibreNMS\Validator;
3031

3132
class Rrd extends BaseValidation
@@ -38,18 +39,18 @@ class Rrd extends BaseValidation
3839
*/
3940
public function validate(Validator $validator)
4041
{
41-
$versions = $validator->getVersions();
42-
4342
// Check that rrdtool config version is what we see
44-
if (Config::has('rrdtool_version')
45-
&& version_compare(Config::get('rrdtool_version'), '1.5.5', '<')
46-
&& version_compare(Config::get('rrdtool_version'), $versions['rrdtool_ver'], '>')
47-
) {
48-
$validator->fail(
49-
'The rrdtool version you have specified is newer than what is installed.',
50-
"Either comment out \$config['rrdtool_version'] = '" .
51-
Config::get('rrdtool_version') . "'; or set \$config['rrdtool_version'] = '{$versions['rrdtool_ver']}';"
52-
);
43+
if (Config::has('rrdtool_version')) {
44+
$rrd_version = Version::get()->rrdtool();
45+
if (version_compare(Config::get('rrdtool_version'), '1.5.5', '<')
46+
&& version_compare(Config::get('rrdtool_version'), $rrd_version, '>')
47+
) {
48+
$validator->fail(
49+
'The rrdtool version you have specified is newer than what is installed.',
50+
"Either comment out \$config['rrdtool_version'] = '" .
51+
Config::get('rrdtool_version') . "'; or set \$config['rrdtool_version'] = '{$rrd_version}';"
52+
);
53+
}
5354
}
5455

5556
if (Config::get('rrdcached')) {

LibreNMS/Validations/Updates.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use LibreNMS\Config;
3333
use LibreNMS\Util\EnvHelper;
3434
use LibreNMS\Util\Git;
35+
use LibreNMS\Util\Version;
3536
use LibreNMS\ValidationResult;
3637
use LibreNMS\Validator;
3738

@@ -58,13 +59,13 @@ public function validate(Validator $validator)
5859
return;
5960
}
6061

61-
$versions = $validator->getVersions(true);
62-
6362
// check if users on master update channel are up to date
6463
if (Config::get('update_channel') == 'master') {
65-
if ($versions['local_sha'] != $versions['github']['sha']) {
64+
$local_ver = Version::get()->localCommit();
65+
$remote_ver = Version::get()->remoteCommit();
66+
if ($local_ver['sha'] != ($remote_ver['sha'] ?? null)) {
6667
try {
67-
$commit_date = new DateTime('@' . $versions['local_date'], new DateTimeZone(date_default_timezone_get()));
68+
$commit_date = new DateTime('@' . $local_ver['date'], new DateTimeZone(date_default_timezone_get()));
6869
if ($commit_date->diff(new DateTime())->days > 0) {
6970
$validator->warn(
7071
'Your install is over 24 hours out of date, last update: ' . $commit_date->format('r'),
@@ -76,13 +77,13 @@ public function validate(Validator $validator)
7677
}
7778
}
7879

79-
if ($versions['local_branch'] != 'master') {
80-
if ($versions['local_branch'] == 'php53') {
80+
if ($local_ver['branch'] != 'master') {
81+
if ($local_ver['branch'] == 'php53') {
8182
$validator->warn(
8283
'You are on the PHP 5.3 support branch, this will prevent automatic updates.',
8384
'Update to PHP 5.6.4 or newer (PHP ' . Php::PHP_RECOMMENDED_VERSION . ' recommended) to continue to receive updates.'
8485
);
85-
} elseif ($versions['local_branch'] == 'php56') {
86+
} elseif ($local_ver['branch'] == 'php56') {
8687
$validator->warn(
8788
'You are on the PHP 5.6/7.0 support branch, this will prevent automatic updates.',
8889
'Update to PHP ' . Php::PHP_MIN_VERSION . ' or newer (PHP ' . Php::PHP_RECOMMENDED_VERSION . ' recommended) to continue to receive updates.'

0 commit comments

Comments
 (0)