mirror of
https://github.com/puppetlabs/vmfloaty.git
synced 2026-01-26 05:28:40 -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/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,34 @@ 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)
|
||||||
|
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 +121,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 +157,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]")
|
||||||
|
|
@ -177,15 +168,15 @@ class Vmfloaty
|
||||||
end
|
end
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
end
|
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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("+")
|
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
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 'spec_helper'
|
||||||
require 'json'
|
require 'json'
|
||||||
require_relative '../../lib/vmfloaty/format'
|
require_relative '../../lib/vmfloaty/utils'
|
||||||
|
|
||||||
describe Pooler do
|
describe Utils do
|
||||||
|
|
||||||
describe "#get_hosts" do
|
describe "#get_hosts" do
|
||||||
before :each do
|
before :each do
|
||||||
|
|
@ -12,7 +12,7 @@ describe Pooler do
|
||||||
|
|
||||||
it "formats a hostname hash into os, hostnames, and domain name" 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
|
end
|
||||||
end
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue