From 7ee73083fd59318ee846895610550849e25402df Mon Sep 17 00:00:00 2001 From: Nick Lewis Date: Tue, 29 Nov 2016 16:36:13 -0800 Subject: [PATCH] Show the status of pools with `floaty status` Previously, `floaty status` would simply pretty-print the JSON output of the status request to the pooler. That data requires additional parsing, either human or machine, in order to be able to deduce any meaningful information about the state of the pooler. This commit updates `floaty status` to visually represent the state of pools, indicating which pools have nodes missing or being built. It also prints the status summary message returned by the pooler, providing an easy way to tell if everything is okay. By default, this command will only show the state of pools that aren't full. If `--verbose` is passed, it will show all pools. If the complete status information provided by the API is desired, the `--json` option will print the raw JSON output, preserving the previous functionality. This also updates the status command to exit non-zero if the status is not ok. --- Gemfile | 1 + lib/vmfloaty.rb | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 5c5b526..13c60e1 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,7 @@ source 'https://rubygems.org' gem 'commander' gem 'faraday', '0.9.2' +gem 'colorize', '~> 0.8' gem 'rspec' gem 'webmock', '1.21.0' diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 6ccd865..4590d8a 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -2,6 +2,8 @@ require 'rubygems' require 'commander' +require 'colorize' +require 'json' require 'pp' require 'vmfloaty/auth' require 'vmfloaty/pooler' @@ -413,12 +415,39 @@ class Vmfloaty c.example 'Gets the current vmpooler status', 'floaty status --url http://vmpooler.example.com' c.option '--verbose', 'Enables verbose output' c.option '--url STRING', String, 'URL of vmpooler' + c.option '--json', 'Prints status in JSON format' c.action do |args, options| verbose = options.verbose || config['verbose'] url = options.url ||= config['url'] status = Pooler.status(verbose, url) - pp status + message = status['status']['message'] + pools = status['pools'] + + if options.json + pp status + else + pools.select! {|name,pool| pool['ready'] < pool['max']} if ! verbose + + width = pools.keys.map(&:length).max + pools.each do |name,pool| + begin + max = pool['max'] + ready = pool['ready'] + pending = pool['pending'] + missing = max - ready - pending + char = 'o' + puts "#{name.ljust(width)} #{(char*ready).green}#{(char*pending).yellow}#{(char*missing).red}" + rescue => e + puts "#{name.ljust(width)} #{e.red}" + end + end + + puts + puts message.colorize(status['status']['ok'] ? :default : :red) + end + + exit status['status']['ok'] end end