Class: Vmfloaty

Inherits:
Object
  • Object
show all
Includes:
Commander::Methods
Defined in:
lib/vmfloaty.rb

Instance Method Summary collapse

Instance Method Details

#runObject



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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/vmfloaty.rb', line 18

def run
  program :version, Version.get
  program :description, 'A CLI helper tool for Puppet Labs vmpooler 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 vmpooler. 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 '--user STRING', String, 'User to authenticate with'
    c.option '--url STRING', String, 'URL of vmpooler'
    c.option '--token STRING', String, 'Token for vmpooler'
    c.option '--notoken', 'Makes a request without a token'
    c.option '--force', 'Forces vmfloaty to get requested vms'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      token = options.token || config['token']
      user = options.user ||= config['user']
      url = options.url ||= config['url']
      no_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{|k,v| v > max_pool_request}
      if ! large_pool_requests.empty? and ! force
        STDERR.puts "Requesting vms over #{max_pool_request} requires a --force flag."
        STDERR.puts "Try again with `floaty get --force`"
        exit 1
      end

      unless os_types.empty?
        if no_token
          begin
            response = Pooler.retrieve(verbose, os_types, nil, url)
          rescue MissingParamError
            STDERR.puts e
            STDERR.puts "See `floaty get --help` for more information on how to get VMs."
          rescue AuthError => e
            STDERR.puts e
            exit 1
          end
          puts Utils.format_hosts(response)
          exit 0
        else
          unless token
            puts "No token found. Retrieving a token..."
            if !user
              STDERR.puts "You did not provide a user to authenticate to vmpooler with"
              exit 1
            end
            pass = password "Enter your password please:", '*'
            begin
              token = Auth.get_token(verbose, url, user, pass)
            rescue TokenError => e
              STDERR.puts e
              exit 1
            end

            puts "\nToken retrieved!"
            puts token
          end

          begin
            response = Pooler.retrieve(verbose, os_types, token, url)
          rescue MissingParamError
            STDERR.puts e
            STDERR.puts "See `floaty get --help` for more information on how to get VMs."
          rescue AuthError => e
            STDERR.puts e
            exit 1
          end
          puts Utils.format_hosts(response)
          exit 0
        end
      else
        STDERR.puts "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs."
        exit 1
      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 vmpooler, or with the --active flag it will list vms obtained with a vmpooler token.'
    c.example 'Filter the list on centos', 'floaty list centos --url http://vmpooler.example.com'
    c.option '--verbose', 'Enables verbose output'
    c.option '--active', 'Prints information about active vms for a given token'
    c.option '--token STRING', String, 'Token for vmpooler'
    c.option '--url STRING', String, 'URL of vmpooler'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      filter = args[0]
      url = options.url ||= config['url']
      token = options.token || config['token']
      active = options.active

      if active
        # list active vms
        begin
          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

        if ! running_vms.nil?
          Utils.prettyprint_hosts(running_vms, verbose, url)
        end
      else
        # list available vms from pooler
        os_list = Pooler.list(verbose, url, 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 vmpooler, vmfloaty with query vmpooler 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 '--url STRING', String, 'URL of vmpooler'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      url = options.url ||= config['url']
      hostname = args[0]

      query_req = Pooler.query(verbose, url, hostname)
      pp query_req
    end
  end

  command :modify do |c|
    c.syntax = 'floaty modify hostname [options]'
    c.summary = 'Modify a vms tags, time to live, and disk space'
    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.'
    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 '--url STRING', String, 'URL of vmpooler'
    c.option '--token STRING', String, 'Token for vmpooler'
    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']
      hostname = args[0]
      lifetime = options.lifetime
      disk = options.disk
      tags = JSON.parse(options.tags) if options.tags
      token = options.token || config['token']
      modify_all = options.all

      running_vms = nil

      if modify_all
        begin
          running_vms = Utils.get_all_token_vms(verbose, url, token)
        rescue Exception => e
          STDERR.puts e
        end
      elsif hostname.include? ","
        running_vms = hostname.split(",")
      end

      if lifetime || tags
        # all vms
        if !running_vms.nil?
          begin
            modify_hash = {}
            modify_flag = true

            running_vms.each do |vm|
              modify_hash[vm] = Pooler.modify(verbose, url, vm, token, lifetime, tags)
            end

            modify_hash.each do |hostname,status|
              if status == false
                STDERR.puts "Could not modify #{hostname}."
                modify_flag = false
              end
            end

            if modify_flag
              puts "Successfully modified all vms. Use `floaty list --active` to see the results."
            end
          rescue Exception => e
            STDERR.puts e
            exit 1
          end
        else
          # 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

      if disk
        # all vms
        if !running_vms.nil?
          begin
            modify_hash = {}
            modify_flag = true

            running_vms.each do |vm|
              modify_hash[vm] = Pooler.disk(verbose, url, vm, token, disk)
            end

            modify_hash.each do |hostname,status|
              if status == false
                STDERR.puts "Could not update disk space on #{hostname}."
                modify_flag = false
              end
            end

            if modify_flag
              puts "Successfully made request to update disk space on all vms."
            end
          rescue Exception => e
            STDERR.puts e
            exit 1
          end
        else
          # single vm
          begin
            disk_req = Pooler.disk(verbose, url, hostname, token, disk)
          rescue TokenError => e
            STDERR.puts e
            exit 1
          end

          if disk_req["ok"]
            puts "Successfully made request to update disk space of vm #{hostname}."
          else
            STDERR.puts "Could not modify given host #{hostname} at #{url}."
            puts disk_req
            exit 1
          end
        end
      end
    end
  end

  command :delete do |c|
    c.syntax = 'floaty delete hostname,hostname2 [options]'
    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 vmpooler to schedule the deletion of those vms.'
    c.example 'Schedules the deletion of a host or hosts', 'floaty delete myhost1,myhost2 --url http://vmpooler.example.com'
    c.option '--verbose', 'Enables verbose output'
    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 vmpooler'
    c.option '--url STRING', String, 'URL of vmpooler'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      hostnames = args[0]
      token = options.token || config['token']
      url = options.url ||= config['url']
      delete_all = options.all
      force = options.f

      if delete_all
        # get vms with token
        begin
          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

        if ! running_vms.nil?
          Utils.prettyprint_hosts(running_vms, verbose, url)
          # query y/n
          puts

          if force
            ans = true
          else
            ans = agree("Delete all VMs associated with token #{token}? [y/N]")
          end

          if ans
            # delete vms
            puts "Scheduling all vms for for deletion"
            response = Pooler.delete(verbose, url, running_vms, token)
            response.each do |host,vals|
              if vals['ok'] == false
                STDERR.puts "There was a problem with your request for vm #{host}."
                STDERR.puts vals
              end
            end
          end
        end

        exit 0
      end

      if hostnames.nil?
        STDERR.puts "You did not provide any hosts to delete"
        exit 1
      else
        hosts = hostnames.split(',')
        begin
          Pooler.delete(verbose, url, hosts, token)
        rescue TokenError => e
          STDERR.puts e
          exit 1
        end

        puts "Schedulered vmpooler to delete vms #{hosts}."
        exit 0
      end
    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 vmpooler. This command is known to take a while depending on how much load is on vmpooler.'
    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 '--url STRING', String, 'URL of vmpooler'
    c.option '--token STRING', String, 'Token for vmpooler'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      url = options.url ||= config['url']
      hostname = args[0]
      token = options.token ||= config['token']

      begin
        snapshot_req = Pooler.snapshot(verbose, url, hostname, token)
      rescue TokenError => 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 vmpooler 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 '--url STRING', String, 'URL of vmpooler'
    c.option '--token STRING', String, 'Token for vmpooler'
    c.option '--snapshot STRING', String, 'SHA of snapshot'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      url = options.url ||= config['url']
      hostname = args[0]
      token = options.token || config['token']
      snapshot_sha = args[1] || options.snapshot

      if args[1] && options.snapshot
        STDERR.puts "Two snapshot arguments were given....using snapshot #{snapshot_sha}"
      end

      begin
        revert_req = Pooler.revert(verbose, url, hostname, token, snapshot_sha)
      rescue TokenError => 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 vmpooler'
    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.'
    c.example 'Gets the current vmpooler status', 'floaty status --url http://vmpooler.example.com'
    c.option '--verbose', 'Enables verbose output'
    c.option '--url STRING', String, 'URL of vmpooler'
    c.option '--json', 'Prints status in JSON format'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      url = options.url ||= config['url']

      status = Pooler.status(verbose, url)
      message = status['status']['message']
      pools = status['pools']

      if options.json
        pp status
      else
        Utils.prettyprint_status(status, message, pools, verbose)
      end

      exit status['status']['ok']
    end
  end

  command :summary do |c|
    c.syntax = 'floaty summary [options]'
    c.summary = 'Prints a summary of vmpooler'
    c.description = 'Gives a very detailed summary of information related to vmpooler.'
    c.example 'Gets the current day summary of vmpooler', 'floaty summary --url http://vmpooler.example.com'
    c.option '--verbose', 'Enables verbose output'
    c.option '--url STRING', String, 'URL of vmpooler'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      url = options.url ||= config['url']

      summary = Pooler.summary(verbose, url)
      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 vmpooler 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 '--url STRING', String, 'URL of vmpooler'
    c.option '--user STRING', String, 'User to authenticate with'
    c.option '--token STRING', String, 'Token for vmpooler'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      action = args.first
      url = options.url ||= config['url']
      token = args[1] ||= options.token ||= config['token']
      user = options.user ||= config['user']

      case action
      when "get"
        pass = password "Enter your password please:", '*'
        begin
          token = Auth.get_token(verbose, url, user, pass)
        rescue TokenError => e
          STDERR.puts e
          exit 1
        end
        puts token
        exit 0
      when "delete"
        pass = password "Enter your password please:", '*'
        begin
          result = Auth.delete_token(verbose, url, user, pass, token)
        rescue TokenError => e
          STDERR.puts e
          exit 1
        end
        puts result
        exit 0
      when "status"
        begin
          status = Auth.token_status(verbose, url, token)
        rescue TokenError => e
          STDERR.puts e
          exit 1
        end
        puts status
        exit 0
      when nil
        STDERR.puts "No action provided"
      else
        STDERR.puts "Unknown action: #{action}"
      end
    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 '--url STRING', String, 'URL of vmpooler'
    c.option '--user STRING', String, 'User to authenticate with'
    c.option '--token STRING', String, 'Token for vmpooler'
    c.option '--notoken', 'Makes a request without a token'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      url = options.url ||= config['url']
      token = options.token ||= config['token']
      user = options.user ||= config['user']
      no_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

      if !no_token && !token
        puts "No token found. Retrieving a token..."
        if !user
          STDERR.puts "You did not provide a user to authenticate to vmpooler with"
          exit 1
        end
        pass = password "Enter your password please:", '*'
        begin
          token = Auth.get_token(verbose, url, user, pass)
        rescue TokenError => e
          STDERR.puts e
          STDERR.puts 'Could not get token...requesting vm without a token anyway...'
        else
          puts "\nToken retrieved!"
          puts token
        end
      end

      Ssh.ssh(verbose, host_os, token, url)
      exit 0
    end
  end

  run!
end