vmfloaty 0.7.9 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/vmfloaty.rb CHANGED
@@ -5,11 +5,13 @@ require 'commander'
5
5
  require 'colorize'
6
6
  require 'json'
7
7
  require 'pp'
8
+ require 'uri'
8
9
  require 'vmfloaty/auth'
9
10
  require 'vmfloaty/pooler'
10
11
  require 'vmfloaty/version'
11
12
  require 'vmfloaty/conf'
12
13
  require 'vmfloaty/utils'
14
+ require 'vmfloaty/service'
13
15
  require 'vmfloaty/ssh'
14
16
 
15
17
  class Vmfloaty
@@ -17,27 +19,26 @@ class Vmfloaty
17
19
 
18
20
  def run
19
21
  program :version, Vmfloaty::VERSION
20
- program :description, 'A CLI helper tool for Puppet Labs vmpooler to help you stay afloat'
22
+ program :description, 'A CLI helper tool for Puppet Labs VM poolers to help you stay afloat'
21
23
 
22
24
  config = Conf.read_config
23
25
 
24
26
  command :get do |c|
25
27
  c.syntax = 'floaty get os_type0 os_type1=x ox_type2=y [options]'
26
28
  c.summary = 'Gets a vm or vms based on the os argument'
27
- c.description = 'A command to retrieve vms from vmpooler. Can either be a single vm, or multiple with the `=` syntax.'
29
+ c.description = 'A command to retrieve vms from a pooler service. Can either be a single vm, or multiple with the `=` syntax.'
28
30
  c.example 'Gets a few vms', 'floaty get centos=3 debian --user brian --url http://vmpooler.example.com'
29
31
  c.option '--verbose', 'Enables verbose output'
32
+ c.option '--service STRING', String, 'Configured pooler service name'
30
33
  c.option '--user STRING', String, 'User to authenticate with'
31
- c.option '--url STRING', String, 'URL of vmpooler'
32
- c.option '--token STRING', String, 'Token for vmpooler'
34
+ c.option '--url STRING', String, 'URL of pooler service'
35
+ c.option '--token STRING', String, 'Token for pooler service'
33
36
  c.option '--notoken', 'Makes a request without a token'
34
37
  c.option '--force', 'Forces vmfloaty to get requested vms'
35
38
  c.action do |args, options|
36
39
  verbose = options.verbose || config['verbose']
37
- token = options.token || config['token']
38
- user = options.user ||= config['user']
39
- url = options.url ||= config['url']
40
- no_token = options.notoken
40
+ service = Service.new(options, config)
41
+ use_token = !options.notoken
41
42
  force = options.force
42
43
 
43
44
  if args.empty?
@@ -48,98 +49,51 @@ class Vmfloaty
48
49
  os_types = Utils.generate_os_hash(args)
49
50
 
50
51
  max_pool_request = 5
51
- large_pool_requests = os_types.select{|k,v| v > max_pool_request}
52
+ large_pool_requests = os_types.select{|_,v| v > max_pool_request}
52
53
  if ! large_pool_requests.empty? and ! force
53
54
  STDERR.puts "Requesting vms over #{max_pool_request} requires a --force flag."
54
55
  STDERR.puts "Try again with `floaty get --force`"
55
56
  exit 1
56
57
  end
57
58
 
58
- unless os_types.empty?
59
- if no_token
60
- begin
61
- response = Pooler.retrieve(verbose, os_types, nil, url)
62
- rescue MissingParamError
63
- STDERR.puts e
64
- STDERR.puts "See `floaty get --help` for more information on how to get VMs."
65
- rescue AuthError => e
66
- STDERR.puts e
67
- exit 1
68
- end
69
- puts Utils.format_hosts(response)
70
- exit 0
71
- else
72
- unless token
73
- puts "No token found. Retrieving a token..."
74
- if !user
75
- STDERR.puts "You did not provide a user to authenticate to vmpooler with"
76
- exit 1
77
- end
78
- pass = password "Enter your vmpooler password please:", '*'
79
- begin
80
- token = Auth.get_token(verbose, url, user, pass)
81
- rescue TokenError => e
82
- STDERR.puts e
83
- exit 1
84
- end
85
-
86
- puts "\nToken retrieved!"
87
- puts token
88
- end
89
-
90
- begin
91
- response = Pooler.retrieve(verbose, os_types, token, url)
92
- rescue MissingParamError
93
- STDERR.puts e
94
- STDERR.puts "See `floaty get --help` for more information on how to get VMs."
95
- rescue AuthError => e
96
- STDERR.puts e
97
- exit 1
98
- end
99
- puts Utils.format_hosts(response)
100
- exit 0
101
- end
102
- else
59
+ if os_types.empty?
103
60
  STDERR.puts "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs."
