[libvirt] added display attributes and allowed to change display of a running server

This commit is contained in:
Ohad Levy 2012-04-08 16:36:06 +03:00 committed by Ohad Levy
parent 1ed7a6b2ee
commit 244bc10010
7 changed files with 66 additions and 10 deletions

View File

@ -44,6 +44,7 @@ module Fog
request :list_interfaces
request :destroy_interface
request :get_node_info
request :update_display
module Shared
include Fog::Compute::LibvirtUtil

View File

@ -24,11 +24,11 @@ module Fog
attribute :domain_type
attribute :uuid
attribute :autostart
attribute :vnc_port
attribute :nics
attribute :volumes
attribute :active
attribute :boot_order
attribute :display
attribute :state
@ -223,6 +223,11 @@ module Fog
Fog::SSH.new(public_ip_address, username, credentials).run(commands)
end
def update_display attrs = {}
connection.update_display attrs.merge(:uuid => uuid)
reload
end
private
attr_accessor :volumes_path
@ -385,7 +390,8 @@ module Fog
:network_interface_type => "network",
:network_nat_network => "default",
:network_bridge_name => "br0",
:boot_order => default_boot_order
:boot_order => default_boot_order,
:display => default_display
}
end
@ -401,6 +407,10 @@ module Fog
end
end
def default_display
{:port => '-1', :listen => '127.0.0.1', :type => 'vnc', :password => '' }
end
end
end

View File

@ -44,7 +44,7 @@
<target port='0'/>
</console>
<input type='mouse' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes'/>
<graphics type='<%= display[:type] %>' port='<%= display[:port] %>' autoport='yes' listen='<%= display[:listen] %>' passwd='<%=display[:password] %>'/>
<video>
<model type='cirrus' vram='9216' heads='1'/>
</video>

View File

@ -20,11 +20,12 @@ module Fog
module Shared
private
def vnc_port xml
xml_element(xml, "domain/devices/graphics[@type='vnc']", "port")
rescue => e
# we might be using SPICE display, or no VNC display at all
nil
def domain_display xml
attrs = {}
[:type, :port, :password, :listen].each do |element|
attrs[element] = xml_element(xml, "domain/devices/graphics",element.to_s) rescue nil
end
attrs.reject{|k,v| v.nil? or v == ""}
end
def domain_volumes xml
@ -61,7 +62,7 @@ module Fog
:autostart => dom.autostart?,
:os_type => dom.os_type,
:active => dom.active?,
:vnc_port => vnc_port(dom.xml_desc),
:display => domain_display(dom.xml_desc),
:boot_order => boot_order(dom.xml_desc),
:nics => domain_interfaces(dom.xml_desc),
:volumes_path => domain_volumes(dom.xml_desc),

View File

@ -0,0 +1,31 @@
module Fog
module Compute
class Libvirt
class Real
def update_display(options = { })
raise ArgumentError, "uuid is a required parameter" unless options.has_key? :uuid
display = { }
display[:type] = options[:type] || 'vnc'
display[:port] = (options[:port] || -1).to_s
display[:listen] = options[:listen].to_s if options[:listen]
display[:passwd] = options[:password].to_s if options[:password]
display[:autoport] = 'yes' if display[:port] == '-1'
builder = Nokogiri::XML::Builder.new { graphics_ (display) }
xml = Nokogiri::XML(builder.to_xml).root.to_s
client.lookup_domain_by_uuid(options[:uuid]).update_device(xml, 0)
# if we got no exceptions, then we're good'
true
end
end
class Mock
def update_display(options = { })
raise ArgumentError, "uuid is a required parameter" unless options.has_key? :uuid
true
end
end
end
end
end

View File

@ -34,7 +34,7 @@ Shindo.tests('Fog::Compute[:libvirt] | server model', ['libvirt']) do
:domain_type,
:uuid,
:autostart,
:vnc_port,
:display,
:nics,
:volumes,
:active,

View File

@ -0,0 +1,13 @@
Shindo.tests('Fog::Compute[:libvirt] | update_display request', ['libvirt']) do
compute = Fog::Compute[:libvirt]
reconfig_target = 'f74d728a-5b62-7e2f-1f84-239aead298ca'
display_spec = {:password => 'ssaaa'}
tests('The response should') do
response = compute.update_display(:uuid => reconfig_target).merge(display_spec)
test('should be true').succeeds { response }
end
end