Class: RhevManager

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

Overview

This class provides utility methods to encapsulates RESTful access to the RHEV Manager. Since the returned response is in XML this class transforms the XML Response into Ruby Objects.

Constant Summary collapse

TMP_DIR =
Dir.tmpdir

Instance Method Summary collapse

Constructor Details

#initialize(host, user, password) ⇒ RhevManager

Returns a new instance of RhevManager.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/console-launcher.rb', line 37

def initialize(host, user, password)
  @host = host
  @user = user
  @pass = password

  # Create a little helper object that we will use to
  # make connections to the REST API
  @rhevm = RestClient::Resource.new(
      "https://" + @host,
      :user => @user,
      :password => @pass,
      :ssl_ca_cert => @cert,
      :ssl_version => "SSLv3",
      :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
  get_cert
end

Instance Method Details

#get_certObject

Download the Server SSL Certificate file on the fly



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/console-launcher.rb', line 55

def get_cert()
  begin
    cert = File.new(TMP_DIR + "/" + @host + ".crt", "w+")
    Net::HTTP.start(@host) do |http|
      begin
        http.request_get('/ca.crt') do |resp|
          resp.read_body do |segment|
            cert.write(segment)
          end
        end
      ensure
        cert.close()
      end
    end
    @cert = cert.path
  rescue => e
    raise "There has been an error downloading the certificate file from #{@host}: #{e.message}"
  end
end

#get_vmsObject

Returns an array of VirtualMachine ruby objects that are running on the RHEV Manager



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/console-launcher.rb', line 77

def get_vms()
  @vms = Array.new # Clear out array

  # get the vms api and get the list of vms
  vms_data = XmlSimple.xml_in(@rhevm["/api/vms"].get.body, {'ForceArray' => false})

  # Iterate through the VM's and get all the
  # required information
  vms_data['vm'].each do |vm|
    # Making sure we only consider VM's that are in state up (so they do have a console to connect to)
    # and that have the spice protocol enabled as the connection mode
    if vm['status']['state'] == "up" && vm['display']['type'] == "spice"
      @vms.push(VirtualMachine.new(vm))
    end
  end
  return @vms
end

#launch_viewer(index) ⇒ Object

This method takes an index that is used to query the VirtualMachine Array for the VM that is supposed to be started. It then queries the RHEV Manager for the correct subject and then requests a ticket for the console session. It then creates a .vv file that is used to be passed to the RemoteViewer Application. It returns the command to be launched because we fork the process and detach from the RemoteViewer process so we are able to launch more than one Console Session at once.



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
# File 'lib/console-launcher.rb', line 101

def launch_viewer(index)
  vm = @vms[index-1]

  # let us no gather the host subject
  hosts_data = XmlSimple.xml_in(@rhevm["/api/hosts/"+vm.host_uuid].get.body, {'ForceArray' => false})
  host_subject = hosts_data['certificate']['subject']

  ticket_data = XmlSimple.xml_in(@rhevm["/api/vms/" + vm.id + "/ticket"].post("<action><ticket><expiry>30</expiry></ticket></action>", :content_type => 'application/xml').body, {'ForceArray' => false})
  password = ticket_data['ticket']['value']

  # Creating the .vv File for the connection
  # download the certificate file on the fly
  @vv = File.new(TMP_DIR + "/" + vm.name + ".vv", "w+")
  begin
    @vv.puts("[virt-viewer]")
    @vv.puts("type=spice")
    @vv.puts("host=#{vm.address}")
    @vv.puts("port=#{vm.port}")
    @vv.puts("password=#{password}")
    @vv.puts("tls-port=#{vm.secure_port}")
    @vv.puts("fullscreen=0")
    @vv.puts("title=vm:#{vm.name} - %d - Press SHIFT+F12 to Release Cursor")
    @vv.puts("enable-smartcard=0")
    @vv.puts("enable-usb-autoshare=1")
    @vv.puts("usb-filter=-1,-1,-1,-1,0")
    @vv.puts("host-subject=#{host_subject}")
    @vv.puts("toggle-fullscreen=shift+f11")
    @vv.puts("release-cursor=shift+f12")
  ensure
    @vv.close()
  end

  # Now that we have all the information we can print the cmd line
  puts "Console to VM: #{vm.name} state: #{vm.state} is started"

  command = [
      Helper::OPTIONS[:viewer],
      "--spice-ca-file",
      @cert,
      @vv.path,
      {
          :out => Tempfile.new(["RemoteViewer",".out"]).path,
          :err => Tempfile.new(["RemoteViewer",".err"]).path
      }
  ]
end