104
61
  exit 1
105
62
  end
63
+
64
+ response = service.retrieve(verbose, os_types, use_token)
65
+ puts Utils.format_hosts(response)
106
66
  end
107
67
  end
108
68
 
109
69
  command :list do |c|
110
70
  c.syntax = 'floaty list [options]'
111
71
  c.summary = 'Shows a list of available vms from the pooler or vms obtained with a token'
112
- c.description = 'List will either show all vm templates available in vmpooler, or with the --active flag it will list vms obtained with a vmpooler token.'
72
+ c.description = 'List will either show all vm templates available in pooler service, or with the --active flag it will list vms obtained with a pooler service token.'
113
73
  c.example 'Filter the list on centos', 'floaty list centos --url http://vmpooler.example.com'
114
74
  c.option '--verbose', 'Enables verbose output'
75
+ c.option '--service STRING', String, 'Configured pooler service name'
115
76
  c.option '--active', 'Prints information about active vms for a given token'
116
- c.option '--token STRING', String, 'Token for vmpooler'
117
- c.option '--url STRING', String, 'URL of vmpooler'
77
+ c.option '--token STRING', String, 'Token for pooler service'
78
+ c.option '--url STRING', String, 'URL of pooler service'
118
79
  c.action do |args, options|
119
80
  verbose = options.verbose || config['verbose']
81
+ service = Service.new(options, config)
120
82
  filter = args[0]
121
- url = options.url ||= config['url']
122
- token = options.token || config['token']
123
- active = options.active
124
83
 
125
- if active
84
+ if options.active
126
85
  # list active vms
127
- begin
128
- running_vms = Utils.get_all_token_vms(verbose, url, token)
129
- rescue TokenError => e
130
- STDERR.puts e
131
- exit 1
132
- rescue Exception => e
133
- STDERR.puts e
134
- exit 1
135
- end
136
-
137
- if ! running_vms.nil?
138
- Utils.prettyprint_hosts(running_vms, verbose, url)
86
+ running_vms = service.list_active(verbose)
87
+ host = URI.parse(service.url).host
88
+ if running_vms.empty?
89
+ puts "You have no running VMs on #{host}"
90
+ else
91
+ puts "Your VMs on #{host}:"
92
+ Utils.pretty_print_hosts(verbose, service, running_vms)
139
93
  end
140
94
  else
141
95
  # list available vms from pooler
142
- os_list = Pooler.list(verbose, url, filter)
96
+ os_list = service.list(verbose, filter)
143
97
  puts os_list
144
98
  end
145
99
  end
@@ -148,139 +102,74 @@ class Vmfloaty
148
102
  command :query do |c|
149
103
  c.syntax = 'floaty query hostname [options]'
150
104
  c.summary = 'Get information about a given vm'
151
- c.description = 'Given a hostname from vmpooler, vmfloaty with query vmpooler to get various details about the vm.'
105
+ c.description = 'Given a hostname from the pooler service, vmfloaty with query the service to get various details about the vm.'
152
106
  c.example 'Get information about a sample host', 'floaty query hostname --url http://vmpooler.example.com'
153
107
  c.option '--verbose', 'Enables verbose output'
154
- c.option '--url STRING', String, 'URL of vmpooler'
108
+ c.option '--service STRING', String, 'Configured pooler service name'
109
+ c.option '--url STRING', String, 'URL of pooler service'
155
110
  c.action do |args, options|
156
111
  verbose = options.verbose || config['verbose']
157
- url = options.url ||= config['url']
112
+ service = Service.new(options, config)
158
113
  hostname = args[0]
159
114
 
160
- query_req = Pooler.query(verbose, url, hostname)
115
+ query_req = service.query(verbose, hostname)
161
116
  pp query_req
162
117
  end
163
118
  end
164
119
 
165
120
  command :modify do |c|
166
121
  c.syntax = 'floaty modify hostname [options]'
167
- c.summary = 'Modify a vms tags, time to live, and disk space'
168
- c.description = 'This command makes modifications to the virtual machines state in vmpooler. You can either append tags to the vm, increase how long it stays active for, or increase the amount of disk space.'
122
+ c.summary = 'Modify a VM\'s tags, time to live, disk space, or reservation reason'
123
+ 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.'
169
124
  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"}\''
170
125
  c.option '--verbose', 'Enables verbose output'
171
- c.option '--url STRING', String, 'URL of vmpooler'
172
- c.option '--token STRING', String, 'Token for vmpooler'
173
- c.option '--lifetime INT', Integer, 'VM TTL (Integer, in hours)'
174
- c.option '--disk INT', Integer, 'Increases VM disk space (Integer, in gb)'
175
- c.option '--tags STRING', String, 'free-form VM tagging (json)'
126
+ c.option '--service STRING', String, 'Configured pooler service name'
127
+ c.option '--url STRING', String, 'URL of pooler service'
128
+ c.option '--token STRING', String, 'Token for pooler service'
129
+ c.option '--lifetime INT', Integer, 'VM TTL (Integer, in hours) [vmpooler only]'
130
+ c.option '--disk INT', Integer, 'Increases VM disk space (Integer, in gb) [vmpooler only]'
131
+ c.option '--tags STRING', String, 'free-form VM tagging (json) [vmpooler only]'
132
+ c.option '--reason STRING', String, 'VM reservation reason [nspooler only]'
176
133
  c.option '--all', 'Modifies all vms acquired by a token'
177
134
  c.action do |args, options|
178
135
  verbose = options.verbose || config['verbose']
179
- url = options.url ||= config['url']
136
+ service = Service.new(options, config)
180
137
  hostname = args[0]
181
- lifetime = options.lifetime
182
- disk = options.disk
183
- tags = JSON.parse(options.tags) if options.tags
184
- token = options.token || config['token']
185
138
  modify_all = options.all
186
139
 
187
- running_vms = nil
188
-
189
- if modify_all
190
- begin
191
- running_vms = Utils.get_all_token_vms(verbose, url, token)
192
- rescue Exception => e
193
- STDERR.puts e
194
- end
195
- elsif hostname.include? ","
196
- running_vms = hostname.split(",")
140
+ if hostname.nil? and !modify_all
141
+ STDERR.puts "ERROR: Provide a hostname or specify --all."
142
+ exit 1
197
143
  end
198
-
199
- if lifetime || tags
200
- # all vms
201
- if !running_vms.nil?
144
+ running_vms = modify_all ? service.list_active(verbose) : hostname.split(",")
145
+
146
+ tags = options.tags ? JSON.parse(options.tags) : nil
147
+ modify_hash = {
148
+ lifetime: options.lifetime,
149
+ disk: options.disk,
150
+ tags: tags,
151
+ reason: options.reason
152
+ }
153
+ modify_hash.delete_if { |_, value| value.nil? }
154
+
155
+ unless modify_hash.empty?
156
+ ok = true
157
+ modified_hash = {}
158
+ running_vms.each do |vm|
202
159
  begin
203
- modify_hash = {}
204
- modify_flag = true
205
-
206
- running_vms.each do |vm|
207
- modify_hash[vm] = Pooler.modify(verbose, url, vm, token, lifetime, tags)
208
- end
209
-
210
- modify_hash.each do |hostname,status|
211
- if status == false
212
- STDERR.puts "Could not modify #{hostname}."
213
- modify_flag = false
214
- end
215
- end
216
-
217
- if modify_flag
218
- puts "Successfully modified all vms. Use `floaty list --active` to see the results."
219
- end
220
- rescue Exception => e
221
- STDERR.puts e
222
- exit 1
223
- end
224
- else
225
- # Single Vm
226
- begin
227
- modify_req = Pooler.modify(verbose, url, hostname, token, lifetime, tags)
228
- rescue TokenError => e
160
+ modified_hash[vm] = service.modify(verbose, vm, modify_hash)
161
+ rescue ModifyError => e
229
162
  STDERR.puts e
230
- exit 1
231
- end
232
-
233
- if modify_req["ok"]
234
- puts "Successfully modified vm #{hostname}."
235
- else
236
- STDERR.puts "Could not modify given host #{hostname} at #{url}."
237
- puts modify_req
238
- exit 1
163
+ ok = false
239
164
  end
240
165
  end
241
- end
242
-
243
- if disk
244
- # all vms
245
- if !running_vms.nil?
246
- begin
247
- modify_hash = {}
248
- modify_flag = true
249
-
250
- running_vms.each do |vm|
251
- modify_hash[vm] = Pooler.disk(verbose, url, vm, token, disk)
252
- end
253
-
254
- modify_hash.each do |hostname,status|
255
- if status == false
256
- STDERR.puts "Could not update disk space on #{hostname}."
257
- modify_flag = false
258
- end
259
- end
260
-
261
- if modify_flag
262
- puts "Successfully made request to update disk space on all vms."
263
- end
264
- rescue Exception => e
265
- STDERR.puts e
266
- exit 1
267
- end
268
- else
269
- # single vm
270
- begin
271
- disk_req = Pooler.disk(verbose, url, hostname, token, disk)
272
- rescue TokenError => e
273
- STDERR.puts e
274
- exit 1
275
- end
276
-
277
- if disk_req["ok"]
278
- puts "Successfully made request to update disk space of vm #{hostname}."
166
+ if ok
167
+ if modify_all
168
+ puts "Successfully modified all VMs."
279
169
  else
280
- STDERR.puts "Could not modify given host #{hostname} at #{url}."
281
- puts disk_req
282
- exit 1
170
+ puts "Successfully modified VM #{hostname}."
283
171
  end
172
+ puts "Use `floaty list --active` to see the results."
284
173
  end
285
174
  end
286
175
  end
@@ -289,95 +178,99 @@ class Vmfloaty
289
178
  command :delete do |c|
290
179
  c.syntax = 'floaty delete hostname,hostname2 [options]'
291
180
  c.summary = 'Schedules the deletion of a host or hosts'
292
- c.description = 'Given a comma separated list of hostnames, or --all for all vms, vmfloaty makes a request to vmpooler to schedule the deletion of those vms.'
181
+ c.description = 'Given a comma separated list of hostnames, or --all for all vms, vmfloaty makes a request to the pooler service to schedule the deletion of those vms.'
293
182
  c.example 'Schedules the deletion of a host or hosts', 'floaty delete myhost1,myhost2 --url http://vmpooler.example.com'
294
183
  c.option '--verbose', 'Enables verbose output'
184
+ c.option '--service STRING', String, 'Configured pooler service name'
295
185
  c.option '--all', 'Deletes all vms acquired by a token'
296
186
  c.option '-f', 'Does not prompt user when deleting all vms'
297
- c.option '--token STRING', String, 'Token for vmpooler'
298
- c.option '--url STRING', String, 'URL of vmpooler'
187
+ c.option '--token STRING', String, 'Token for pooler service'
188
+ c.option '--url STRING', String, 'URL of pooler service'
299
189
  c.action do |args, options|
300
190
  verbose = options.verbose || config['verbose']
191
+ service = Service.new(options, config)
301
192
  hostnames = args[0]
302
- token = options.token || config['token']
303
- url = options.url ||= config['url']
304
193
  delete_all = options.all
305
194
  force = options.f
