class Floatyhelper

Public Instance Methods

run() click to toggle source
# File lib/floatyhelper.rb, line 17
  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.action do |args, options|
        platform = args[0] || 'centos-7-x86_64'
        tag = args.length > 1 ? args[1] : 'Unassigned'
        begin
          host = VM.get_vm(platform: platform, force_abs: options.abs)
          Groups.addhosts([host], tag)
          puts "Added #{host} to tag #{tag}".green
          amount = Config.get_config_setting('increaselife').to_i
          VM.increaselife(host, 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

    run!
  end