Class: Floatyhelper

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

Instance Method Summary collapse

Instance Method Details

#runObject



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
# File 'lib/floatyhelper.rb', line 19

def run
  program :name, 'floatyhelper'
  program :version, FloatyhelperVersion::VERSION
  program :description, <<~EOT
    Commands for manipulating vmpooler VMs with floaty. Use the addhosts command from within a pe_acceptance_tests folder
    that has recently finished a run to add those hosts to the list of hosts managed by floatyhelper (or specify a sut.log
    file directly).
  EOT

  default_command :help

  command :config do |c|
    c.syntax = 'floatyhelper config [get <setting>|set <setting> <value>|list]'
    c.summary = 'Get, set, or list floatyhelper configuration options'
    c.description = <<~EOT
      This command will get the current value or set a new value for configuration options, or list all
      available configuration options.
    EOT
    c.action do |args|
      # Need to add type checking and value validation here at some point
      valid_settings = Config::VALID_SETTINGS
      action = args[0]
      setting = args[1]
      value = args[2]
      case action
      when 'get'
        unless setting && valid_settings.keys.include?(setting)
          puts "Please provide a valid config setting to get (floatyhelper get <setting>). For a list of valid settings, use 'floatyhelper config list'.".red
          exit 1
        end
        value = Config.get_config_setting(setting) || ''
        puts value
      when 'set'
        unless setting && value
          puts 'Please provide a config setting and value to set (floatyhelper set <setting> <value>).'.red
          exit 1
        end
        unless valid_settings.keys.include?(setting)
          puts "Please provide a valid config setting to set (floatyhelper get <setting>). For a list of valid settings, use 'floatyhelper config list'.".red
          exit 1
        end
        Config.set_config_setting(setting, value)
        puts "'#{setting}' set to #{value}".green
      when 'list'
        valid_settings.each do |s,info|
          puts "#{s}:"
          puts "  #{info['description']}"
          puts "  Default: #{info['default']}"
        end
      else
        puts 'Please provide a valid action (get, set, or list) to the config command.'.red
        exit 1
      end
    end
  end


  command :tags do |c|
    c.syntax = 'floatyhelper tags'
    c.summary = 'List all VM group tags'
    c.description = 'This lists all of the VM group tags that are currently defined, added via the addhosts command.'
    c.action do
      tags = Groups.get_tags
      if tags.length.zero?
        puts 'No VM groups are defined'.yellow
      else
        puts tags
      end
    end
  end

  command :list do |c|
    c.syntax = 'floatyhelper list [tag]'
    c.summary = 'List all VMs and their associated tags'
    c.description = <<~EOT
      This lists all VMs currently defined in the status.yaml file, added via the addhosts command, and their associated tags.
      If a tag is specified, it will only lists the hosts in that tag.
    EOT
    c.option '--check', 'Check remaining lifetime of each VM'
    c.action do |args, options|
      tags = Groups.get_tags
      if tags.empty?
        puts 'No hosts are currently managed by floatyhelper. To add a host, use the addhosts command.'.yellow
        exit 0
      end
      requested_tag = args.count.positive? ? args[0] : nil
      if requested_tag && !tags.include?(requested_tag)
        puts "Tag #{requested_tag} not found".red
        exit 1
      end
      tags_to_fetch = requested_tag ? [requested_tag] : tags
      tags_to_fetch.each do |tag|
        puts "#{tag}:"
        hosts = Groups.get_hosts(tag)
        hosts.each do |host|
          remaining = ''
          type = ''
          if options.check
            query = VM.query(host)
            if query && query['ok']
              type = query[host]['template'].gsub('-pixa4','')
              remaining = VM.alive(host, query) ? "#{query[host]['remaining']} hours remaining" : 'Expired'
            end
          end
          puts '  %-15s  %-28s  %s' % [host, type, remaining]
        end
      end
    end
  end

  command :check do |c|
    c.syntax = 'floatyhelper check [tag]'
    c.summary = 'Checks the remaining lifetime of each tracked VM.'
    c.description = <<~EOT
      Same as floatyhelper list --check. Checks the remaining lifetime of each tracked VM.
      If a tag is specified, it will only check hosts in that tag.
    EOT
    c.action do |args|
      command(:list).run(*args,'--check')
    end
  end

  command :snaplist do |c|
    c.syntax = 'floatyhelper snaplist <tag>'
    c.summary = 'List all snaptags saved for the given tag'
    c.description = 'Lists all of the snaptags available for the given VM group tag'
    c.action do |args|
      if args.empty?
        puts 'Please provide a tag'.red
        exit 1
      end
      tag = args[0]
      if !Groups.tag?(tag)
        puts "#{tag} is not a valid VM group tag.".red
        exit 1
      end
      snaptags = VM.snaplist(tag)
      if snaptags.empty?
        puts "Could not find any snaptags for VM group tag #{tag}".yellow
      else
        snaptags.each { |snaptag| puts snaptag }
      end
    end
  end

  command :showconfig do |c|
    c.syntax = 'floatyhelper showconfig'
    c.summary = 'Show the .floatyhelper.yaml file'
    c.description = 'This prints the raw contents of the .floatyhelper.yaml file. Mostly just for debug purposes.'
    c.action do
      puts File.read(Config.fhfile)
    end
  end

  command :addhosts do |c|
    c.syntax = 'floatyhelper addhosts <tag> [--hosts HOST1,HOST2,...] [--file sutfile]'
    c.summary = 'Adds hosts to list of hosts floatyhelper is tracking, with the given tag'
    c.description = <<~EOT
      This takes all of the hosts defined in a beaker sut.log file, and adds them to the list of hosts floatyhelper is tracking.
      These hosts will be grouped under the given tag, so that this tag can be used to operate on the whole set of hosts at once.
      If --file is not specified, this assumes you are inside a pe_acceptance_tests folder and grabs the list of hosts
      from log/latest/sut.log. If --hosts is specified, it will add the given comma-separated list of hosts. If the tag already exists,
      it will append the given hosts to the given tag.'
    EOT
    c.option '--hosts HOSTS', String, 'Comma separated list of hosts to add instead of reading sut.log'
    c.option '--file SUTFILE', String, 'Path to a sut.log file to get hosts from'
    c.action do |args, options|
      disallowed_tags = ['all','All','Unassigned']
      if args.empty?
        puts 'Please add a tag to use.'.red
        exit 1
      end
      tag = args[0]
      if disallowed_tags.include?(tag)
        puts "Can not use a tag named #{tag}".red
        exit 1
      end

      if options.hosts
        hosts = Hosts.get_options_hosts(options.hosts)
      else
        sutlog = options.file || "#{Dir.pwd}/log/latest/sut.log"
        hosts = Hosts.get_hosts_from_sut_log(sutlog)
      end
      Groups.addhosts(hosts, tag)
      amount = Config.get_config_setting('increaselife').to_i
      VM.increaselife(tag, amount)
      puts "Added the following hosts with tag #{tag}:".green
      hosts.each { |host| puts host }
    end
  end

  command :movehost do |c|
    c.syntax = 'floatyhelper movehost <host> <tag>'
    c.summary = 'Moves given host into the given tag group'
    c.description = 'Moves the given host from its existing tag group to the given tag group. Will throw an error if either the host is not currently being managed by floatyhelper. Note that snapshots will NOT be migrated to the new group.'
    c.action do |args|
      if args.size < 2
        puts 'Please specify both a host and a tag.'.red
        exit 1
      end
      host = args[0]
      tag = args[1]
      if !Groups.managed_host?(host)
        puts "Could not find host #{host}".red
        exit 1
      end

      Groups.removehosts([host], Groups.host_tag(host))
      Groups.addhosts([host], tag)
      puts "Moved #{host} to #{tag}".green
    end
  end


  command :destroy do |c|
    c.syntax = 'floatyhelper destroy <tag> [options]'
    c.summary = 'Deletes active VMs'
    c.description = 'Calls "floaty delete" on the VMPooler hosts with the given tag. Also removes these hosts and the tag from the status file.'
    c.option '--all', 'Destroys all hosts in status file. USE WITH CAUTION!'
    c.option '--hosts HOSTS', String, 'Comma separated list of hosts to destroy instead of using a tag. Note that this will NOT remove the hosts from the status file.'
    c.action do |args, options|
      if args.empty? && !options.all && !options.hosts
        puts 'Please specify a host or tag'.red
        exit 1
      end
      tag = args[0]
      id = options.all ? 'all' : options.hosts ? Hosts.get_options_hosts(options.hosts) : tag
      VM.destroy(id)
      if options.all
        puts 'Destroyed all hosts managed by floatyhelper'.green
      else
        puts "Destroyed hosts in #{id}".green
      end
    end
  end

  command :destroy_from_token do |c|
    c.syntax = 'floatyhelper destroy_from_token'
    c.summary = 'Destroys all VMs assigned to the user\'s token'
    c.description = 'This runs a "floaty token status" to get all VMs currently assigned to the user\'s token, and then deletes them all. Use with care.'
    c.action do
      info = Floaty.floaty_cmd('token status --service vmpooler')
      info = Floaty.parse_floaty_json_output(info)
      info.delete('ok')
      vms = info[info.keys[0]]['vms']
      running = vms.nil? ? nil : vms['running']
      if running.nil?
        puts 'No running VMs assigned to token to delete'.yellow
      else
        VM.destroy(running)
      end
    end
  end

  command :increaselife do |c|
    c.syntax = 'floatyhelper increaselife <tag> [options]'
    c.summary = 'Increase the lifetime of the given hosts'
    c.description = 'Increases the current life of the given host or tagged hosts. If no host is given, increases all hosts. Defaults to 12 hours. May be used with the --hosts flag instead of a tag.'
    c.option '--amount HOURS', Integer, 'Amount of hours to increase life. Default = 12'
    c.option '--all', 'Destroys all hosts in status file. USE WITH CAUTION!'
    c.option '--hosts HOST', String, 'Comma separated list of hosts to destroy instead of using a tag.'
    c.action do |args, options|
      if args.empty? && !options.all && !options.hosts
        puts 'Please specify a host or tag'.red
        exit 1
      end
      tag = args[0]
      id = options.all ? 'all' : options.hosts ? Hosts.get_options_hosts(options.hosts) : tag
      VM.increaselife(id,options.amount)
    end
  end

  command :revert do |c|
    c.syntax = 'floatyhelper revert <host|tag> <snaptag>'
    c.summary = 'Revert VM to the given snaptag'
    c.description = 'Reverts the given VM or host group to given snaptag. This snaptag is created when the snapshot command is run.'
    c.action do |args|
      if args.length < 2
        puts 'Please specify both a host/tag and a snaptag'.red
        exit 1
      end
      host_or_tag = args[0]
      snaptag = args[1]
      if !VM.snaptag_exists?(host_or_tag, snaptag)
        puts "Snaptag '#{snaptag}' not found for #{host_or_tag}".red
        exit 1
      end
      VM.revert(host_or_tag, snaptag)
    end
  end

  command :snapshot do |c|
    c.syntax = 'floatyhelper snapshot <host|tag> <snaptag>'
    c.summary = 'Snapshot the given VM or group of taggedhosts'
    c.description = 'Creates a snapshot of a host or group of tagged hosts, and defines a string to use as a "snaptag" to refer to this group of snapshots.'
    c.action do |args|
      if args.length < 2
        puts 'Please specify both a host/tag and a snaptag'.red
        exit 1
      end

      host_or_tag = args[0]
      snaptag = args[1]
      if VM.snaptag_exists?(host_or_tag, snaptag)
        answer = ask "Snaptag #{snaptag} already exists. Are you sure you want to overwrite? [y/N] ".yellow
        return unless answer.capitalize == 'Y'
      end
      VM.snapshot(host_or_tag, snaptag)
    end
  end

  command :refresh do |c|
    c.syntax = 'floatyhelper refresh'
    c.summary = 'Check for any expired VMs, and remove them from the list of hosts floatyhelper is tracking'
    c.description = 'Runs floaty query on VMs that floatyhelper is tracking, to check for any expired VMs, and allows the user to remove tags that contain only expired VMs.'
    c.option '-y', 'Do not prompt to remove tags with expired VMs'
    c.action do |_args, options|
      expired = false
      tags = Groups.get_tags
      tags.each do |tag|
        expired_hosts = []
        hosts = Groups.get_hosts(tag)
        hosts.each do |host|
          expired_hosts << host unless VM.alive(host)
        end
        next if expired_hosts.empty?

        expired = true
        if expired_hosts & hosts == hosts
          answer = options.y ? 'y' : ask("All hosts in tag #{tag} have expired. Delete this tag? [Y/n] ".yellow)
          if answer.empty? || answer.capitalize == 'Y'
            Groups.delete_tag(tag)
            puts "Tag #{tag} deleted".green
          end
        else
          expired_hosts.each { |host| puts host.yellow } unless options.y
          answer = options.y ? 'y' : ask("The above hosts in tag #{tag} have expired. Delete these hosts from the tag? [Y/n]".yellow)
          if answer.empty? || answer.capitalize == 'Y'
            Groups.removehosts(expired_hosts, tag)
            puts "Expired hosts from #{tag} deleted".green
          end
        end
      end
      puts 'No managed VMs have expired'.green unless expired
    end
  end

  command :getvm do |c|
    c.syntax = 'floatyhelper getvm <platform> <tag>'
    c.summary = 'Request a VM from floaty'
    c.description = <<~EOT
      Request a VM of the given platform type from floaty, and add the host to either the given tag or the Unassigned tag, if none is given.
      If no platform is given, default to centos-7-x86_64. This will use vmpooler for pooled platforms, and ABS otherwise. Force usage of
      ABS with the --abs flag.
    EOT
    c.option '--abs', 'Force use of ABS to get platform, even if platform is pooled.'
    c.option '--number INTEGER', Integer, 'Number of VMs of this flavor to check out. Default 1.'
    c.option '-y', "Don't ask if we're sure we want to request more than 10 VMs."
    c.action do |args, options|
      platform = args[0] || 'centos-7-x86_64'
      tag = args.length > 1 ? args[1] : 'Unassigned'
      num = options.number || 1
      if num > 10 && !options.y
        answer = ask("Checking out more than 10 VMs at once can have an impact on the Vmpooler infrastructure. Please do not use this option unless you know what you're doing and clean them up when you're finished. Proceed? [Y/n]".yellow)
        if !answer.empty? && answer.capitalize != 'Y'
          return
        end
      end
      begin
        hosts = VM.get_vm(platform: platform, force_abs: options.abs, number: num)
        Groups.addhosts(hosts, tag)
        puts "Added #{hosts.join(', ')} to tag #{tag}".green
        amount = Config.get_config_setting('increaselife').to_i
        VM.increaselife(hosts, amount)
      rescue StandardError => e
        puts e.message.red
      end
    end
  end

  command :checktokens do |c|
    c.syntax = 'floatyhelper checktokens'
    c.summary = 'Verify tokens for all services are valid'
    c.description = 'Verify that tokens for abs, vmpooler, and nspooler are valid in .vmfloaty.yml. If they are not, get new tokens.'
    c.action do
      Floaty.check_tokens
    end
  end

  command :runon do |c|
    c.syntax = 'floatyhelper runon <tag> <command>'
    c.summary = 'Run a command on all hosts under the given tag'
    c.description = 'Run a command on all hosts under the given tag using SSH. Requires that the key to SSH into the hosts is in ssh-agent already.'
    c.option '--user', 'Run command as a user other than root'
    c.action do |args, options|
      if args.length == 0
        puts 'Must specify a tag and command to run'.red
        return
      end
      if args.length == 1
        puts 'Must specify a command to run'.red
        return
      end

      tag = args[0]
      command = args[1..].join(' ')
      user = options.user || 'root'
      
      if !Groups.tag?(tag)
        puts "#{tag} does not appear to be a tag managed by floatyhelper.".red
        return
      end
      hosts = Groups.get_hosts(tag)
      threads = []
      responses = []
      mutex = Mutex.new
      hosts.each do |host|
        threads << Thread.new do
          output, status = Open3.capture2e("ssh -A -o StrictHostKeyChecking=no #{user}@#{host}.delivery.puppetlabs.net \"#{command}\"")
          mutex.synchronize do
            responses << [host,output,status]
          end
        end
      end
      puts
      (1..threads.count).each do |i|
        print "\rWaiting for thread #{i}/#{threads.count} to finish..."
        threads[i-1].join
      end
      print "\rDone\n"
      responses.each do |r|
        color = r[2].exitstatus.zero? ? :white : :red
        puts "*** #{r[0]}: Exit code #{r[2].exitstatus} ***".colorize(color)
        puts r[1].colorize(color)
      end
    end
  end

  command :scpto do |c|
    c.syntax = 'floatyhelper scp <tag> <path_to_local_file> <path_to_remote_file>'
    c.summary = 'SCP a file from this host to all hosts under the given tag at the given path'
    c.description = 'SCP a file from this host to all hosts under the given tag at the given path. This assumes the necessary SSH key is already loaded into ssh-agent.'
    c.option '--user', 'Log in as a user other than root'
    c.action do |args, options|
      if args.length < 3
        puts 'Must specify a tag, local path, and remote path'.red
        return
      end

      tag = args[0]
      local = args[1]
      remote = args[2]
      user = options.user || 'root'
      
      if !Groups.tag?(tag)
        puts "#{tag} does not appear to be a tag managed by floatyhelper.".red
        return
      end
      hosts = Groups.get_hosts(tag)
      threads = []
      responses = []
      mutex = Mutex.new
      hosts.each do |host|
        threads << Thread.new do
          output, status = Open3.capture2e("scp #{local} #{user}@#{host}.delivery.puppetlabs.net:#{remote}")
          mutex.synchronize do
            responses << [host,output,status]
          end
        end
      end
      puts
      (1..threads.count).each do |i|
        print "\rWaiting for thread #{i}/#{threads.count} to finish..."
        threads[i-1].join
      end
      print "\rDone\n"
      responses.each do |r|
        color = r[2].exitstatus.zero? ? :white : :red
        puts "*** #{r[0]}: Exit code #{r[2].exitstatus} ***".colorize(color)
        puts r[1].colorize(color)
      end
    end
  end

  run!
end