mirror of
https://github.com/puppetlabs/vmfloaty.git
synced 2026-01-26 13:28:42 -05:00
Cleanup vmfloaty library and command line functions
This commit is contained in:
parent
c1689da3c4
commit
c6cf86669a
5 changed files with 87 additions and 67 deletions
|
|
@ -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 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
|
||||
|
||||
os_types = Utils.generate_os_hash(args)
|
||||
|
||||
unless os_types.empty?
|
||||
if no_token
|
||||
response = Pooler.retrieve(verbose, os_types, nil, url)
|
||||
puts response
|
||||
return
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
unless os_types.nil?
|
||||
response = Pooler.retrieve(verbose, os_types, token, url)
|
||||
puts Format.get_hosts(response)
|
||||
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
|
||||
end
|
||||
|
||||
else
|
||||
hosts = hostnames.split(',')
|
||||
Pooler.delete(verbose, url, hosts, token)
|
||||
exit 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
50
lib/vmfloaty/utils.rb
Normal file
50
lib/vmfloaty/utils.rb
Normal 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:"
|
||||
running_vms.each do |vm|
|
||||
puts "- #{vm}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue