-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathvmfloaty.rb
459 lines (412 loc) · 19 KB
/
vmfloaty.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# frozen_string_literal: true
require 'rubygems'
require 'commander'
require 'colorize'
require 'json'
require 'pp'
require 'uri'
require 'vmfloaty/auth'
require 'vmfloaty/pooler'
require 'vmfloaty/version'
require 'vmfloaty/conf'
require 'vmfloaty/utils'
require 'vmfloaty/service'
require 'vmfloaty/ssh'
class Vmfloaty
include Commander::Methods
def run # rubocop:disable Metrics/AbcSize
program :version, Vmfloaty::VERSION
program :description, 'A CLI helper tool for Puppet Labs VM poolers to help you stay afloat'
config = Conf.read_config
command :get do |c|
c.syntax = 'floaty get os_type0 os_type1=x ox_type2=y [options]'
c.summary = 'Gets a vm or vms based on the os argument'
c.description = 'A command to retrieve vms from a pooler service. Can either be a single vm, or multiple with the `=` syntax.'
c.example 'Gets a few vms', 'floaty get centos=3 debian --user brian --url http://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--user STRING', String, 'User to authenticate with'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--priority STRING', 'Priority for supported backends(ABS) (High(1), Medium(2), Low(3))'
c.option '--notoken', 'Makes a request without a token'
c.option '--force', 'Forces vmfloaty to get requested vms'
c.option '--json', 'Prints retrieved vms in JSON format'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service = Service.new(options, config)
use_token = !options.notoken
force = options.force
if args.empty?
STDERR.puts 'No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs.'
exit 1
end
os_types = Utils.generate_os_hash(args)
max_pool_request = 5
large_pool_requests = os_types.select { |_, v| v > max_pool_request }
if !large_pool_requests.empty? && !force
STDERR.puts "Requesting vms over #{max_pool_request} requires a --force flag."
STDERR.puts 'Try again with `floaty get --force`'
exit 1
end
if os_types.empty?
STDERR.puts 'No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs.'
exit 1
end
response = service.retrieve(verbose, os_types, use_token)
hosts = Utils.standardize_hostnames(response)
if options.json
puts JSON.pretty_generate(hosts)
else
puts Utils.format_host_output(hosts)
end
end
end
command :list do |c|
c.syntax = 'floaty list [options]'
c.summary = 'Shows a list of available vms from the pooler or vms obtained with a token'
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.'
c.example 'Filter the list on centos', 'floaty list centos --url http://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--active', 'Prints information about active vms for a given token'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--url STRING', String, 'URL of pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service = Service.new(options, config)
filter = args[0]
if options.active
# list active vms
running_vms = service.list_active(verbose)
host = URI.parse(service.url).host
if running_vms.empty?
puts "You have no running VMs on #{host}"
else
puts "Your VMs on #{host}:"
Utils.pretty_print_hosts(verbose, service, running_vms)
end
else
# list available vms from pooler
os_list = service.list(verbose, filter)
puts os_list
end
end
end
command :query do |c|
c.syntax = 'floaty query hostname [options]'
c.summary = 'Get information about a given vm'
c.description = 'Given a hostname from the pooler service, vmfloaty with query the service to get various details about the vm.'
c.example 'Get information about a sample host', 'floaty query hostname --url http://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service = Service.new(options, config)
hostname = args[0]
query_req = service.query(verbose, hostname)
pp query_req
end
end
command :modify do |c|
c.syntax = 'floaty modify hostname [options]'
c.summary = 'Modify a VM\'s tags, time to live, disk space, or reservation reason'
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.'
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"}\''
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--lifetime INT', Integer, 'VM TTL (Integer, in hours) [vmpooler only]'
c.option '--disk INT', Integer, 'Increases VM disk space (Integer, in gb) [vmpooler only]'
c.option '--tags STRING', String, 'free-form VM tagging (json) [vmpooler only]'
c.option '--reason STRING', String, 'VM reservation reason [nspooler only]'
c.option '--all', 'Modifies all vms acquired by a token'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service = Service.new(options, config)
hostname = args[0]
modify_all = options.all
if hostname.nil? && !modify_all
STDERR.puts 'ERROR: Provide a hostname or specify --all.'
exit 1
end
running_vms = modify_all ? service.list_active(verbose) : hostname.split(',')
tags = options.tags ? JSON.parse(options.tags) : nil
modify_hash = {
:lifetime => options.lifetime,
:disk => options.disk,
:tags => tags,
:reason => options.reason,
}
modify_hash.delete_if { |_, value| value.nil? }
unless modify_hash.empty?
ok = true
modified_hash = {}
running_vms.each do |vm|
begin
modified_hash[vm] = service.modify(verbose, vm, modify_hash)
rescue ModifyError => e
STDERR.puts e
ok = false
end
end
if ok
if modify_all
puts 'Successfully modified all VMs.'
else
puts "Successfully modified VM #{hostname}."
end
puts 'Use `floaty list --active` to see the results.'
end
end
end
end
command :delete do |c|
c.syntax = 'floaty delete hostname,hostname2 [options]'
c.syntax += "\n floaty delete job1,job2 [options] (only supported with ABS)"
c.summary = 'Schedules the deletion of a host or hosts'
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. If you are using the ABS service, you can also pass in JobIDs here. Note that passing in a Job ID will delete *all* of the hosts in the job.' # rubocop:disable Layout/LineLength
c.example 'Schedules the deletion of a host or hosts', 'floaty delete myhost1,myhost2 --url http://vmpooler.example.com'
c.example 'Schedules the deletion of a JobID or JobIDs', 'floaty delete 1579300120799,1579300120800 --url http://abs.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--all', 'Deletes all vms acquired by a token'
c.option '-f', 'Does not prompt user when deleting all vms'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--url STRING', String, 'URL of pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service = Service.new(options, config)
hostnames = args[0]
delete_all = options.all
force = options.f
failures = []
successes = []
if delete_all
running_vms = service.list_active(verbose)
if running_vms.empty?
STDERR.puts 'You have no running VMs.'
else
Utils.pretty_print_hosts(verbose, service, running_vms)
# Confirm deletion
puts
confirmed = true
confirmed = agree('Delete all these VMs? [y/N]') unless force
if confirmed
response = service.delete(verbose, running_vms)
response.each do |hostname, result|
if result['ok']
successes << hostname
else
failures << hostname
end
end
end
end
elsif hostnames || args
hostnames = hostnames.split(',')
results = service.delete(verbose, hostnames)
results.each do |hostname, result|
if result['ok']
successes << hostname
else
failures << hostname
end
end
else
STDERR.puts 'You did not provide any hosts to delete'
exit 1
end
unless failures.empty?
STDERR.puts 'Unable to delete the following VMs:'
failures.each do |hostname|
STDERR.puts "- #{hostname}"
end
STDERR.puts 'Check `floaty list --active`; Do you need to specify a different service?'
end
unless successes.empty?
puts unless failures.empty?
puts 'Scheduled the following VMs for deletion:'
successes.each do |hostname|
puts "- #{hostname}"
end
end
exit 1 unless failures.empty?
end
end
command :snapshot do |c|
c.syntax = 'floaty snapshot hostname [options]'
c.summary = 'Takes a snapshot of a given vm'
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.'
c.example 'Takes a snapshot for a given host', 'floaty snapshot myvm.example.com --url http://vmpooler.example.com --token a9znth9dn01t416hrguu56ze37t790bl'
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--token STRING', String, 'Token for pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service = Service.new(options, config)
hostname = args[0]
begin
snapshot_req = service.snapshot(verbose, hostname)
rescue TokenError, ModifyError => e
STDERR.puts e
exit 1
end
puts "Snapshot pending. Use `floaty query #{hostname}` to determine when snapshot is valid."
pp snapshot_req
end
end
command :revert do |c|
c.syntax = 'floaty revert hostname snapshot [options]'
c.summary = 'Reverts a vm to a specified snapshot'
c.description = 'Given a snapshot SHA, vmfloaty will request a revert to the pooler service to go back to a previous snapshot.'
c.example 'Reverts to a snapshot for a given host', 'floaty revert myvm.example.com n4eb4kdtp7rwv4x158366vd9jhac8btq --url http://vmpooler.example.com --token a9znth9dn01t416hrguu56ze37t790bl'
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--snapshot STRING', String, 'SHA of snapshot'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service = Service.new(options, config)
hostname = args[0]
snapshot_sha = args[1] || options.snapshot
STDERR.puts "Two snapshot arguments were given....using snapshot #{snapshot_sha}" if args[1] && options.snapshot
begin
revert_req = service.revert(verbose, hostname, snapshot_sha)
rescue TokenError, ModifyError => e
STDERR.puts e
exit 1
end
pp revert_req
end
end
command :status do |c|
c.syntax = 'floaty status [options]'
c.summary = 'Prints the status of pools in the pooler service'
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.'
c.example 'Gets the current pooler service status', 'floaty status --url http://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--json', 'Prints status in JSON format'
c.action do |_, options|
verbose = options.verbose || config['verbose']
service = Service.new(options, config)
if options.json
pp service.status(verbose)
else
Utils.pretty_print_status(verbose, service)
end
end
end
command :summary do |c|
c.syntax = 'floaty summary [options]'
c.summary = 'Prints a summary of a pooler service'
c.description = 'Gives a very detailed summary of information related to the pooler service.'
c.example 'Gets the current day summary of the pooler service', 'floaty summary --url http://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.action do |_, options|
verbose = options.verbose || config['verbose']
service = Service.new(options, config)
summary = service.summary(verbose)
pp summary
exit 0
end
end
command :token do |c|
c.syntax = 'floaty token <get delete status> [options]'
c.summary = 'Retrieves or deletes a token or checks token status'
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.'
c.example 'Gets a token from the pooler', 'floaty token get'
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--user STRING', String, 'User to authenticate with'
c.option '--token STRING', String, 'Token for pooler service'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service = Service.new(options, config)
action = args.first
begin
case action
when 'get'
token = service.get_new_token(verbose)
puts token
when 'delete'
result = service.delete_token(verbose, options.token)
puts result
when 'status'
token_value = options.token
token_value = args[1] if token_value.nil?
status = service.token_status(verbose, token_value)
puts status
when nil
STDERR.puts 'No action provided'
exit 1
else
STDERR.puts "Unknown action: #{action}"
exit 1
end
rescue TokenError => e
STDERR.puts e
exit 1
end
exit 0
end
end
command :ssh do |c|
c.syntax = 'floaty ssh os_type [options]'
c.summary = 'Grabs a single vm and sshs into it'
c.description = 'This command simply will grab a vm template that was requested, and then ssh the user into the machine all at once.'
c.example 'SSHs into a centos vm', 'floaty ssh centos7 --url https://vmpooler.example.com'
c.option '--verbose', 'Enables verbose output'
c.option '--service STRING', String, 'Configured pooler service name'
c.option '--url STRING', String, 'URL of pooler service'
c.option '--user STRING', String, 'User to authenticate with'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--notoken', 'Makes a request without a token'
c.action do |args, options|
verbose = options.verbose || config['verbose']
service = Service.new(options, config)
use_token = !options.notoken
if args.empty?
STDERR.puts 'No operating systems provided to obtain. See `floaty ssh --help` for more information on how to get VMs.'
exit 1
end
host_os = args.first
STDERR.puts "Can't ssh to multiple hosts; Using #{host_os} only..." if args.length > 1
service.ssh(verbose, host_os, use_token)
exit 0
end
end
command :completion do |c|
c.syntax = 'floaty completion [options]'
c.summary = 'Outputs path to completion script'
c.description = Utils.strip_heredoc(<<-DESCRIPTION)
Outputs path to a completion script for the specified shell (or 'bash' if not specified). This makes it easy to add the completion script to your profile:
source $(floaty completion --shell bash)
This subcommand will exit non-zero with an error message if no completion script is available for the requested shell.
DESCRIPTION
c.example 'Gets path to bash tab completion script', 'floaty completion --shell bash'
c.option '--shell STRING', String, 'Shell to request completion script for'
c.action do |_, options|
shell = (options.shell || 'bash').downcase.strip
completion_file = File.expand_path(File.join('..', '..', 'extras', 'completions', "floaty.#{shell}"), __FILE__)
if File.exist?(completion_file)
puts completion_file
exit 0
else
STDERR.puts "Could not find completion file for '#{shell}': No such file #{completion_file}"
exit 1
end
end
end
run!
end
end