save-test-data.php improvements (#16367)

* save-test-data.php improvements
Message when snmprec data is not found
Output when venv setup fails

* Apply fixes from StyleCI

---------

Co-authored-by: Tony Murray <murrant@users.noreply.github.com>
This commit is contained in:
Tony Murray 2024-09-06 13:28:23 -05:00 committed by GitHub
parent fc9b77c672
commit 674bb7e9f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View File

@ -25,6 +25,7 @@
namespace LibreNMS\Util; namespace LibreNMS\Util;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
class Snmpsim extends Process class Snmpsim extends Process
@ -75,15 +76,25 @@ class Snmpsim extends Process
$snmpsim_venv_path = $this->getVenvPath(); $snmpsim_venv_path = $this->getVenvPath();
if (! $this->isVenvSetUp()) { if (! $this->isVenvSetUp()) {
\Log::info('Setting up snmpsim virtual env in ' . $snmpsim_venv_path); Log::info('Setting up snmpsim virtual env in ' . $snmpsim_venv_path);
$setupProcess = new Process(['python', '-m', 'venv', $snmpsim_venv_path]); $setupProcess = new Process(['python', '-m', 'venv', $snmpsim_venv_path]);
$setupProcess->setTty($print_output); $setupProcess->setTty($print_output);
$setupProcess->run(); $setupProcess->run();
if (! $setupProcess->isSuccessful()) {
Log::info($setupProcess->getOutput());
Log::error($setupProcess->getErrorOutput());
}
$installProcess = new Process([$snmpsim_venv_path . '/bin/pip', 'install', 'snmpsim']); $installProcess = new Process([$snmpsim_venv_path . '/bin/pip', 'install', 'snmpsim']);
$installProcess->setTty($print_output); $installProcess->setTty($print_output);
$installProcess->run(); $installProcess->run();
if (! $installProcess->isSuccessful()) {
Log::info($installProcess->getOutput());
Log::error($installProcess->getErrorOutput());
}
} }
} }

View File

@ -102,6 +102,12 @@ if (isset($os_name) && isset($variant)) {
$os_list = ModuleTestHelper::findOsWithData($modules); $os_list = ModuleTestHelper::findOsWithData($modules);
} }
if (empty($os_list)) {
echo "No matching snmprec(s) found.\n";
exit(1);
}
if (isset($options['f'])) { if (isset($options['f'])) {
if (count($os_list) != 1) { if (count($os_list) != 1) {
echo "Failed to create test data, -f/--file option can be used with one os/variant combination.\n"; echo "Failed to create test data, -f/--file option can be used with one os/variant combination.\n";
@ -113,7 +119,7 @@ if (isset($options['f'])) {
// Now use the saved data to update the saved database data // Now use the saved data to update the saved database data
$snmpsim = new Snmpsim(); $snmpsim = new Snmpsim();
$snmpsim->setupVenv(); $snmpsim->setupVenv(true);
$snmpsim->start(); $snmpsim->start();
echo "Waiting for snmpsim to initialize...\n"; echo "Waiting for snmpsim to initialize...\n";
$snmpsim->waitForStartup(); $snmpsim->waitForStartup();