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, "    Commands for manipulating vmpooler VMs with floaty. Use the addhosts command from within a pe_acceptance_tests folder\n    that has recently finished a run to add those hosts to the list of hosts managed by floatyhelper (or specify a sut.log\n    file directly).\n  EOT\n\n  default_command :help\n\n  command :config do |c|\n    c.syntax = 'floatyhelper config [get <setting>|set <setting> <value>|list]'\n    c.summary = 'Get, set, or list floatyhelper configuration options'\n    c.description = <<~EOT\n      This command will get the current value or set a new value for configuration options, or list all\n      available configuration options.\n    EOT\n    c.action do |args|\n      # Need to add type checking and value validation here at some point\n      valid_settings = Config::VALID_SETTINGS\n      action = args[0]\n      setting = args[1]\n      value = args[2]\n      case action\n      when 'get'\n        unless setting && valid_settings.keys.include?(setting)\n          puts \"Please provide a valid config setting to get (floatyhelper get <setting>). For a list of valid settings, use 'floatyhelper config list'.\".red\n          exit 1\n        end\n        value = Config.get_config_setting(setting) || ''\n        puts value\n      when 'set'\n        unless setting && value\n          puts 'Please provide a config setting and value to set (floatyhelper set <setting> <value>).'.red\n          exit 1\n        end\n        unless valid_settings.keys.include?(setting)\n          puts \"Please provide a valid config setting to set (floatyhelper get <setting>). For a list of valid settings, use 'floatyhelper config list'.\".red\n          exit 1\n        end\n        Config.set_config_setting(setting, value)\n        puts \"'\#{setting}' set to \#{value}\".green\n      when 'list'\n        valid_settings.each do |s,info|\n          puts \"\#{s}:\"\n          puts \"  \#{info['description']}\"\n          puts \"  Default: \#{info['default']}\"\n        end\n      else\n        puts 'Please provide a valid action (get, set, or list) to the config command.'.red\n        exit 1\n      end\n    end\n  end\n\n\n  command :tags do |c|\n    c.syntax = 'floatyhelper tags'\n    c.summary = 'List all VM group tags'\n    c.description = 'This lists all of the VM group tags that are currently defined, added via the addhosts command.'\n    c.action do\n      tags = Groups.get_tags\n      if tags.length.zero?\n        puts 'No VM groups are defined'.yellow\n      else\n        puts tags\n      end\n    end\n  end\n\n  command :list do |c|\n    c.syntax = 'floatyhelper list [tag]'\n    c.summary = 'List all VMs and their associated tags'\n    c.description = <<~EOT\n      This lists all VMs currently defined in the status.yaml file, added via the addhosts command, and their associated tags.\n      If a tag is specified, it will only lists the hosts in that tag.\n    EOT\n    c.option '--check', 'Check remaining lifetime of each VM'\n    c.action do |args, options|\n      tags = Groups.get_tags\n      if tags.empty?\n        puts 'No hosts are currently managed by floatyhelper. To add a host, use the addhosts command.'.yellow\n        exit 0\n      end\n      requested_tag = args.count.positive? ? args[0] : nil\n      if requested_tag && !tags.include?(requested_tag)\n        puts \"Tag \#{requested_tag} not found\".red\n        exit 1\n      end\n      tags_to_fetch = requested_tag ? [requested_tag] : tags\n      tags_to_fetch.each do |tag|\n        puts \"\#{tag}:\"\n        hosts = Groups.get_hosts(tag)\n        hosts.each do |host|\n          remaining = ''\n          type = ''\n          if options.check\n            query = VM.query(host)\n            if query && query['ok']\n              type = query[host]['template'].gsub('-pixa4','')\n              remaining = VM.alive(host, query) ? \"\#{query[host]['remaining']} hours remaining\" : 'Expired'\n            end\n          end\n          puts '  %-15s  %-28s  %s' % [host, type, remaining]\n        end\n      end\n    end\n  end\n\n  command :check do |c|\n    c.syntax = 'floatyhelper check [tag]'\n    c.summary = 'Checks the remaining lifetime of each tracked VM.'\n    c.description = <<~EOT\n      Same as floatyhelper list --check. Checks the remaining lifetime of each tracked VM.\n      If a tag is specified, it will only check hosts in that tag.\n    EOT\n    c.action do |args|\n      command(:list).run(*args,'--check')\n    end\n  end\n\n  command :snaplist do |c|\n    c.syntax = 'floatyhelper snaplist <tag>'\n    c.summary = 'List all snaptags saved for the given tag'\n    c.description = 'Lists all of the snaptags available for the given VM group tag'\n    c.action do |args|\n      if args.empty?\n        puts 'Please provide a tag'.red\n        exit 1\n      end\n      tag = args[0]\n      if !Groups.tag?(tag)\n        puts \"\#{tag} is not a valid VM group tag.\".red\n        exit 1\n      end\n      snaptags = VM.snaplist(tag)\n      if snaptags.empty?\n        puts \"Could not find any snaptags for VM group tag \#{tag}\".yellow\n      else\n        snaptags.each { |snaptag| puts snaptag }\n      end\n    end\n  end\n\n  command :showconfig do |c|\n    c.syntax = 'floatyhelper showconfig'\n    c.summary = 'Show the .floatyhelper.yaml file'\n    c.description = 'This prints the raw contents of the .floatyhelper.yaml file. Mostly just for debug purposes.'\n    c.action do\n      puts File.read(Config.fhfile)\n    end\n  end\n\n  command :addhosts do |c|\n    c.syntax = 'floatyhelper addhosts <tag> [--hosts HOST1,HOST2,...] [--file sutfile]'\n    c.summary = 'Adds hosts to list of hosts floatyhelper is tracking, with the given tag'\n    c.description = <<~EOT\n      This takes all of the hosts defined in a beaker sut.log file, and adds them to the list of hosts floatyhelper is tracking.\n      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.\n      If --file is not specified, this assumes you are inside a pe_acceptance_tests folder and grabs the list of hosts\n      from log/latest/sut.log. If --hosts is specified, it will add the given comma-separated list of hosts. If the tag already exists,\n      it will append the given hosts to the given tag.'\n    EOT\n    c.option '--hosts HOSTS', String, 'Comma separated list of hosts to add instead of reading sut.log'\n    c.option '--file SUTFILE', String, 'Path to a sut.log file to get hosts from'\n    c.action do |args, options|\n      disallowed_tags = ['all','All','Unassigned']\n      if args.empty?\n        puts 'Please add a tag to use.'.red\n        exit 1\n      end\n      tag = args[0]\n      if disallowed_tags.include?(tag)\n        puts \"Can not use a tag named \#{tag}\".red\n        exit 1\n      end\n\n      if options.hosts\n        hosts = Hosts.get_options_hosts(options.hosts)\n      else\n        sutlog = options.file || \"\#{Dir.pwd}/log/latest/sut.log\"\n        hosts = Hosts.get_hosts_from_sut_log(sutlog)\n      end\n      Groups.addhosts(hosts, tag)\n      amount = Config.get_config_setting('increaselife').to_i\n      VM.increaselife(tag, amount)\n      puts \"Added the following hosts with tag \#{tag}:\".green\n      hosts.each { |host| puts host }\n    end\n  end\n\n  command :movehost do |c|\n    c.syntax = 'floatyhelper movehost <host> <tag>'\n    c.summary = 'Moves given host into the given tag group'\n    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.'\n    c.action do |args|\n      if args.size < 2\n        puts 'Please specify both a host and a tag.'.red\n        exit 1\n      end\n      host = args[0]\n      tag = args[1]\n      if !Groups.managed_host?(host)\n        puts \"Could not find host \#{host}\".red\n        exit 1\n      end\n\n      Groups.removehosts([host], Groups.host_tag(host))\n      Groups.addhosts([host], tag)\n      puts \"Moved \#{host} to \#{tag}\".green\n    end\n  end\n\n\n  command :destroy do |c|\n    c.syntax = 'floatyhelper destroy <tag> [options]'\n    c.summary = 'Deletes active VMs'\n    c.description = 'Calls \"floaty delete\" on the VMPooler hosts with the given tag. Also removes these hosts and the tag from the status file.'\n    c.option '--all', 'Destroys all hosts in status file. USE WITH CAUTION!'\n    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.'\n    c.action do |args, options|\n      if args.empty? && !options.all && !options.hosts\n        puts 'Please specify a host or tag'.red\n        exit 1\n      end\n      tag = args[0]\n      id = options.all ? 'all' : options.hosts ? Hosts.get_options_hosts(options.hosts) : tag\n      VM.destroy(id)\n      if options.all\n        puts 'Destroyed all hosts managed by floatyhelper'.green\n      else\n        puts \"Destroyed hosts in \#{id}\".green\n      end\n    end\n  end\n\n  command :destroy_from_token do |c|\n    c.syntax = 'floatyhelper destroy_from_token'\n    c.summary = 'Destroys all VMs assigned to the user\\'s token'\n    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.'\n    c.action do\n      info = Floaty.floaty_cmd('token status --service vmpooler')\n      info = Floaty.parse_floaty_json_output(info)\n      info.delete('ok')\n      vms = info[info.keys[0]]['vms']\n      running = vms.nil? ? nil : vms['running']\n      if running.nil?\n        puts 'No running VMs assigned to token to delete'.yellow\n      else\n        VM.destroy(running)\n      end\n    end\n  end\n\n  command :increaselife do |c|\n    c.syntax = 'floatyhelper increaselife <tag> [options]'\n    c.summary = 'Increase the lifetime of the given hosts'\n    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.'\n    c.option '--amount HOURS', Integer, 'Amount of hours to increase life. Default = 12'\n    c.option '--all', 'Destroys all hosts in status file. USE WITH CAUTION!'\n    c.option '--hosts HOST', String, 'Comma separated list of hosts to destroy instead of using a tag.'\n    c.action do |args, options|\n      if args.empty? && !options.all && !options.hosts\n        puts 'Please specify a host or tag'.red\n        exit 1\n      end\n      tag = args[0]\n      id = options.all ? 'all' : options.hosts ? Hosts.get_options_hosts(options.hosts) : tag\n      VM.increaselife(id,options.amount)\n    end\n  end\n\n  command :revert do |c|\n    c.syntax = 'floatyhelper revert <host|tag> <snaptag>'\n    c.summary = 'Revert VM to the given snaptag'\n    c.description = 'Reverts the given VM or host group to given snaptag. This snaptag is created when the snapshot command is run.'\n    c.action do |args|\n      if args.length < 2\n        puts 'Please specify both a host/tag and a snaptag'.red\n        exit 1\n      end\n      host_or_tag = args[0]\n      snaptag = args[1]\n      if !VM.snaptag_exists?(host_or_tag, snaptag)\n        puts \"Snaptag '\#{snaptag}' not found for \#{host_or_tag}\".red\n        exit 1\n      end\n      VM.revert(host_or_tag, snaptag)\n    end\n  end\n\n  command :snapshot do |c|\n    c.syntax = 'floatyhelper snapshot <host|tag> <snaptag>'\n    c.summary = 'Snapshot the given VM or group of taggedhosts'\n    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.'\n    c.action do |args|\n      if args.length < 2\n        puts 'Please specify both a host/tag and a snaptag'.red\n        exit 1\n      end\n\n      host_or_tag = args[0]\n      snaptag = args[1]\n      if VM.snaptag_exists?(host_or_tag, snaptag)\n        answer = ask \"Snaptag \#{snaptag} already exists. Are you sure you want to overwrite? [y/N] \".yellow\n        return unless answer.capitalize == 'Y'\n      end\n      VM.snapshot(host_or_tag, snaptag)\n    end\n  end\n\n  command :refresh do |c|\n    c.syntax = 'floatyhelper refresh'\n    c.summary = 'Check for any expired VMs, and remove them from the list of hosts floatyhelper is tracking'\n    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.'\n    c.option '-y', 'Do not prompt to remove tags with expired VMs'\n    c.action do |_args, options|\n      expired = false\n      tags = Groups.get_tags\n      tags.each do |tag|\n        expired_hosts = []\n        hosts = Groups.get_hosts(tag)\n        hosts.each do |host|\n          expired_hosts << host unless VM.alive(host)\n        end\n        next if expired_hosts.empty?\n\n        expired = true\n        if expired_hosts & hosts == hosts\n          answer = options.y ? 'y' : ask(\"All hosts in tag \#{tag} have expired. Delete this tag? [Y/n] \".yellow)\n          if answer.empty? || answer.capitalize == 'Y'\n            Groups.delete_tag(tag)\n            puts \"Tag \#{tag} deleted\".green\n          end\n        else\n          expired_hosts.each { |host| puts host.yellow } unless options.y\n          answer = options.y ? 'y' : ask(\"The above hosts in tag \#{tag} have expired. Delete these hosts from the tag? [Y/n]\".yellow)\n          if answer.empty? || answer.capitalize == 'Y'\n            Groups.removehosts(expired_hosts, tag)\n            puts \"Expired hosts from \#{tag} deleted\".green\n          end\n        end\n      end\n      puts 'No managed VMs have expired'.green unless expired\n    end\n  end\n\n  command :getvm do |c|\n    c.syntax = 'floatyhelper getvm <platform> <tag>'\n    c.summary = 'Request a VM from floaty'\n    c.description = <<~EOT\n      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.\n      If no platform is given, default to centos-7-x86_64. This will use vmpooler for pooled platforms, and ABS otherwise. Force usage of\n      ABS with the --abs flag.\n    EOT\n    c.option '--abs', 'Force use of ABS to get platform, even if platform is pooled.'\n    c.option '--number INTEGER', Integer, 'Number of VMs of this flavor to check out. Default 1.'\n    c.option '-y', \"Don't ask if we're sure we want to request more than 10 VMs.\"\n    c.action do |args, options|\n      platform = args[0] || 'centos-7-x86_64'\n      tag = args.length > 1 ? args[1] : 'Unassigned'\n      num = options.number || 1\n      if num > 10 && !options.y\n        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)\n        if !answer.empty? && answer.capitalize != 'Y'\n          return\n        end\n      end\n      begin\n        hosts = VM.get_vm(platform: platform, force_abs: options.abs, number: num)\n        Groups.addhosts(hosts, tag)\n        puts \"Added \#{hosts.join(', ')} to tag \#{tag}\".green\n        amount = Config.get_config_setting('increaselife').to_i\n        VM.increaselife(hosts, amount)\n      rescue StandardError => e\n        puts e.message.red\n      end\n    end\n  end\n\n  command :checktokens do |c|\n    c.syntax = 'floatyhelper checktokens'\n    c.summary = 'Verify tokens for all services are valid'\n    c.description = 'Verify that tokens for abs, vmpooler, and nspooler are valid in .vmfloaty.yml. If they are not, get new tokens.'\n    c.action do\n      Floaty.check_tokens\n    end\n  end\n\n  command :runon do |c|\n    c.syntax = 'floatyhelper runon <tag> <command>'\n    c.summary = 'Run a command on all hosts under the given tag'\n    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.'\n    c.option '--user', 'Run command as a user other than root'\n    c.action do |args, options|\n      if args.length == 0\n        puts 'Must specify a tag and command to run'.red\n        return\n      end\n      if args.length == 1\n        puts 'Must specify a command to run'.red\n        return\n      end\n\n      tag = args[0]\n      command = args[1..].join(' ')\n      user = options.user || 'root'\n      \n      if !Groups.tag?(tag)\n        puts \"\#{tag} does not appear to be a tag managed by floatyhelper.\".red\n        return\n      end\n      hosts = Groups.get_hosts(tag)\n      threads = []\n      responses = []\n      mutex = Mutex.new\n      hosts.each do |host|\n        threads << Thread.new do\n          output, status = Open3.capture2e(\"ssh -A -o StrictHostKeyChecking=no \#{user}@\#{host}.delivery.puppetlabs.net \\\"\#{command}\\\"\")\n          mutex.synchronize do\n            responses << [host,output,status]\n          end\n        end\n      end\n      puts\n      (1..threads.count).each do |i|\n        print \"\\rWaiting for thread \#{i}/\#{threads.count} to finish...\"\n        threads[i-1].join\n      end\n      print \"\\rDone\\n\"\n      responses.each do |r|\n        color = r[2].exitstatus.zero? ? :white : :red\n        puts \"*** \#{r[0]}: Exit code \#{r[2].exitstatus} ***\".colorize(color)\n        puts r[1].colorize(color)\n      end\n    end\n  end\n\n  command :scpto do |c|\n    c.syntax = 'floatyhelper scp <tag> <path_to_local_file> <path_to_remote_file>'\n    c.summary = 'SCP a file from this host to all hosts under the given tag at the given path'\n    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.'\n    c.option '--user', 'Log in as a user other than root'\n    c.action do |args, options|\n      if args.length < 3\n        puts 'Must specify a tag, local path, and remote path'.red\n        return\n      end\n\n      tag = args[0]\n      local = args[1]\n      remote = args[2]\n      user = options.user || 'root'\n      \n      if !Groups.tag?(tag)\n        puts \"\#{tag} does not appear to be a tag managed by floatyhelper.\".red\n        return\n      end\n      hosts = Groups.get_hosts(tag)\n      threads = []\n      responses = []\n      mutex = Mutex.new\n      hosts.each do |host|\n        threads << Thread.new do\n          output, status = Open3.capture2e(\"scp \#{local} \#{user}@\#{host}.delivery.puppetlabs.net:\#{remote}\")\n          mutex.synchronize do\n            responses << [host,output,status]\n          end\n        end\n      end\n      puts\n      (1..threads.count).each do |i|\n        print \"\\rWaiting for thread \#{i}/\#{threads.count} to finish...\"\n        threads[i-1].join\n      end\n      print \"\\rDone\\n\"\n      responses.each do |r|\n        color = r[2].exitstatus.zero? ? :white : :red\n        puts \"*** \#{r[0]}: Exit code \#{r[2].exitstatus} ***\".colorize(color)\n        puts r[1].colorize(color)\n      end\n    end\n  end\n\n  run!\nend\n"