306
195
 
307
- if delete_all
308
- # get vms with token
309
- begin
310
- running_vms = Utils.get_all_token_vms(verbose, url, token)
311
- rescue TokenError => e
312
- STDERR.puts e
313
- exit 1
314
- rescue Exception => e
315
- STDERR.puts e
316
- exit 1
317
- end
196
+ failures = []
197
+ successes = []
318
198
 
319
- if ! running_vms.nil?
320
- Utils.prettyprint_hosts(running_vms, verbose, url)
321
- # query y/n
199
+ if delete_all
200
+ running_vms = service.list_active(verbose)
201
+ if running_vms.empty?
202
+ STDERR.puts "You have no running VMs."
203
+ else
204
+ Utils.pretty_print_hosts(verbose, service, running_vms)
205
+ # Confirm deletion
322
206
  puts
323
-
324
- if force
325
- ans = true
326
- else
327
- ans = agree("Delete all VMs associated with token #{token}? [y/N]")
207
+ confirmed = true
208
+ unless force
209
+ confirmed = agree('Delete all these VMs? [y/N]')
328
210
  end
329
-
330
- if ans
331
- # delete vms
332
- puts "Scheduling all vms for for deletion"
333
- response = Pooler.delete(verbose, url, running_vms, token)
334
- response.each do |host,vals|
335
- if vals['ok'] == false
336
- STDERR.puts "There was a problem with your request for vm #{host}."
337
- STDERR.puts vals
211
+ if confirmed
212
+ response = service.delete(verbose, running_vms)
213
+ response.each do |hostname, result|
214
+ if result['ok']
215
+ successes << hostname
216
+ else
217
+ failures << hostname
338
218
  end
339
219
  end
340
220
  end
341
221
  end
342
-
343
- exit 0
344
- end
345
-
346
- if hostnames.nil?
222
+ elsif hostnames || args
223
+ hostnames = hostnames.split(',')
224
+ results = service.delete(verbose, hostnames)
225
+ results.each do |hostname, result|
226
+ if result['ok']
227
+ successes << hostname
228
+ else
229
+ failures << hostname
230
+ end
231
+ end
232
+ else
347
233
  STDERR.puts "You did not provide any hosts to delete"
348
234
  exit 1
349
- else
350
- hosts = hostnames.split(',')
351
- begin
352
- Pooler.delete(verbose, url, hosts, token)
353
- rescue TokenError => e
354
- STDERR.puts e
355
- exit 1
235
+ end
236
+
237
+ unless failures.empty?
238
+ STDERR.puts 'Unable to delete the following VMs:'
239
+ failures.each do |hostname|
240
+ STDERR.puts "- #{hostname}"
356
241
  end
242
+ STDERR.puts 'Check `floaty list --active`; Do you need to specify a different service?'
243
+ end
357
244
 
358
- puts "Schedulered vmpooler to delete vms #{hosts}."
359
- exit 0
245
+ unless successes.empty?
246
+ puts unless failures.empty?
247
+ puts 'Scheduled the following VMs for deletion:'
248
+ successes.each do |hostname|
249
+ puts "- #{hostname}"
250
+ end
360
251
  end
252
+
253
+ exit 1 unless failures.empty?
361
254
  end
362
255
  end
363
256
 
364
257
  command :snapshot do |c|
365
258
  c.syntax = 'floaty snapshot hostname [options]'
366
259
  c.summary = 'Takes a snapshot of a given vm'
367
- c.description = 'Will request a snapshot be taken of the given hostname in vmpooler. This command is known to take a while depending on how much load is on vmpooler.'
260
+ c.description = 'Will request a snapshot be taken of the given hostname in the pooler service. This command is known to take a while depending on how much load is on the pooler service.'
368
261
  c.example 'Takes a snapshot for a given host', 'floaty snapshot myvm.example.com --url http://vmpooler.example.com --token a9znth9dn01t416hrguu56ze37t790bl'
369
262
  c.option '--verbose', 'Enables verbose output'
