class ForemanAP::ConsoleViewer

Functions related viewing to the virtual machine console

Attributes

html[RW]

If true, output will be formatted for HTML display.

Public Class Methods

new(cluster) click to toggle source

Create an object.

cluster

a ForemanAP::Cluster object.

# File lib/foreman_vm/console.rb, line 17
def initialize(cluster)
  @cluster = cluster
  @html = false
  @autoclose = nil
end

Public Instance Methods

attach(guest) click to toggle source

Attach to the serial console of the virtual machine.

# File lib/foreman_vm/console.rb, line 35
def attach(guest)
  host = @cluster.find(guest)
  puts "Connecting to the serial console of #{guest} via #{host}... "
  print '<pre>' if @html
  ENV['LIBVIRT_AUTH_FILE'] = File.dirname(__FILE__) + '/../../conf/auth.conf'
begin
  PTY.spawn("virsh -c qemu+tcp://#{host}/system console #{guest}") do |stdin, stdout, pid|
  begin
    # Regularly try to flush the output in a different thread
    # This allows us to detect when the client hangs up even if
    # the main thread is blocked trying to read from the VM console.
    if @html
      t = Thread.new {
        while true
          $stdout.flush or raise 'client has disconnected'
          sleep(5)
        end
      }
      t.abort_on_exception = true
    end
 
    stdout.write ""
    stdout.flush

    stdin.each do |line| 
      if @html
        # TODO: translate ANSI colors into HTML colors
        print CGI::escapeHTML(line).chomp
      else
        print line
      end
      $stdout.flush or exit(0)
      if @autoclose and line =~ @autoclose
        puts "(the console was automatically closed)"
        exit 0
      end
    end
    rescue Errno::EIO
      puts "Errno:EIO error, but this probably just means " +
            "that the process has finished giving output"
    end
  end
  rescue PTY::ChildExited
    puts "The child process exited!"
  end
  print '</pre>' if @html
end
autoclose=(pattern) click to toggle source

Specify a pattern that will cause the console to be automatically closed when it is found in the output.

Example:  / login:/
# File lib/foreman_vm/console.rb, line 28
def autoclose=(pattern)
  raise 'Regular expression expected' unless pattern.class == Regexp
  @autoclose = pattern
end