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 if active
# list active vms # list active vms
begin begin
status = Auth.token_status(verbose, url, token) running_vms = Utils.get_all_token_vms(verbose, url, token)
rescue TokenError => e rescue TokenError => e
STDERR.puts e STDERR.puts e
exit 1 exit 1
rescue Exception => e
STDERR.puts e
exit 1
end 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? if ! running_vms.nil?
Utils.prettyprint_hosts(running_vms, verbose, url) Utils.prettyprint_hosts(running_vms, verbose, url)
end end
@ -167,6 +161,7 @@ class Vmfloaty
c.option '--lifetime INT', Integer, 'VM TTL (Integer, in hours)' c.option '--lifetime INT', Integer, 'VM TTL (Integer, in hours)'
c.option '--disk INT', Integer, 'Increases VM disk space (Integer, in gb)' c.option '--disk INT', Integer, 'Increases VM disk space (Integer, in gb)'
c.option '--tags STRING', String, 'free-form VM tagging (json)' 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| c.action do |args, options|
verbose = options.verbose || config['verbose'] verbose = options.verbose || config['verbose']
url = options.url ||= config['url'] url = options.url ||= config['url']
@ -175,8 +170,14 @@ class Vmfloaty
disk = options.disk disk = options.disk
tags = JSON.parse(options.tags) if options.tags tags = JSON.parse(options.tags) if options.tags
token = options.token || config['token'] token = options.token || config['token']
modify_all = options.all
if lifetime || tags if lifetime || tags
# All Vs
if modify_all
else
# Single Vm
begin begin
modify_req = Pooler.modify(verbose, url, hostname, token, lifetime, tags) modify_req = Pooler.modify(verbose, url, hostname, token, lifetime, tags)
rescue TokenError => e rescue TokenError => e
@ -192,6 +193,7 @@ class Vmfloaty
exit 1 exit 1
end end
end end
end
if disk if disk
begin begin
@ -234,21 +236,15 @@ class Vmfloaty
if delete_all if delete_all
# get vms with token # get vms with token
begin begin
status = Auth.token_status(verbose, url, token) running_vms = Utils.get_all_token_vms(verbose, url, token)
rescue TokenError => e rescue TokenError => e
STDERR.puts e STDERR.puts e
exit 1 exit 1
rescue Exception => e
STDERR.puts e
exit 1
end 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? if ! running_vms.nil?
Utils.prettyprint_hosts(running_vms, verbose, url) Utils.prettyprint_hosts(running_vms, verbose, url)
# query y/n # query y/n

View file

@ -79,4 +79,17 @@ class Utils
puts "- #{vm}.#{domain} (#{metadata.join(", ")})" puts "- #{vm}.#{domain} (#{metadata.join(", ")})"
end end
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 end

View file

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

View file

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