370
- c.option '--url STRING', String, 'URL of vmpooler'
371
- c.option '--token STRING', String, 'Token for vmpooler'
263
+ c.option '--service STRING', String, 'Configured pooler service name'
264
+ c.option '--url STRING', String, 'URL of pooler service'
265
+ c.option '--token STRING', String, 'Token for pooler service'
372
266
  c.action do |args, options|
373
267
  verbose = options.verbose || config['verbose']
374
- url = options.url ||= config['url']
268
+ service = Service.new(options, config)
375
269
  hostname = args[0]
376
- token = options.token ||= config['token']
377
270
 
378
271
  begin
379
- snapshot_req = Pooler.snapshot(verbose, url, hostname, token)
380
- rescue TokenError => e
272
+ snapshot_req = service.snapshot(verbose, hostname)
273
+ rescue TokenError, ModifyError => e
381
274
  STDERR.puts e
382
275
  exit 1
383
276
  end
@@ -390,17 +283,17 @@ class Vmfloaty
390
283
  command :revert do |c|
391
284
  c.syntax = 'floaty revert hostname snapshot [options]'
392
285
  c.summary = 'Reverts a vm to a specified snapshot'
393
- c.description = 'Given a snapshot SHA, vmfloaty will request a revert to vmpooler to go back to a previous snapshot.'
286
+ c.description = 'Given a snapshot SHA, vmfloaty will request a revert to the pooler service to go back to a previous snapshot.'
394
287
  c.example 'Reverts to a snapshot for a given host', 'floaty revert myvm.example.com n4eb4kdtp7rwv4x158366vd9jhac8btq --url http://vmpooler.example.com --token a9znth9dn01t416hrguu56ze37t790bl'
395
288
  c.option '--verbose', 'Enables verbose output'
396
- c.option '--url STRING', String, 'URL of vmpooler'
397
- c.option '--token STRING', String, 'Token for vmpooler'
289
+ c.option '--service STRING', String, 'Configured pooler service name'
290
+ c.option '--url STRING', String, 'URL of pooler service'
291
+ c.option '--token STRING', String, 'Token for pooler service'
398
292
  c.option '--snapshot STRING', String, 'SHA of snapshot'
399
293
  c.action do |args, options|
400
294
  verbose = options.verbose || config['verbose']
401
- url = options.url ||= config['url']
295
+ service = Service.new(options, config)
402
296
  hostname = args[0]
403
- token = options.token || config['token']
404
297
  snapshot_sha = args[1] || options.snapshot
405
298
 
406
299
  if args[1] && options.snapshot
@@ -408,8 +301,8 @@ class Vmfloaty
408
301
  end
409
302
 
410
303
  begin
411
- revert_req = Pooler.revert(verbose, url, hostname, token, snapshot_sha)
412
- rescue TokenError => e
304
+ revert_req = service.revert(verbose, hostname, snapshot_sha)
305
+ rescue TokenError, ModifyError => e
413
306
  STDERR.puts e
414
307
  exit 1
415
308
  end
@@ -420,42 +313,37 @@ class Vmfloaty
420
313
 
421
314
  command :status do |c|
422
315
  c.syntax = 'floaty status [options]'
423
- c.summary = 'Prints the status of pools in vmpooler'
424
- c.description = 'Makes a request to vmpooler to request the information about vm pools and how many are ready to be used, what pools are empty, etc.'
425
- c.example 'Gets the current vmpooler status', 'floaty status --url http://vmpooler.example.com'
316
+ c.summary = 'Prints the status of pools in the pooler service'
317
+ c.description = 'Makes a request to the pooler service to request the information about vm pools and how many are ready to be used, what pools are empty, etc.'
318
+ c.example 'Gets the current pooler service status', 'floaty status --url http://vmpooler.example.com'
426
319
  c.option '--verbose', 'Enables verbose output'
427
- c.option '--url STRING', String, 'URL of vmpooler'
320
+ c.option '--service STRING', String, 'Configured pooler service name'
321
+ c.option '--url STRING', String, 'URL of pooler service'
428
322
  c.option '--json', 'Prints status in JSON format'
