diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 835b2cf..283830c 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,36 @@ 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) + puts "\nToken retrieved!" + puts token + 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 +123,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 +159,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]") @@ -182,10 +175,11 @@ class Vmfloaty 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 @@ -280,12 +274,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/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/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| 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..043ae4d --- /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:" + hosts.each do |vm| + puts "- #{vm}" + end + end +end 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 diff --git a/spec/vmfloaty/format_spec.rb b/spec/vmfloaty/format_spec.rb deleted file mode 100644 index 9442225..0000000 --- a/spec/vmfloaty/format_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'spec_helper' -require 'json' -require_relative '../../lib/vmfloaty/format' - -describe Pooler do - - describe "#get_hosts" do - before :each do - @hostname_hash = "{\"ok\":true,\"debian-7-i386\":{\"hostname\":[\"sc0o4xqtodlul5w\",\"4m4dkhqiufnjmxy\"]},\"debian-7-x86_64\":{\"hostname\":\"zb91y9qbrbf6d3q\"},\"domain\":\"company.com\"}" - @format_hash = "{\"debian-7-i386\":[\"sc0o4xqtodlul5w\",\"4m4dkhqiufnjmxy\"],\"debian-7-x86_64\":\"zb91y9qbrbf6d3q\",\"domain\":\"company.com\"}" - end - - it "formats a hostname hash into os, hostnames, and domain name" do - - expect(Format.get_hosts(JSON.parse(@hostname_hash))).to eq @format_hash - end - end -end diff --git a/spec/vmfloaty/utils_spec.rb b/spec/vmfloaty/utils_spec.rb new file mode 100644 index 0000000..8ed6a90 --- /dev/null +++ b/spec/vmfloaty/utils_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' +require 'json' +require_relative '../../lib/vmfloaty/utils' + +describe Utils do + + describe "#get_hosts" do + before :each do + @hostname_hash = "{\"ok\":true,\"debian-7-i386\":{\"hostname\":[\"sc0o4xqtodlul5w\",\"4m4dkhqiufnjmxy\"]},\"debian-7-x86_64\":{\"hostname\":\"zb91y9qbrbf6d3q\"},\"domain\":\"company.com\"}" + @format_hash = "{\"debian-7-i386\":[\"sc0o4xqtodlul5w\",\"4m4dkhqiufnjmxy\"],\"debian-7-x86_64\":\"zb91y9qbrbf6d3q\",\"domain\":\"company.com\"}" + end + + it "formats a hostname hash into os, hostnames, and domain name" 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