Skip to content

Commit 6447a5f

Browse files
committed
(#12) List active vms for a given token
This commit updates the list command to add a flag --active. It will list all active vms based on the given token as well as what template they are and how long they've been running for. As a result, this also updates how the delete --all command formats running vms.
1 parent 2a5e5ba commit 6447a5f

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

lib/vmfloaty.rb

+26-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def run
2222
c.syntax = 'floaty get os_type1=x ox_type2=y ...'
2323
c.summary = 'Gets a vm or vms based on the os flag'
2424
c.description = ''
25-
c.example 'Gets 3 vms', 'floaty get centos=3 debian --user brian --url http://vmpooler.example.com'
25+
c.example 'Gets a few vms', 'floaty get centos=3 debian --user brian --url http://vmpooler.example.com'
2626
c.option '--verbose', 'Enables verbose output'
2727
c.option '--user STRING', String, 'User to authenticate with'
2828
c.option '--url STRING', String, 'URL of vmpooler'
@@ -73,14 +73,36 @@ def run
7373
c.description = ''
7474
c.example 'Filter the list on centos', 'floaty list centos --url http://vmpooler.example.com'
7575
c.option '--verbose', 'Enables verbose output'
76+
c.option '--active', 'Prints information about active vms for a given token'
77+
c.option '--token STRING', String, 'Token for vmpooler'
7678
c.option '--url STRING', String, 'URL of vmpooler'
7779
c.action do |args, options|
7880
verbose = options.verbose || config['verbose']
7981
filter = args[0]
8082
url = options.url ||= config['url']
83+
token = options.token || config['token']
84+
active = options.active
85+
86+
if active
87+
# list active vms
88+
status = Auth.token_status(verbose, url, token)
89+
# print vms
90+
vms = status[token]['vms']
91+
if vms.nil?
92+
STDERR.puts "You have no running vms"
93+
exit 0
94+
end
95+
96+
running_vms = vms['running']
8197

82-
os_list = Pooler.list(verbose, url, filter)
83-
puts os_list
98+
if ! running_vms.nil?
99+
Utils.prettyprint_hosts(running_vms, verbose, url)
100+
end
101+
else
102+
# list available vms from pooler
103+
os_list = Pooler.list(verbose, url, filter)
104+
puts os_list
105+
end
84106
end
85107
end
86108

@@ -159,7 +181,7 @@ def run
159181
running_vms = vms['running']
160182

161183
if ! running_vms.nil?
162-
Utils.prettyprint_hosts(running_vms)
184+
Utils.prettyprint_hosts(running_vms, verbose, url)
163185
# query y/n
164186
puts ""
165187
ans = agree("Delete all VMs associated with token #{token}? [y/N]")

lib/vmfloaty/utils.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,18 @@ def self.generate_os_hash(os_args)
4141
os_types
4242
end
4343

44-
def self.prettyprint_hosts(hosts)
44+
def self.prettyprint_hosts(hosts, verbose, url)
4545
puts "Running VMs:"
4646
hosts.each do |vm|
47-
puts "- #{vm}"
47+
vm_info = Pooler.query(verbose, url, vm)
48+
if vm_info['ok']
49+
domain = vm_info[vm]['domain']
50+
template = vm_info[vm]['template']
51+
lifetime = vm_info[vm]['lifetime']
52+
running = vm_info[vm]['running']
53+
54+
puts "- #{vm}#{domain} (#{running}/#{lifetime} hours)"
55+
end
4856
end
4957
end
5058
end

0 commit comments

Comments
 (0)