429
- c.action do |args, options|
323
+ c.action do |_, options|
430
324
  verbose = options.verbose || config['verbose']
431
- url = options.url ||= config['url']
432
-
433
- status = Pooler.status(verbose, url)
434
- message = status['status']['message']
435
- pools = status['pools']
436
-
325
+ service = Service.new(options, config)
437
326
  if options.json
438
- pp status
327
+ pp service.status(verbose)
439
328
  else
440
- Utils.prettyprint_status(status, message, pools, verbose)
329
+ Utils.pretty_print_status(verbose, service)
441
330
  end
442
-
443
- exit status['status']['ok']
444
331
  end
445
332
  end
446
333
 
447
334
  command :summary do |c|
448
335
  c.syntax = 'floaty summary [options]'
449
- c.summary = 'Prints a summary of vmpooler'
450
- c.description = 'Gives a very detailed summary of information related to vmpooler.'
451
- c.example 'Gets the current day summary of vmpooler', 'floaty summary --url http://vmpooler.example.com'
336
+ c.summary = 'Prints a summary of a pooler service'
337
+ c.description = 'Gives a very detailed summary of information related to the pooler service.'
338
+ c.example 'Gets the current day summary of the pooler service', 'floaty summary --url http://vmpooler.example.com'
452
339
  c.option '--verbose', 'Enables verbose output'
453
- c.option '--url STRING', String, 'URL of vmpooler'
454
- c.action do |args, options|
340
+ c.option '--service STRING', String, 'Configured pooler service name'
341
+ c.option '--url STRING', String, 'URL of pooler service'
342
+ c.action do |_, options|
455
343
  verbose = options.verbose || config['verbose']
456
- url = options.url ||= config['url']
344
+ service = Service.new(options, config)
457
345
 
458
- summary = Pooler.summary(verbose, url)
346
+ summary = service.summary(verbose)
459
347
  pp summary
460
348
  exit 0
461
349
  end
@@ -464,54 +352,45 @@ class Vmfloaty
464
352
  command :token do |c|
465
353
  c.syntax = 'floaty token <get delete status> [options]'
466
354
  c.summary = 'Retrieves or deletes a token or checks token status'
467
- c.description = 'This command is used to manage your vmpooler token. Through the various options, you are able to get a new token, delete an existing token, and request a tokens status.'
355
+ c.description = 'This command is used to manage your pooler service token. Through the various options, you are able to get a new token, delete an existing token, and request a tokens status.'
468
356
  c.example 'Gets a token from the pooler', 'floaty token get'
469
357
  c.option '--verbose', 'Enables verbose output'
470
- c.option '--url STRING', String, 'URL of vmpooler'
358
+ c.option '--service STRING', String, 'Configured pooler service name'
359
+ c.option '--url STRING', String, 'URL of pooler service'
471
360
  c.option '--user STRING', String, 'User to authenticate with'
472
- c.option '--token STRING', String, 'Token for vmpooler'
361
+ c.option '--token STRING', String, 'Token for pooler service'
473
362
  c.action do |args, options|
474
363
  verbose = options.verbose || config['verbose']
364
+ service = Service.new(options, config)
475
365
  action = args.first
476
- url = options.url ||= config['url']
477
- token = args[1] ||= options.token ||= config['token']
478
- user = options.user ||= config['user']
479
-
480
- case action
481
- when "get"
482
- pass = password "Enter your vmpooler password please:", '*'
483
- begin
484
- token = Auth.get_token(verbose, url, user, pass)
485
- rescue TokenError => e
486
- STDERR.puts e
487
- exit 1
488
- end
489
- puts token
490
- exit 0
491
- when "delete"
492
- pass = password "Enter your vmpooler password please:", '*'
493
- begin
494
- result = Auth.delete_token(verbose, url, user, pass, token)
495
- rescue TokenError => e
496
- STDERR.puts e
497
- exit 1
498
- end
499
- puts result
500
- exit 0
501
- when "status"
502
- begin
503
- status = Auth.token_status(verbose, url, token)
504
- rescue TokenError => e
505
- STDERR.puts e
506
- exit 1
366
+
367
+ begin
368
+ case action
369
+ when 'get'
370
+ token = service.get_new_token(verbose)
371
+ puts token
372
+ when 'delete'
373
+ result = service.delete_token(verbose, options.token)
374
+ puts result
375
+ when 'status'
376
+ token_value = options.token
377
+ if token_value.nil?
378
+ token_value = args[1]
379
+ end
380
+ status = service.token_status(verbose, token_value)
381
+ puts status
382
+ when nil
383
+ STDERR.puts 'No action provided'
384
+ exit 1
385
+ else
386
+ STDERR.puts "Unknown action: #{action}"
387
+ exit 1
507
388
  end
