
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@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. Note that I was trying to following the info1.c example on the libvirt website: http://libvirt.org/examples/info1.c As you can tell, this is the first time I am trying to create any sort of language bindings for a C library. Some questions: 1. Is there a better way of creating ruby bindings for libvirt besides SWIG? If so, any pointers on what should be done? 2. Alternatively I can try to make my ruby code use libvirt-python bindings as a python library. Has anyone done so? Any problems with this approach? cheers, mengkuan

On Thu, Aug 30, 2007 at 05:32:19PM +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@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.
Honnestly, I don't know ruby so no idea.
Note that I was trying to following the info1.c example on the libvirt website:
http://libvirt.org/examples/info1.c
As you can tell, this is the first time I am trying to create any sort of language bindings for a C library.
Some questions:
1. Is there a better way of creating ruby bindings for libvirt besides SWIG? If so, any pointers on what should be done?
There is a formal XML definition of the libvirt API http://libvirt.org/libvirt-api.xml The waythe python bindings are built is that a python script parses this XML and then generate the C and python modules needed for the bindings. I'm not sure this really helps you but at least this mean you don't have parse the C header files if you want to automate the process.
2. Alternatively I can try to make my ruby code use libvirt-python bindings as a python library. Has anyone done so? Any problems with this approach?
No idea. But I would be a bit worried about piling up layers like that, might work for you though.. Daniel -- Red Hat Virtualization group http://redhat.com/virtualization/ Daniel Veillard | virtualization library http://libvirt.org/ veillard@redhat.com | libxml GNOME XML XSLT toolkit http://xmlsoft.org/ http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/

On 30 Aug 2007, at 9:01 PM, Daniel Veillard wrote:
2. Alternatively I can try to make my ruby code use libvirt-python bindings as a python library. Has anyone done so? Any problems with this approach?
No idea. But I would be a bit worried about piling up layers like that, might work for you though..
Yes, that was my worry too. Hopefully I don't have to do it this way. Thanks for your input, Daniel. cheers, mengkuan

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@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/

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
participants (3)
-
Daniel Veillard
-
David Lutterkort
-
Meng Kuan