ruby-libvirt 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +510 -0
- data/NEWS +36 -0
- data/README +26 -0
- data/README.rdoc +11 -0
- data/Rakefile +138 -0
- data/ext/libvirt/_libvirt.c +1967 -0
- data/ext/libvirt/extconf.h +6 -0
- data/ext/libvirt/extconf.rb +26 -0
- data/lib/libvirt.rb +40 -0
- data/tests/node.xml +110 -0
- data/tests/tc_connect.rb +178 -0
- metadata +63 -0
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
|
3
|
+
def have_libvirt_funcs(funcs)
|
4
|
+
funcs.each { |f| have_func(f, "libvirt/libvirt.h") }
|
5
|
+
end
|
6
|
+
|
7
|
+
def have_libvirt_types(types)
|
8
|
+
types.each { |t| have_type(t, "libvirt/libvirt.h") }
|
9
|
+
end
|
10
|
+
|
11
|
+
extension_name = '_libvirt'
|
12
|
+
|
13
|
+
dir_config(extension_name)
|
14
|
+
|
15
|
+
unless pkg_config("libvirt")
|
16
|
+
raise "libvirt not found"
|
17
|
+
end
|
18
|
+
|
19
|
+
libvirt_types = [ 'virNetworkPtr',
|
20
|
+
'virStoragePoolPtr',
|
21
|
+
'virStorageVolPtr' ]
|
22
|
+
|
23
|
+
have_libvirt_types(libvirt_types)
|
24
|
+
|
25
|
+
create_header
|
26
|
+
create_makefile(extension_name)
|
data/lib/libvirt.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#
|
2
|
+
# libvirt.rb: main module for the ruby-libvirt bindings
|
3
|
+
#
|
4
|
+
# Copyright (C) 2007 Red Hat, Inc.
|
5
|
+
#
|
6
|
+
# Distributed under the GNU Lesser General Public License v2.1 or later.
|
7
|
+
# See COPYING for details
|
8
|
+
#
|
9
|
+
# David Lutterkort <dlutter@redhat.com>
|
10
|
+
|
11
|
+
require '_libvirt'
|
12
|
+
|
13
|
+
module Libvirt
|
14
|
+
|
15
|
+
# A version in Libvirt's representation
|
16
|
+
class Version
|
17
|
+
attr_reader :version, :type
|
18
|
+
|
19
|
+
def initialize(type, version)
|
20
|
+
@type = type
|
21
|
+
@version = version
|
22
|
+
end
|
23
|
+
|
24
|
+
def major
|
25
|
+
version / 1000000
|
26
|
+
end
|
27
|
+
|
28
|
+
def minor
|
29
|
+
version % 1000000 / 1000
|
30
|
+
end
|
31
|
+
|
32
|
+
def release
|
33
|
+
version % 1000
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s
|
37
|
+
"#{major}.#{minor}.#{release}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/tests/node.xml
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
<?xml version="1.0"?>
|
2
|
+
<node>
|
3
|
+
<!-- This file gives an example config for the mock 'test' backend
|
4
|
+
driver to libvirt. This is intended to allow relible unit testing
|
5
|
+
of applications using libvirt. To use this with virsh, run something
|
6
|
+
like:
|
7
|
+
|
8
|
+
virsh -connect test:////path/to/this/dir/testnode.xml nodeinfo
|
9
|
+
|
10
|
+
-->
|
11
|
+
<domain type="test">
|
12
|
+
<name>fv0</name>
|
13
|
+
<uuid>4dea22b31d52d8f32516782e98ab3fa0</uuid>
|
14
|
+
<os>
|
15
|
+
<type>hvm</type>
|
16
|
+
<loader>/usr/lib/xen/boot/hvmloader</loader>
|
17
|
+
<boot dev="hd"/>
|
18
|
+
</os>
|
19
|
+
<currentMemory>2097152</currentMemory>
|
20
|
+
<memory>8388608</memory>
|
21
|
+
<vcpu>2</vcpu>
|
22
|
+
<on_poweroff>destroy</on_poweroff>
|
23
|
+
<on_reboot>restart</on_reboot>
|
24
|
+
<on_crash>restart</on_crash>
|
25
|
+
<features>
|
26
|
+
<pae/>
|
27
|
+
<acpi/>
|
28
|
+
<apic/>
|
29
|
+
</features>
|
30
|
+
<devices>
|
31
|
+
<emulator>/usr/lib/xen/bin/qemu-dm</emulator>
|
32
|
+
<interface type="bridge">
|
33
|
+
<source bridge="xenbr0"/>
|
34
|
+
<mac address="00:16:3e:5d:c7:9e"/>
|
35
|
+
<script path="vif-bridge"/>
|
36
|
+
</interface>
|
37
|
+
<disk type="file">
|
38
|
+
<source file="/root/fv0"/>
|
39
|
+
<target dev="hda"/>
|
40
|
+
</disk>
|
41
|
+
<disk type="file" device="cdrom">
|
42
|
+
<source file="/root/fc5-x86_64-boot.iso"/>
|
43
|
+
<target dev="hdc"/>
|
44
|
+
<readonly/>
|
45
|
+
</disk>
|
46
|
+
<disk type="file" device="floppy">
|
47
|
+
<source file="/root/fd.img"/>
|
48
|
+
<target dev="fda"/>
|
49
|
+
</disk>
|
50
|
+
<graphics type="vnc" port="5904"/>
|
51
|
+
</devices>
|
52
|
+
</domain>
|
53
|
+
<domain type="test">
|
54
|
+
<name>fc4</name>
|
55
|
+
<uuid>EF86180145B911CB88E3AFBFE5370493</uuid>
|
56
|
+
<os>
|
57
|
+
<type>xen</type>
|
58
|
+
<kernel>/boot/vmlinuz-2.6.15-1.43_FC5guest</kernel>
|
59
|
+
<initrd>/boot/initrd-2.6.15-1.43_FC5guest.img</initrd>
|
60
|
+
<root>/dev/sda1</root>
|
61
|
+
<cmdline> ro selinux=0 3</cmdline>
|
62
|
+
</os>
|
63
|
+
<memory>261072</memory>
|
64
|
+
<currentMemory>131072</currentMemory>
|
65
|
+
<vcpu>1</vcpu>
|
66
|
+
<devices>
|
67
|
+
<disk type="file">
|
68
|
+
<source file="/u/fc4.img"/>
|
69
|
+
<target dev="sda1"/>
|
70
|
+
</disk>
|
71
|
+
<interface type="bridge">
|
72
|
+
<source bridge="xenbr0"/>
|
73
|
+
<mac address="aa:00:00:00:00:11"/>
|
74
|
+
<script path="/etc/xen/scripts/vif-bridge"/>
|
75
|
+
</interface>
|
76
|
+
<console tty="/dev/pts/5"/>
|
77
|
+
</devices>
|
78
|
+
</domain>
|
79
|
+
<network>
|
80
|
+
<name>private</name>
|
81
|
+
<uuid>004b22212d78c30f5aa5f03c87d21e69</uuid>
|
82
|
+
<bridge name="brpriv"/>
|
83
|
+
<ip address="192.168.124.1" netmask="255.255.255.0">
|
84
|
+
<dhcp>
|
85
|
+
<range start="192.168.124.128" end="192.168.124.253"/>
|
86
|
+
</dhcp>
|
87
|
+
</ip>
|
88
|
+
</network>
|
89
|
+
<network>
|
90
|
+
<name>default</name>
|
91
|
+
<uuid>004b96e12d78c30f5aa5f03c87d21e69</uuid>
|
92
|
+
<bridge name="brdefault"/>
|
93
|
+
<forward dev="eth0"/>
|
94
|
+
<ip address="192.168.122.1" netmask="255.255.255.0">
|
95
|
+
<dhcp>
|
96
|
+
<range start="192.168.122.128" end="192.168.122.253"/>
|
97
|
+
</dhcp>
|
98
|
+
</ip>
|
99
|
+
</network>
|
100
|
+
<cpu>
|
101
|
+
<mhz>6000</mhz>
|
102
|
+
<model>i986</model>
|
103
|
+
<active>50</active>
|
104
|
+
<nodes>4</nodes>
|
105
|
+
<sockets>4</sockets>
|
106
|
+
<cores>4</cores>
|
107
|
+
<threads>2</threads>
|
108
|
+
</cpu>
|
109
|
+
<memory>8192000</memory>
|
110
|
+
</node>
|
data/tests/tc_connect.rb
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
$:.unshift(File::join(File::dirname(__FILE__), "..", "lib"))
|
4
|
+
$:.unshift(File::join(File::dirname(__FILE__), "..", "ext", "libvirt"))
|
5
|
+
require 'libvirt'
|
6
|
+
|
7
|
+
class TestConnect < Test::Unit::TestCase
|
8
|
+
|
9
|
+
LIBVIRT_VERSION = Libvirt::version("Test")[0]
|
10
|
+
|
11
|
+
TEST_CAPS_OLD = "<capabilities>\n <host>\n <cpu>\n <arch>i686</arch>\n <features>\n <pae/>\n <nonpae/>\n </features>\n </cpu>\n </host>\n\n <guest>\n <os_type>linux</os_type>\n <arch name=\"i686\">\n <wordsize>32</wordsize>\n <domain type=\"test\"/>\n </arch>\n <features>\n <pae/>\n <nonpae/>\n </features>\n </guest>\n</capabilities>\n"
|
12
|
+
|
13
|
+
TEST_CAPS_0_40_1 = "<capabilities>\n\n <host>\n <cpu>\n <arch>i686</arch>\n <features>\n <pae/>\n <nonpae/>\n </features>\n </cpu>\n <topology>\n <cells num='2'>\n <cell id='0'>\n <cpus num='8'>\n <cpu id='0'>\n <cpu id='2'>\n <cpu id='4'>\n <cpu id='6'>\n <cpu id='8'>\n <cpu id='10'>\n <cpu id='12'>\n <cpu id='14'>\n </cpus>\n </cell>\n <cell id='1'>\n <cpus num='8'>\n <cpu id='1'>\n <cpu id='3'>\n <cpu id='5'>\n <cpu id='7'>\n <cpu id='9'>\n <cpu id='11'>\n <cpu id='13'>\n <cpu id='15'>\n </cpus>\n </cell>\n </cells>\n </topology>\n </host>\n\n <guest>\n <os_type>linux</os_type>\n <arch name='i686'>\n <wordsize>32</wordsize>\n <domain type='test'>\n </domain>\n </arch>\n <features>\n <pae/>\n <nonpae/>\n </features>\n </guest>\n\n</capabilities>\n"
|
14
|
+
|
15
|
+
if LIBVIRT_VERSION.major >= 0 &&
|
16
|
+
LIBVIRT_VERSION.minor >= 4 &&
|
17
|
+
LIBVIRT_VERSION.release >= 1
|
18
|
+
TEST_CAPS = TEST_CAPS_0_40_1
|
19
|
+
else
|
20
|
+
TEST_CAPS = TEST_CAPS_OLD
|
21
|
+
end
|
22
|
+
|
23
|
+
UUID = "4dea22b3-1d52-d8f3-2516-782e98ab3fa0"
|
24
|
+
|
25
|
+
NETWORK_UUID = "004b96e1-2d78-c30f-5aa5-f03c87d21e69"
|
26
|
+
|
27
|
+
NETWORK_XML = "<network>
|
28
|
+
<name>local</name>
|
29
|
+
<uuid>9b562b27-0969-4b39-8c96-ef7858152ccc</uuid>
|
30
|
+
<bridge name='virbr0'/>
|
31
|
+
<forward/>
|
32
|
+
<ip address='172.31.122.1' netmask='255.255.255.0'>
|
33
|
+
<dhcp>
|
34
|
+
<range start='172.31.122.2' end='172.31.122.254'/>
|
35
|
+
</dhcp>
|
36
|
+
</ip>
|
37
|
+
</network>
|
38
|
+
"
|
39
|
+
|
40
|
+
def test_url
|
41
|
+
"test://" + File::join(File::expand_path(File::dirname(__FILE__)),
|
42
|
+
"node.xml")
|
43
|
+
end
|
44
|
+
|
45
|
+
def connect
|
46
|
+
c = Libvirt::open(test_url)
|
47
|
+
assert_not_nil(c)
|
48
|
+
assert(! c.closed?)
|
49
|
+
return c
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_open
|
53
|
+
c = connect
|
54
|
+
assert_nothing_raised {
|
55
|
+
c.close
|
56
|
+
}
|
57
|
+
assert(c.closed?)
|
58
|
+
assert_nothing_raised {
|
59
|
+
c.close
|
60
|
+
}
|
61
|
+
assert(c.closed?)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_node_info
|
65
|
+
ni = connect.node_get_info
|
66
|
+
assert_equal(4, ni.nodes)
|
67
|
+
assert_equal(50, ni.cpus)
|
68
|
+
assert_equal(2, ni.threads)
|
69
|
+
assert_equal(4, ni.sockets)
|
70
|
+
assert_equal(6000, ni.mhz)
|
71
|
+
assert_equal(4, ni.cores)
|
72
|
+
assert_equal("i986", ni.model)
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_misc
|
76
|
+
c = connect
|
77
|
+
assert_equal("Test", c.type)
|
78
|
+
assert_equal(2, c.version)
|
79
|
+
hostname=`hostname`.chomp
|
80
|
+
|
81
|
+
assert_nothing_raised {
|
82
|
+
c.lookup_network_by_name("default").create
|
83
|
+
}
|
84
|
+
|
85
|
+
assert_equal(hostname, c.hostname)
|
86
|
+
assert_equal(test_url, c.uri)
|
87
|
+
assert_equal(32, c.max_vcpus("bogus"))
|
88
|
+
assert(c.capabilities.size > 0)
|
89
|
+
assert_equal(2, c.num_of_domains)
|
90
|
+
assert_equal([1, 2], c.list_domains)
|
91
|
+
assert_equal(0, c.num_of_defined_domains)
|
92
|
+
assert_equal([], c.list_defined_domains)
|
93
|
+
assert_equal(1, c.num_of_networks)
|
94
|
+
assert_equal(["default"], c.list_networks)
|
95
|
+
assert_equal(1, c.num_of_defined_networks)
|
96
|
+
assert_equal(["private"], c.list_defined_networks)
|
97
|
+
|
98
|
+
v = Libvirt::version("Test")
|
99
|
+
assert_equal("libvirt", v[0].type)
|
100
|
+
assert_equal("Test", v[1].type)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_domain
|
104
|
+
c = connect
|
105
|
+
|
106
|
+
dom = c.lookup_domain_by_id(1)
|
107
|
+
assert_equal("fv0", dom.name)
|
108
|
+
assert_equal("linux", dom.os_type)
|
109
|
+
assert_equal(UUID, dom.uuid)
|
110
|
+
assert_equal(UUID, c.lookup_domain_by_uuid(UUID).uuid)
|
111
|
+
assert_equal(UUID, c.lookup_domain_by_name("fv0").uuid)
|
112
|
+
|
113
|
+
info = dom.info
|
114
|
+
assert_equal(8388608, info.max_mem)
|
115
|
+
assert_equal(2097152, info.memory)
|
116
|
+
assert_equal(2, info.nr_virt_cpu)
|
117
|
+
assert_equal(Libvirt::Domain::RUNNING, info.state)
|
118
|
+
|
119
|
+
dom.memory = info.memory/2
|
120
|
+
dom.vcpus = 1
|
121
|
+
info = dom.info
|
122
|
+
assert_equal(2097152/2, info.memory)
|
123
|
+
assert_equal(1, info.nr_virt_cpu)
|
124
|
+
|
125
|
+
# pin_vcpu is not implemented in the test driver
|
126
|
+
# enable this once it becomes available
|
127
|
+
# dom.pin_vcpu(0,[0])
|
128
|
+
|
129
|
+
dom.free()
|
130
|
+
assert_raise ArgumentError do
|
131
|
+
dom.name
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_error
|
136
|
+
c = connect
|
137
|
+
raised = false
|
138
|
+
begin
|
139
|
+
c.lookup_domain_by_id(42)
|
140
|
+
rescue Libvirt::RetrieveError => e
|
141
|
+
raised = true
|
142
|
+
assert(e.message.size > 0)
|
143
|
+
assert_equal("virDomainLookupByID", e.libvirt_function_name)
|
144
|
+
assert_not_nil e.libvirt_message
|
145
|
+
end
|
146
|
+
assert(raised)
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_network
|
150
|
+
c = connect
|
151
|
+
|
152
|
+
netw = c.lookup_network_by_name("default")
|
153
|
+
assert_equal("default", netw.name)
|
154
|
+
assert_equal("brdefault", netw.bridge_name)
|
155
|
+
uuid = NETWORK_UUID
|
156
|
+
assert_equal(uuid, netw.uuid)
|
157
|
+
assert_equal(uuid, c.lookup_network_by_uuid(uuid).uuid)
|
158
|
+
assert_equal(uuid, c.lookup_network_by_name("default").uuid)
|
159
|
+
assert_equal(false, netw.autostart)
|
160
|
+
netw.autostart = true
|
161
|
+
assert_equal(true, netw.autostart)
|
162
|
+
netw.autostart = false
|
163
|
+
assert_equal(false, netw.autostart)
|
164
|
+
|
165
|
+
netw = c.define_network_xml(NETWORK_XML)
|
166
|
+
assert(netw.xml_desc.size > 0)
|
167
|
+
assert_equal(c, netw.connection)
|
168
|
+
|
169
|
+
netw.create
|
170
|
+
assert_equal(1, c.num_of_networks)
|
171
|
+
assert_equal(["local"], c.list_networks)
|
172
|
+
|
173
|
+
netw.free
|
174
|
+
assert_raise ArgumentError do
|
175
|
+
netw.name
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-libvirt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors: []
|
7
|
+
|
8
|
+
autorequire: libvirt
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-11-18 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Provides bindings for libvirt.
|
17
|
+
email: libvir-list@redhat.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions:
|
21
|
+
- ext/libvirt/extconf.rb
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- COPYING
|
27
|
+
- README
|
28
|
+
- NEWS
|
29
|
+
- README.rdoc
|
30
|
+
- lib/libvirt.rb
|
31
|
+
- ext/libvirt/_libvirt.c
|
32
|
+
- ext/libvirt/extconf.h
|
33
|
+
- ext/libvirt/extconf.rb
|
34
|
+
- tests/node.xml
|
35
|
+
- tests/tc_connect.rb
|
36
|
+
has_rdoc: false
|
37
|
+
homepage: http://libvirt.org/ruby/
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.8.1
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: Ruby bindings for LIBVIRT
|
62
|
+
test_files: []
|
63
|
+
|