Abstract out getting all active token vms into Utils

This commit is contained in:
Brian Cain 2016-11-12 13:59:26 -08:00
parent 2c9ce5644e
commit 586f4f5c05
4 changed files with 42 additions and 33 deletions

View file

@ -113,21 +113,15 @@ class Vmfloaty
if active
# list active vms
begin
status = Auth.token_status(verbose, url, token)
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
# print vms
vms = status[token]['vms']
if vms.nil?
STDERR.puts "You have no running vms"
exit 0
end
running_vms = vms['running']
if ! running_vms.nil?
Utils.prettyprint_hosts(running_vms, verbose, url)
end
@ -167,6 +161,7 @@ class Vmfloaty
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 '--all', 'Modifies all vms acquired by a token'
c.action do |args, options|
verbose = options.verbose || config['verbose']
url = options.url ||= config['url']
@ -175,21 +170,28 @@ class Vmfloaty
disk = options.disk
tags = JSON.parse(options.tags) if options.tags
token = options.token || config['token']
modify_all = options.all
if lifetime || tags
begin
modify_req = Pooler.modify(verbose, url, hostname, token, lifetime, tags)
rescue TokenError => e
STDERR.puts e
exit 1
end
# All Vs
if modify_all
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
# 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
end
end
end
@ -234,21 +236,15 @@ class Vmfloaty
if delete_all
# get vms with token
begin
status = Auth.token_status(verbose, url, token)
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
# print vms
vms = status[token]['vms']
if vms.nil?
STDERR.puts "You have no running vms"
exit 0
end
running_vms = vms['running']
if ! running_vms.nil?
Utils.prettyprint_hosts(running_vms, verbose, url)
# query y/n

View file

@ -79,4 +79,17 @@ class Utils
puts "- #{vm}.#{domain} (#{metadata.join(", ")})"
end
end
def self.get_all_token_vms(verbose, url, token)
# get vms with token
status = Auth.token_status(verbose, url, token)
vms = status[token]['vms']
if vms.nil?
raise "You have no running vms"
end
running_vms = vms['running']
running_vms
end
end

View file

@ -1,6 +1,6 @@
class Version
@version = '0.7.1'
@version = '0.7.2'
def self.get
@version

View file

@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'vmfloaty'
s.version = '0.7.1'
s.version = '0.7.2'
s.authors = ['Brian Cain']
s.email = ['brian.cain@puppetlabs.com']
s.license = 'Apache'