From def8b3e514bef55b0de7a4b64aa5ac4cb919787d Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Tue, 8 Jan 2019 21:42:56 -0600 Subject: [PATCH] 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 --- LibreNMS/Util/Git.php | 43 ++++++++++++++++ LibreNMS/Util/Version.php | 53 ++++++++++++++++++++ LibreNMS/Validations/Dependencies.php | 3 +- LibreNMS/Validations/Updates.php | 5 +- LibreNMS/Validations/User.php | 3 +- app/Console/Commands/Ping.php | 46 ----------------- app/Console/Commands/Release.php | 72 --------------------------- app/Console/Kernel.php | 21 ++++++-- app/Providers/CliServiceProvider.php | 23 +++++++++ config/app.php | 2 +- includes/common.php | 24 ++------- librenms | 53 ++++++++++++++++++++ routes/console.php | 9 ++-- routes/dev-console.php | 37 ++++++++++++++ 14 files changed, 244 insertions(+), 150 deletions(-) create mode 100644 LibreNMS/Util/Git.php create mode 100644 LibreNMS/Util/Version.php delete mode 100644 app/Console/Commands/Ping.php delete mode 100644 app/Console/Commands/Release.php create mode 100644 app/Providers/CliServiceProvider.php create mode 100755 librenms create mode 100644 routes/dev-console.php diff --git a/LibreNMS/Util/Git.php b/LibreNMS/Util/Git.php new file mode 100644 index 0000000000..cb6c096b6a --- /dev/null +++ b/LibreNMS/Util/Git.php @@ -0,0 +1,43 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2019 Tony Murray + * @author Tony Murray + */ + +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; + } +} diff --git a/LibreNMS/Util/Version.php b/LibreNMS/Util/Version.php new file mode 100644 index 0000000000..4ed6a5dcc8 --- /dev/null +++ b/LibreNMS/Util/Version.php @@ -0,0 +1,53 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2019 Tony Murray + * @author Tony Murray + */ + +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; + } +} diff --git a/LibreNMS/Validations/Dependencies.php b/LibreNMS/Validations/Dependencies.php index aa3741b5d9..1b8329666a 100644 --- a/LibreNMS/Validations/Dependencies.php +++ b/LibreNMS/Validations/Dependencies.php @@ -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; } diff --git a/LibreNMS/Validations/Updates.php b/LibreNMS/Validations/Updates.php index 3ff18ff8de..befe7f9095 100644 --- a/LibreNMS/Validations/Updates.php +++ b/LibreNMS/Validations/Updates.php @@ -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; } diff --git a/LibreNMS/Validations/User.php b/LibreNMS/Validations/User.php index 58a504c11c..f1222325ca 100644 --- a/LibreNMS/Validations/User.php +++ b/LibreNMS/Validations/User.php @@ -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; } diff --git a/app/Console/Commands/Ping.php b/app/Console/Commands/Ping.php deleted file mode 100644 index b6edd00808..0000000000 --- a/app/Console/Commands/Ping.php +++ /dev/null @@ -1,46 +0,0 @@ -alert("Do not use this command yet, use ./ping.php"); - exit(); - - PingCheck::dispatch(new PingCheck($this->argument('groups'))); - } -} diff --git a/app/Console/Commands/Release.php b/app/Console/Commands/Release.php deleted file mode 100644 index 1d53228563..0000000000 --- a/app/Console/Commands/Release.php +++ /dev/null @@ -1,72 +0,0 @@ -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()); - } - } -} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 3d0a5fc880..a0c96a33dc 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -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; } } diff --git a/app/Providers/CliServiceProvider.php b/app/Providers/CliServiceProvider.php new file mode 100644 index 0000000000..fbfb2a4616 --- /dev/null +++ b/app/Providers/CliServiceProvider.php @@ -0,0 +1,23 @@ +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(); + } + } +} diff --git a/config/app.php b/config/app.php index dff35c5dcd..b49e0d9871 100644 --- a/config/app.php +++ b/config/app.php @@ -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, diff --git a/includes/common.php b/includes/common.php index 3d6fcb98a9..120f58a40d 100644 --- a/includes/common.php +++ b/includes/common.php @@ -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; diff --git a/librenms b/librenms new file mode 100755 index 0000000000..ea2b9795c7 --- /dev/null +++ b/librenms @@ -0,0 +1,53 @@ +#!/usr/bin/env php +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); diff --git a/routes/console.php b/routes/console.php index 75dd0cdedb..06dd2aa7f3 100644 --- a/routes/console.php +++ b/routes/console.php @@ -1,6 +1,6 @@ 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'); diff --git a/routes/dev-console.php b/routes/dev-console.php new file mode 100644 index 0000000000..36d2e02d39 --- /dev/null +++ b/routes/dev-console.php @@ -0,0 +1,37 @@ +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');