Add librenms command (#9619)

* Add librenms command

Hook up to artisan.  Hide dev commands and most other commands if app environment is production.
Register all commands for php artisan or when not in production.

* remove dead end line

* Console application name and version.
Had to shift some stuff from legacy code, but deferred others as it was too extensive of a change.

* switch check order

* always get local version now

* update array format

* whitepace

* fix style
This commit is contained in:
Tony Murray
2019-01-08 21:42:56 -06:00
committed by GitHub
parent c6e5a3f283
commit def8b3e514
14 changed files with 244 additions and 150 deletions

43
LibreNMS/Util/Git.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
/**
* Git.php
*
* -Description-
*
* 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 2019 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Util;
use LibreNMS\Config;
class Git
{
public static function repoPresent()
{
$install_dir = Config::get('install_dir', realpath(__DIR__ . '/../..'));
return file_exists("$install_dir/.git");
}
public static function binaryExists()
{
exec('git > /dev/null 2>&1', $response, $exit_code);
return $exit_code === 1;
}
}

53
LibreNMS/Util/Version.php Normal file
View File

@@ -0,0 +1,53 @@
<?php
/**
* Version.php
*
* Get version info about LibreNMS and various components/dependencies
*
* 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 2019 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Util;
class Version
{
// Update this on release
const VERSION = '1.47';
protected $is_git_install = false;
public function __construct()
{
$this->is_git_install = Git::repoPresent() && Git::binaryExists();
}
public static function get()
{
return new static;
}
public function local()
{
if ($this->is_git_install) {
return rtrim(shell_exec('git describe --tags'));
}
return self::VERSION;
}
}

View File

@@ -25,6 +25,7 @@
namespace LibreNMS\Validations;
use LibreNMS\Util\Git;
use LibreNMS\ValidationResult;
use LibreNMS\Validator;
@@ -39,7 +40,7 @@ class Dependencies extends BaseValidation
public function validate(Validator $validator)
{
# if git is not installed, do not assume composer is either
if (!is_git_install()) {
if (!Git::repoPresent()) {
$validator->ok("Installed from package; no Composer required");
return;
}

View File

@@ -29,6 +29,7 @@ use DateTime;
use DateTimeZone;
use Exception;
use LibreNMS\Config;
use LibreNMS\Util\Git;
use LibreNMS\ValidationResult;
use LibreNMS\Validator;
@@ -36,13 +37,13 @@ class Updates extends BaseValidation
{
public function validate(Validator $validator)
{
if (!is_git_install()) {
if (!Git::repoPresent()) {
$validator->warn('Non-git install, updates are manual or from package');
return;
}
// if git is not available, we cannot do the other tests
if (!check_git_exists()) {
if (!Git::binaryExists()) {
$validator->warn('Unable to locate git. This should probably be installed.');
return;
}

View File

@@ -26,6 +26,7 @@
namespace LibreNMS\Validations;
use LibreNMS\Config;
use LibreNMS\Util\Git;
use LibreNMS\ValidationResult;
use LibreNMS\Validator;
@@ -60,7 +61,7 @@ class User extends BaseValidation
}
# if no git, then we probably have different permissions by design
if (!is_git_install()) {
if (!Git::repoPresent()) {
return;
}

View File

@@ -1,46 +0,0 @@
<?php
namespace App\Console\Commands;
use App\Jobs\PingCheck;
use Illuminate\Console\Command;
class Ping extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ping {--d|debug} {groups?* | Optional List of distributed poller groups to poll}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check if devices are up or down via icmp';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->alert("Do not use this command yet, use ./ping.php");
exit();
PingCheck::dispatch(new PingCheck($this->argument('groups')));
}
}

View File

@@ -1,72 +0,0 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use LibreNMS\Util\GitHub;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;
class Release extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'release:tag
{tag : The new tag / version}
{from : The previous tag / version}
{--file= : The filename to update}
{--pr= : The last PR to include in this release if not master branch}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new LibreNMS release including changelog';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*/
public function handle()
{
$tag = $this->argument('tag');
$from = $this->argument('from');
$file = $this->option('file') ?: 'doc/General/Changelog.md';
$pr = $this->option('pr');
$token = getenv('GH_TOKEN') ?: $this->secret('Enter a GitHub Token?');
$this->info("Creating release $tag.....");
try {
$gh = new GitHub($tag, $from, $file, $token, $pr);
$gh->createChangelog();
$this->info("Changelog generated for $tag");
if ($this->confirm('Do you want to view the generated Changelog?')) {
echo $gh->getMarkdown();
}
if ($this->confirm("Do you want to create the release $tag on GitHub?")) {
if ($gh->createRelease()) {
$this->info('Release created.');
} else {
$this->error('Failed to create release, check github to see what was completed.');
}
}
} catch (\Exception $e) {
$this->error($e->getMessage());
}
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use LibreNMS\Util\Version;
class Kernel extends ConsoleKernel
{
@@ -12,10 +13,7 @@ class Kernel extends ConsoleKernel
*
* @var array
*/
protected $commands = [
Commands\Release::class,
Commands\Ping::class,
];
protected $commands = [];
/**
* Define the application's command schedule.
@@ -37,5 +35,20 @@ class Kernel extends ConsoleKernel
protected function commands()
{
require base_path('routes/console.php');
if ($this->app->environment() !== 'production') {
require base_path('routes/dev-console.php');
}
}
protected function getArtisan()
{
if (is_null($this->artisan)) {
parent::getArtisan();
$this->artisan->setName(\LibreNMS\Config::get('project_name', 'LibreNMS'));
$this->artisan->setVersion(Version::get()->local());
}
return $this->artisan;
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Providers;
use Illuminate\Foundation\Providers\ArtisanServiceProvider;
class CliServiceProvider extends ArtisanServiceProvider
{
public function register()
{
// Restrict LibreNMS CLI commands
if (defined('LIBRENMS_CLI') && $this->app->environment() == 'production') {
$this->commands = array_intersect_key($this->commands, [
"Migrate" => true,
]);
$this->registerCommands($this->commands);
} else {
$this->app->register(\Laravel\Tinker\TinkerServiceProvider::class);
parent::register();
}
}
}

View File

@@ -165,7 +165,6 @@ return [
/*
* Package Service Providers...
*/
Laravel\Tinker\TinkerServiceProvider::class,
Kamaln7\Toastr\ToastrServiceProvider::class,
Fideloper\Proxy\TrustedProxyServiceProvider::class,
@@ -173,6 +172,7 @@ return [
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\CliServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,

View File

@@ -19,6 +19,7 @@
use LibreNMS\Authentication\LegacyAuth;
use LibreNMS\Config;
use LibreNMS\Exceptions\InvalidIpException;
use LibreNMS\Util\Git;
use LibreNMS\Util\Html;
use LibreNMS\Util\IP;
@@ -1159,8 +1160,10 @@ function parse_location($location)
function version_info($remote = false)
{
global $config;
$output = array();
if (is_git_install() && check_git_exists()) {
$output = [
'local_ver' => \LibreNMS\Util\Version::get()->local(),
];
if (Git::repoPresent() && Git::binaryExists()) {
if ($remote === true && $config['update_channel'] == 'master') {
$api = curl_init();
set_curl_proxy($api);
@@ -1173,7 +1176,6 @@ function version_info($remote = false)
$output['github'] = json_decode(curl_exec($api), true);
}
list($local_sha, $local_date) = explode('|', rtrim(`git show --pretty='%H|%ct' -s HEAD`));
$output['local_ver'] = rtrim(`git describe --tags`);
$output['local_sha'] = $local_sha;
$output['local_date'] = $local_date;
$output['local_branch'] = rtrim(`git rev-parse --abbrev-ref HEAD`);
@@ -1722,22 +1724,6 @@ function set_numeric($value, $default = 0)
return $value;
}
function is_git_install()
{
$install_dir = Config::get('install_dir', realpath(__DIR__ . '/..'));
return file_exists("$install_dir/.git");
}
function check_git_exists()
{
exec('git > /dev/null 2>&1', $response, $exit_code);
if ($exit_code === 1) {
return true;
} else {
return false;
}
}
function get_vm_parent_id($device)
{
global $config;

53
librenms Executable file
View File

@@ -0,0 +1,53 @@
#!/usr/bin/env php
<?php
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Artisan Application
|--------------------------------------------------------------------------
|
| When we run the console application, the current CLI command will be
| executed in this console and the response sent back to a terminal
| or another output device for the developers. Here goes nothing!
|
*/
define('LIBRENMS_CLI', true);
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$status = $kernel->handle(
$input = new Symfony\Component\Console\Input\ArgvInput,
new Symfony\Component\Console\Output\ConsoleOutput
);
/*
|--------------------------------------------------------------------------
| Shutdown The Application
|--------------------------------------------------------------------------
|
| Once Artisan has finished running, we will fire off the shutdown events
| so that any final work may be done by the application before we shut
| down the process. This is the last thing to happen to the request.
|
*/
$kernel->terminate($input, $status);
exit($status);

View File

@@ -1,6 +1,6 @@
<?php
use Illuminate\Foundation\Inspiring;
use App\Jobs\PingCheck;
/*
|--------------------------------------------------------------------------
@@ -13,6 +13,7 @@ use Illuminate\Foundation\Inspiring;
|
*/
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->describe('Display an inspiring quote');
Artisan::command('ping {--d|debug} {groups?* : Optional List of distributed poller groups to poll}', function () {
$this->alert("Do not use this command yet, use ./ping.php");
// PingCheck::dispatch(new PingCheck($this->argument('groups')));
})->describe('Check if devices are up or down via icmp');

37
routes/dev-console.php Normal file
View File

@@ -0,0 +1,37 @@
<?php
use LibreNMS\Util\GitHub;
Artisan::command('release:tag
{tag : The new tag / version}
{from : The previous tag / version}
{--file= : The filename to update}
{--pr= : The last PR to include in this release if not master branch}', function () {
$tag = $this->argument('tag');
$this->info("Creating release $tag.....");
try {
$gh = new GitHub(
$tag,
$this->argument('from'),
$this->option('file') ?: 'doc/General/Changelog.md',
getenv('GH_TOKEN') ?: $this->secret('Enter a GitHub Token?'),
$this->option('pr')
);
$gh->createChangelog();
$this->info("Changelog generated for $tag");
if ($this->confirm('Do you want to view the generated Changelog?')) {
echo $gh->getMarkdown();
}
if ($this->confirm("Do you want to create the release $tag on GitHub?")) {
if ($gh->createRelease()) {
$this->info('Release created.');
} else {
$this->error('Failed to create release, check github to see what was completed.');
}
}
} catch (\Exception $e) {
$this->error($e->getMessage());
}
})->describe('Create a new LibreNMS release including changelog');