Re: Ruby bindings for libvirt

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 31 Aug 2007, at 1:56 AM, David Lutterkort wrote:

On Thu, 2007-08-30 at 17:32 +0800, Meng Kuan wrote:
   [root@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


What I was missing was the "-autorename" argument when calling swig, i.e.

  swig -ruby -autorename libvirt.i

After compile and install of the resulting libvirt.so, here's what I can do in ruby:

  require 'libvirt'
  conn = Libvirt.vir_connect_open_read_only("")
  dom = Libvirt.vir_domain_lookup_by_id(conn, 0)
  info = Libvirt::VirDomainInfo.new
  Libvirt.vir_domain_get_info(dom, info)
  puts info.state   # => 1
  puts info.maxMem  # => 1047552

It's far from ruby-ish, but it works.


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].

I'll probably look into writing bindings by hand later on, but at this time due to my inexperience with C programming and lack of time, I am thinking of writing Ruby classes that wrap these C calls and use those Ruby classes instead.

Thanks for the pointers, David. Much appreciated.

cheers,
mengkuan


--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]