diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index ce80f47..59671bd 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -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 diff --git a/lib/vmfloaty/utils.rb b/lib/vmfloaty/utils.rb index cd9dbaa..dd63a74 100644 --- a/lib/vmfloaty/utils.rb +++ b/lib/vmfloaty/utils.rb @@ -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 diff --git a/lib/vmfloaty/version.rb b/lib/vmfloaty/version.rb index e170d47..378b4ad 100644 --- a/lib/vmfloaty/version.rb +++ b/lib/vmfloaty/version.rb @@ -1,6 +1,6 @@ class Version - @version = '0.7.1' + @version = '0.7.2' def self.get @version diff --git a/vmfloaty.gemspec b/vmfloaty.gemspec index 0d8a7b2..c448423 100644 --- a/vmfloaty.gemspec +++ b/vmfloaty.gemspec @@ -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'