2017-10-26 06:56:09 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Updates.php
|
|
|
|
*
|
|
|
|
* Checks the status of git and updates.
|
|
|
|
*
|
|
|
|
* 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
|
2021-02-08 23:29:04 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-10-26 06:56:09 +00:00
|
|
|
*
|
2021-02-08 23:29:04 +00:00
|
|
|
* @link https://www.librenms.org
|
2021-09-10 18:09:53 +00:00
|
|
|
*
|
2017-10-26 06:56:09 +00:00
|
|
|
* @copyright 2017 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace LibreNMS\Validations;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use DateTimeZone;
|
|
|
|
use Exception;
|
2022-02-02 16:33:23 +00:00
|
|
|
use LibreNMS\ComposerHelper;
|
2017-10-26 06:56:09 +00:00
|
|
|
use LibreNMS\Config;
|
2020-07-09 20:03:12 +00:00
|
|
|
use LibreNMS\Util\EnvHelper;
|
2019-01-09 03:42:56 +00:00
|
|
|
use LibreNMS\Util\Git;
|
2017-10-26 06:56:09 +00:00
|
|
|
use LibreNMS\ValidationResult;
|
|
|
|
use LibreNMS\Validator;
|
2022-06-14 18:36:46 +00:00
|
|
|
use Symfony\Component\Process\Process;
|
2017-10-26 06:56:09 +00:00
|
|
|
|
2018-02-27 15:57:20 +00:00
|
|
|
class Updates extends BaseValidation
|
2017-10-26 06:56:09 +00:00
|
|
|
{
|
2022-06-10 21:25:33 +00:00
|
|
|
public function validate(Validator $validator): void
|
2017-10-26 06:56:09 +00:00
|
|
|
{
|
2020-07-09 20:03:12 +00:00
|
|
|
if (EnvHelper::librenmsDocker()) {
|
2019-11-25 14:05:34 +00:00
|
|
|
$validator->warn('Updates are managed through the official Docker image');
|
2020-09-21 12:54:51 +00:00
|
|
|
|
2019-11-25 14:05:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-02 05:41:56 +00:00
|
|
|
if (! Git::make()->repoPresent()) {
|
2018-12-07 22:29:37 +00:00
|
|
|
$validator->warn('Non-git install, updates are manual or from package');
|
2020-09-21 12:54:51 +00:00
|
|
|
|
2018-12-07 22:29:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-26 06:56:09 +00:00
|
|
|
// if git is not available, we cannot do the other tests
|
2022-10-02 05:41:56 +00:00
|
|
|
if (! Git::make()->binaryExists()) {
|
2017-10-26 06:56:09 +00:00
|
|
|
$validator->warn('Unable to locate git. This should probably be installed.');
|
2020-09-21 12:54:51 +00:00
|
|
|
|
2017-10-26 06:56:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if users on master update channel are up to date
|
|
|
|
if (Config::get('update_channel') == 'master') {
|
2022-10-02 05:41:56 +00:00
|
|
|
$git = Git::make();
|
|
|
|
if ($git->commitHash() != $git->remoteHash()) {
|
|
|
|
if (! $git->commitDate()) {
|
2022-07-31 14:05:37 +00:00
|
|
|
$process = new Process(['git', 'show', '--quiet', '--pretty=%H|%ct'], base_path());
|
2022-06-14 18:36:46 +00:00
|
|
|
$process->run();
|
|
|
|
$error = rtrim($process->getErrorOutput());
|
|
|
|
|
|
|
|
$validator->fail('Failed to fetch version from local git: ' . $error);
|
|
|
|
} else {
|
|
|
|
try {
|
2022-10-02 05:41:56 +00:00
|
|
|
$commit_date = new DateTime('@' . $git->commitDate(), new DateTimeZone(date_default_timezone_get()));
|
2022-06-14 18:36:46 +00:00
|
|
|
if ($commit_date->diff(new DateTime())->days > 0) {
|
|
|
|
$validator->warn(
|
|
|
|
'Your install is over 24 hours out of date, last update: ' . $commit_date->format('r'),
|
|
|
|
'Make sure your daily.sh cron is running and run ./daily.sh by hand to see if there are any errors.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$validator->fail($e->getMessage());
|
2017-10-26 06:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-02 05:41:56 +00:00
|
|
|
$branch = $git->branch();
|
|
|
|
if ($branch != 'master') {
|
|
|
|
if ($branch == 'php53') {
|
2018-01-10 14:40:58 +00:00
|
|
|
$validator->warn(
|
|
|
|
'You are on the PHP 5.3 support branch, this will prevent automatic updates.',
|
2020-04-24 09:04:36 +00:00
|
|
|
'Update to PHP 5.6.4 or newer (PHP ' . Php::PHP_RECOMMENDED_VERSION . ' recommended) to continue to receive updates.'
|
2019-02-07 19:23:30 +00:00
|
|
|
);
|
2022-10-02 05:41:56 +00:00
|
|
|
} elseif ($branch == 'php56') {
|
2019-02-07 19:23:30 +00:00
|
|
|
$validator->warn(
|
|
|
|
'You are on the PHP 5.6/7.0 support branch, this will prevent automatic updates.',
|
2020-04-24 09:04:36 +00:00
|
|
|
'Update to PHP ' . Php::PHP_MIN_VERSION . ' or newer (PHP ' . Php::PHP_RECOMMENDED_VERSION . ' recommended) to continue to receive updates.'
|
2018-01-10 14:40:58 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$validator->warn(
|
|
|
|
'Your local git branch is not master, this will prevent automatic updates.',
|
|
|
|
'You can switch back to master with git checkout master'
|
|
|
|
);
|
|
|
|
}
|
2017-10-26 06:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO check update channel stable version
|
|
|
|
|
|
|
|
// check for modified files
|
|
|
|
$modifiedcmd = 'git diff --name-only --exit-code';
|
|
|
|
$validator->execAsUser($modifiedcmd, $cmdoutput, $code);
|
|
|
|
if ($code !== 0 && ! empty($cmdoutput)) {
|
2022-02-02 16:33:23 +00:00
|
|
|
|
|
|
|
// Check so it's not only plugins that "pests" the diff
|
|
|
|
if (! ($cmdoutput === ['composer.json', 'composer.lock'] && ComposerHelper::getPlugins())) {
|
|
|
|
$result = ValidationResult::warn(
|
|
|
|
'Your local git contains modified files, this could prevent automatic updates.',
|
|
|
|
'You can fix this with ./scripts/github-remove'
|
|
|
|
);
|
|
|
|
|
|
|
|
$result->setList('Modified Files', $cmdoutput);
|
|
|
|
$validator->result($result);
|
|
|
|
}
|
2017-10-26 06:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|