Integrate nonstandard pooler service into vmfloaty

This commit is contained in:
Casey Williams 2017-09-19 12:08:09 -07:00
parent e78bcc6216
commit ca5b0f5e8b
12 changed files with 1190 additions and 482 deletions

View file

@ -5,11 +5,13 @@ require 'commander'
require 'colorize'
require 'json'
require 'pp'
require 'uri'
require 'vmfloaty/auth'
require 'vmfloaty/pooler'
require 'vmfloaty/version'
require 'vmfloaty/conf'
require 'vmfloaty/utils'
require 'vmfloaty/service'
require 'vmfloaty/ssh'
class Vmfloaty
@ -35,11 +37,8 @@ class Vmfloaty
c.option '--force', 'Forces vmfloaty to get requested vms'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
token = options.token || service_config['token'] || config['token']
user = options.user ||= service_config['user'] || config['user']
url = options.url ||= service_config['url'] || config['url']
no_token = options.notoken
service = Service.new(options, config)
use_token = !options.notoken
force = options.force
if args.empty?
@ -50,61 +49,20 @@ class Vmfloaty
os_types = Utils.generate_os_hash(args)
max_pool_request = 5
large_pool_requests = os_types.select{|k,v| v > max_pool_request}
large_pool_requests = os_types.select{|_,v| v > max_pool_request}
if ! large_pool_requests.empty? and ! force
STDERR.puts "Requesting vms over #{max_pool_request} requires a --force flag."
STDERR.puts "Try again with `floaty get --force`"
exit 1
end
unless os_types.empty?
if no_token
begin
response = Pooler.retrieve(verbose, os_types, nil, url)
rescue MissingParamError
STDERR.puts e
STDERR.puts "See `floaty get --help` for more information on how to get VMs."
rescue AuthError => e
STDERR.puts e
exit 1
end
puts Utils.format_hosts(response)
exit 0
else
unless token
puts "No token found. Retrieving a token..."
if !user
STDERR.puts "You did not provide a user to authenticate to the pooler service with"
exit 1
end
pass = password "Enter your pooler service password please:", '*'
begin
token = Auth.get_token(verbose, url, user, pass)
rescue TokenError => e
STDERR.puts e
exit 1
end
puts "\nToken retrieved!"
puts token
end
begin
response = Pooler.retrieve(verbose, os_types, token, url)
rescue MissingParamError
STDERR.puts e
STDERR.puts "See `floaty get --help` for more information on how to get VMs."
rescue AuthError => e
STDERR.puts e
exit 1
end
puts Utils.format_hosts(response)
exit 0
end
else
if os_types.empty?
STDERR.puts "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs."
exit 1
end
response = service.retrieve(verbose, os_types, use_token)
puts Utils.format_hosts(response)
end
end
@ -120,30 +78,22 @@ class Vmfloaty
c.option '--url STRING', String, 'URL of pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
service = Service.new(options, config)
filter = args[0]
url = options.url ||= service_config['url'] ||=config['url']
token = options.token || service_config['token'] || config['token']
active = options.active
if active
if options.active
# list active vms
begin
running_vms = Utils.get_all_token_vms(verbose, url, token)
rescue TokenError => e
STDERR.puts e
exit 1
rescue Exception => e
STDERR.puts e
exit 1
end
if ! running_vms.nil?
Utils.prettyprint_hosts(running_vms, verbose, url)
running_vms = service.list_active(verbose)
host = URI.parse(service.url).host
if running_vms.empty?
puts "You have no running VMs on #{host}"
else
puts "Your VMs on #{host}:"
Utils.pretty_print_hosts(verbose, service, running_vms)
end
else
# list available vms from pooler
os_list = Pooler.list(verbose, url, filter)
os_list = service.list(verbose, filter)
puts os_list
end
end
@ -159,136 +109,67 @@ class Vmfloaty
c.option '--url STRING', String, 'URL of pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
service = Service.new(options, config)
hostname = args[0]
query_req = Pooler.query(verbose, url, hostname)
query_req = service.query(verbose, hostname)
pp query_req
end
end
command :modify do |c|
c.syntax = 'floaty modify hostname [options]'
c.summary = 'Modify a vms tags, time to live, and disk space'
c.summary = 'Modify a VM\'s tags, time to live, disk space, or reservation reason'
c.description = 'This command makes modifications to the virtual machines state in the pooler service. You can either append tags to the vm, increase how long it stays active for, or increase the amount of disk space.'
c.example 'Modifies myhost1 to have a TTL of 12 hours and adds a custom tag', 'floaty modify myhost1 --lifetime 12 --url https://myurl --token mytokenstring --tags \'{"tag":"myvalue"}\''
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--lifetime INT', Integer, 'VM TTL (Integer, in hours)'
c.option '--disk INT', Integer, 'Increases VM disk space (Integer, in gb)'
c.option '--tags STRING', String, 'free-form VM tagging (json)'
c.option '--lifetime INT', Integer, 'VM TTL (Integer, in hours) [vmpooler only]'
c.option '--disk INT', Integer, 'Increases VM disk space (Integer, in gb) [vmpooler only]'
c.option '--tags STRING', String, 'free-form VM tagging (json) [vmpooler only]'
c.option '--reason STRING', String, 'VM reservation reason [nspooler only]'
c.option '--all', 'Modifies all vms acquired by a token'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
service = Service.new(options, config)
hostname = args[0]
lifetime = options.lifetime
disk = options.disk
tags = JSON.parse(options.tags) if options.tags
token = options.token || service_config['token'] || config['token']
modify_all = options.all
running_vms = nil
if modify_all
begin
running_vms = Utils.get_all_token_vms(verbose, url, token)
rescue Exception => e
STDERR.puts e
end
elsif hostname.include? ","
running_vms = hostname.split(",")
if hostname.nil? and !modify_all
STDERR.puts "ERROR: Provide a hostname or specify --all."
exit 1
end
running_vms = modify_all ? service.list_active(verbose) : hostname.split(",")
if lifetime || tags
# all vms
if !running_vms.nil?
tags = options.tags ? JSON.parse(options.tags) : nil
modify_hash = {
lifetime: options.lifetime,
disk: options.disk,
tags: tags,
reason: options.reason
}
modify_hash.delete_if { |_, value| value.nil? }
unless modify_hash.empty?
ok = true
modified_hash = {}
running_vms.each do |vm|
begin
modify_hash = {}
modify_flag = true
running_vms.each do |vm|
modify_hash[vm] = Pooler.modify(verbose, url, vm, token, lifetime, tags)
end
modify_hash.each do |hostname,status|
if status == false
STDERR.puts "Could not modify #{hostname}."
modify_flag = false
end
end
if modify_flag
puts "Successfully modified all vms. Use `floaty list --active` to see the results."
end
rescue Exception => e
modified_hash[vm] = service.modify(verbose, vm, modify_hash)
rescue ModifyError => e
STDERR.puts e
exit 1
end
else
# Single Vm
begin
modify_req = Pooler.modify(verbose, url, hostname, token, lifetime, tags)
rescue TokenError => e
STDERR.puts e
exit 1
end
if modify_req["ok"]
puts "Successfully modified vm #{hostname}."
else
STDERR.puts "Could not modify given host #{hostname} at #{url}."
puts modify_req
exit 1
ok = false
end
end
end
if disk
# all vms
if !running_vms.nil?
begin
modify_hash = {}
modify_flag = true
running_vms.each do |vm|
modify_hash[vm] = Pooler.disk(verbose, url, vm, token, disk)
end
modify_hash.each do |hostname,status|
if status == false
STDERR.puts "Could not update disk space on #{hostname}."
modify_flag = false
end
end
if modify_flag
puts "Successfully made request to update disk space on all vms."
end
rescue Exception => e
STDERR.puts e
exit 1
end
else
# single vm
begin
disk_req = Pooler.disk(verbose, url, hostname, token, disk)
rescue TokenError => e
STDERR.puts e
exit 1
end
if disk_req["ok"]
puts "Successfully made request to update disk space of vm #{hostname}."
if ok
if modify_all
puts "Successfully modified all VMs."
else
STDERR.puts "Could not modify given host #{hostname} at #{url}."
puts disk_req
exit 1
puts "Successfully modified VM #{hostname}."
end
puts "Use `floaty list --active` to see the results."
end
end
end
@ -307,67 +188,69 @@ class Vmfloaty
c.option '--url STRING', String, 'URL of pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
service = Service.new(options, config)
hostnames = args[0]
token = options.token || service_config['token'] || config['token']
url = options.url ||= service_config['url'] ||= config['url']
delete_all = options.all
force = options.f
failures = []
successes = []
if delete_all
# get vms with token
begin
running_vms = Utils.get_all_token_vms(verbose, url, token)
rescue TokenError => e
STDERR.puts e
exit 1
rescue Exception => e
STDERR.puts e
exit 1
end
if ! running_vms.nil?
Utils.prettyprint_hosts(running_vms, verbose, url)
# query y/n
running_vms = service.list_active(verbose)
if running_vms.empty?
STDERR.puts "You have no running VMs."
else
Utils.pretty_print_hosts(verbose, service, running_vms)
# Confirm deletion
puts
if force
ans = true
else
ans = agree("Delete all VMs associated with token #{token}? [y/N]")
confirmed = true
unless force
confirmed = agree('Delete all these VMs? [y/N]')
end
if ans
# delete vms
puts "Scheduling all vms for for deletion"
response = Pooler.delete(verbose, url, running_vms, token)
response.each do |host,vals|
if vals['ok'] == false
STDERR.puts "There was a problem with your request for vm #{host}."
STDERR.puts vals
if confirmed
response = service.delete(verbose, running_vms)
response.each do |hostname, result|
if result['ok']
successes << hostname
else
failures << hostname
end
end
end
end
exit 0
end
if hostnames.nil?
elsif hostnames || args
hostnames = hostnames.split(',')
results = service.delete(verbose, hostnames)
results.each do |hostname, result|
if result['ok']
successes << hostname
else
failures << hostname
end
end
else
STDERR.puts "You did not provide any hosts to delete"
exit 1
else
hosts = hostnames.split(',')
begin
Pooler.delete(verbose, url, hosts, token)
rescue TokenError => e
STDERR.puts e
exit 1
end
puts "Scheduled pooler service to delete vms #{hosts}."
exit 0
end
unless failures.empty?
STDERR.puts 'Unable to delete the following VMs:'
failures.each do |hostname|
STDERR.puts "- #{hostname}"
end
STDERR.puts 'Check `floaty list --active`; Do you need to specify a different service?'
end
unless successes.empty?
puts unless failures.empty?
puts 'Scheduled the following VMs for deletion:'
successes.each do |hostname|
puts "- #{hostname}"
end
end
exit 1 unless failures.empty?
end
end
@ -382,14 +265,12 @@ class Vmfloaty
c.option '--token STRING', String, 'Token for pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
service = Service.new(options, config)
hostname = args[0]
token = options.token ||= service_config['token'] ||= config['token']
begin
snapshot_req = Pooler.snapshot(verbose, url, hostname, token)
rescue TokenError => e
snapshot_req = service.snapshot(verbose, hostname)
rescue TokenError, ModifyError => e
STDERR.puts e
exit 1
end
@ -411,10 +292,8 @@ class Vmfloaty
c.option '--snapshot STRING', String, 'SHA of snapshot'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
service = Service.new(options, config)
hostname = args[0]
token = options.token || service_config['token'] || config['token']
snapshot_sha = args[1] || options.snapshot
if args[1] && options.snapshot
@ -422,8 +301,8 @@ class Vmfloaty
end
begin
revert_req = Pooler.revert(verbose, url, hostname, token, snapshot_sha)
rescue TokenError => e
revert_req = service.revert(verbose, hostname, snapshot_sha)
rescue TokenError, ModifyError => e
STDERR.puts e
exit 1
end
@ -441,22 +320,14 @@ class Vmfloaty
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--json', 'Prints status in JSON format'
c.action do |args, options|
c.action do |_, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
status = Pooler.status(verbose, url)
message = status['status']['message']
pools = status['pools']
service = Service.new(options, config)
if options.json
pp status
pp service.status(verbose)
else
Utils.prettyprint_status(status, message, pools, verbose)
Utils.pretty_print_status(verbose, service)
end
exit status['status']['ok']
end
end
@ -468,12 +339,11 @@ class Vmfloaty
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.action do |args, options|
c.action do |_, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
service = Service.new(options, config)
summary = Pooler.summary(verbose, url)
summary = service.summary(verbose)
pp summary
exit 0
end
@ -491,47 +361,36 @@ class Vmfloaty
c.option '--token STRING', String, 'Token for pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
service = Service.new(options, config)
action = args.first
url = options.url ||= service_config['url'] ||= config['url']
token = args[1] ||= options.token ||= service_config['token'] ||= config['token']
user = options.user ||= service_config['user'] ||= config['user']
case action
when "get"
pass = password "Enter your pooler service password please:", '*'
begin
token = Auth.get_token(verbose, url, user, pass)
rescue TokenError => e
STDERR.puts e
exit 1
begin
case action
when 'get'
token = service.get_new_token(verbose)
puts token
when 'delete'
result = service.delete_token(verbose, options.token)
puts result
when 'status'
token_value = options.token
if token_value.nil?
token_value = args[1]
end
status = service.token_status(verbose, token_value)
puts status
when nil
STDERR.puts 'No action provided'
exit 1
else
STDERR.puts "Unknown action: #{action}"
exit 1
end
puts token
exit 0
when "delete"
pass = password "Enter your pooler service password please:", '*'
begin
result = Auth.delete_token(verbose, url, user, pass, token)
rescue TokenError => e
STDERR.puts e
exit 1
end
puts result
exit 0
when "status"
begin
status = Auth.token_status(verbose, url, token)
rescue TokenError => e
STDERR.puts e
exit 1
end
puts status
exit 0
when nil
STDERR.puts "No action provided"
else
STDERR.puts "Unknown action: #{action}"
rescue TokenError => e
STDERR.puts e
exit 1
end
exit 0
end
end
@ -548,11 +407,8 @@ class Vmfloaty
c.option '--notoken', 'Makes a request without a token'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service_config = Utils.get_service_from_config(config, options.service)
url = options.url ||= service_config['url'] ||= config['url']
token = options.token ||= service_config['token'] ||= config['token']
user = options.user ||= service_config['user'] ||= config['user']
no_token = options.notoken
service = Service.new(options, config)
use_token = !options.notoken
if args.empty?
STDERR.puts "No operating systems provided to obtain. See `floaty ssh --help` for more information on how to get VMs."
@ -561,25 +417,11 @@ class Vmfloaty
host_os = args.first
if !no_token && !token
puts "No token found. Retrieving a token..."
if !user
STDERR.puts "You did not provide a user to authenticate to the pooler service with"
exit 1
end
pass = password "Enter your pooler service password please:", '*'
begin
token = Auth.get_token(verbose, url, user, pass)
rescue TokenError => e
STDERR.puts e
STDERR.puts 'Could not get token...requesting vm without a token anyway...'
else
puts "\nToken retrieved!"
puts token
end
if args.length > 1
STDERR.puts "Can't ssh to multiple hosts; Using #{host_os} only..."
end
Ssh.ssh(verbose, host_os, token, url)
service.ssh(verbose, host_os, use_token)
exit 0
end
end
@ -596,7 +438,7 @@ class Vmfloaty
EOF
c.example 'Gets path to bash tab completion script', 'floaty completion --shell bash'
c.option '--shell STRING', String, 'Shell to request completion script for'
c.action do |args, options|
c.action do |_, options|
shell = (options.shell || 'bash').downcase.strip
completion_file = File.expand_path(File.join('..', '..', 'extras', 'completions', "floaty.#{shell}"), __FILE__)