[Libvir] Ruby bindings for libvirt

David Lutterkort dlutter at redhat.com
Thu Aug 30 17:56:39 UTC 2007


On Thu, 2007-08-30 at 17:32 +0800, Meng Kuan wrote:
> Hi,
> 
> I am exploring the possibility of creating ruby bindings for libvirt.  
> The only attempt I found after some searching is this:
> 
>    http://www.cs.ucsb.edu/~ckrintz/racelab/jisha/
> 
> Following the SWIG method as described in the above URL, I have  
> gotten to the following point:
> 
>    [root at dell1 libvirt_ruby]# irb
>    irb(main):001:0> require 'libvirt_ruby'
>    => true
>    irb(main):002:0> conn = Libvirt_ruby.virConnectOpenReadOnly("")
>    => #<SWIG::TYPE_p__virConnect:0x2aaaaabc46c8>
>    irb(main):003:0>  dom = Libvirt_ruby.virDomainLookupByID(conn, 0)
>    => #<SWIG::TYPE_p__virDomain:0x2aaaaabb5df8>
>    irb(main):004:0> ret = Libvirt_ruby.virDomainGetInfo(dom, info)
>    NameError: undefined local variable or method `info' for main:Object
>            from (irb):4
>    irb(main):005:0>
> 
> I am not sure how to create and pass in a ptr to a virDomainInfo  
> struct object into the virDomainGetInfo call within ruby.

virDomainGetInfo uses the info structure to return the info. It's going
to be hard to replicate that 1-1 in Ruby; but you'd get a much better,
more Ruby-ish API if you structure the various libvirt calls into a
number of classes (Libvirt::Connection, Libvirt::Domain,
Libvirt::DomainInfo etc.) so that your above code would look something
like

  conn = Libvirt::connectReadOnly("") # Returns a Libvirt::Connection
  dom = conn.lookupDomainByID(0)      # Returns a Libvirt::Domain
  info = dom.getInfo()                # Returns a Libvirt::DomainInfo

I doubt that you can do that with Swig, but it's not terribly hard to
write bindings by hand. Have a look at existing bindings, e.g. for
rpm[1] or gamin[2].

David

[1] http://rubyforge.org/projects/ruby-rpm/
[2] http://www.pablotron.org/software/fam-ruby/





More information about the libvir-list mailing list