xen 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,16 @@
1
+ = Xen -- Ruby interface to the Xen API.
2
+
3
+ Xen strives to be the Ruby interface talking to the Xen API. It grew from our own need to talk to Xen Servers with Ruby.
4
+
5
+ === Usage:
6
+
7
+ # Connect to xenhost
8
+ xenhost = Xen::Host.new('xenhost.example.com')
9
+
10
+ # Get status of each VM running
11
+ xenhost.vms.each do |vm|
12
+ puts vm.state unless vm.is_dom0?
13
+ end
14
+
15
+ # Restart a certain VM
16
+ xenhost.find_vm('example_vm').clean_reboot!
data/lib/xen.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'xen/connection.rb'
2
+ require 'xen/host.rb'
3
+ require 'xen/vm.rb'
4
+ require 'xen/network.rb'
@@ -0,0 +1,38 @@
1
+ require 'xmlrpc/client'
2
+
3
+ module Xen
4
+
5
+ class Connection
6
+
7
+ # Initializes an xml-rpc connection to a Xen host, and logs in with the given credentials.
8
+ # === Example
9
+ # Xen::Connection.new ('xenhost.example.com', 2000, 'login', 'pass')
10
+ def initialize(host, port = 2000, login = 'user', pass = '')
11
+ @server = XMLRPC::Client.new(host,'/', port)
12
+ @session = @server.call("session.login_with_password", login, pass)
13
+ end
14
+
15
+ # Returns the session_id of the connection
16
+ def session_id
17
+ @session['Value']
18
+ end
19
+
20
+ # Make a direct call to the Xen API.
21
+ # === Example
22
+ # call('session.get_this_host', session_id) # => {"Status"=>"Success", "Value"=>"3a8f495d-939d-823b-fa08-fc407c73dc94"}
23
+ def call(function_name, *params)
24
+ call_without_session(function_name, session_id, *params)
25
+ end
26
+
27
+ def to_s
28
+ session_id.to_s
29
+ end
30
+
31
+ private
32
+ def call_without_session(function_name, *params)
33
+ @server.call(function_name, *params)
34
+ end
35
+
36
+ end
37
+
38
+ end
data/lib/xen/host.rb ADDED
@@ -0,0 +1,65 @@
1
+ module Xen
2
+
3
+ class Host
4
+
5
+ attr_reader :connection
6
+
7
+ # Creates a new Host object, and initiates an API connection to the given host on the given port, with a login and pass.
8
+ def initialize(host, port = 2000, login = 'user', pass = '')
9
+ @host = host
10
+ @connection = Connection.new(host, port, login, pass)
11
+ end
12
+
13
+ # Returns the uid of the host.
14
+ def uid # Lazy Loaded
15
+ @uid ||= get_value("session.get_this_host", @connection.session_id)
16
+ end
17
+
18
+ # Gives an array of active VM's on that Host.
19
+ def vms
20
+ get_value("host.get_resident_VMs", uid).collect do |vm_uid|
21
+ Vm.new(vm_uid, self) # Isn't this expensive?
22
+ end
23
+ end
24
+
25
+ # Find a certain VM by a given domain-name.
26
+ def find_vm name
27
+ vms.detect { |vm| vm.name == name } # || raise Exception
28
+ end
29
+
30
+ # Gives the different bridges on your system
31
+ def networks
32
+ get_value("network.get_all").collect do |network_uid|
33
+ Network.new(network_uid, self)
34
+ end
35
+ end
36
+
37
+ # Gives the API version number in the form of major.minor vendor.
38
+ def version
39
+ major = get_value("host.get_API_version_major", uid)
40
+ minor = get_value("host.get_API_version_minor", uid)
41
+ vendor = get_value("host.get_API_version_vendor", uid)
42
+
43
+ "#{major}.#{minor} #{vendor}"
44
+ end
45
+
46
+ def record
47
+ get_value("host.get_record")
48
+ end
49
+
50
+ def to_s
51
+ uid.to_s
52
+ end
53
+
54
+ # Call to the API
55
+ def call(function_name, *params)
56
+ @connection.call(function_name, *params)
57
+ end
58
+
59
+ # Call to the API, Returns only the return value.
60
+ def get_value(function, *params)
61
+ call(function, *params)['Value']
62
+ end
63
+ end
64
+
65
+ end
@@ -0,0 +1,21 @@
1
+ module Xen
2
+ class Network
3
+
4
+ def initialize(uid, host)
5
+ @uid = uid
6
+ @host = host
7
+ end
8
+
9
+ def record
10
+ @host.call("network.get_record", @uid)['Value']
11
+ end
12
+
13
+ def uuid
14
+ self.to_s
15
+ end
16
+
17
+ def to_s
18
+ @uid.to_s
19
+ end
20
+ end
21
+ end
data/lib/xen/vm.rb ADDED
@@ -0,0 +1,91 @@
1
+ module Xen
2
+
3
+ class Vm
4
+
5
+ attr_reader :uid
6
+
7
+ # Creates a new VM, Given the VM's UID and the host it is running on.
8
+ def initialize(uid, host)
9
+ @uid, @host = uid, host
10
+ # self.get_state # Too expensive!
11
+ end
12
+
13
+ # Gets the state of the VM. The first time it is called it query's the server, other times it is cached. There is an optional parameter 'refresh' which, when true, will re-query the server.
14
+ # === Example
15
+ # # Having vm being a new initiated Vm
16
+ # vm.state # => "Running"
17
+ # vm.clean_shutdown! # => "Running"
18
+ # # We wait a while
19
+ # vm.state # => "Running" (cached)
20
+ # vm.state(true) # => "Halted"
21
+ def state refresh = false
22
+ @state = refresh || !@state ? @host.get_value("VM.get_power_state",@uid) : @state
23
+ end
24
+
25
+ # Returns the VM's name
26
+ def name
27
+ @host.get_value("VM.get_name_label",@uid)
28
+ end
29
+
30
+ # Returns true if the current vm happens to be dom0.
31
+ def is_dom0?
32
+ name.to_s == "Domain-0"
33
+ end
34
+
35
+ # Returns the VM's uuid
36
+ def uuid
37
+ @host.get_value("VM.get_uuid",@uid)
38
+ end
39
+
40
+ # Starts a VM, and returns the current state
41
+ def start!(paused = false)
42
+ @host.call("VM.start",@uid, paused)
43
+ state(true)
44
+ end
45
+
46
+ # Pauses the VM, and returns the current state
47
+ def pause!
48
+ @host.call("VM.pause",@uid)
49
+ state(true)
50
+ end
51
+
52
+ # Unpauses the VM, and returns the current state
53
+ def unpause!
54
+ @host.call("VM.unpause",@uid)
55
+ state(true)
56
+ end
57
+
58
+ # Shuts down the VM in a clean way, and returns the current state
59
+ def clean_shutdown!
60
+ @host.call("VM.clean_shutdown",@uid)
61
+ state(true)
62
+ end
63
+
64
+ # Reboots the VM in a clean way, and returns the current state
65
+ def clean_reboot!
66
+ @host.call("VM.clean_reboot",@uid)
67
+ state(true)
68
+ end
69
+
70
+ # Shuts down the VM immediatly, hard, and returns the current state
71
+ def hard_shutdown!
72
+ @host.call("VM.hard_shutdown",@uid)
73
+ state(true)
74
+ end
75
+
76
+ # Hard reboots the VM, and returns the current state
77
+ def hard_reboot!
78
+ @host.call("VM.hard_reboot",@uid)
79
+ state(true)
80
+ end
81
+
82
+ # Record..
83
+ def record
84
+ @host.get_value("VM.get_record", @uid)
85
+ end
86
+
87
+ def to_s
88
+ @uid.to_s
89
+ end
90
+ end
91
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: xen
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.1
7
+ date: 2007-08-29 00:00:00 +02:00
8
+ summary: A package that is a link between Ruby and Xen API.
9
+ require_paths:
10
+ - lib
11
+ email: xen-ruby@openminds.be
12
+ homepage:
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: xen
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - - Bernard Grymonpon
31
+ - Jan De Poorter
32
+ files:
33
+ - lib/xen
34
+ - lib/xen/connection.rb
35
+ - lib/xen/host.rb
36
+ - lib/xen/network.rb
37
+ - lib/xen/vm.rb
38
+ - lib/xen.rb
39
+ - README
40
+ test_files: []
41
+
42
+ rdoc_options: []
43
+
44
+ extra_rdoc_files:
45
+ - README
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ requirements: []
51
+
52
+ dependencies: []
53
+