From 3394a14ea07e80551d7a70b96178336f8af2af61 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sun, 15 Nov 2015 13:27:07 -0800 Subject: [PATCH 1/7] (#14) Remove printing in auth library --- lib/vmfloaty.rb | 9 ++++++--- lib/vmfloaty/auth.rb | 4 ++-- spec/vmfloaty/auth_spec.rb | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 835b2cf..f0bb761 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -280,12 +280,15 @@ class Vmfloaty case action when "get" pass = password "Enter your password please:", '*' - puts Auth.get_token(verbose, url, user, pass) + token = Auth.get_token(verbose, url, user, pass) + puts token when "delete" pass = password "Enter your password please:", '*' - Auth.delete_token(verbose, url, user, pass, token) + result = Auth.delete_token(verbose, url, user, pass, token) + puts result when "status" - puts Auth.token_status(verbose, url, token) + status = Auth.token_status(verbose, url, token) + puts status when nil STDERR.puts "No action provided" else diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index 7c823e5..a973ae1 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -29,7 +29,7 @@ class Auth response = conn.delete "/token/#{token}" res_body = JSON.parse(response.body) if res_body["ok"] - puts res_body + return res_body else STDERR.puts "There was a problem with your request:" puts res_body @@ -49,7 +49,7 @@ class Auth res_body = JSON.parse(response.body) if res_body["ok"] - res_body + return res_body else STDERR.puts "There was a problem with your request:" puts res_body diff --git a/spec/vmfloaty/auth_spec.rb b/spec/vmfloaty/auth_spec.rb index b30a3ad..05af433 100644 --- a/spec/vmfloaty/auth_spec.rb +++ b/spec/vmfloaty/auth_spec.rb @@ -33,7 +33,7 @@ describe Pooler do with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.9.1'}). to_return(:status => 200, :body => @delete_token_response, :headers => {}) - #expect(Auth.delete_token(false, @vmpooler_url, "first.last", "password", @token)).to eq @delete_token_response + expect(Auth.delete_token(false, @vmpooler_url, "first.last", "password", @token)).to eq JSON.parse(@delete_token_response) end end From c1689da3c4381b05c70b7330eb61d67e227a4408 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sun, 15 Nov 2015 13:33:21 -0800 Subject: [PATCH 2/7] (#13) Raise exception when required params are missing Instead of doing a system exit and printing to stderr, raise an exception if parameters are missing when attempting to make an http connection. --- lib/vmfloaty/http.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/vmfloaty/http.rb b/lib/vmfloaty/http.rb index bd1cc30..07a8a2d 100644 --- a/lib/vmfloaty/http.rb +++ b/lib/vmfloaty/http.rb @@ -3,8 +3,7 @@ require 'faraday' class Http def self.get_conn(verbose, url) if url.nil? - STDERR.puts "The url you provided was empty" - exit 1 + raise "Did not provide a url to connect to" end conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday| @@ -18,13 +17,11 @@ class Http def self.get_conn_with_auth(verbose, url, user, password) if url.nil? - STDERR.puts "The url you provided was empty" - exit 1 + raise "Did not provide a url to connect to" end if user.nil? - STDERR.puts "You did not provide a user to authenticate with" - exit 1 + raise "You did not provide a user to authenticate with" end conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday| From c6cf86669a8272604902012ce38512a5bb6d915d Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sun, 15 Nov 2015 14:24:24 -0800 Subject: [PATCH 3/7] Cleanup vmfloaty library and command line functions --- lib/vmfloaty.rb | 69 ++++++++----------- lib/vmfloaty/format.rb | 19 ----- lib/vmfloaty/pooler.rb | 10 ++- lib/vmfloaty/utils.rb | 50 ++++++++++++++ .../{format_spec.rb => utils_spec.rb} | 6 +- 5 files changed, 87 insertions(+), 67 deletions(-) delete mode 100644 lib/vmfloaty/format.rb create mode 100644 lib/vmfloaty/utils.rb rename spec/vmfloaty/{format_spec.rb => utils_spec.rb} (78%) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index f0bb761..796be1d 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -7,7 +7,7 @@ require 'vmfloaty/auth' require 'vmfloaty/pooler' require 'vmfloaty/version' require 'vmfloaty/conf' -require 'vmfloaty/format' +require 'vmfloaty/utils' class Vmfloaty include Commander::Methods @@ -22,7 +22,7 @@ class Vmfloaty c.syntax = 'floaty get os_type1=x ox_type2=y ...' c.summary = 'Gets a vm or vms based on the os flag' c.description = '' - c.example 'Gets 3 vms', 'floaty get centos=3 debian=1 --user brian --url http://vmpooler.example.com' + c.example 'Gets 3 vms', 'floaty get centos=3 debian --user brian --url http://vmpooler.example.com' c.option '--verbose', 'Enables verbose output' c.option '--user STRING', String, 'User to authenticate with' c.option '--url STRING', String, 'URL of vmpooler' @@ -33,40 +33,34 @@ class Vmfloaty token = options.token || config['token'] user = options.user ||= config['user'] url = options.url ||= config['url'] - - if args.empty? - STDERR.puts "You did not provide any vms to grab" - end - - os_types = {} - args.each do |arg| - os_arr = arg.split("=") - if os_arr.size == 1 - # assume they didn't specify an = sign if split returns 1 size - os_types[os_arr[0]] = 1 - else - os_types[os_arr[0]] = os_arr[1].to_i - end - end - no_token = options.notoken - if no_token - response = Pooler.retrieve(verbose, os_types, nil, url) - puts response - return + if args.empty? + STDERR.puts "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs." + exit 1 end - unless token - pass = password "Enter your password please:", '*' - token = Auth.get_token(verbose, url, user, pass) - end + os_types = Utils.generate_os_hash(args) - unless os_types.nil? - response = Pooler.retrieve(verbose, os_types, token, url) - puts Format.get_hosts(response) + unless os_types.empty? + if no_token + response = Pooler.retrieve(verbose, os_types, nil, url) + puts response + exit 0 + else + unless token + puts "No token found. Retrieving a token..." + pass = password "Enter your password please:", '*' + token = Auth.get_token(verbose, url, user, pass) + end + + response = Pooler.retrieve(verbose, os_types, token, url) + puts Utils.format_hosts(response) + exit 0 + end else - puts 'You did not provide an OS to get' + STDERR.puts "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs." + exit 1 end end end @@ -127,7 +121,7 @@ class Vmfloaty if modify_req["ok"] puts "Successfully modified vm #{hostname}." else - STDERR.puts "Something went wrong with your request" + STDERR.puts "Could not modify given host #{hostname} at #{url}." puts modify_req exit 1 end @@ -163,10 +157,7 @@ class Vmfloaty running_vms = vms['running'] if ! running_vms.nil? - puts "Running VMs:" - running_vms.each do |vm| - puts "- #{vm}" - end + Utils.prettyprint_hosts(running_vms) # query y/n puts "" ans = agree("Delete all VMs associated with token #{token}? [y/N]") @@ -177,15 +168,15 @@ class Vmfloaty end exit 0 - end if hostnames.nil? STDERR.puts "You did not provide any hosts to delete" exit 1 + else + hosts = hostnames.split(',') + Pooler.delete(verbose, url, hosts, token) + exit 0 end - - hosts = hostnames.split(',') - Pooler.delete(verbose, url, hosts, token) end end diff --git a/lib/vmfloaty/format.rb b/lib/vmfloaty/format.rb deleted file mode 100644 index d89e700..0000000 --- a/lib/vmfloaty/format.rb +++ /dev/null @@ -1,19 +0,0 @@ - -class Format - # TODO: Takes the json response body from an HTTP GET - # request and "pretty prints" it - def self.get_hosts(hostname_hash) - host_hash = {} - - hostname_hash.delete("ok") - hostname_hash.each do |type, hosts| - if type == "domain" - host_hash[type] = hosts - else - host_hash[type] = hosts["hostname"] - end - end - - host_hash.to_json - end -end diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index 04d670c..6106a85 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -34,8 +34,7 @@ class Pooler os_string = os_string.chomp("+") if os_string.size == 0 - STDERR.puts "No request was made, os hash specified no vms #{os_type}" - exit 1 + raise "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs." end response = conn.post "/vm/#{os_string}" @@ -44,9 +43,7 @@ class Pooler if res_body["ok"] res_body else - STDERR.puts "There was a problem with your request" - STDERR.puts res_body - exit 1 + raise "Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}" end end @@ -85,7 +82,8 @@ class Pooler if res_body['ok'] puts "Deletion for vm #{host} successfully scheduled" else - STDERR.puts "There was a problem with your request for vm #{host}" + STDERR.puts "There was a problem with your request for vm #{host}." + STDERR.puts res_body end end end diff --git a/lib/vmfloaty/utils.rb b/lib/vmfloaty/utils.rb new file mode 100644 index 0000000..24f822f --- /dev/null +++ b/lib/vmfloaty/utils.rb @@ -0,0 +1,50 @@ + +require 'vmfloaty/pooler' + +class Utils + # TODO: Takes the json response body from an HTTP GET + # request and "pretty prints" it + def self.format_hosts(hostname_hash) + host_hash = {} + + hostname_hash.delete("ok") + hostname_hash.each do |type, hosts| + if type == "domain" + host_hash[type] = hosts + else + host_hash[type] = hosts["hostname"] + end + end + + host_hash.to_json + end + + def self.generate_os_hash(os_args) + # expects args to look like: + # ["centos", "debian=5", "windows=1"] + + # Build vm hash where + # + # [operating_system_type1 -> total, + # operating_system_type2 -> total, + # ...] + os_types = {} + os_args.each do |arg| + os_arr = arg.split("=") + if os_arr.size == 1 + # assume they didn't specify an = sign if split returns 1 size + os_types[os_arr[0]] = 1 + else + os_types[os_arr[0]] = os_arr[1].to_i + end + end + os_types + end + + def self.prettyprint_hosts(hosts) + puts "Running VMs:" + running_vms.each do |vm| + puts "- #{vm}" + end + end +end diff --git a/spec/vmfloaty/format_spec.rb b/spec/vmfloaty/utils_spec.rb similarity index 78% rename from spec/vmfloaty/format_spec.rb rename to spec/vmfloaty/utils_spec.rb index 9442225..8a31307 100644 --- a/spec/vmfloaty/format_spec.rb +++ b/spec/vmfloaty/utils_spec.rb @@ -1,8 +1,8 @@ require 'spec_helper' require 'json' -require_relative '../../lib/vmfloaty/format' +require_relative '../../lib/vmfloaty/utils' -describe Pooler do +describe Utils do describe "#get_hosts" do before :each do @@ -12,7 +12,7 @@ describe Pooler do it "formats a hostname hash into os, hostnames, and domain name" do - expect(Format.get_hosts(JSON.parse(@hostname_hash))).to eq @format_hash + expect(Utils.format_hosts(JSON.parse(@hostname_hash))).to eq @format_hash end end end From f15df69eeff1d55572600277918e0f1d78e105cd Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sun, 15 Nov 2015 14:35:16 -0800 Subject: [PATCH 4/7] Fix missing end --- lib/vmfloaty.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 796be1d..e69898d 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -168,6 +168,7 @@ class Vmfloaty end exit 0 + end if hostnames.nil? STDERR.puts "You did not provide any hosts to delete" From a0b0016bcd470f798741fc071d9af1fd761bd188 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sun, 15 Nov 2015 14:35:25 -0800 Subject: [PATCH 5/7] Add spec test for formatting hashes --- spec/vmfloaty/utils_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/vmfloaty/utils_spec.rb b/spec/vmfloaty/utils_spec.rb index 8a31307..8ed6a90 100644 --- a/spec/vmfloaty/utils_spec.rb +++ b/spec/vmfloaty/utils_spec.rb @@ -15,4 +15,20 @@ describe Utils do expect(Utils.format_hosts(JSON.parse(@hostname_hash))).to eq @format_hash end end + + describe "#generate_os_hash" do + before :each do + @host_hash = {"centos"=>1, "debian"=>5, "windows"=>1} + end + + it "takes an array of os arguments and returns a formatted hash" do + host_arg = ["centos", "debian=5", "windows=1"] + expect(Utils.generate_os_hash(host_arg)).to eq @host_hash + end + + it "returns an empty hash if there are no arguments provided" do + host_arg = [] + expect(Utils.generate_os_hash(host_arg)).to be_empty + end + end end From e8094fb91d539d630276c2d0f26ceb4dcfd9c022 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 16 Nov 2015 08:59:25 -0800 Subject: [PATCH 6/7] Use correct variable for formatting hosts --- lib/vmfloaty/utils.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vmfloaty/utils.rb b/lib/vmfloaty/utils.rb index 24f822f..043ae4d 100644 --- a/lib/vmfloaty/utils.rb +++ b/lib/vmfloaty/utils.rb @@ -43,7 +43,7 @@ class Utils def self.prettyprint_hosts(hosts) puts "Running VMs:" - running_vms.each do |vm| + hosts.each do |vm| puts "- #{vm}" end end From 3b92f12ee96196a4e1f5aaaf31578bb54e5d6acb Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 16 Nov 2015 09:04:52 -0800 Subject: [PATCH 7/7] Print obtained token during get command if no token exists --- lib/vmfloaty.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index e69898d..283830c 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -52,6 +52,8 @@ class Vmfloaty puts "No token found. Retrieving a token..." pass = password "Enter your password please:", '*' token = Auth.get_token(verbose, url, user, pass) + puts "\nToken retrieved!" + puts token end response = Pooler.retrieve(verbose, os_types, token, url)