Skip to content

Commit

Permalink
[libvirt] added display attributes and allowed to change display of a…
Browse files Browse the repository at this point in the history
… running server
  • Loading branch information
ohadlevy committed Apr 8, 2012
1 parent 1ed7a6b commit 244bc10
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/fog/libvirt/compute.rb
Expand Up @@ -44,6 +44,7 @@ class Libvirt < Fog::Service
request :list_interfaces
request :destroy_interface
request :get_node_info
request :update_display

module Shared
include Fog::Compute::LibvirtUtil
Expand Down
14 changes: 12 additions & 2 deletions lib/fog/libvirt/models/compute/server.rb
Expand Up @@ -24,11 +24,11 @@ class Server < Fog::Compute::Server
attribute :domain_type
attribute :uuid
attribute :autostart
attribute :vnc_port
attribute :nics
attribute :volumes
attribute :active
attribute :boot_order
attribute :display

attribute :state

Expand Down Expand Up @@ -223,6 +223,11 @@ def setup(credentials = {})
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

Expand Down Expand Up @@ -385,7 +390,8 @@ def defaults
: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

Expand All @@ -401,6 +407,10 @@ def verify_boot_order order = []
end
end

def default_display
{:port => '-1', :listen => '127.0.0.1', :type => 'vnc', :password => '' }
end

end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/libvirt/models/compute/templates/server.xml.erb
Expand Up @@ -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>
Expand Down
13 changes: 7 additions & 6 deletions lib/fog/libvirt/requests/compute/list_domains.rb
Expand Up @@ -20,11 +20,12 @@ def list_domains(filter = { })
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
Expand Down Expand Up @@ -61,7 +62,7 @@ def domain_to_attributes(dom)
: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),
Expand Down
31 changes: 31 additions & 0 deletions lib/fog/libvirt/requests/compute/update_display.rb
@@ -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
2 changes: 1 addition & 1 deletion tests/libvirt/models/compute/server_tests.rb
Expand Up @@ -34,7 +34,7 @@
:domain_type,
:uuid,
:autostart,
:vnc_port,
:display,
:nics,
:volumes,
:active,
Expand Down
13 changes: 13 additions & 0 deletions tests/libvirt/requests/compute/update_display.rb
@@ -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

0 comments on commit 244bc10

Please sign in to comment.