Merge pull request #15 from briancain/clean-library-methods

Cleanup vmfloaty library and command processor
This commit is contained in:
Brian Cain 2015-11-16 09:17:17 -08:00
commit c8076625db
9 changed files with 132 additions and 93 deletions

View file

@ -7,7 +7,7 @@ require 'vmfloaty/auth'
require 'vmfloaty/pooler' require 'vmfloaty/pooler'
require 'vmfloaty/version' require 'vmfloaty/version'
require 'vmfloaty/conf' require 'vmfloaty/conf'
require 'vmfloaty/format' require 'vmfloaty/utils'
class Vmfloaty class Vmfloaty
include Commander::Methods include Commander::Methods
@ -22,7 +22,7 @@ class Vmfloaty
c.syntax = 'floaty get os_type1=x ox_type2=y ...' c.syntax = 'floaty get os_type1=x ox_type2=y ...'
c.summary = 'Gets a vm or vms based on the os flag' c.summary = 'Gets a vm or vms based on the os flag'
c.description = '' 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 '--verbose', 'Enables verbose output'
c.option '--user STRING', String, 'User to authenticate with' c.option '--user STRING', String, 'User to authenticate with'
c.option '--url STRING', String, 'URL of vmpooler' c.option '--url STRING', String, 'URL of vmpooler'
@ -33,40 +33,36 @@ class Vmfloaty
token = options.token || config['token'] token = options.token || config['token']
user = options.user ||= config['user'] user = options.user ||= config['user']
url = options.url ||= config['url'] 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 no_token = options.notoken
if no_token if args.empty?
response = Pooler.retrieve(verbose, os_types, nil, url) STDERR.puts "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs."
puts response exit 1
return
end end
unless token os_types = Utils.generate_os_hash(args)
pass = password "Enter your password please:", '*'
token = Auth.get_token(verbose, url, user, pass)
end
unless os_types.nil? unless os_types.empty?
response = Pooler.retrieve(verbose, os_types, token, url) if no_token
puts Format.get_hosts(response) 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 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 end
end end
@ -127,7 +123,7 @@ class Vmfloaty
if modify_req["ok"] if modify_req["ok"]
puts "Successfully modified vm #{hostname}." puts "Successfully modified vm #{hostname}."
else else
STDERR.puts "Something went wrong with your request" STDERR.puts "Could not modify given host #{hostname} at #{url}."
puts modify_req puts modify_req
exit 1 exit 1
end end
@ -163,10 +159,7 @@ class Vmfloaty
running_vms = vms['running'] running_vms = vms['running']
if ! running_vms.nil? if ! running_vms.nil?
puts "Running VMs:" Utils.prettyprint_hosts(running_vms)
running_vms.each do |vm|
puts "- #{vm}"
end
# query y/n # query y/n
puts "" puts ""
ans = agree("Delete all VMs associated with token #{token}? [y/N]") ans = agree("Delete all VMs associated with token #{token}? [y/N]")
@ -182,10 +175,11 @@ class Vmfloaty
if hostnames.nil? if hostnames.nil?
STDERR.puts "You did not provide any hosts to delete" STDERR.puts "You did not provide any hosts to delete"
exit 1 exit 1
else
hosts = hostnames.split(',')
Pooler.delete(verbose, url, hosts, token)
exit 0
end end
hosts = hostnames.split(',')
Pooler.delete(verbose, url, hosts, token)
end end
end end
@ -280,12 +274,15 @@ class Vmfloaty
case action case action
when "get" when "get"
pass = password "Enter your password please:", '*' 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" when "delete"
pass = password "Enter your password please:", '*' 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" when "status"
puts Auth.token_status(verbose, url, token) status = Auth.token_status(verbose, url, token)
puts status
when nil when nil
STDERR.puts "No action provided" STDERR.puts "No action provided"
else else

View file

@ -29,7 +29,7 @@ class Auth
response = conn.delete "/token/#{token}" response = conn.delete "/token/#{token}"
res_body = JSON.parse(response.body) res_body = JSON.parse(response.body)
if res_body["ok"] if res_body["ok"]
puts res_body return res_body
else else
STDERR.puts "There was a problem with your request:" STDERR.puts "There was a problem with your request:"
puts res_body puts res_body
@ -49,7 +49,7 @@ class Auth
res_body = JSON.parse(response.body) res_body = JSON.parse(response.body)
if res_body["ok"] if res_body["ok"]
res_body return res_body
else else
STDERR.puts "There was a problem with your request:" STDERR.puts "There was a problem with your request:"
puts res_body puts res_body

View file

@ -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

View file

@ -3,8 +3,7 @@ require 'faraday'
class Http class Http
def self.get_conn(verbose, url) def self.get_conn(verbose, url)
if url.nil? if url.nil?
STDERR.puts "The url you provided was empty" raise "Did not provide a url to connect to"
exit 1
end end
conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday| 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) def self.get_conn_with_auth(verbose, url, user, password)
if url.nil? if url.nil?
STDERR.puts "The url you provided was empty" raise "Did not provide a url to connect to"
exit 1
end end
if user.nil? if user.nil?
STDERR.puts "You did not provide a user to authenticate with" raise "You did not provide a user to authenticate with"
exit 1
end end
conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday| conn = Faraday.new(:url => url, :ssl => {:verify => false}) do |faraday|

View file

@ -34,8 +34,7 @@ class Pooler
os_string = os_string.chomp("+") os_string = os_string.chomp("+")
if os_string.size == 0 if os_string.size == 0
STDERR.puts "No request was made, os hash specified no vms #{os_type}" raise "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs."
exit 1
end end
response = conn.post "/vm/#{os_string}" response = conn.post "/vm/#{os_string}"
@ -44,9 +43,7 @@ class Pooler
if res_body["ok"] if res_body["ok"]
res_body res_body
else else
STDERR.puts "There was a problem with your request" raise "Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}"
STDERR.puts res_body
exit 1
end end
end end
@ -85,7 +82,8 @@ class Pooler
if res_body['ok'] if res_body['ok']
puts "Deletion for vm #{host} successfully scheduled" puts "Deletion for vm #{host} successfully scheduled"
else 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 end
end end

50
lib/vmfloaty/utils.rb Normal file
View file

@ -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

View file

@ -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'}). 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 => {}) 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
end end

View file

@ -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

View file

@ -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