From 244bc100103f3237745ea54005b532a7f4a728ce Mon Sep 17 00:00:00 2001 From: Ohad Levy Date: Sun, 8 Apr 2012 16:36:06 +0300 Subject: [PATCH] [libvirt] added display attributes and allowed to change display of a running server --- lib/fog/libvirt/compute.rb | 1 + lib/fog/libvirt/models/compute/server.rb | 14 +++++++-- .../models/compute/templates/server.xml.erb | 2 +- .../libvirt/requests/compute/list_domains.rb | 13 ++++---- .../requests/compute/update_display.rb | 31 +++++++++++++++++++ tests/libvirt/models/compute/server_tests.rb | 2 +- .../requests/compute/update_display.rb | 13 ++++++++ 7 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 lib/fog/libvirt/requests/compute/update_display.rb create mode 100644 tests/libvirt/requests/compute/update_display.rb diff --git a/lib/fog/libvirt/compute.rb b/lib/fog/libvirt/compute.rb index ee04722ec..c7ea6cd23 100644 --- a/lib/fog/libvirt/compute.rb +++ b/lib/fog/libvirt/compute.rb @@ -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 diff --git a/lib/fog/libvirt/models/compute/server.rb b/lib/fog/libvirt/models/compute/server.rb index 6f93d0e12..401ba59d8 100644 --- a/lib/fog/libvirt/models/compute/server.rb +++ b/lib/fog/libvirt/models/compute/server.rb @@ -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 diff --git a/lib/fog/libvirt/models/compute/templates/server.xml.erb b/lib/fog/libvirt/models/compute/templates/server.xml.erb index 7350c5e48..c4e783f3b 100644 --- a/lib/fog/libvirt/models/compute/templates/server.xml.erb +++ b/lib/fog/libvirt/models/compute/templates/server.xml.erb @@ -44,7 +44,7 @@ - + diff --git a/lib/fog/libvirt/requests/compute/list_domains.rb b/lib/fog/libvirt/requests/compute/list_domains.rb index 46db88e63..8d92f46c8 100644 --- a/lib/fog/libvirt/requests/compute/list_domains.rb +++ b/lib/fog/libvirt/requests/compute/list_domains.rb @@ -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), diff --git a/lib/fog/libvirt/requests/compute/update_display.rb b/lib/fog/libvirt/requests/compute/update_display.rb new file mode 100644 index 000000000..7d44cd9c7 --- /dev/null +++ b/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 diff --git a/tests/libvirt/models/compute/server_tests.rb b/tests/libvirt/models/compute/server_tests.rb index fb8eb695b..ea1b7f40b 100644 --- a/tests/libvirt/models/compute/server_tests.rb +++ b/tests/libvirt/models/compute/server_tests.rb @@ -34,7 +34,7 @@ Shindo.tests('Fog::Compute[:libvirt] | server model', ['libvirt']) do :domain_type, :uuid, :autostart, - :vnc_port, + :display, :nics, :volumes, :active, diff --git a/tests/libvirt/requests/compute/update_display.rb b/tests/libvirt/requests/compute/update_display.rb new file mode 100644 index 000000000..1ca8e7f53 --- /dev/null +++ b/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