508
- puts status
509
- exit 0
510
- when nil
511
- STDERR.puts "No action provided"
512
- else
513
- STDERR.puts "Unknown action: #{action}"
389
+ rescue TokenError => e
390
+ STDERR.puts e
391
+ exit 1
514
392
  end
393
+ exit 0
515
394
  end
516
395
  end
517
396
 
@@ -521,16 +400,15 @@ class Vmfloaty
521
400
  c.description = 'This command simply will grab a vm template that was requested, and then ssh the user into the machine all at once.'
522
401
  c.example 'SSHs into a centos vm', 'floaty ssh centos7 --url https://vmpooler.example.com'
523
402
  c.option '--verbose', 'Enables verbose output'
524
- c.option '--url STRING', String, 'URL of vmpooler'
403
+ c.option '--service STRING', String, 'Configured pooler service name'
404
+ c.option '--url STRING', String, 'URL of pooler service'
525
405
  c.option '--user STRING', String, 'User to authenticate with'
526
- c.option '--token STRING', String, 'Token for vmpooler'
406
+ c.option '--token STRING', String, 'Token for pooler service'
527
407
  c.option '--notoken', 'Makes a request without a token'
528
408
  c.action do |args, options|
529
409
  verbose = options.verbose || config['verbose']
530
- url = options.url ||= config['url']
531
- token = options.token ||= config['token']
532
- user = options.user ||= config['user']
533
- no_token = options.notoken
410
+ service = Service.new(options, config)
411
+ use_token = !options.notoken
534
412
 
535
413
  if args.empty?
536
414
  STDERR.puts "No operating systems provided to obtain. See `floaty ssh --help` for more information on how to get VMs."
@@ -539,25 +417,11 @@ class Vmfloaty
539
417
 
540
418
  host_os = args.first
541
419
 
542
- if !no_token && !token
543
- puts "No token found. Retrieving a token..."
544
- if !user
545
- STDERR.puts "You did not provide a user to authenticate to vmpooler with"
546
- exit 1
547
- end
548
- pass = password "Enter your vmpooler password please:", '*'
549
- begin
550
- token = Auth.get_token(verbose, url, user, pass)
551
- rescue TokenError => e
552
- STDERR.puts e
553
- STDERR.puts 'Could not get token...requesting vm without a token anyway...'
554
- else
555
- puts "\nToken retrieved!"
556
- puts token
557
- end
420
+ if args.length > 1
421
+ STDERR.puts "Can't ssh to multiple hosts; Using #{host_os} only..."
558
422
  end
559
423
 
560
- Ssh.ssh(verbose, host_os, token, url)
424
+ service.ssh(verbose, host_os, use_token)
561
425
  exit 0
562
426
  end
563
427
  end
@@ -574,7 +438,7 @@ class Vmfloaty
574
438
  EOF
575
439
  c.example 'Gets path to bash tab completion script', 'floaty completion --shell bash'
576
440
  c.option '--shell STRING', String, 'Shell to request completion script for'
577
- c.action do |args, options|
441
+ c.action do |_, options|
578
442
  shell = (options.shell || 'bash').downcase.strip
579
443
  completion_file = File.expand_path(File.join('..', '..', 'extras', 'completions', "floaty.#{shell}"), __FILE__)
580
444