Class: ForemanAP::ConsoleViewer

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_vm/console.rb

Overview

Functions related viewing to the virtual machine console

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster) ⇒ ConsoleViewer

Create an object.

cluster

a ForemanAP::Cluster object.



17
18
19
20
21
# File 'lib/foreman_vm/console.rb', line 17

def initialize(cluster)
  @cluster = cluster
  @html = false
  @autoclose = nil
end

Instance Attribute Details

#htmlObject

If true, output will be formatted for HTML display.



12
13
14
# File 'lib/foreman_vm/console.rb', line 12

def html
  @html
end

Instance Method Details

#attach(guest) ⇒ Object

Attach to the serial console of the virtual machine.



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
# 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) ⇒ Object

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

Example:  / login:/


28
29
30
31
# File 'lib/foreman_vm/console.rb', line 28

def autoclose=(pattern)
  raise 'Regular expression expected' unless pattern.class == Regexp
  @autoclose = pattern
end