Python3 Module Dependency Check (#11544)

* Python3 Modul Dependency Check

* code climate

* binary existance check

* move Python Modul Version Check

* .

* adding verbose argument

* args parser nicement

* enhance Validator for detailed approvements

* Update Python.php

* Update check_requirements.py

* python version to Version class
use Process

Co-authored-by: Tony Murray <murraytony@gmail.com>
This commit is contained in:
SourceDoctor
2020-05-06 17:58:17 +02:00
committed by GitHub
parent 598c8c0848
commit e170529692
7 changed files with 104 additions and 2 deletions

View File

@@ -26,6 +26,7 @@
namespace LibreNMS\Util;
use LibreNMS\DB\Eloquent;
use Symfony\Component\Process\Process;
class Version
{
@@ -88,4 +89,16 @@ class Version
? rtrim(shell_exec("git show --pretty='%ct' -s HEAD"))
: '';
}
public static function python()
{
$proc = new Process(['python3', '--version']);
$proc->run();
if ($proc->getExitCode() !== 0) {
return null;
}
return explode(' ', rtrim($proc->getOutput()), 2)[1] ?? null;
}
}

View File

@@ -0,0 +1,71 @@
<?php
/**
* Python.php
*
* Check that various Python modules and functions exist.
*
* 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 2020 Thomas Berberich
* @author Thomas Berberich <sourcehhdoctor@gmail.com>
*/
namespace LibreNMS\Validations;
use LibreNMS\Config;
use LibreNMS\Util\Version;
use LibreNMS\Validator;
class Python extends BaseValidation
{
const PYTHON_MIN_VERSION = '3.4.0';
/**
* Validate this module.
* To return ValidationResults, call ok, warn, fail, or result methods on the $validator
*
* @param Validator $validator
*/
public function validate(Validator $validator)
{
$version = Version::python();
if (empty($version)) {
$validator->fail('python3 not found');
return; // no need to check anything else
}
$this->checkVersion($validator, $version);
$this->checkExtensions($validator);
}
private function checkVersion(Validator $validator, $version)
{
if (version_compare($version, self::PYTHON_MIN_VERSION, '<')) {
$validator->warn('Python version ' . self::PYTHON_MIN_VERSION . ' is the minimum supported version. We recommend you update Python to a supported version.');
}
}
private function checkExtensions(Validator $validator)
{
$pythonExtensions = 'scripts/check_requirements.py';
exec(Config::get('install_dir') . '/' . $pythonExtensions . ' -v', $output, $returnval);
if ($returnval !== 0) {
$validator->fail("Python3 module issue found: '" . ($output[0] ?? '') . "'");
}
}
}

View File

@@ -72,6 +72,7 @@ class AboutController extends Controller
'version_local' => $version->local(),
'version_mysql' => current(DB::selectOne('select version()')),
'version_php' => phpversion(),
'version_python' => Version::python(),
'version_webserver' => $request->server('SERVER_SOFTWARE'),
'version_rrdtool' => str_replace('1.7.01.7.0', '1.7.0', implode(' ', array_slice(explode(' ', shell_exec(
Config::get('rrdtool', 'rrdtool') . ' --version | head -n1'

View File

@@ -1007,6 +1007,7 @@ function version_info($remote = false)
}
$output['db_schema'] = vsprintf('%s (%s)', $version->database());
$output['php_ver'] = phpversion();
$output['python_ver'] = \LibreNMS\Util\Version::python();
$output['mysql_ver'] = dbIsConnected() ? dbFetchCell('SELECT version()') : '?';
$output['rrdtool_ver'] = str_replace('1.7.01.7.0', '1.7.0', implode(' ', array_slice(explode(' ', shell_exec(
Config::get('rrdtool', 'rrdtool') . ' --version |head -n1'

View File

@@ -42,6 +42,10 @@
<td><b>@lang('PHP')</b></td>
<td>{{ $version_php }}</td>
</tr>
<tr>
<td><b>@lang('Python')</b></td>
<td>{{ $version_python }}</td>
</tr>
<tr>
<td><b>@lang('MySQL')</b></td>
<td>{{ $version_mysql }}</td>

View File

@@ -1,17 +1,27 @@
#! /usr/bin/env python3
import os
import sys
import pkg_resources
from pkg_resources import DistributionNotFound, VersionConflict
args = sys.argv
# verbose flag
verbose = '-v' in args
target = os.path.realpath(os.path.dirname(__file__) + '/../requirements.txt')
with open(target, 'r') as file:
requirements = file.read().rstrip().split("\n")
try:
pkg_resources.require(requirements)
except DistributionNotFound:
except DistributionNotFound as req:
if verbose:
print(req)
exit(1)
except VersionConflict:
except VersionConflict as req:
if verbose:
print(req)
exit(2)
exit(0)

View File

@@ -47,6 +47,7 @@ if (isset($options['h'])) {
- php: check that various PHP modules and functions exist
- poller: check that the poller and discovery are running properly
- programs: check that external programs exist and are executable
- python: check that various Python modules and functions exist
- updates: checks the status of git and updates
- user: check that the LibreNMS user is set properly
@@ -159,6 +160,7 @@ Component | Version
LibreNMS | ${versions['local_ver']}
DB Schema | ${versions['db_schema']}
PHP | ${versions['php_ver']}
Python | ${versions['python_ver']}
MySQL | ${versions['mysql_ver']}
RRDTool | ${versions['rrdtool_ver']}
SNMP | ${versions['netsnmp_ver']}