Class: Vmfloaty

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

Constant Summary collapse

VERSION =
'1.8.1'

Instance Method Summary collapse

Instance Method Details

#runObject

rubocop:disable Metrics/AbcSize



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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/vmfloaty.rb', line 20

def run # rubocop:disable Metrics/AbcSize
  program :version, Vmfloaty::VERSION
  program :description,
          "A CLI helper tool for Puppet's vmpooler to help you stay afloat.\n\nConfiguration may be placed in a ~/.vmfloaty.yml file."

  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.option '--ondemand', 'Requested vms are provisioned upon receival of the request, tracked by a request ID'
    c.option '--continue STRING', String, 'resume polling ABS for job_id, for use when the cli was interrupted'
    c.option '--loglevel STRING', String, 'the log level to use (debug, info, error)'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      FloatyLogger.setlevel = options.loglevel if options.loglevel
      service = Service.new(options, config)
      use_token = !options.notoken
      force = options.force

      if args.empty?
        FloatyLogger.error '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)

      if os_types.empty?
        FloatyLogger.error 'No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs.'
        exit 1
      end

      max_pool_request = 5
      large_pool_requests = os_types.select { |_, v| v > max_pool_request }
      if !large_pool_requests.empty? && !force
        FloatyLogger.error "Requesting vms over #{max_pool_request} requires a --force flag."
        FloatyLogger.error 'Try again with `floaty get --force`'
        exit 1
      end

      response = service.retrieve(verbose, os_types, use_token, options.ondemand, options.continue)
      request_id = response['request_id'] if options.ondemand
      response = service.wait_for_request(verbose, request_id) if options.ondemand

      hosts = Utils.standardize_hostnames(response)

      if options.json || options.ondemand
        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 '--json', 'Prints information as JSON'
    c.option '--hostnameonly', 'When listing active vms, prints only hostnames, one per line'
    c.option '--token STRING', String, 'Token for pooler service'
    c.option '--url STRING', String, 'URL of pooler service'
    c.option '--user STRING', String, 'User to authenticate with'
    c.option '--loglevel STRING', String, 'the log level to use (debug, info, error)'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      FloatyLogger.setlevel = options.loglevel if options.loglevel

      service = Service.new(options, config)
      filter = args[0]

      if options.active
        # list active vms
        running_vms = if service.type == 'ABS'
                        # this is actually job_ids
                        service.list_active_job_ids(verbose, service.url, service.user)
                      else
                        service.list_active(verbose)
                      end
        host = URI.parse(service.url).host
        if running_vms.empty?
          if options.json
            puts {}.to_json
          else
            FloatyLogger.info "You have no running VMs on #{host}"
          end
        elsif options.json
          puts Utils.get_host_data(verbose, service, running_vms).to_json
        elsif options.hostnameonly
          Utils.get_host_data(verbose, service, running_vms).each do |hostname, host_data|
            Utils.print_fqdn_for_host(service, hostname, host_data)
          end
        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. If using ABS, you can query a job_id'
    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
        FloatyLogger.error 'ERROR: Provide a hostname or specify --all.'
        exit 1
      end
      running_vms =
        if modify_all
          service.list_active(verbose)
        else
          hostname.split(',')
        end

      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|
          modified_hash[vm] = service.modify(verbose, vm, modify_hash)
        rescue ModifyError => e
          FloatyLogger.error e
          ok = false
        end
        if ok
          if modify_all
            puts "Successfully modified all #{running_vms.count} 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 '--json', 'Outputs hosts scheduled for deletion as JSON'
    c.option '--token STRING', String, 'Token for pooler service'
    c.option '--url STRING', String, 'URL of pooler service'
    c.option '--user STRING', String, 'User to authenticate with'
    c.option '--loglevel STRING', String, 'the log level to use (debug, info, error)'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      FloatyLogger.setlevel = options.loglevel if options.loglevel

      service = Service.new(options, config)
      hostnames = args[0]
      delete_all = options.all
      force = options.f

      failures = []
      successes = []

      if delete_all
        running_vms = if service.type == 'ABS'
                        # this is actually job_ids
                        service.list_active_job_ids(verbose, service.url, service.user)
                      else
                        service.list_active(verbose)
                      end
        if running_vms.empty?
          if options.json
            puts {}.to_json
          else
            FloatyLogger.info 'You have no running VMs.'
          end
        else
          confirmed = true
          unless force
            Utils.pretty_print_hosts(verbose, service, running_vms, true)
            # Confirm deletion
            confirmed = agree('Delete all these VMs? [y/N]')
          end
          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
        FloatyLogger.info 'You did not provide any hosts to delete'
        exit 1
      end

      unless failures.empty?
        FloatyLogger.info 'Unable to delete the following VMs:'
        failures.each do |hostname|
          FloatyLogger.info "- #{hostname}"
        end
        FloatyLogger.info 'Check `floaty list --active`; Do you need to specify a different service?'
      end

      unless successes.empty?
        if options.json
          puts successes.to_json
        else
          puts 'Scheduled the following VMs for deletion:'
          output = ''
          successes.each do |hostname|
            output += "- #{hostname}\n"
          end
          puts output
        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
        FloatyLogger.error 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

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

      begin
        revert_req = service.revert(verbose, hostname, snapshot_sha)
      rescue TokenError, ModifyError => e
        FloatyLogger.error 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.option '--loglevel STRING', String, 'the log level to use (debug, info, error)'
    c.action do |_, options|
      verbose = options.verbose || config['verbose']
      FloatyLogger.setlevel = options.loglevel if options.loglevel
      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
          FloatyLogger.error 'No action provided'
          exit 1
        else
          FloatyLogger.error "Unknown action: #{action}"
          exit 1
        end
      rescue TokenError => e
        FloatyLogger.error 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.option '--priority STRING', 'Priority for supported backends(ABS) (High(1), Medium(2), Low(3))'
    c.option '--ondemand', 'Requested vms are provisioned upon receival of the request, tracked by a request ID'
    c.action do |args, options|
      verbose = options.verbose || config['verbose']
      service = Service.new(options, config)
      use_token = !options.notoken

      if args.empty?
        FloatyLogger.error '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

      FloatyLogger.info "Can't ssh to multiple hosts; Using #{host_os} only..." if args.length > 1

      service.ssh(verbose, host_os, use_token, options.ondemand)
      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
        FloatyLogger.error "Could not find completion file for '#{shell}': No such file #{completion_file}"
        exit 1
      end
    end
  end

  command :service do |c|
    c.syntax = 'floaty service <types examples>'
    c.summary = 'Display information about floaty services and their configuration'
    c.description = 'Display information about floaty services to aid in setting up a configuration file.'
    c.example 'Print a list of the valid service types', 'floaty service types'
    c.example 'Print a sample config file with multiple services', 'floaty service examples'
    c.example 'list vms from the service named "nspooler-prod"', 'floaty list --service nspooler-prod'
    c.action do |args, _options|
      action = args.first

      example_config = Utils.strip_heredoc(<<-CONFIG)
        # Sample ~/.vmfloaty.yml with just vmpooler
        user: 'jdoe'
        url: 'https://vmpooler.example.net'
        token: '456def789'

        # Sample ~/.vmfloaty.yml with multiple services
        # Note: when the --service is not specified on the command line,
        # the first service listed here is selected automatically
        user: 'jdoe'
        services:
          abs-prod:
            type: 'abs'
            url: 'https://abs.example.net/api/v2'
            token: '123abc456'
            vmpooler_fallback: 'vmpooler-prod'
          nspooler-prod:
            type: 'nspooler'
            url: 'https://nspooler.example.net'
            token:  '789ghi012'
          vmpooler-dev:
            type: 'vmpooler'
            url: 'https://vmpooler-dev.example.net'
            token: '987dsa654'
          vmpooler-prod:
            type: 'vmpooler'
            url: 'https://vmpooler.example.net'
            token: '456def789'

      CONFIG

      types_output = Utils.strip_heredoc(<<-TYPES)
        The values on the left below can be used in ~/.vmfloaty.yml as the value of type:

        abs:       Puppet's Always Be Scheduling
        nspooler:  Puppet's Non-standard Pooler, aka NSPooler
        vmpooler:  Puppet's VMPooler
      TYPES

      case action
      when 'examples'
        FloatyLogger.info example_config
      when 'types'
        FloatyLogger.info types_output
      when nil
        FloatyLogger.error 'No action provided'
        exit 1
      else
        FloatyLogger.error "Unknown action: #{action}"
        exit 1
      end
    end
  end

  run!
end